2021-02-13 07:31:18 -05:00
|
|
|
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
2019-12-19 19:15:52 -05:00
|
|
|
use crate::isolate::Isolate;
|
2020-03-05 17:41:43 -08:00
|
|
|
use crate::Context;
|
2022-05-06 00:31:29 +08:00
|
|
|
use crate::Function;
|
2020-06-03 07:38:34 +02:00
|
|
|
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;
|
2020-01-19 00:38:41 +01:00
|
|
|
use crate::ObjectTemplate;
|
|
|
|
use crate::Value;
|
|
|
|
use std::ptr::null;
|
2019-12-04 14:12:27 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2020-01-19 00:38:41 +01:00
|
|
|
fn v8__Context__New(
|
2020-04-13 14:43:56 +02:00
|
|
|
isolate: *mut Isolate,
|
2020-01-19 00:38:41 +01:00
|
|
|
templ: *const ObjectTemplate,
|
2020-04-13 14:43:56 +02:00
|
|
|
global_object: *const Value,
|
|
|
|
) -> *const Context;
|
|
|
|
fn v8__Context__Global(this: *const Context) -> *const Object;
|
2022-05-06 00:31:29 +08:00
|
|
|
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 {
|
2020-01-19 00:38:41 +01:00
|
|
|
/// Creates a new context.
|
2020-06-03 07:38:34 +02:00
|
|
|
pub fn new<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, Context> {
|
2019-12-04 14:12:27 +01:00
|
|
|
// TODO: optional arguments;
|
2020-05-31 15:00:23 +02:00
|
|
|
unsafe {
|
|
|
|
scope
|
2020-06-03 07:38:34 +02:00
|
|
|
.cast_local(|sd| v8__Context__New(sd.get_isolate_ptr(), null(), null()))
|
2020-05-31 15:00:23 +02:00
|
|
|
}
|
|
|
|
.unwrap()
|
2020-01-19 00:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new context using the object template as the template for
|
|
|
|
/// the global object.
|
2020-06-03 07:38:34 +02:00
|
|
|
pub fn new_from_template<'s>(
|
|
|
|
scope: &mut HandleScope<'s, ()>,
|
2020-01-19 00:38:41 +01:00
|
|
|
templ: Local<ObjectTemplate>,
|
2020-06-03 07:38:34 +02:00
|
|
|
) -> Local<'s, Context> {
|
2020-05-31 15:00:23 +02:00
|
|
|
unsafe {
|
2020-06-03 07:38:34 +02:00
|
|
|
scope.cast_local(|sd| {
|
|
|
|
v8__Context__New(sd.get_isolate_ptr(), &*templ, null())
|
|
|
|
})
|
2020-05-31 15:00:23 +02:00
|
|
|
}
|
|
|
|
.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.
|
2020-06-03 07:38:34 +02:00
|
|
|
pub fn global<'s>(
|
2020-01-18 05:55:09 +01:00
|
|
|
&self,
|
2020-06-03 07:38:34 +02:00
|
|
|
scope: &mut HandleScope<'s, ()>,
|
|
|
|
) -> Local<'s, Object> {
|
2020-05-31 15:00:23 +02:00
|
|
|
unsafe { scope.cast_local(|_| v8__Context__Global(self)) }.unwrap()
|
2019-12-11 04:43:22 +01:00
|
|
|
}
|
2022-05-06 00:31:29 +08: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
|
|
|
}
|