Skip to content

Commit

Permalink
Make to_local() take a closure instead of a raw pointer (#386)
Browse files Browse the repository at this point in the history
This makes it possible to add a run-time check that verifies that the
specified closure is actually the one that contains the local handle.
  • Loading branch information
piscisaureus committed May 31, 2020
1 parent cfbfb95 commit bdd5fc4
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 172 deletions.
21 changes: 12 additions & 9 deletions src/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,24 @@ impl ArrayBuffer {
scope: &mut impl ToLocal<'sc>,
byte_length: usize,
) -> Local<'sc, ArrayBuffer> {
let isolate = scope.isolate();
let ptr =
unsafe { v8__ArrayBuffer__New__with_byte_length(isolate, byte_length) };
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope.to_local(|scope| {
v8__ArrayBuffer__New__with_byte_length(scope.isolate(), byte_length)
})
}
.unwrap()
}

pub fn with_backing_store<'sc>(
scope: &mut impl ToLocal<'sc>,
backing_store: &SharedRef<BackingStore>,
) -> Local<'sc, ArrayBuffer> {
let isolate = scope.isolate();
let ptr = unsafe {
v8__ArrayBuffer__New__with_backing_store(isolate, backing_store)
};
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope.to_local(|scope| {
v8__ArrayBuffer__New__with_backing_store(scope.isolate(), backing_store)
})
}
.unwrap()
}

/// Data length in bytes.
Expand Down
2 changes: 1 addition & 1 deletion src/array_buffer_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ArrayBufferView {
&self,
scope: &'_ mut impl ToLocal<'sc>,
) -> Option<Local<'sc, ArrayBuffer>> {
unsafe { scope.to_local(v8__ArrayBufferView__Buffer(self)) }
unsafe { scope.to_local(|_| v8__ArrayBufferView__Buffer(self)) }
}

/// Size of a view in bytes.
Expand Down
14 changes: 9 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ impl Context {
/// Creates a new context.
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Context> {
// TODO: optional arguments;
let ptr = unsafe { v8__Context__New(scope.isolate(), null(), null()) };
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope.to_local(|scope| v8__Context__New(scope.isolate(), null(), null()))
}
.unwrap()
}

/// Creates a new context using the object template as the template for
Expand All @@ -33,8 +35,10 @@ impl Context {
scope: &mut impl ToLocal<'sc>,
templ: Local<ObjectTemplate>,
) -> Local<'sc, Context> {
let ptr = unsafe { v8__Context__New(scope.isolate(), &*templ, null()) };
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope.to_local(|scope| v8__Context__New(scope.isolate(), &*templ, null()))
}
.unwrap()
}

/// Returns the global proxy object.
Expand All @@ -51,7 +55,7 @@ impl Context {
&self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Object> {
unsafe { scope.to_local(v8__Context__Global(self)) }.unwrap()
unsafe { scope.to_local(|_| v8__Context__Global(self)) }.unwrap()
}

/// Enter this context. After entering a context, all code compiled
Expand Down
85 changes: 46 additions & 39 deletions src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl StackTrace {
scope: &mut impl ToLocal<'sc>,
index: usize,
) -> Option<Local<'sc, StackFrame>> {
let isolate = scope.isolate();
unsafe {
let ptr = v8__StackTrace__GetFrame(self, isolate, index as u32);
scope.to_local(ptr)
scope.to_local(|scope| {
v8__StackTrace__GetFrame(self, scope.isolate(), index as u32)
})
}
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl StackFrame {
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
unsafe { scope.to_local(v8__StackFrame__GetScriptName(self)) }
unsafe { scope.to_local(|_| v8__StackFrame__GetScriptName(self)) }
}

/// Returns the name of the resource that contains the script for the
Expand All @@ -129,15 +129,17 @@ impl StackFrame {
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
unsafe { scope.to_local(v8__StackFrame__GetScriptNameOrSourceURL(self)) }
unsafe {
scope.to_local(|_| v8__StackFrame__GetScriptNameOrSourceURL(self))
}
}

/// Returns the name of the function associated with this stack frame.
pub fn get_function_name<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, String>> {
unsafe { scope.to_local(v8__StackFrame__GetFunctionName(self)) }
unsafe { scope.to_local(|_| v8__StackFrame__GetFunctionName(self)) }
}

/// Returns whether or not the associated function is compiled via a call to
Expand Down Expand Up @@ -165,7 +167,7 @@ impl StackFrame {

impl Message {
pub fn get<'sc>(&self, scope: &mut impl ToLocal<'sc>) -> Local<'sc, String> {
unsafe { scope.to_local(v8__Message__Get(self)) }.unwrap()
unsafe { scope.to_local(|_| v8__Message__Get(self)) }.unwrap()
}

/// Exception stack trace. By default stack traces are not captured for
Expand All @@ -175,15 +177,15 @@ impl Message {
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, StackTrace>> {
unsafe { scope.to_local(v8__Message__GetStackTrace(self)) }
unsafe { scope.to_local(|_| v8__Message__GetStackTrace(self)) }
}

pub fn get_source_line<'s>(
&self,
scope: &mut impl ToLocal<'s>,
context: Local<Context>,
) -> Option<Local<'s, String>> {
unsafe { scope.to_local(v8__Message__GetSourceLine(self, &*context)) }
unsafe { scope.to_local(|_| v8__Message__GetSourceLine(self, &*context)) }
}

/// Returns the resource name for the script from where the function causing
Expand All @@ -192,7 +194,7 @@ impl Message {
&self,
scope: &mut impl ToLocal<'s>,
) -> Option<Local<'s, Value>> {
unsafe { scope.to_local(v8__Message__GetScriptResourceName(self)) }
unsafe { scope.to_local(|_| v8__Message__GetScriptResourceName(self)) }
}

/// Returns the number, 1-based, of the line where the error occurred.
Expand Down Expand Up @@ -260,55 +262,57 @@ impl Exception {
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
) -> Local<'sc, Value> {
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__Error(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
scope.isolate().enter();
let e =
unsafe { scope.to_local(|_| v8__Exception__Error(&*message)) }.unwrap();
scope.isolate().exit();
e
}

pub fn range_error<'sc>(
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
) -> Local<'sc, Value> {
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__RangeError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
scope.isolate().enter();
let e = unsafe { scope.to_local(|_| v8__Exception__RangeError(&*message)) }
.unwrap();
scope.isolate().exit();
e
}

pub fn reference_error<'sc>(
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
) -> Local<'sc, Value> {
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__ReferenceError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
scope.isolate().enter();
let e =
unsafe { scope.to_local(|_| v8__Exception__ReferenceError(&*message)) }
.unwrap();
scope.isolate().exit();
e
}

pub fn syntax_error<'sc>(
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
) -> Local<'sc, Value> {
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__SyntaxError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
scope.isolate().enter();
let e =
unsafe { scope.to_local(|_| v8__Exception__SyntaxError(&*message)) }
.unwrap();
scope.isolate().exit();
e
}

pub fn type_error<'sc>(
scope: &mut impl ToLocal<'sc>,
message: Local<String>,
) -> Local<'sc, Value> {
let isolate = scope.isolate();
isolate.enter();
let e = unsafe { v8__Exception__TypeError(&*message) };
isolate.exit();
unsafe { scope.to_local(e) }.unwrap()
scope.isolate().enter();
let e = unsafe { scope.to_local(|_| v8__Exception__TypeError(&*message)) }
.unwrap();
scope.isolate().exit();
e
}

/// Creates an error message for the given exception.
Expand All @@ -318,9 +322,12 @@ impl Exception {
scope: &mut impl ToLocal<'sc>,
exception: Local<Value>,
) -> Local<'sc, Message> {
let isolate = scope.isolate();
let ptr = unsafe { v8__Exception__CreateMessage(isolate, &*exception) };
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope.to_local(|scope| {
v8__Exception__CreateMessage(scope.isolate(), &*exception)
})
}
.unwrap()
}

