0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-26 09:13:46 -05:00

Remove 'context' param from (almost) all public methods (#406)

This commit is contained in:
Bert Belder 2020-06-26 02:56:24 +02:00
parent de2cca4c7f
commit b1a4dfea8b
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
12 changed files with 325 additions and 286 deletions

View file

@ -183,9 +183,12 @@ impl Message {
pub fn get_source_line<'s>( pub fn get_source_line<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, String>> { ) -> Option<Local<'s, String>> {
unsafe { scope.cast_local(|_| v8__Message__GetSourceLine(self, &*context)) } unsafe {
scope.cast_local(|sd| {
v8__Message__GetSourceLine(self, sd.get_current_context())
})
}
} }
/// Returns the resource name for the script from where the function causing /// Returns the resource name for the script from where the function causing
@ -198,8 +201,10 @@ impl Message {
} }
/// Returns the number, 1-based, of the line where the error occurred. /// Returns the number, 1-based, of the line where the error occurred.
pub fn get_line_number(&self, context: Local<Context>) -> Option<usize> { pub fn get_line_number(&self, scope: &mut HandleScope) -> Option<usize> {
let i = unsafe { v8__Message__GetLineNumber(self, &*context) }; let i = unsafe {
v8__Message__GetLineNumber(self, &*scope.get_current_context())
};
if i < 0 { if i < 0 {
None None
} else { } else {

View file

@ -264,11 +264,12 @@ impl Function {
/// for a given FunctionCallback. /// for a given FunctionCallback.
pub fn new<'s>( pub fn new<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
callback: impl MapFnTo<FunctionCallback>, callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'s, Function>> { ) -> Option<Local<'s, Function>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Function__New(&*context, callback.map_fn_to())) scope.cast_local(|sd| {
v8__Function__New(sd.get_current_context(), callback.map_fn_to())
})
} }
} }
@ -276,13 +277,16 @@ impl Function {
/// for a given FunctionCallback and associated data. /// for a given FunctionCallback and associated data.
pub fn new_with_data<'s>( pub fn new_with_data<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
data: Local<Value>, data: Local<Value>,
callback: impl MapFnTo<FunctionCallback>, callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'s, Function>> { ) -> Option<Local<'s, Function>> {
unsafe { unsafe {
scope.cast_local(|_| { scope.cast_local(|sd| {
v8__Function__NewWithData(&*context, callback.map_fn_to(), &*data) v8__Function__NewWithData(
sd.get_current_context(),
callback.map_fn_to(),
&*data,
)
}) })
} }
} }
@ -290,7 +294,6 @@ impl Function {
pub fn call<'s>( pub fn call<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
recv: Local<Value>, recv: Local<Value>,
args: &[Local<Value>], args: &[Local<Value>],
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
@ -298,8 +301,9 @@ impl Function {
let argc = int::try_from(args.len()).unwrap(); let argc = int::try_from(args.len()).unwrap();
let argv = args.as_ptr(); let argv = args.as_ptr();
unsafe { unsafe {
scope scope.cast_local(|sd| {
.cast_local(|_| v8__Function__Call(self, &*context, &*recv, argc, argv)) v8__Function__Call(self, sd.get_current_context(), &*recv, argc, argv)
})
} }
} }
} }

View file

@ -21,18 +21,23 @@ extern "C" {
/// successful. /// successful.
pub fn parse<'s>( pub fn parse<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<'_, Context>,
json_string: Local<'_, String>, json_string: Local<'_, String>,
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
unsafe { scope.cast_local(|_| v8__JSON__Parse(&*context, &*json_string)) } unsafe {
scope
.cast_local(|sd| v8__JSON__Parse(sd.get_current_context(), &*json_string))
}
} }
/// Tries to stringify the JSON-serializable object `json_object` and returns /// Tries to stringify the JSON-serializable object `json_object` and returns
/// it as string if successful. /// it as string if successful.
pub fn stringify<'s>( pub fn stringify<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<'s, Context>,
json_object: Local<'s, Value>, json_object: Local<'s, Value>,
) -> Option<Local<'s, String>> { ) -> Option<Local<'s, String>> {
unsafe { scope.cast_local(|_| v8__JSON__Stringify(&*context, &*json_object)) } unsafe {
scope.cast_local(|sd| {
v8__JSON__Stringify(sd.get_current_context(), &*json_object)
})
}
} }

View file

