Skip to content

Commit fb967ef

Browse files
committed
lint: de-duplicate boxed value copying logic
1 parent 57d9b87 commit fb967ef

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/boxed.cc

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,9 @@ static void BoxedConstructor(const Nan::FunctionCallbackInfo<Value> &info) {
179179
boxed = External::Cast(*info[0])->Value();
180180

181181
if (mustCopy) {
182-
if (gtype != G_TYPE_NONE) {
183-
boxed = g_boxed_copy (gtype, boxed);
184-
}
185-
else if ((size = Boxed::GetSize(gi_info)) != 0) {
186-
void *boxedCopy = malloc(size);
187-
memcpy(boxedCopy, boxed, size);
188-
boxed = boxedCopy;
189-
}
190-
else {
182+
boxed = CopyBoxed(gi_info, boxed, &size);
183+
if (!boxed)
191184
ERROR("Could not copy boxed object.");
192-
}
193185
}
194186
else {
195187
owns_memory = false;
@@ -445,4 +437,24 @@ void* PointerFromWrapper(Local<Value> value) {
445437
return boxed;
446438
}
447439

440+
void* CopyBoxed(GIBaseInfo *gi_info, void *boxed, size_t *size_out)
441+
{
442+
GType gtype = g_registered_type_info_get_g_type (gi_info);
443+
size_t size;
444+
445+
if (gtype != G_TYPE_NONE) {
446+
return g_boxed_copy (gtype, boxed);
447+
} else if ((size = Boxed::GetSize(gi_info)) != 0) {
448+
void *boxedCopy = malloc(size);
449+
memcpy(boxedCopy, boxed, size);
450+
if (size_out)
451+
*size_out = size;
452+
return boxedCopy;
453+
} else {
454+
WARN("Cannot copy a boxed object of type '%s'.",
455+
g_base_info_get_name(gi_info));
456+
return NULL;
457+
}
458+
}
459+
448460
};

src/boxed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ Local<Function> MakeBoxedClass (GIBaseInfo *info);
3232
Local<FunctionTemplate> GetBoxedTemplate (GIBaseInfo *info, GType gtype);
3333
Local<Value> WrapperFromBoxed (GIBaseInfo *info, void *data, bool mustCopy = false);
3434
void * PointerFromWrapper (Local<Value>);
35+
void * CopyBoxed (GIBaseInfo *gi_info, void *boxed, size_t *size_out = nullptr);
3536

3637
};

src/value.cc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -644,20 +644,9 @@ bool V8ToGIArgument(GIBaseInfo *gi_info, GIArgument *arg, Local<Value> value, GI
644644
case GI_INFO_TYPE_UNION:
645645
arg->v_pointer = PointerFromWrapper(value);
646646
if (transfer == GI_TRANSFER_EVERYTHING) {
647-
GType gtype = g_registered_type_info_get_g_type (gi_info);
648-
size_t size;
649-
650-
if (gtype != G_TYPE_NONE) {
651-
arg->v_pointer = g_boxed_copy (gtype, arg->v_pointer);
652-
} else if ((size = Boxed::GetSize(gi_info)) != 0) {
653-
void *boxedCopy = malloc(size);
654-
memcpy(boxedCopy, arg->v_pointer, size);
655-
arg->v_pointer = boxedCopy;
656-
} else {
657-
// XXX what else can we do? Should we just abort?
658-
WARN("Cannot copy a boxed object of type '%s'; memory corruption might occur.",
659-
g_base_info_get_name(gi_info));
660-
}
647+
arg->v_pointer = CopyBoxed(gi_info, arg->v_pointer);
648+
if (!arg->v_pointer)
649+
return false;
661650
}
662651
break;
663652

0 commit comments

Comments
 (0)