0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-02-22 05:12:30 -05:00
denoland-rusty-v8/src/context.rs

88 lines
2.3 KiB
Rust
Raw Normal View History

2021-02-13 07:31:18 -05:00
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
use crate::isolate::Isolate;
use crate::Context;
use crate::Function;
use crate::HandleScope;
2019-12-04 14:12:27 +01:00
use crate::Local;
2019-12-11 04:43:22 +01:00
use crate::Object;
use crate::ObjectTemplate;
use crate::Value;
use std::ptr::null;
2019-12-04 14:12:27 +01:00
extern "C" {
fn v8__Context__New(
isolate: *mut Isolate,
templ: *const ObjectTemplate,
global_object: *const Value,
) -> *const Context;
fn v8__Context__Global(this: *const Context) -> *const Object;
fn v8__Context__SetPromiseHooks(
this: *const Context,
init_hook: *const Function,
before_hook: *const Function,
after_hook: *const Function,
resolve_hook: *const Function,
);
2019-12-04 14:12:27 +01:00
}
impl Context {
/// Creates a new context.
pub fn new<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, Context> {
2019-12-04 14:12:27 +01:00
// TODO: optional arguments;
unsafe {
scope
.cast_local(|sd| v8__Context__New(sd.get_isolate_ptr(), null(), null()))
}
.unwrap()
}
/// Creates a new context using the object template as the template for
/// the global object.
pub fn new_from_template<'s>(
scope: &mut HandleScope<'s, ()>,
templ: Local<ObjectTemplate>,
) -> Local<'s, Context> {
unsafe {
scope.cast_local(|sd| {
v8__Context__New(sd.get_isolate_ptr(), &*templ, null())
})
}
.unwrap()
2019-12-04 14:12:27 +01:00
}
2019-12-11 04:43:22 +01:00
/// Returns the global proxy object.
///
/// Global proxy object is a thin wrapper whose prototype points to actual
/// context's global object with the properties like Object, etc. This is done
/// that way for security reasons (for more details see
/// https://wiki.mozilla.org/Gecko:SplitWindow).
///
/// Please note that changes to global proxy object prototype most probably
/// would break VM---v8 expects only global object as a prototype of global
/// proxy object.
pub fn global<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, Object> {
unsafe { scope.cast_local(|_| v8__Context__Global(self)) }.unwrap()
2019-12-11 04:43:22 +01:00
}
pub fn set_promise_hooks(
&self,
init_hook: Local<Function>,
before_hook: Local<Function>,
after_hook: Local<Function>,
resolve_hook: Local<Function>,
) {
unsafe {
v8__Context__SetPromiseHooks(
self,
&*init_hook,
&*before_hook,
&*after_hook,
&*resolve_hook,
)
}
}
2019-12-04 14:12:27 +01:00
}