Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/boxed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Local<Value> WrapperFromBoxed(GIBaseInfo *info, void *data, ResourceOwnership ow
return instance.ToLocalChecked();
}

void* PointerFromWrapper(Local<Value> value) {
void* BoxedFromWrapper(Local<Value> value) {
Local<Object> object = TO_OBJECT (value);
g_assert(object->InternalFieldCount() > 0);
void *boxed = object->GetAlignedPointerFromInternalField(0);
Expand Down
2 changes: 1 addition & 1 deletion src/boxed.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class Boxed {
Local<Function> MakeBoxedClass (GIBaseInfo *info);
Local<FunctionTemplate> GetBoxedTemplate (GIBaseInfo *info, GType gtype);
Local<Value> WrapperFromBoxed (GIBaseInfo *info, void *data, ResourceOwnership ownership = kNone);
void * PointerFromWrapper (Local<Value>);
void * BoxedFromWrapper (Local<Value>);

};
27 changes: 21 additions & 6 deletions src/closure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Value;
using Nan::Persistent;

namespace GNodeJS {

GClosure *Closure::New (Local<Function> function, GICallableInfo* info, guint signalId) {
Closure *closure = (Closure *) g_closure_new_simple (sizeof (*closure), GUINT_TO_POINTER(signalId));
closure->persistent.Reset(function);
closure->isolate = function->GetIsolate();
closure->functionRef.Reset(closure->isolate, function);
if (info) {
closure->info = g_base_info_ref(info);
} else {
Expand All @@ -34,11 +34,12 @@ GClosure *Closure::New (Local<Function> function, GICallableInfo* info, guint si
}

void Closure::Execute(GICallableInfo *info, guint signal_id,
const Nan::Persistent<v8::Function> &persFn,
v8::Isolate *isolate,
const v8::TracedReference<v8::Function> &fnRef,
GValue *g_return_value, guint n_param_values,
const GValue *param_values) {
Nan::HandleScope scope;
auto func = Nan::New<Function>(persFn);
auto func = fnRef.Get(isolate);

GSignalQuery signal_query = { 0, };

Expand Down Expand Up @@ -139,11 +140,25 @@ void Closure::Marshal(GClosure *base,

if (env->IsSameThread()) {
/* Case 1: same thread */
Closure::Execute(closure->info, signal_id, closure->persistent, g_return_value, n_param_values, param_values);
Closure::Execute(
closure->info,
signal_id,
closure->isolate,
closure->functionRef,
g_return_value,
n_param_values,
param_values);
} else {
/* Case 2: different thread */
env->Call([&]() {
Closure::Execute(closure->info, signal_id, closure->persistent, g_return_value, n_param_values, param_values);
Closure::Execute(
closure->info,
signal_id,
closure->isolate,
closure->functionRef,
g_return_value,
n_param_values,
param_values);
});
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ namespace GNodeJS {

struct Closure {
GClosure base;
Nan::Persistent<v8::Function> persistent;
v8::Isolate *isolate;
v8::TracedReference<v8::Function> functionRef;
GICallableInfo* info;

~Closure() {
persistent.Reset();
functionRef.Reset();
if (info) g_base_info_unref (info);
}

Expand All @@ -29,7 +30,8 @@ struct Closure {
guint signalId);

static void Execute(GICallableInfo *info, guint signal_id,
const Nan::Persistent<v8::Function> &persFn,
v8::Isolate *isolate,
const v8::TracedReference<v8::Function> &persFn,
GValue *g_return_value, guint n_param_values,
const GValue *param_values);

Expand Down
8 changes: 8 additions & 0 deletions src/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ Local<Value> FunctionCall (
return jsReturnValue;
}

static void* PointerFromWrapper(Local<Value> value) {
/* FIXME: find a better way to do this. */
if (ValueIsInstanceOfGType(value, G_TYPE_OBJECT)) {
return GObjectFromWrapper(value);
} else {
return BoxedFromWrapper(value);
}
}

/**
* Creates the JS return value from the C arguments list
Expand Down
12 changes: 6 additions & 6 deletions src/gi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ NAN_METHOD(Bootstrap) {
}

NAN_METHOD(GetConstantValue) {
GIBaseInfo *gi_info = (GIBaseInfo *) GNodeJS::PointerFromWrapper (info[0]);
GIBaseInfo *gi_info = (GIBaseInfo *) GNodeJS::BoxedFromWrapper (info[0]);
GITypeInfo *type_info = g_constant_info_get_type(gi_info);

if (type_info == NULL) {
Expand Down Expand Up @@ -213,8 +213,8 @@ NAN_METHOD(StructFieldSetter) {
Local<Object> fieldInfo = info[1].As<Object>();
Local<Value> value = info[2];

void *boxed = GNodeJS::PointerFromWrapper(boxedWrapper);
GIFieldInfo *field = (GIFieldInfo *) GNodeJS::PointerFromWrapper(fieldInfo);
void *boxed = GNodeJS::BoxedFromWrapper(boxedWrapper);
GIFieldInfo *field = (GIFieldInfo *) GNodeJS::BoxedFromWrapper(fieldInfo);
GITypeInfo *field_type = g_field_info_get_type(field);

g_assert(boxed);
Expand Down Expand Up @@ -267,10 +267,10 @@ NAN_METHOD(StructFieldGetter) {
return;
}

auto boxed = GNodeJS::PointerFromWrapper(jsBoxed);
auto boxed = GNodeJS::BoxedFromWrapper(jsBoxed);
// ref it because the unwrapped ref belongs to the JS wrapper
BaseInfo fieldInfo =
g_base_info_ref((GIFieldInfo *) GNodeJS::PointerFromWrapper(jsFieldInfo));
g_base_info_ref((GIFieldInfo *) GNodeJS::BoxedFromWrapper(jsFieldInfo));

if (boxed == NULL) {
Nan::ThrowError("StructFieldGetter: instance is NULL");
Expand Down Expand Up @@ -320,7 +320,7 @@ NAN_METHOD(GetBaseClass) {
}

NAN_METHOD(GetTypeSize) {
GITypeInfo *gi_info = (GITypeInfo *) GNodeJS::PointerFromWrapper (info[0]);
GITypeInfo *gi_info = (GITypeInfo *) GNodeJS::BoxedFromWrapper (info[0]);
auto size = GNodeJS::GetTypeSize (gi_info);
info.GetReturnValue().Set(Nan::New<Number>(size));
}
Expand Down
2 changes: 1 addition & 1 deletion src/gi.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class BaseInfo {
BaseInfo (Local<Value> value) {
Local<Object> object = value.As<Object>();
_info = g_base_info_ref(
(GIBaseInfo *) GNodeJS::PointerFromWrapper(object));
(GIBaseInfo *) GNodeJS::BoxedFromWrapper(object));
};

~BaseInfo () {
Expand Down
Loading
Loading