/// Returns the original stack trace that was captured at the creation time
Expand All @@ -329,6 +336,6 @@ impl Exception {
scope: &mut impl ToLocal<'sc>,
exception: Local<Value>,
) -> Option<Local<'sc, StackTrace>> {
unsafe { scope.to_local(v8__Exception__GetStackTrace(&*exception)) }
unsafe { scope.to_local(|_| v8__Exception__GetStackTrace(&*exception)) }
}
}
15 changes: 7 additions & 8 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'cb> ReturnValue<'cb> {
&mut self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Value> {
unsafe { scope.to_local(v8__ReturnValue__Get(self)) }.unwrap()
unsafe { scope.to_local(|_| v8__ReturnValue__Get(self)) }.unwrap()
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ impl Function {
callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope.to_local(v8__Function__New(&*context, callback.map_fn_to()))
scope.to_local(|_| v8__Function__New(&*context, callback.map_fn_to()))
}
}

Expand All @@ -304,11 +304,9 @@ impl Function {
callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'sc, Function>> {
unsafe {
scope.to_local(v8__Function__NewWithData(
&*context,
callback.map_fn_to(),
&*data,
))
scope.to_local(|_| {
v8__Function__NewWithData(&*context, callback.map_fn_to(), &*data)
})
}
}

Expand All @@ -323,7 +321,8 @@ impl Function {
let argc = int::try_from(args.len()).unwrap();
let argv = args.as_ptr();
unsafe {
scope.to_local(v8__Function__Call(self, &*context, &*recv, argc, argv))
scope
.to_local(|_| v8__Function__Call(self, &*context, &*recv, argc, argv))
}
}
}
8 changes: 4 additions & 4 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl<T> Global<T> {
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, T>> {
let isolate = scope.isolate();
self.check_isolate(isolate);
self.check_isolate(scope.isolate());
self
.value
.map(|g| g.as_ptr() as *const Data)
.map(|g| unsafe { v8__Local__New(isolate, g) })
.and_then(|l| unsafe { scope.to_local(l as *const T) })
.and_then(|g| unsafe {
scope.to_local(|scope| v8__Local__New(scope.isolate(), g) as *const T)
})
}

/// If non-empty, destroy the underlying storage cell
Expand Down
2 changes: 1 addition & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ impl Module {
scope: &mut impl ToLocal<'sc>,
context: Local<Context>,
) -> Option<Local<'sc, Value>> {
unsafe { scope.to_local(v8__Module__Evaluate(&*self, &*context)) }
unsafe { scope.to_local(|_| v8__Module__Evaluate(&*self, &*context)) }
}
}
15 changes: 9 additions & 6 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl Number {
scope: &mut impl ToLocal<'sc>,
value: f64,
) -> Local<'sc, Number> {
let local = unsafe { v8__Number__New(scope.isolate(), value) };
unsafe { scope.to_local(local) }.unwrap()
unsafe { scope.to_local(|scope| v8__Number__New(scope.isolate(), value)) }
.unwrap()
}

pub fn value(&self) -> f64 {
Expand All @@ -34,16 +34,19 @@ impl Integer {
scope: &mut impl ToLocal<'sc>,
value: i32,
) -> Local<'sc, Integer> {
let local = unsafe { v8__Integer__New(scope.isolate(), value) };
unsafe { scope.to_local(local) }.unwrap()
unsafe { scope.to_local(|scope| v8__Integer__New(scope.isolate(), value)) }
.unwrap()
}

pub fn new_from_unsigned<'sc>(
scope: &mut impl ToLocal<'sc>,
value: u32,
) -> Local<'sc, Integer> {
let local = unsafe { v8__Integer__NewFromUnsigned(scope.isolate(), value) };
unsafe { scope.to_local(local) }.unwrap()
unsafe {
scope
.to_local(|scope| v8__Integer__NewFromUnsigned(scope.isolate(), value))
}
.unwrap()
}

pub fn value(&self) -> i64 {
Expand Down
Loading

0 comments on commit bdd5fc4

Please sign in to comment.