mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-01-11 08:34:01 -05:00
Add ScriptCompiler::compile_function_in_context() (#670)
This commit is contained in:
parent
72749ae4e7
commit
92ea90bdbf
3 changed files with 88 additions and 0 deletions
|
@ -402,6 +402,22 @@ const v8::Script* v8__ScriptCompiler__Compile(
|
|||
return maybe_local_to_ptr(maybe_local);
|
||||
}
|
||||
|
||||
const v8::Function* v8__ScriptCompiler__CompileFunctionInContext(
|
||||
const v8::Context* context, v8::ScriptCompiler::Source* source,
|
||||
size_t arguments_count, const v8::String** arguments,
|
||||
size_t context_extensions_count, const v8::Object** context_extensions,
|
||||
v8::ScriptCompiler::CompileOptions options,
|
||||
v8::ScriptCompiler::NoCacheReason no_cache_reason) {
|
||||
return maybe_local_to_ptr(
|
||||
v8::ScriptCompiler::CompileFunctionInContext(
|
||||
ptr_to_local(context), source,
|
||||
arguments_count,
|
||||
reinterpret_cast<v8::Local<v8::String>*>(arguments),
|
||||
context_extensions_count,
|
||||
reinterpret_cast<v8::Local<v8::Object>*>(context_extensions),
|
||||
options, no_cache_reason, nullptr));
|
||||
}
|
||||
|
||||
const v8::UnboundScript* v8__ScriptCompiler__CompileUnboundScript(
|
||||
v8::Isolate* isolate, v8::ScriptCompiler::Source* source,
|
||||
v8::ScriptCompiler::CompileOptions options,
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
||||
use std::{marker::PhantomData, mem::MaybeUninit};
|
||||
|
||||
use crate::Function;
|
||||
use crate::Local;
|
||||
use crate::Module;
|
||||
use crate::Object;
|
||||
use crate::ScriptOrigin;
|
||||
use crate::String;
|
||||
use crate::{Context, Isolate, Script, UnboundScript};
|
||||
|
@ -36,6 +38,16 @@ extern "C" {
|
|||
options: CompileOptions,
|
||||
no_cache_reason: NoCacheReason,
|
||||
) -> *const Script;
|
||||
fn v8__ScriptCompiler__CompileFunctionInContext(
|
||||
context: *const Context,
|
||||
source: *mut Source,
|
||||
arguments_count: usize,
|
||||
arguments: *const *const String,
|
||||
context_extensions_count: usize,
|
||||
context_extensions: *const *const Object,
|
||||
options: CompileOptions,
|
||||
no_cache_reason: NoCacheReason,
|
||||
) -> *const Function;
|
||||
fn v8__ScriptCompiler__CompileUnboundScript(
|
||||
isolate: *mut Isolate,
|
||||
source: *mut Source,
|
||||
|
@ -224,6 +236,32 @@ pub fn compile<'s>(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn compile_function_in_context<'s>(
|
||||
scope: &mut HandleScope<'s>,
|
||||
mut source: Source,
|
||||
arguments: &[Local<String>],
|
||||
context_extensions: &[Local<Object>],
|
||||
options: CompileOptions,
|
||||
no_cache_reason: NoCacheReason,
|
||||
) -> Option<Local<'s, Function>> {
|
||||
let arguments = Local::slice_into_raw(arguments);
|
||||
let context_extensions = Local::slice_into_raw(context_extensions);
|
||||
unsafe {
|
||||
scope.cast_local(|sd| {
|
||||
v8__ScriptCompiler__CompileFunctionInContext(
|
||||
&*sd.get_current_context(),
|
||||
&mut source,
|
||||
arguments.len(),
|
||||
arguments.as_ptr(),
|
||||
context_extensions.len(),
|
||||
context_extensions.as_ptr(),
|
||||
options,
|
||||
no_cache_reason,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_unbound_script<'s>(
|
||||
scope: &mut HandleScope<'s>,
|
||||
mut source: Source,
|
||||
|
|
|
@ -5019,6 +5019,40 @@ fn code_cache_script() {
|
|||
assert_eq!(ret.uint32_value(scope).unwrap(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_function_in_context() {
|
||||
let _setup_guard = setup();
|
||||
let isolate = &mut v8::Isolate::new(Default::default());
|
||||
let scope = &mut v8::HandleScope::new(isolate);
|
||||
let context = v8::Context::new(scope);
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
|
||||
let x = v8::Integer::new(scope, 42);
|
||||
let y = v8::Integer::new(scope, 1337);
|
||||
|
||||
let argument = v8::String::new(scope, "x").unwrap();
|
||||
let extension = v8::Object::new(scope);
|
||||
let name = v8::String::new(scope, "y").unwrap();
|
||||
extension.set(scope, name.into(), y.into()).unwrap();
|
||||
|
||||
let source = v8::String::new(scope, "return x * y").unwrap();
|
||||
let source = v8::script_compiler::Source::new(source, None);
|
||||
let function = v8::script_compiler::compile_function_in_context(
|
||||
scope,
|
||||
source,
|
||||
&[argument],
|
||||
&[extension],
|
||||
v8::script_compiler::CompileOptions::NoCompileOptions,
|
||||
v8::script_compiler::NoCacheReason::NoReason,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let undefined = v8::undefined(scope).into();
|
||||
let result = function.call(scope, undefined, &[x.into()]).unwrap();
|
||||
assert!(result.is_int32());
|
||||
assert_eq!(42 * 1337, result.int32_value(scope).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_strings() {
|
||||
let _setup_guard = setup();
|
||||
|
|
Loading…
Reference in a new issue