Skip to content

Commit

Permalink
Merge pull request #35 from rustbridge/pure-rust-module-registration
Browse files Browse the repository at this point in the history
Pure rust module registration (closes #33)
  • Loading branch information
Dave Herman committed Jan 5, 2016
2 parents 65a3bb4 + 7867478 commit 23e2f13
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neon"
version = "0.1.1"
version = "0.1.2"
authors = ["Dave Herman <[email protected]>"]
description = "A safe abstraction layer for Node.js."
repository = "https://github.com/rustbridge/neon"
Expand Down
2 changes: 1 addition & 1 deletion crates/neon-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neon-sys"
version = "0.1.1"
version = "0.1.2"
authors = ["Dave Herman <[email protected]>"]
description = "Exposes Node and V8 C++ API's for use by Neon."
repository = "https://github.com/dherman/neon"
Expand Down
64 changes: 58 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,63 @@ pub mod value;
pub mod error;
pub mod buffer;

use internal::vm::{Module, Throw};
use internal::mem::Handle;
use internal::value::SomeObject;
#[macro_export]
macro_rules! register_module {
($module:ident, $init:block) => {
// Mark this function as a global constructor (like C++).
#[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 __init_neon_module(mut $module: $crate::vm::Module) -> $crate::vm::Result<()> $init

#[no_mangle]
pub extern "C" fn neon_init(module: Handle<SomeObject>, init: fn(Module) -> Result<(), Throw>) {
Module::initialize(module, 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<extern "C" fn(
$crate::mem::Handle<$crate::value::SomeObject>, *mut u8, *mut u8)>,
context_register_func: Option<extern "C" fn(
$crate::mem::Handle<$crate::value::SomeObject>, *mut u8, *mut u8, *mut u8)>,
modname: *const u8,
priv_data: *mut u8,
link: *mut __NodeModule
}

static mut __NODE_MODULE: __NodeModule = __NodeModule {
version: 0,
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, __init_neon_module);
}

extern "C" {
fn node_module_register(module: *mut __NodeModule);
}

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);
}
}

__load_neon_module
};
}
}

0 comments on commit 23e2f13

Please sign in to comment.