@ -18,8 +18,8 @@
//! let code = v8::String::new(scope, "'Hello' + ' World!'").unwrap(); //! let code = v8::String::new(scope, "'Hello' + ' World!'").unwrap();
//! println!("javascript code: {}", code.to_rust_string_lossy(scope)); //! println!("javascript code: {}", code.to_rust_string_lossy(scope));
//! //!
//! let mut script = v8::Script::compile(scope, context, code, None).unwrap(); //! let mut script = v8::Script::compile(scope, code, None).unwrap();
//! let result = script.run(scope, context).unwrap(); //! let result = script.run(scope).unwrap();
//! let result = result.to_string(scope).unwrap(); //! let result = result.to_string(scope).unwrap();
//! println!("result: {}", result.to_rust_string_lossy(scope)); //! println!("result: {}", result.to_rust_string_lossy(scope));
//! ``` //! ```

View file

@ -205,11 +205,15 @@ impl Module {
#[must_use] #[must_use]
pub fn instantiate_module<'a>( pub fn instantiate_module<'a>(
&mut self, &mut self,
context: Local<Context>, scope: &mut HandleScope,
callback: impl MapFnTo<ResolveCallback<'a>>, callback: impl MapFnTo<ResolveCallback<'a>>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { unsafe {
v8__Module__InstantiateModule(self, &*context, callback.map_fn_to()) v8__Module__InstantiateModule(
self,
&*scope.get_current_context(),
callback.map_fn_to(),
)
} }
.into() .into()
} }
@ -224,8 +228,10 @@ impl Module {
pub fn evaluate<'s>( pub fn evaluate<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
unsafe { scope.cast_local(|_| v8__Module__Evaluate(&*self, &*context)) } unsafe {
scope
.cast_local(|sd| v8__Module__Evaluate(&*self, sd.get_current_context()))
}
} }
} }

View file

@ -130,32 +130,41 @@ impl Object {
/// result.Check(). /// result.Check().
pub fn set( pub fn set(
&self, &self,
context: Local<Context>, scope: &mut HandleScope,
key: Local<Value>, key: Local<Value>,
value: Local<Value>, value: Local<Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Object__Set(self, &*context, &*key, &*value) }.into() unsafe {
v8__Object__Set(self, &*scope.get_current_context(), &*key, &*value)
}
.into()
} }
/// Set only return Just(true) or Empty(), so if it should never fail, use /// Set only return Just(true) or Empty(), so if it should never fail, use
/// result.Check(). /// result.Check().
pub fn set_index( pub fn set_index(
&self, &self,
context: Local<Context>, scope: &mut HandleScope,
index: u32, index: u32,
value: Local<Value>, value: Local<Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Object__SetIndex(self, &*context, index, &*value) }.into() unsafe {
v8__Object__SetIndex(self, &*scope.get_current_context(), index, &*value)
}
.into()
} }
/// Set the prototype object. This does not skip objects marked to be /// Set the prototype object. This does not skip objects marked to be
/// skipped by proto and it does not consult the security handler. /// skipped by proto and it does not consult the security handler.
pub fn set_prototype( pub fn set_prototype(
&self, &self,
context: Local<Context>, scope: &mut HandleScope,
prototype: Local<Value>, prototype: Local<Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Object__SetPrototype(self, &*context, &*prototype) }.into() unsafe {
v8__Object__SetPrototype(self, &*scope.get_current_context(), &*prototype)
}
.into()
} }
/// Implements CreateDataProperty (ECMA-262, 7.3.4). /// Implements CreateDataProperty (ECMA-262, 7.3.4).
@ -167,12 +176,19 @@ impl Object {
/// Returns true on success. /// Returns true on success.
pub fn create_data_property( pub fn create_data_property(
&self, &self,
context: Local<Context>, scope: &mut HandleScope,
key: Local<Name>, key: Local<Name>,
value: Local<Value>, value: Local<Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Object__CreateDataProperty(self, &*context, &*key, &*value) } unsafe {
.into() v8__Object__CreateDataProperty(
self,
&*scope.get_current_context(),
&*key,
&*value,
)
}
.into()
} }
/// Implements DefineOwnProperty. /// Implements DefineOwnProperty.
@ -183,13 +199,19 @@ impl Object {
/// Returns true on success. /// Returns true on success.
pub fn define_own_property( pub fn define_own_property(
&self, &self,
context: Local<Context>, scope: &mut HandleScope,
key: Local<Name>, key: Local<Name>,
value: Local<Value>, value: Local<Value>,
attr: PropertyAttribute, attr: PropertyAttribute,
) -> Option<bool> { ) -> Option<bool> {
unsafe { unsafe {
v8__Object__DefineOwnProperty(self, &*context, &*key, &*value, attr) v8__Object__DefineOwnProperty(
self,
&*scope.get_current_context(),
&*key,
&*value,
attr,
)
} }
.into() .into()
} }
@ -197,20 +219,23 @@ impl Object {
pub fn get<'s>( pub fn get<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
key: Local<Value>, key: Local<Value>,
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
unsafe { scope.cast_local(|_| v8__Object__Get(self, &*context, &*key)) } unsafe {
scope
.cast_local(|sd| v8__Object__Get(self, sd.get_current_context(), &*key))
}
} }
pub fn get_index<'s>( pub fn get_index<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
index: u32, index: u32,
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Object__GetIndex(self, &*context, index)) scope.cast_local(|sd| {
v8__Object__GetIndex(self, sd.get_current_context(), index)
})
} }
} }
@ -226,12 +251,17 @@ impl Object {
/// Note: SideEffectType affects the getter only, not the setter. /// Note: SideEffectType affects the getter only, not the setter.
pub fn set_accessor( pub fn set_accessor(
&mut self, &mut self,
context: Local<Context>, scope: &mut HandleScope,
name: Local<Name>, name: Local<Name>,
getter: impl for<'s> MapFnTo<AccessorNameGetterCallback<'s>>, getter: impl for<'s> MapFnTo<AccessorNameGetterCallback<'s>>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { unsafe {
v8__Object__SetAccessor(self, &*context, &*name, getter.map_fn_to()) v8__Object__SetAccessor(
self,
&*scope.get_current_context(),
&*name,
getter.map_fn_to(),
)
} }
.into() .into()
} }
@ -259,10 +289,11 @@ impl Object {
pub fn get_own_property_names<'s>( pub fn get_own_property_names<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Array>> { ) -> Option<Local<'s, Array>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Object__GetOwnPropertyNames(self, &*context)) scope.cast_local(|sd| {
v8__Object__GetOwnPropertyNames(self, sd.get_current_context())
})
} }
} }
@ -273,10 +304,11 @@ impl Object {
pub fn get_property_names<'s>( pub fn get_property_names<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Array>> { ) -> Option<Local<'s, Array>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Object__GetPropertyNames(self, &*context)) scope.cast_local(|sd| {
v8__Object__GetPropertyNames(self, sd.get_current_context())
})
} }
} }
} }

