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

60 lines
2 KiB
Rust
Raw Normal View History

2020-01-02 13:57:00 -05:00
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
use crate::isolate::Isolate;
2019-12-04 14:12:27 +01:00
use crate::support::Opaque;
use crate::Local;
2019-12-11 04:43:22 +01:00
use crate::Object;
use crate::ToLocal;
2019-12-04 14:12:27 +01:00
extern "C" {
2019-12-19 20:11:22 -05:00
fn v8__Context__New(isolate: &Isolate) -> *mut Context;
2019-12-04 14:12:27 +01:00
fn v8__Context__Enter(this: &mut Context);
fn v8__Context__Exit(this: &mut Context);
2019-12-11 04:43:22 +01:00
fn v8__Context__Global(this: *mut Context) -> *mut Object;
2019-12-04 14:12:27 +01:00
}
/// A sandboxed execution context with its own set of built-in objects and
/// functions.
2019-12-04 14:12:27 +01:00
#[repr(C)]
pub struct Context(Opaque);
impl Context {
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Context> {
2019-12-04 14:12:27 +01:00
// TODO: optional arguments;
let ptr = unsafe { v8__Context__New(scope.isolate()) };
unsafe { scope.to_local(ptr) }.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<'sc>(
&mut self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Object> {
unsafe { scope.to_local(v8__Context__Global(&mut *self)) }.unwrap()
2019-12-11 04:43:22 +01: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.
2019-12-04 14:12:27 +01:00
pub fn enter(&mut self) {
// TODO: enter/exit should be controlled by a scope.
unsafe { v8__Context__Enter(self) };
}
/// Exit this context. Exiting the current context restores the
/// context that was in place when entering the current context.
2019-12-04 14:12:27 +01:00
pub fn exit(&mut self) {
// TODO: enter/exit should be controlled by a scope.
unsafe { v8__Context__Exit(self) };
}
}