0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

Add Script::get_unbound_script() and UnboundScript::bind_to_current_context() (#572)

This commit is contained in:
Heyang Zhou 2021-01-12 11:39:51 +08:00 committed by GitHub
parent d78dcd087f
commit 584a037800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 0 deletions

View file

@ -1490,6 +1490,16 @@ const v8::Script* v8__Script__Compile(const v8::Context& context,
const_cast<v8::ScriptOrigin*>(&origin)));
}
const v8::UnboundScript* v8__Script__GetUnboundScript(
const v8::Script& script) {
return local_to_ptr(ptr_to_local(&script)->GetUnboundScript());
}
const v8::Script* v8__UnboundScript__BindToCurrentContext(
const v8::UnboundScript& unbound_script) {
return local_to_ptr(ptr_to_local(&unbound_script)->BindToCurrentContext());
}
const v8::Value* v8__Script__Run(const v8::Script& script,
const v8::Context& context) {
return maybe_local_to_ptr(ptr_to_local(&script)->Run(ptr_to_local(&context)));

View file

@ -67,6 +67,7 @@ mod support;
mod symbol;
mod template;
mod typed_array;
mod unbound_script;
mod value;
mod value_deserializer;
mod value_serializer;

View file

@ -9,6 +9,7 @@ use crate::Integer;
use crate::Local;
use crate::Script;
use crate::String;
use crate::UnboundScript;
use crate::Value;
/// The origin, within a file, of a script.
@ -22,6 +23,9 @@ extern "C" {
source: *const String,
origin: *const ScriptOrigin,
) -> *const Script;
fn v8__Script__GetUnboundScript(
script: *const Script,
) -> *const UnboundScript;
fn v8__Script__Run(
script: *const Script,
context: *const Context,
@ -59,6 +63,18 @@ impl Script {
}
}
/// Returns the corresponding context-unbound script.
pub fn get_unbound_script<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, UnboundScript> {
unsafe {
scope
.cast_local(|_| v8__Script__GetUnboundScript(self))
.unwrap()
}
}
/// Runs the script returning the resulting value. It will be run in the
/// context in which it was created (ScriptCompiler::CompileBound or
/// UnboundScript::BindToCurrentContext()).

23
src/unbound_script.rs Normal file
View file

@ -0,0 +1,23 @@
use crate::HandleScope;
use crate::Local;
use crate::Script;
use crate::UnboundScript;
extern "C" {
fn v8__UnboundScript__BindToCurrentContext(
script: *const UnboundScript,
) -> *const Script;
}
impl UnboundScript {
/// Binds the script to the currently entered context.
pub fn bind_to_current_context<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, Script> {
unsafe {
scope.cast_local(|_| v8__UnboundScript__BindToCurrentContext(self))
}
.unwrap()
}
}

View file

@ -4393,3 +4393,31 @@ fn wasm_streaming_callback() {
scope.perform_microtask_checkpoint();
assert!(global.get(scope, name).unwrap().strict_equals(exception));
}
#[test]
fn unbound_script_conversion() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let unbound_script = {
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let source = v8::String::new(scope, "'Hello ' + value").unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
script.get_unbound_script(scope)
};
{
// Execute the script in another context.
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let global_object = scope.get_current_context().global(scope);
let key = v8::String::new(scope, "value").unwrap();
let value = v8::String::new(scope, "world").unwrap();
global_object.set(scope, key.into(), value.into());
let script = unbound_script.bind_to_current_context(scope);
let result = script.run(scope).unwrap();
assert_eq!(result.to_rust_string_lossy(scope), "Hello world");
}
}