View file

@ -89,11 +89,12 @@ impl Promise {
pub fn catch<'s>( pub fn catch<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
handler: Local<Function>, handler: Local<Function>,
) -> Option<Local<'s, Promise>> { ) -> Option<Local<'s, Promise>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Promise__Catch(&*self, &*context, &*handler)) scope.cast_local(|sd| {
v8__Promise__Catch(&*self, sd.get_current_context(), &*handler)
})
} }
} }
@ -103,11 +104,12 @@ impl Promise {
pub fn then<'s>( pub fn then<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
handler: Local<Function>, handler: Local<Function>,
) -> Option<Local<'s, Promise>> { ) -> Option<Local<'s, Promise>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Promise__Then(&*self, &*context, &*handler)) scope.cast_local(|sd| {
v8__Promise__Then(&*self, sd.get_current_context(), &*handler)
})
} }
} }
@ -118,13 +120,17 @@ impl Promise {
pub fn then2<'s>( pub fn then2<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
on_fulfilled: Local<Function>, on_fulfilled: Local<Function>,
on_rejected: Local<Function>, on_rejected: Local<Function>,
) -> Option<Local<'s, Promise>> { ) -> Option<Local<'s, Promise>> {
unsafe { unsafe {
scope.cast_local(|_| { scope.cast_local(|sd| {
v8__Promise__Then2(&*self, &*context, &*on_fulfilled, &*on_rejected) v8__Promise__Then2(
&*self,
sd.get_current_context(),
&*on_fulfilled,
&*on_rejected,
)
}) })
} }
} }
@ -134,9 +140,11 @@ impl PromiseResolver {
/// Create a new resolver, along with an associated promise in pending state. /// Create a new resolver, along with an associated promise in pending state.
pub fn new<'s>( pub fn new<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<'s, Context>,
) -> Option<Local<'s, PromiseResolver>> { ) -> Option<Local<'s, PromiseResolver>> {
unsafe { scope.cast_local(|_| v8__Promise__Resolver__New(&*context)) } unsafe {
scope
.cast_local(|sd| v8__Promise__Resolver__New(sd.get_current_context()))
}
} }
/// Extract the associated promise. /// Extract the associated promise.
@ -150,22 +158,36 @@ impl PromiseResolver {
/// Resolve the associated promise with a given value. /// Resolve the associated promise with a given value.
/// Ignored if the promise is no longer pending. /// Ignored if the promise is no longer pending.
pub fn resolve<'s>( pub fn resolve(
&self, &self,
context: Local<'s, Context>, scope: &mut HandleScope,
value: Local<'s, Value>, value: Local<'_, Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Promise__Resolver__Resolve(&*self, &*context, &*value).into() } unsafe {
v8__Promise__Resolver__Resolve(
&*self,
&*scope.get_current_context(),
&*value,
)
.into()
}
} }
/// Reject the associated promise with a given value. /// Reject the associated promise with a given value.
/// Ignored if the promise is no longer pending. /// Ignored if the promise is no longer pending.
pub fn reject<'s>( pub fn reject(
&self, &self,
context: Local<'s, Context>, scope: &mut HandleScope,
value: Local<'s, Value>, value: Local<'_, Value>,
) -> Option<bool> { ) -> Option<bool> {
unsafe { v8__Promise__Resolver__Reject(&*self, &*context, &*value).into() } unsafe {
v8__Promise__Resolver__Reject(
&*self,
&*scope.get_current_context(),
&*value,
)
.into()
}
} }
} }

