0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

Remove LockedIsolate

This commit is contained in:
Ryan Dahl 2019-12-19 20:11:22 -05:00 committed by Ry Dahl
parent 71140b4cc4
commit 887af28790
12 changed files with 154 additions and 189 deletions

View file

@ -1,12 +1,10 @@
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Object;
extern "C" {
fn v8__Context__New(isolate: *mut Isolate) -> *mut Context;
fn v8__Context__New(isolate: &Isolate) -> *mut Context;
fn v8__Context__Enter(this: &mut Context);
fn v8__Context__Exit(this: &mut Context);
fn v8__Context__GetIsolate(this: &mut Context) -> *mut Isolate;
@ -17,9 +15,9 @@ extern "C" {
pub struct Context(Opaque);
impl Context {
pub fn new<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Context> {
pub fn new<'sc>(isolate: &Isolate) -> Local<'sc, Context> {
// TODO: optional arguments;
unsafe { Local::from_raw(v8__Context__New(scope.cxx_isolate())).unwrap() }
unsafe { Local::from_raw(v8__Context__New(isolate)).unwrap() }
}
/// Returns the global proxy object.

View file

@ -18,7 +18,7 @@ extern "C" {
fn v8__Exception__Error(message: *mut String) -> *mut Value;
fn v8__Exception__CreateMessage(
isolate: *mut Isolate,
isolate: &Isolate,
exception: *mut Value,
) -> *mut Message;
@ -52,20 +52,16 @@ impl Message {
/// constructor with the message.
pub mod Exception {
use super::*;
use crate::isolate::LockedIsolate;
/// Creates an error message for the given exception.
/// Will try to reconstruct the original stack trace from the exception value,
/// or capture the current stack trace if not available.
pub fn CreateMessage<'sc>(
isolate: &mut impl LockedIsolate,
isolate: &Isolate,
mut exception: Local<'sc, Value>,
) -> Local<'sc, Message> {
unsafe {
Local::from_raw(v8__Exception__CreateMessage(
isolate.cxx_isolate(),
&mut *exception,
))
Local::from_raw(v8__Exception__CreateMessage(isolate, &mut *exception))
}
.unwrap()
}

View file

@ -1,7 +1,6 @@
use crate::isolate::{Isolate, LockedIsolate};
use crate::support::{int, Opaque};
use crate::Context;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Value;
@ -19,7 +18,7 @@ extern "C" {
) -> *mut Value;
fn v8__FunctionTemplate__New(
isolate: *mut Isolate,
isolate: &Isolate,
callback: extern "C" fn(&FunctionCallbackInfo),
) -> *mut FunctionTemplate;
fn v8__FunctionTemplate__GetFunction(
@ -62,10 +61,7 @@ impl ReturnValue {
/// Getter. Creates a new Local<> so it comes with a certain performance
/// hit. If the ReturnValue was not yet set, this will return the undefined
/// value.
pub fn get<'sc>(
&mut self,
_scope: &mut HandleScope<'sc>,
) -> Local<'sc, Value> {
pub fn get<'sc>(&mut self) -> Local<'sc, Value> {
unsafe { Local::from_raw(v8__ReturnValue__Get(&mut *self)).unwrap() }
}
}
@ -120,15 +116,11 @@ pub struct FunctionTemplate(Opaque);
impl FunctionTemplate {
/// Creates a function template.
pub fn new(
isolate: &mut impl LockedIsolate,
isolate: &Isolate,
callback: extern "C" fn(&FunctionCallbackInfo),
) -> Local<'_, FunctionTemplate> {
unsafe {
Local::from_raw(v8__FunctionTemplate__New(
isolate.cxx_isolate(),
callback,
))
.unwrap()
Local::from_raw(v8__FunctionTemplate__New(isolate, callback)).unwrap()
}
}

View file

@ -2,7 +2,6 @@ use std::marker::PhantomData;
use std::mem::MaybeUninit;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
extern "C" {
fn v8__HandleScope__CONSTRUCT(
@ -27,13 +26,10 @@ impl<'sc> HandleScope<'sc> {
unsafe { v8__HandleScope__CONSTRUCT(&mut scope, isolate) };
let scope = unsafe { &mut *(scope.as_mut_ptr()) };
f(scope);
unsafe { v8__HandleScope__DESTRUCT(scope) };
}
}
impl<'sc> LockedIsolate for HandleScope<'sc> {
fn cxx_isolate(&mut self) -> &mut Isolate {
fn get_isolate(&self) -> &Isolate {
unsafe { v8__HandleScope__GetIsolate(self) }
}
}

View file

@ -36,10 +36,6 @@ extern "C" {
#[repr(C)]
pub struct Isolate(Opaque);
pub trait LockedIsolate {
fn cxx_isolate(&mut self) -> &mut Isolate;
}
impl Isolate {
/// Creates a new isolate. Does not change the currently entered
/// isolate.

View file

@ -1,18 +1,16 @@
use std::ops::Deref;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::value::Value;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Value;
extern "C" {
fn v8__Number__New(isolate: &mut Isolate, value: f64) -> *mut Number;
fn v8__Number__New(isolate: &Isolate, value: f64) -> *mut Number;
fn v8__Number__Value(this: &Number) -> f64;
fn v8__Integer__New(isolate: &mut Isolate, value: i32) -> *mut Integer;
fn v8__Integer__New(isolate: &Isolate, value: i32) -> *mut Integer;
fn v8__Integer__NewFromUnsigned(
isolate: &mut Isolate,
isolate: &Isolate,
value: u32,
) -> *mut Integer;
fn v8__Integer__Value(this: &Integer) -> i64;
@ -23,12 +21,9 @@ extern "C" {
pub struct Number(Opaque);
impl Number {
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
value: f64,
) -> Local<'sc, Number> {
pub fn new<'sc>(isolate: &Isolate, value: f64) -> Local<'sc, Number> {
unsafe {
let local = v8__Number__New(scope.cxx_isolate(), value);
let local = v8__Number__New(isolate, value);
Local::from_raw(local).unwrap()
}
}
@ -50,22 +45,19 @@ impl Deref for Number {
pub struct Integer(Opaque);
impl Integer {
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
value: i32,
) -> Local<'sc, Integer> {
pub fn new<'sc>(isolate: &Isolate, value: i32) -> Local<'sc, Integer> {
unsafe {
let local = v8__Integer__New(scope.cxx_isolate(), value);
let local = v8__Integer__New(isolate, value);
Local::from_raw(local).unwrap()
}
}
pub fn new_from_unsigned<'sc>(
scope: &mut HandleScope<'sc>,
isolate: &Isolate,
value: u32,
) -> Local<'sc, Integer> {
unsafe {
let local = v8__Integer__NewFromUnsigned(scope.cxx_isolate(), value);
let local = v8__Integer__NewFromUnsigned(isolate, value);
Local::from_raw(local).unwrap()
}
}

View file

@ -1,9 +1,7 @@
use std::ops::Deref;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Name;
use crate::Value;
@ -14,7 +12,7 @@ pub struct Object(Opaque);
extern "C" {
fn v8__Object__New(
isolate: *mut Isolate,
isolate: &Isolate,
prototype_or_null: *mut Value,
names: *mut *mut Name,
values: *mut *mut Value,
@ -31,7 +29,7 @@ impl Object {
/// All properties will be created as enumerable, configurable
/// and writable properties.
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
isolate: &Isolate,
mut prototype_or_null: Local<'sc, Value>,
names: Vec<Local<'sc, Name>>,
values: Vec<Local<'sc, Value>>,
@ -50,7 +48,7 @@ impl Object {
}
unsafe {
Local::from_raw(v8__Object__New(
scope.cxx_isolate(),
isolate,
&mut *prototype_or_null,
names_.as_mut_ptr(),
values_.as_mut_ptr(),

View file

@ -1,9 +1,7 @@
use std::ops::Deref;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Value;
@ -21,31 +19,29 @@ pub struct Boolean(Opaque);
pub struct Name(Opaque);
extern "C" {
fn v8__Null(isolate: *mut Isolate) -> *mut Primitive;
fn v8__Null(isolate: &Isolate) -> *mut Primitive;
fn v8__Undefined(isolate: *mut Isolate) -> *mut Primitive;
fn v8__Undefined(isolate: &Isolate) -> *mut Primitive;
fn v8__True(isolate: *mut Isolate) -> *mut Boolean;
fn v8__True(isolate: &Isolate) -> *mut Boolean;
fn v8__False(isolate: *mut Isolate) -> *mut Boolean;
fn v8__False(isolate: &Isolate) -> *mut Boolean;
}
pub fn new_null<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Primitive> {
unsafe { Local::from_raw(v8__Null(scope.cxx_isolate())) }.unwrap()
pub fn new_null<'sc>(isolate: &Isolate) -> Local<'sc, Primitive> {
unsafe { Local::from_raw(v8__Null(isolate)) }.unwrap()
}
pub fn new_undefined<'sc>(
scope: &mut HandleScope<'sc>,
) -> Local<'sc, Primitive> {
unsafe { Local::from_raw(v8__Undefined(scope.cxx_isolate())) }.unwrap()
pub fn new_undefined<'sc>(isolate: &Isolate) -> Local<'sc, Primitive> {
unsafe { Local::from_raw(v8__Undefined(isolate)) }.unwrap()
}
pub fn new_true<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
unsafe { Local::from_raw(v8__True(scope.cxx_isolate())) }.unwrap()
pub fn new_true<'sc>(isolate: &Isolate) -> Local<'sc, Boolean> {
unsafe { Local::from_raw(v8__True(isolate)) }.unwrap()
}
pub fn new_false<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Boolean> {
unsafe { Local::from_raw(v8__False(scope.cxx_isolate())) }.unwrap()
pub fn new_false<'sc>(isolate: &Isolate) -> Local<'sc, Boolean> {
unsafe { Local::from_raw(v8__False(isolate)) }.unwrap()
}
impl Deref for Primitive {

View file

@ -2,7 +2,6 @@ use crate::support::MaybeBool;
use crate::support::Opaque;
use crate::Context;
use crate::Function;
use crate::HandleScope;
use crate::Local;
use crate::Value;
@ -78,10 +77,7 @@ impl Promise {
/// Returns the content of the [[PromiseResult]] field. The Promise must not
/// be pending.
pub fn result<'sc>(
&mut self,
_scope: &mut HandleScope<'sc>,
) -> Local<'sc, Value> {
pub fn result<'sc>(&mut self) -> Local<'sc, Value> {
unsafe { Local::from_raw(v8__Promise__Result(&mut *self)).unwrap() }
}
@ -152,10 +148,7 @@ impl PromiseResolver {
}
/// Extract the associated promise.
pub fn get_promise<'sc>(
&mut self,
_scope: &mut HandleScope<'sc>,
) -> Local<'sc, Promise> {
pub fn get_promise<'sc>(&mut self) -> Local<'sc, Promise> {
unsafe {
Local::from_raw(v8__Promise__Resolver__GetPromise(&mut *self)).unwrap()
}

View file

@ -4,7 +4,6 @@ use std::ptr::null;
use crate::support::Opaque;
use crate::Boolean;
use crate::Context;
use crate::HandleScope;
use crate::Integer;
use crate::Local;
use crate::String;
@ -44,7 +43,6 @@ pub struct Script(Opaque);
impl Script {
/// A shorthand for ScriptCompiler::Compile().
pub fn compile<'sc>(
_scope: &mut HandleScope<'sc>,
mut context: Local<'_, Context>,
mut source: Local<'_, String>,
origin: Option<&'_ ScriptOrigin>,
@ -65,7 +63,6 @@ impl Script {
/// UnboundScript::BindToCurrentContext()).
pub fn run<'sc>(
&mut self,
_scope: &mut HandleScope<'sc>,
mut context: Local<'_, Context>,
) -> Option<Local<Value>> {
unsafe { Local::from_raw(v8__Script__Run(self, &mut *context)) }

View file

@ -5,17 +5,15 @@ use std::ops::Deref;
use std::slice;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::char;
use crate::support::int;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Local;
use crate::Value;
extern "C" {
fn v8__String__NewFromUtf8(
isolate: *mut Isolate,
isolate: &Isolate,
data: *const char,
new_type: NewStringType,
length: int,
@ -23,11 +21,11 @@ extern "C" {
fn v8__String__Length(this: &String) -> int;
fn v8__String__Utf8Length(this: &String, isolate: *mut Isolate) -> int;
fn v8__String__Utf8Length(this: &String, isolate: &Isolate) -> int;
fn v8__String__WriteUtf8(
this: &String,
isolate: *mut Isolate,
isolate: &Isolate,
buffer: *mut char,
length: int,
nchars_ref: *mut int,
@ -68,13 +66,13 @@ pub struct String(Opaque);
impl String {
pub fn new_from_utf8<'sc>(
scope: &mut HandleScope<'sc>,
isolate: &Isolate,
buffer: &[u8],
new_type: NewStringType,
) -> Option<Local<'sc, String>> {
unsafe {
let ptr = v8__String__NewFromUtf8(
scope.cxx_isolate(),
isolate,
buffer.as_ptr() as *const char,
new_type,
buffer.len().try_into().ok()?,
@ -90,13 +88,13 @@ impl String {
/// Returns the number of bytes in the UTF-8 encoded representation of this
/// string.
pub fn utf8_length(&self, isolate: &mut impl LockedIsolate) -> usize {
unsafe { v8__String__Utf8Length(self, isolate.cxx_isolate()) as usize }
pub fn utf8_length(&self, isolate: &Isolate) -> usize {
unsafe { v8__String__Utf8Length(self, isolate) as usize }
}
pub fn write_utf8(
&self,
isolate: &mut impl LockedIsolate,
isolate: &Isolate,
buffer: &mut [u8],
nchars_ref: Option<&mut usize>,
options: WriteOptions,
@ -105,7 +103,7 @@ impl String {
let bytes = unsafe {
v8__String__WriteUtf8(
self,
isolate.cxx_isolate(),
isolate,
buffer.as_mut_ptr() as *mut char,
buffer.len().try_into().unwrap_or(int::max_value()),
&mut nchars_ref_int,
@ -120,18 +118,15 @@ impl String {
// Convenience function not present in the original V8 API.
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
isolate: &Isolate,
value: &str,
new_type: NewStringType,
) -> Option<Local<'sc, String>> {
Self::new_from_utf8(scope, value.as_ref(), new_type)
Self::new_from_utf8(isolate, value.as_ref(), new_type)
}
// Convenience function not present in the original V8 API.
pub fn to_rust_string_lossy(
&self,
isolate: &mut impl LockedIsolate,
) -> std::string::String {
pub fn to_rust_string_lossy(&self, isolate: &Isolate) -> std::string::String {
let capacity = self.utf8_length(isolate);
let mut string = std::string::String::with_capacity(capacity);
let data = string.as_mut_ptr();

View file

@ -64,11 +64,11 @@ fn handle_scope_numbers() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let l1 = v8::Integer::new(scope, -123);
let l2 = v8::Integer::new_from_unsigned(scope, 456);
v8::HandleScope::enter(&isolate, |scope2| {
let l3 = v8::Number::new(scope2, 78.9);
v8::HandleScope::enter(&isolate, |_scope| {
let l1 = v8::Integer::new(&isolate, -123);
let l2 = v8::Integer::new_from_unsigned(&isolate, 456);
v8::HandleScope::enter(&isolate, |_scope2| {
let l3 = v8::Number::new(&isolate, 78.9);
assert_eq!(l1.value(), -123);
assert_eq!(l2.value(), 456);
assert_eq!(l3.value(), 78.9);
@ -89,13 +89,13 @@ fn test_string() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
v8::HandleScope::enter(&isolate, |_scope| {
let reference = "Hello 🦕 world!";
let local =
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, reference, v8::NewStringType::Normal).unwrap();
assert_eq!(15, local.length());
assert_eq!(17, local.utf8_length(scope));
assert_eq!(reference, local.to_rust_string_lossy(scope));
assert_eq!(17, local.utf8_length(&isolate));
assert_eq!(reference, local.to_rust_string_lossy(&isolate));
});
drop(locker);
}
@ -122,19 +122,22 @@ fn script_compile_and_run() {
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s);
v8::HandleScope::enter(&isolate, |_s| {
let mut context = v8::Context::new(&isolate);
context.enter();
let source =
v8::String::new(s, "'Hello ' + 13 + 'th planet'", Default::default())
.unwrap();
let mut script = v8::Script::compile(s, context, source, None).unwrap();
source.to_rust_string_lossy(s);
let result = script.run(s, context).unwrap();
let source = v8::String::new(
&isolate,
"'Hello ' + 13 + 'th planet'",
Default::default(),
)
.unwrap();
let mut script = v8::Script::compile(context, source, None).unwrap();
source.to_rust_string_lossy(&isolate);
let result = script.run(context).unwrap();
// TODO: safer casts.
let result: v8::Local<v8::String> =
unsafe { std::mem::transmute_copy(&result) };
assert_eq!(result.to_rust_string_lossy(s), "Hello 13th planet");
assert_eq!(result.to_rust_string_lossy(&isolate), "Hello 13th planet");
context.exit();
});
drop(locker);
@ -150,21 +153,21 @@ fn script_origin() {
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s);
v8::HandleScope::enter(&isolate, |_s| {
let mut context = v8::Context::new(&isolate);
context.enter();
let resource_name =
v8::String::new(s, "foo.js", Default::default()).unwrap();
let resource_line_offset = v8::Integer::new(s, 4);
let resource_column_offset = v8::Integer::new(s, 5);
let resource_is_shared_cross_origin = v8::new_true(s);
let script_id = v8::Integer::new(s, 123);
v8::String::new(&isolate, "foo.js", Default::default()).unwrap();
let resource_line_offset = v8::Integer::new(&isolate, 4);
let resource_column_offset = v8::Integer::new(&isolate, 5);
let resource_is_shared_cross_origin = v8::new_true(&isolate);
let script_id = v8::Integer::new(&isolate, 123);
let source_map_url =
v8::String::new(s, "source_map_url", Default::default()).unwrap();
let resource_is_opaque = v8::new_true(s);
let is_wasm = v8::new_false(s);
let is_module = v8::new_false(s);
v8::String::new(&isolate, "source_map_url", Default::default()).unwrap();
let resource_is_opaque = v8::new_true(&isolate);
let is_wasm = v8::new_false(&isolate);
let is_module = v8::new_false(&isolate);
let script_origin = v8::ScriptOrigin::new(
resource_name.into(),
@ -178,11 +181,11 @@ fn script_origin() {
is_module,
);
let source = v8::String::new(s, "1+2", Default::default()).unwrap();
let source = v8::String::new(&isolate, "1+2", Default::default()).unwrap();
let mut script =
v8::Script::compile(s, context, source, Some(&script_origin)).unwrap();
source.to_rust_string_lossy(s);
let _result = script.run(s, context).unwrap();
v8::Script::compile(context, source, Some(&script_origin)).unwrap();
source.to_rust_string_lossy(&isolate);
let _result = script.run(context).unwrap();
context.exit();
});
drop(locker);
@ -243,23 +246,23 @@ fn test_primitives() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let null = v8::new_null(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let null = v8::new_null(&isolate);
assert!(!null.is_undefined());
assert!(null.is_null());
assert!(null.is_null_or_undefined());
let undefined = v8::new_undefined(scope);
let undefined = v8::new_undefined(&isolate);
assert!(undefined.is_undefined());
assert!(!undefined.is_null());
assert!(undefined.is_null_or_undefined());
let true_ = v8::new_true(scope);
let true_ = v8::new_true(&isolate);
assert!(!true_.is_undefined());
assert!(!true_.is_null());
assert!(!true_.is_null_or_undefined());
let false_ = v8::new_false(scope);
let false_ = v8::new_false(&isolate);
assert!(!false_.is_undefined());
assert!(!false_.is_null());
assert!(!false_.is_null_or_undefined());
@ -277,20 +280,20 @@ fn exception() {
let mut isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
isolate.enter();
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let reference = "This is a test error";
let local =
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, reference, v8::NewStringType::Normal).unwrap();
v8::Exception::RangeError(local);
v8::Exception::ReferenceError(local);
v8::Exception::SyntaxError(local);
v8::Exception::TypeError(local);
let exception = v8::Exception::Error(local);
let mut msg = v8::Exception::CreateMessage(scope, exception);
let mut msg = v8::Exception::CreateMessage(&isolate, exception);
let msg_string = msg.get();
let rust_msg_string = msg_string.to_rust_string_lossy(scope);
let rust_msg_string = msg_string.to_rust_string_lossy(&isolate);
assert_eq!(
"Uncaught Error: This is a test error".to_string(),
rust_msg_string
@ -311,18 +314,19 @@ fn json() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s);
v8::HandleScope::enter(&isolate, |_s| {
let mut context = v8::Context::new(&isolate);
context.enter();
let json_string =
v8::String::new(s, "{\"a\": 1, \"b\": 2}", Default::default()).unwrap();
v8::String::new(&isolate, "{\"a\": 1, \"b\": 2}", Default::default())
.unwrap();
let maybe_value = v8::JSON::Parse(context, json_string);
assert!(maybe_value.is_some());
let value = maybe_value.unwrap();
let maybe_stringified = v8::JSON::Stringify(context, value);
assert!(maybe_stringified.is_some());
let stringified = maybe_stringified.unwrap();
let rust_str = stringified.to_rust_string_lossy(s);
let rust_str = stringified.to_rust_string_lossy(&isolate);
assert_eq!("{\"a\":1,\"b\":2}".to_string(), rust_str);
context.exit();
});
@ -344,19 +348,19 @@ fn object() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let null: v8::Local<v8::Value> = new_null(scope).into();
let s1 = v8::String::new(scope, "a", v8::NewStringType::Normal).unwrap();
let s2 = v8::String::new(scope, "b", v8::NewStringType::Normal).unwrap();
let null: v8::Local<v8::Value> = new_null(&isolate).into();
let s1 = v8::String::new(&isolate, "a", v8::NewStringType::Normal).unwrap();
let s2 = v8::String::new(&isolate, "b", v8::NewStringType::Normal).unwrap();
let name1: Local<v8::Name> = cast(s1);
let name2: Local<v8::Name> = cast(s2);
let names = vec![name1, name2];
let v1: v8::Local<v8::Value> = v8::Number::new(scope, 1.0).into();
let v2: v8::Local<v8::Value> = v8::Number::new(scope, 2.0).into();
let v1: v8::Local<v8::Value> = v8::Number::new(&isolate, 1.0).into();
let v2: v8::Local<v8::Value> = v8::Number::new(&isolate, 2.0).into();
let values = vec![v1, v2];
let object = v8::Object::new(scope, null, names, values, 2);
let object = v8::Object::new(&isolate, null, names, values, 2);
assert!(!object.is_null_or_undefined());
context.exit();
});
@ -372,32 +376,38 @@ fn promise_resolved() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let maybe_resolver = v8::PromiseResolver::new(context);
assert!(maybe_resolver.is_some());
let mut resolver = maybe_resolver.unwrap();
let mut promise = resolver.get_promise(scope);
let mut promise = resolver.get_promise();
assert!(!promise.has_handler());
assert_eq!(promise.state(), v8::PromiseState::Pending);
let str =
v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, "test", v8::NewStringType::Normal).unwrap();
let value: Local<v8::Value> = cast(str);
resolver.resolve(context, value);
assert_eq!(promise.state(), v8::PromiseState::Fulfilled);
let result = promise.result(scope);
let result = promise.result();
let result_str: v8::Local<v8::String> = cast(result);
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
assert_eq!(
result_str.to_rust_string_lossy(&isolate),
"test".to_string()
);
// Resolve again with different value, since promise is already in `Fulfilled` state
// it should be ignored.
let str =
v8::String::new(scope, "test2", v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, "test2", v8::NewStringType::Normal).unwrap();
let value: Local<v8::Value> = cast(str);
resolver.resolve(context, value);
let result = promise.result(scope);
let result = promise.result();
let result_str: v8::Local<v8::String> = cast(result);
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
assert_eq!(
result_str.to_rust_string_lossy(&isolate),
"test".to_string()
);
context.exit();
});
drop(locker);
@ -412,33 +422,39 @@ fn promise_rejected() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let maybe_resolver = v8::PromiseResolver::new(context);
assert!(maybe_resolver.is_some());
let mut resolver = maybe_resolver.unwrap();
let mut promise = resolver.get_promise(scope);
let mut promise = resolver.get_promise();
assert!(!promise.has_handler());
assert_eq!(promise.state(), v8::PromiseState::Pending);
let str =
v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, "test", v8::NewStringType::Normal).unwrap();
let value: Local<v8::Value> = cast(str);
let rejected = resolver.reject(context, value);
assert!(rejected.unwrap());
assert_eq!(promise.state(), v8::PromiseState::Rejected);
let result = promise.result(scope);
let result = promise.result();
let result_str: v8::Local<v8::String> = cast(result);
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
assert_eq!(
result_str.to_rust_string_lossy(&isolate),
"test".to_string()
);
// Reject again with different value, since promise is already in `Rejected` state
// it should be ignored.
let str =
v8::String::new(scope, "test2", v8::NewStringType::Normal).unwrap();
v8::String::new(&isolate, "test2", v8::NewStringType::Normal).unwrap();
let value: Local<v8::Value> = cast(str);
resolver.reject(context, value);
let result = promise.result(scope);
let result = promise.result();
let result_str: v8::Local<v8::String> = cast(result);
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
assert_eq!(
result_str.to_rust_string_lossy(&isolate),
"test".to_string()
);
context.exit();
});
drop(locker);
@ -447,15 +463,15 @@ fn promise_rejected() {
extern "C" fn fn_callback(info: &FunctionCallbackInfo) {
assert_eq!(info.length(), 0);
let isolate = info.get_isolate();
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let s =
v8::String::new(scope, "Hello callback!", v8::NewStringType::Normal)
v8::String::new(&isolate, "Hello callback!", v8::NewStringType::Normal)
.unwrap();
let value: Local<v8::Value> = s.into();
let rv = info.get_return_value();
let rv_value = rv.get(scope);
let rv_value = rv.get();
assert!(rv_value.is_undefined());
rv.set(value);
context.exit();
@ -471,13 +487,13 @@ fn function() {
);
let isolate = v8::Isolate::new(params);
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let global = context.global();
let recv: Local<v8::Value> = global.into();
// create function using template
let mut fn_template = v8::FunctionTemplate::new(scope, fn_callback);
let mut fn_template = v8::FunctionTemplate::new(&isolate, fn_callback);
let mut function = fn_template
.get_function(context)
.expect("Unable to create function");
@ -489,7 +505,7 @@ fn function() {
v8::Function::call(&mut *function, context, recv, 0, vec![]);
let value = maybe_value.unwrap();
let value_str: v8::Local<v8::String> = cast(value);
let rust_str = value_str.to_rust_string_lossy(scope);
let rust_str = value_str.to_rust_string_lossy(&isolate);
assert_eq!(rust_str, "Hello callback!".to_string());
context.exit();
});
@ -505,9 +521,9 @@ extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) {
let isolate = promise_obj.get_isolate();
let value = msg.get_value();
let locker = v8::Locker::new(isolate);
v8::HandleScope::enter(&isolate, |scope| {
v8::HandleScope::enter(&isolate, |_scope| {
let value_str: v8::Local<v8::String> = cast(value);
let rust_str = value_str.to_rust_string_lossy(scope);
let rust_str = value_str.to_rust_string_lossy(&isolate);
assert_eq!(rust_str, "promise rejected".to_string());
});
drop(locker);
@ -524,12 +540,12 @@ fn set_promise_reject_callback() {
isolate.set_promise_reject_callback(promise_reject_callback);
isolate.enter();
let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope);
v8::HandleScope::enter(&isolate, |_scope| {
let mut context = v8::Context::new(&isolate);
context.enter();
let mut resolver = v8::PromiseResolver::new(context).unwrap();
let str_ =
v8::String::new(scope, "promise rejected", v8::NewStringType::Normal)
v8::String::new(&isolate, "promise rejected", v8::NewStringType::Normal)
.unwrap();
let value: Local<v8::Value> = cast(str_);
resolver.reject(context, value);