mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-03-09 13:38:51 -04:00
Add Context::new_from_template() (#225)
This commit is contained in:
parent
2db5e10b9f
commit
42af31ff38
3 changed files with 47 additions and 7 deletions
|
@ -767,11 +767,10 @@ void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|||
deserialized_data.push_back(embedder_field);
|
||||
}
|
||||
|
||||
v8::Context* v8__Context__New(v8::Isolate* isolate) {
|
||||
// TODO: optional arguments.
|
||||
return *v8::Context::New(isolate, nullptr,
|
||||
v8::MaybeLocal<v8::ObjectTemplate>(),
|
||||
v8::MaybeLocal<v8::Value>(),
|
||||
v8::Context* v8__Context__New(v8::Isolate* isolate,
|
||||
v8::MaybeLocal<v8::ObjectTemplate> templ,
|
||||
v8::MaybeLocal<v8::Value> global_object) {
|
||||
return *v8::Context::New(isolate, nullptr, templ, global_object,
|
||||
v8::DeserializeInternalFieldsCallback(
|
||||
DeserializeInternalFields, nullptr));
|
||||
}
|
||||
|
|
|
@ -3,10 +3,17 @@ use crate::isolate::Isolate;
|
|||
use crate::support::Opaque;
|
||||
use crate::Local;
|
||||
use crate::Object;
|
||||
use crate::ObjectTemplate;
|
||||
use crate::ToLocal;
|
||||
use crate::Value;
|
||||
use std::ptr::null;
|
||||
|
||||
extern "C" {
|
||||
fn v8__Context__New(isolate: &Isolate) -> *mut Context;
|
||||
fn v8__Context__New(
|
||||
isolate: &Isolate,
|
||||
templ: *const ObjectTemplate,
|
||||
data: *const Value,
|
||||
) -> *mut Context;
|
||||
fn v8__Context__Enter(this: &mut Context);
|
||||
fn v8__Context__Exit(this: &mut Context);
|
||||
fn v8__Context__Global(this: *mut Context) -> *mut Object;
|
||||
|
@ -18,9 +25,20 @@ extern "C" {
|
|||
pub struct Context(Opaque);
|
||||
|
||||
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()) };
|
||||
let ptr = unsafe { v8__Context__New(scope.isolate(), null(), null()) };
|
||||
unsafe { scope.to_local(ptr) }.unwrap()
|
||||
}
|
||||
|
||||
/// Creates a new context using the object template as the template for
|
||||
/// the global object.
|
||||
pub fn new_from_template<'sc>(
|
||||
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()
|
||||
}
|
||||
|
||||
|
|
|
@ -2247,3 +2247,26 @@ fn inspector_dispatch_protocol_message() {
|
|||
assert_eq!(channel.send_notification_count, 0);
|
||||
assert_eq!(channel.flush_protocol_notifications_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn context_from_object_template() {
|
||||
let _setup_guard = setup();
|
||||
let mut params = v8::Isolate::create_params();
|
||||
params.set_array_buffer_allocator(v8::new_default_allocator());
|
||||
let isolate = v8::Isolate::new(params);
|
||||
let mut locker = v8::Locker::new(&isolate);
|
||||
{
|
||||
let mut hs = v8::HandleScope::new(&mut locker);
|
||||
let scope = hs.enter();
|
||||
let object_templ = v8::ObjectTemplate::new(scope);
|
||||
let function_templ = v8::FunctionTemplate::new(scope, fortytwo_callback);
|
||||
let name = v8_str(scope, "f");
|
||||
object_templ.set(name.into(), function_templ.into());
|
||||
let context = v8::Context::new_from_template(scope, object_templ);
|
||||
let mut cs = v8::ContextScope::new(scope, context);
|
||||
let scope = cs.enter();
|
||||
let actual = eval(scope, context, "f()").unwrap();
|
||||
let expected = v8::Integer::new(scope, 42);
|
||||
assert!(expected.strict_equals(actual));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue