2020-01-02 13:57:00 -05:00
|
|
|
// Copyright 2019-2020 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;
|
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;
|
2019-12-25 00:31:36 +01:00
|
|
|
use crate::ToLocal;
|
2020-01-19 00:38:41 +01:00
|
|
|
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__Enter(this: *const Context);
|
|
|
|
fn v8__Context__Exit(this: *const Context);
|
|
|
|
fn v8__Context__Global(this: *const Context) -> *const Object;
|
2019-12-04 14:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2020-01-19 00:38:41 +01:00
|
|
|
/// Creates a new context.
|
2019-12-25 00:31:36 +01:00
|
|
|
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Context> {
|
2019-12-04 14:12:27 +01:00
|
|
|
// TODO: optional arguments;
|
2020-05-31 15:00:23 +02:00
|
|
|
unsafe {
|
|
|
|
scope
|
|
|
|
.cast_local(|scope| v8__Context__New(scope.isolate(), null(), null()))
|
|
|
|
}
|
|
|
|
.unwrap()
|
2020-01-19 00:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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> {
|
2020-05-31 15:00:23 +02:00
|
|
|
unsafe {
|
|
|
|
scope
|
|
|
|
.cast_local(|scope| v8__Context__New(scope.isolate(), &*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.
|
2019-12-25 00:31:36 +01:00
|
|
|
pub fn global<'sc>(
|
2020-01-18 05:55:09 +01:00
|
|
|
&self,
|
2019-12-25 00:31:36 +01:00
|
|
|
scope: &mut impl ToLocal<'sc>,
|
|
|
|
) -> Local<'sc, 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
|
|
|
}
|
|
|
|
|
2019-12-20 08:47:20 -05:00
|
|
|
/// Enter this context. After entering a context, all code compiled
|
|
|
|
/// and run is compiled and run in this context. If another context
|
|
|
|
/// is already entered, this old context is saved so it can be
|
|
|
|
/// restored when the new context is exited.
|
2020-04-13 14:43:56 +02:00
|
|
|
pub(crate) fn enter(&self) {
|
2019-12-04 14:12:27 +01:00
|
|
|
// TODO: enter/exit should be controlled by a scope.
|
|
|
|
unsafe { v8__Context__Enter(self) };
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:47:20 -05:00
|
|
|
/// Exit this context. Exiting the current context restores the
|
|
|
|
/// context that was in place when entering the current context.
|
2020-04-13 14:43:56 +02:00
|
|
|
pub(crate) fn exit(&self) {
|
2019-12-04 14:12:27 +01:00
|
|
|
// TODO: enter/exit should be controlled by a scope.
|
|
|
|
unsafe { v8__Context__Exit(self) };
|
|
|
|
}
|
|
|
|
}
|