Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix neon on new node versions, and make it work on Electron as well #123

Closed
Closed
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
3 changes: 3 additions & 0 deletions crates/neon-sys/src/fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "system" {
#[link_name = "NeonSys_Fun_New"]
pub fn new(out: &mut Local, isolate: *mut c_void, callback: *mut c_void, kernel: *mut c_void) -> bool;

#[link_name = "NeonSys_Fun_New_Template"]
pub fn new_template(out: &mut Local, isolate: *mut c_void, callback: *mut c_void, kernel: *mut c_void) -> bool;

#[link_name = "NeonSys_Fun_ExecKernel"]
pub fn exec_kernel(kernel: *mut c_void, callback: extern fn(*mut c_void, *mut c_void, *mut c_void), info: &FunctionCallbackInfo, scope: *mut c_void);

Expand Down
3 changes: 3 additions & 0 deletions crates/neon-sys/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ extern "system" {
#[link_name = "NeonSys_Module_ExecKernel"]
pub fn exec_kernel(kernel: *mut c_void, callback: extern fn(*mut c_void, *mut c_void, *mut c_void), exports: Local, scope: *mut c_void);

#[link_name = "NeonSys_Module_GetVersion"]
pub fn get_version() -> i32;

}
15 changes: 14 additions & 1 deletion crates/neon-sys/src/neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ extern "C" void NeonSys_Module_ExecKernel(void *kernel, NeonSys_ModuleScopeCallb
callback(kernel, exports, scope);
}

extern "C" uint32_t NeonSys_Module_GetVersion() {
return NODE_MODULE_VERSION;
}

extern "C" void NeonSys_Class_ConstructBaseCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
Nan::HandleScope scope;
v8::Local<v8::External> wrapper = v8::Local<v8::External>::Cast(info.Data());
Expand Down Expand Up @@ -351,7 +355,7 @@ extern "C" void NeonSys_Class_ThrowThisError(v8::Isolate *isolate, void *metadat
Nan::ThrowTypeError(metadata->GetThisError().ToJsString(isolate, "this is not an object of the expected type."));
}

extern "C" bool NeonSys_Class_AddMethod(v8::Isolate *isolate, void *metadata_pointer, const char *name, uint32_t byte_length, v8::Local<v8::Function> method) {
extern "C" bool NeonSys_Class_AddMethod(v8::Isolate *isolate, void *metadata_pointer, const char *name, uint32_t byte_length, v8::Local<v8::FunctionTemplate> method) {
neon::ClassMetadata *metadata = static_cast<neon::ClassMetadata *>(metadata_pointer);
v8::Local<v8::FunctionTemplate> ft = metadata->GetTemplate(isolate);
v8::Local<v8::ObjectTemplate> pt = ft->PrototypeTemplate();
Expand All @@ -373,6 +377,15 @@ extern "C" void *NeonSys_Class_GetInstanceInternals(v8::Local<v8::Object> obj) {
return static_cast<neon::BaseClassInstanceMetadata *>(obj->GetAlignedPointerFromInternalField(0))->GetInternals();
}

extern "C" bool NeonSys_Fun_New_Template(v8::Local<v8::FunctionTemplate> *out, v8::Isolate *isolate, v8::FunctionCallback callback, void *kernel) {
v8::Local<v8::External> wrapper = v8::External::New(isolate, kernel);
if (wrapper.IsEmpty()) {
return false;
}

v8::MaybeLocal<v8::FunctionTemplate> maybe_result = v8::FunctionTemplate::New(isolate, callback, wrapper);
return maybe_result.ToLocal(out);
}

extern "C" bool NeonSys_Fun_New(v8::Local<v8::Function> *out, v8::Isolate *isolate, v8::FunctionCallback callback, void *kernel) {
v8::Local<v8::External> wrapper = v8::External::New(isolate, kernel);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/src/neon.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extern "C" {
bool NeonSys_Class_HasInstance(void *metadata, v8::Local<v8::Value> v);
bool NeonSys_Class_SetName(v8::Isolate *isolate, void *metadata, const char *name, uint32_t byte_length);
void NeonSys_Class_ThrowThisError(v8::Isolate *isolate, void *metadata_pointer);
bool NeonSys_Class_AddMethod(v8::Isolate *isolate, void *metadata, const char *name, uint32_t byte_length, v8::Local<v8::Function> method);
bool NeonSys_Class_AddMethod(v8::Isolate *isolate, void *metadata, const char *name, uint32_t byte_length, v8::Local<v8::FunctionTemplate> method);
void NeonSys_Class_MetadataToClass(v8::Local<v8::FunctionTemplate> *out, v8::Isolate *isolate, void *metadata);
void *NeonSys_Class_GetInstanceInternals(v8::Local<v8::Object> obj);

Expand Down
4 changes: 2 additions & 2 deletions src/internal/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ pub trait ClassInternal: Class {
}

for (name, method) in descriptor.methods {
let method: Handle<JsFunction> = try!(build(|out| {
let method: Handle<JsValue> = try!(build(|out| {
let (method_callback, method_kernel) = method.export();
neon_sys::fun::new(out, isolate, method_callback, method_kernel)
neon_sys::fun::new_template(out, isolate, method_callback, method_kernel)
}));
if !neon_sys::class::add_method(isolate, metadata_pointer, name.as_ptr(), name.len() as u32, method.to_raw()) {
return Err(Throw);
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ macro_rules! register_module {
}

unsafe {
// Set the ABI version, which is passed in by `neon build` as an env var.
__NODE_MODULE.version = env!("NEON_NODE_ABI").parse().unwrap();

// Set the ABI version based on the NODE_MODULE_VERSION constant provided by the current node headers.
__NODE_MODULE.version = $crate::macro_internal::sys::module::get_version();
node_module_register(&mut __NODE_MODULE);
}
}
Expand Down