View file

@ -20,12 +20,13 @@ extern "C" {
impl Proxy { impl Proxy {
pub fn new<'s>( pub fn new<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
target: Local<Object>, target: Local<Object>,
handler: Local<Object>, handler: Local<Object>,
) -> Option<Local<'s, Proxy>> { ) -> Option<Local<'s, Proxy>> {
unsafe { unsafe {
scope.cast_local(|_| v8__Proxy__New(&*context, &*target, &*handler)) scope.cast_local(|sd| {
v8__Proxy__New(sd.get_current_context(), &*target, &*handler)
})
} }
} }

View file

@ -44,14 +44,13 @@ impl Script {
/// A shorthand for ScriptCompiler::Compile(). /// A shorthand for ScriptCompiler::Compile().
pub fn compile<'s>( pub fn compile<'s>(
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
source: Local<String>, source: Local<String>,
origin: Option<&ScriptOrigin>, origin: Option<&ScriptOrigin>,
) -> Option<Local<'s, Script>> { ) -> Option<Local<'s, Script>> {
unsafe { unsafe {
scope.cast_local(|_| { scope.cast_local(|sd| {
v8__Script__Compile( v8__Script__Compile(
&*context, sd.get_current_context(),
&*source, &*source,
origin.map(|r| r as *const _).unwrap_or_else(null), origin.map(|r| r as *const _).unwrap_or_else(null),
) )
@ -65,9 +64,10 @@ impl Script {
pub fn run<'s>( pub fn run<'s>(
&mut self, &mut self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Value>> { ) -> Option<Local<'s, Value>> {
unsafe { scope.cast_local(|_| v8__Script__Run(self, &*context)) } unsafe {
scope.cast_local(|sd| v8__Script__Run(self, sd.get_current_context()))
}
} }
} }

View file

@ -81,11 +81,12 @@ impl FunctionTemplate {
/// Returns the unique function instance in the current execution context. /// Returns the unique function instance in the current execution context.
pub fn get_function<'s>( pub fn get_function<'s>(
&mut self, &mut self,
scope: &mut HandleScope<'s, ()>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Function>> { ) -> Option<Local<'s, Function>> {
unsafe { unsafe {
scope.cast_local(|_| v8__FunctionTemplate__GetFunction(&*self, &*context)) scope.cast_local(|sd| {
v8__FunctionTemplate__GetFunction(&*self, sd.get_current_context())
})
} }
} }
@ -124,10 +125,11 @@ impl ObjectTemplate {
pub fn new_instance<'s>( pub fn new_instance<'s>(
&self, &self,
scope: &mut HandleScope<'s>, scope: &mut HandleScope<'s>,
context: Local<Context>,
) -> Option<Local<'s, Object>> { ) -> Option<Local<'s, Object>> {
unsafe { unsafe {
scope.cast_local(|_| v8__ObjectTemplate__NewInstance(self, &*context)) scope.cast_local(|sd| {
v8__ObjectTemplate__NewInstance(self, sd.get_current_context())
})
} }
} }
} }

View file

@ -43,8 +43,8 @@ impl CoreIsolate {
let context = v8::Context::new(scope); let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context); let scope = &mut v8::ContextScope::new(scope, context);
let source = v8::String::new(scope, code).unwrap(); let source = v8::String::new(scope, code).unwrap();
let mut script = v8::Script::compile(scope, context, source, None).unwrap(); let mut script = v8::Script::compile(scope, source, None).unwrap();
let r = script.run(scope, context); let r = script.run(scope);
r.is_some() r.is_some()
} }

File diff suppressed because it is too large Load diff