From 00189c739672dc7da32387b1567dd61930e3836b Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Thu, 31 Dec 2015 00:02:15 +0200 Subject: [PATCH 1/4] Switch to a Rust-only macro-based implementation of module registration. --- src/lib.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6ae0ffb94..09ecb1f33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,65 @@ pub mod value; pub mod error; pub mod buffer; -use internal::vm::{Module, Throw}; -use internal::mem::Handle; -use internal::value::SomeObject; +/// The module version for Node.js 4.x is 46. +// TODO detect this based on what we're compiling for. +pub const NODE_MODULE_VERSION: i32 = 46; -#[no_mangle] -pub extern "C" fn neon_init(module: Handle, init: fn(Module) -> Result<(), Throw>) { - Module::initialize(module, init); +#[macro_export] +macro_rules! neon_module { + ($name:ident($module:ident) $init:block) => { + // Mark this function as a global constructor (like C++). + // TODO Support more OSes here. + #[cfg_attr(target_os = "linux", link_section = ".ctors")] + #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] + #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")] + pub static __LOAD_NEON_MODULE: extern "C" fn() = { + fn $name(mut $module: $crate::vm::Module) -> $crate::vm::Result<()> $init + + extern "C" fn __load_neon_module() { + // Put everything else in the ctor fn so the user fn can't see it. + #[repr(C)] + struct __NodeModule { + version: i32, + flags: u32, + dso_handle: *mut u8, + filename: *const u8, + register_func: Option, *mut u8, *mut u8)>, + context_register_func: Option, *mut u8, *mut u8, *mut u8)>, + modname: *const u8, + priv_data: *mut u8, + link: *mut __NodeModule + } + + static mut __NODE_MODULE: __NodeModule = __NodeModule { + version: $crate::NODE_MODULE_VERSION, + flags: 0, + dso_handle: 0 as *mut _, + filename: b"neon_source.rs\0" as *const u8, + register_func: Some(__register_neon_module), + context_register_func: None, + modname: b"neon_module\0" as *const u8, + priv_data: 0 as *mut _, + link: 0 as *mut _ + }; + + extern "C" fn __register_neon_module( + m: $crate::mem::Handle<$crate::value::SomeObject>, _: *mut u8, _: *mut u8) { + $crate::vm::Module::initialize(m, $name); + } + + extern "C" { + fn node_module_register(module: *mut __NodeModule); + } + + unsafe { + node_module_register(&mut __NODE_MODULE); + } + } + + __load_neon_module + }; + } } From e7ded2f28bc26b56231f3221452799b93bfb581f Mon Sep 17 00:00:00 2001 From: Dave Herman Date: Tue, 5 Jan 2016 08:36:08 -0800 Subject: [PATCH 2/4] tweaking the macro UI --- src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 09ecb1f33..2c88e19b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,20 +10,20 @@ pub mod value; pub mod error; pub mod buffer; -/// The module version for Node.js 4.x is 46. -// TODO detect this based on what we're compiling for. +// The module version for Node.js 4.x is 46. +// FIXME: detect this based on what we're compiling for. pub const NODE_MODULE_VERSION: i32 = 46; #[macro_export] -macro_rules! neon_module { - ($name:ident($module:ident) $init:block) => { +macro_rules! onload { + ($module:ident, $init:block) => { // Mark this function as a global constructor (like C++). - // TODO Support more OSes here. + // FIXME: Support more OSes here. #[cfg_attr(target_os = "linux", link_section = ".ctors")] #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")] pub static __LOAD_NEON_MODULE: extern "C" fn() = { - fn $name(mut $module: $crate::vm::Module) -> $crate::vm::Result<()> $init + fn __init_neon_module(mut $module: $crate::vm::Module) -> $crate::vm::Result<()> $init extern "C" fn __load_neon_module() { // Put everything else in the ctor fn so the user fn can't see it. @@ -56,7 +56,7 @@ macro_rules! neon_module { extern "C" fn __register_neon_module( m: $crate::mem::Handle<$crate::value::SomeObject>, _: *mut u8, _: *mut u8) { - $crate::vm::Module::initialize(m, $name); + $crate::vm::Module::initialize(m, __init_neon_module); } extern "C" { From db8a21e7105d3b4599d6456614f2f421a7dbeb0e Mon Sep 17 00:00:00 2001 From: Dave Herman Date: Tue, 5 Jan 2016 11:09:56 -0800 Subject: [PATCH 3/4] gets the ABI version from an env var set by `neon build` --- src/lib.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2c88e19b6..a11cbb90e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,15 +10,10 @@ pub mod value; pub mod error; pub mod buffer; -// The module version for Node.js 4.x is 46. -// FIXME: detect this based on what we're compiling for. -pub const NODE_MODULE_VERSION: i32 = 46; - #[macro_export] -macro_rules! onload { +macro_rules! register_module { ($module:ident, $init:block) => { // Mark this function as a global constructor (like C++). - // FIXME: Support more OSes here. #[cfg_attr(target_os = "linux", link_section = ".ctors")] #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")] #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")] @@ -43,7 +38,7 @@ macro_rules! onload { } static mut __NODE_MODULE: __NodeModule = __NodeModule { - version: $crate::NODE_MODULE_VERSION, + version: 0, flags: 0, dso_handle: 0 as *mut _, filename: b"neon_source.rs\0" as *const u8, @@ -64,6 +59,9 @@ macro_rules! onload { } 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(); + node_module_register(&mut __NODE_MODULE); } } From 7867478d11cff233cf39e40c7f9e316eccc9bd7c Mon Sep 17 00:00:00 2001 From: Dave Herman Date: Tue, 5 Jan 2016 11:56:28 -0800 Subject: [PATCH 4/4] v0.1.2 --- Cargo.toml | 2 +- crates/neon-sys/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 02e62b195..ef79bd7d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neon" -version = "0.1.1" +version = "0.1.2" authors = ["Dave Herman "] description = "A safe abstraction layer for Node.js." repository = "https://github.com/rustbridge/neon" diff --git a/crates/neon-sys/Cargo.toml b/crates/neon-sys/Cargo.toml index 2eb313036..344d641e0 100644 --- a/crates/neon-sys/Cargo.toml +++ b/crates/neon-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "neon-sys" -version = "0.1.1" +version = "0.1.2" authors = ["Dave Herman "] description = "Exposes Node and V8 C++ API's for use by Neon." repository = "https://github.com/dherman/neon"