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

Create an API for accessing the global object #249 #253

Merged
merged 2 commits into from
Oct 11, 2017
Merged
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
5 changes: 5 additions & 0 deletions crates/neon-runtime/src/neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ extern "C" size_t Neon_Scope_AlignofEscapable() {
return alignof(v8::EscapableHandleScope);
}

extern "C" void Neon_Scope_GetGlobal(v8::Isolate *isolate, v8::Local<v8::Value> *out) {
auto ctx = isolate->GetCurrentContext();
*out = ctx->Global();
}

extern "C" void Neon_Fun_ExecKernel(void *kernel, Neon_RootScopeCallback callback, v8::FunctionCallbackInfo<v8::Value> *info, void *scope) {
Nan::HandleScope v8_scope;
callback(info, kernel, scope);
Expand Down
1 change: 1 addition & 0 deletions crates/neon-runtime/src/neon.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ extern "C" {
size_t Neon_Scope_Alignof();
size_t Neon_Scope_SizeofEscapable();
size_t Neon_Scope_AlignofEscapable();
void Neon_Scope_GetGlobal(v8::Isolate *isolate, v8::Local<v8::Value> *out);

bool Neon_Fun_New(v8::Local<v8::Function> *out, v8::Isolate *isolate, v8::FunctionCallback callback, void *kernel);
void Neon_Fun_ExecKernel(void *kernel, Neon_RootScopeCallback callback, v8::FunctionCallbackInfo<v8::Value> *info, void *scope);
Expand Down
5 changes: 5 additions & 0 deletions crates/neon-runtime/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ extern "C" {
#[link_name = "Neon_Scope_AlignofEscapable"]
pub fn escapable_alignment() -> usize;

/// Mutates the `out` argument provided to refer to the `v8::Local` value of the `global`
/// object
#[link_name = "Neon_Scope_GetGlobal"]
pub fn get_global(isolate: *mut c_void, out: &mut Local);

}
10 changes: 9 additions & 1 deletion src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cell::RefCell;
use neon_runtime;
use neon_runtime::raw;
use mem::Handle;
use js::Value;
use js::{Value, JsObject};
use vm::internal::Isolate;
use self::internal::ScopeInternal;

Expand All @@ -24,6 +24,14 @@ pub(crate) mod internal {
pub trait Scope<'a>: ScopeInternal {
fn nested<T, F: for<'inner> FnOnce(&mut NestedScope<'inner>) -> T>(&self, f: F) -> T;
fn chained<T, F: for<'inner> FnOnce(&mut ChainedScope<'inner, 'a>) -> T>(&self, f: F) -> T;

fn global(&self) -> Handle<'a, JsObject> {
JsObject::build(|out| {
unsafe {
neon_runtime::scope::get_global(self.isolate().to_raw(), out);
}
})
}
}

fn ensure_active<T: ScopeInternal>(scope: &T) {
Expand Down
4 changes: 4 additions & 0 deletions test/dynamic/lib/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var addon = require('../native');
var assert = require('chai').assert;

describe('JsObject', function() {
it('return the v8::Global object', function () {
assert(global === addon.return_js_global_object());
});

it('return a JsObject built in Rust', function () {
assert.deepEqual({}, addon.return_js_object());
});
Expand Down
6 changes: 6 additions & 0 deletions test/dynamic/native/src/js/objects.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use neon::vm::{Call, JsResult};
use neon::mem::Handle;
use neon::js::{JsNumber, JsString, JsObject, Object};
use neon::scope::Scope;

pub fn return_js_global_object(call: Call) -> JsResult<JsObject> {
let scope = call.scope;
Ok(scope.global())
}

pub fn return_js_object(call: Call) -> JsResult<JsObject> {
Ok(JsObject::new(call.scope))
Expand Down
1 change: 1 addition & 0 deletions test/dynamic/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ register_module!(m, {
m.export("return_js_array_with_number", return_js_array_with_number)?;
m.export("return_js_array_with_string", return_js_array_with_string)?;

m.export("return_js_global_object", return_js_global_object)?;
m.export("return_js_object", return_js_object)?;
m.export("return_js_object_with_number", return_js_object_with_number)?;
m.export("return_js_object_with_string", return_js_object_with_string)?;
Expand Down