Created
January 1, 2021 17:39
-
-
Save paxbun/bc56423aa813aa36baa912fa43f367c5 to your computer and use it in GitHub Desktop.
rusty_v8 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rusty_v8 as v8; | |
static MODULE_CODE_1: &'static str = r#" | |
import { foo, bar } from "module_2"; | |
let input = foo(3); | |
bar(...input); | |
console.log('foo'); | |
"#; | |
fn foo( | |
scope: &mut v8::HandleScope, | |
args: v8::FunctionCallbackArguments, | |
mut retval: v8::ReturnValue, | |
) { | |
assert_eq!(args.length(), 1); | |
let rep = args.get(0).integer_value(scope).unwrap(); | |
let mut arr = vec![]; | |
for _ in 0..rep { | |
let mut s = String::new(); | |
std::io::stdin().read_line(&mut s).unwrap(); | |
arr.push(v8::String::new(scope, &s.trim()).unwrap().into()); | |
} | |
retval.set(v8::Array::new_with_elements(scope, &arr).into()); | |
} | |
fn bar( | |
scope: &mut v8::HandleScope, | |
args: v8::FunctionCallbackArguments, | |
_: v8::ReturnValue, | |
) { | |
let length = args.length(); | |
for i in 0..length { | |
println!("{}: {}", i + 1, args.get(i).to_rust_string_lossy(scope)); | |
} | |
} | |
fn my_syntatic_module_callback<'a>( | |
context: v8::Local<'a, v8::Context>, | |
module: v8::Local<'a, v8::Module> | |
) -> Option<v8::Local<'a, v8::Value>> { | |
let scope = &mut unsafe { v8::CallbackScope::new(context) }; | |
let scope = &mut v8::EscapableHandleScope::new(scope); | |
let scope = &mut v8::ContextScope::new(scope, context); | |
let export_name = v8::String::new(scope, "foo").unwrap(); | |
let function = v8::Function::new(scope, foo).unwrap(); | |
module.set_synthetic_module_export(scope, export_name, function.into()).unwrap(); | |
let export_name = v8::String::new(scope, "bar").unwrap(); | |
let function = v8::Function::new(scope, bar).unwrap(); | |
module.set_synthetic_module_export(scope, export_name, function.into()).unwrap(); | |
let value = v8::Boolean::new(scope, true).into(); | |
return Some(scope.escape(value)); | |
} | |
fn my_resolve_callback<'a>( | |
context: v8::Local<'a, v8::Context>, | |
_: v8::Local<'a, v8::String>, | |
_: v8::Local<'a, v8::Module>, | |
) -> Option<v8::Local<'a, v8::Module>> { | |
let scope = &mut unsafe { v8::CallbackScope::new(context) }; | |
let scope = &mut v8::EscapableHandleScope::new(scope); | |
let scope = &mut v8::ContextScope::new(scope, context); | |
let module_name = v8::String::new(scope, "module_2").unwrap(); | |
let export_names = &[v8::String::new(scope, "foo").unwrap(), v8::String::new(scope, "bar").unwrap()]; | |
let module = v8::Module::create_synthetic_module( | |
scope, | |
module_name, | |
export_names, | |
my_syntatic_module_callback); | |
Some(scope.escape(module)) | |
} | |
fn main() { | |
let platform = v8::new_default_platform().unwrap(); | |
v8::V8::initialize_platform(platform); | |
v8::V8::initialize(); | |
let isolate = &mut v8::Isolate::new(Default::default()); | |
let scope = &mut v8::HandleScope::new(isolate); | |
let module = { | |
let context = v8::Context::new(scope); | |
let scope = &mut v8::EscapableHandleScope::new(scope); | |
let scope = &mut v8::ContextScope::new(scope, context); | |
let code = v8::String::new(scope, MODULE_CODE_1).unwrap(); | |
let source = v8::script_compiler::Source::new( | |
code, &v8::ScriptOrigin::new( | |
v8::String::new(scope, "module_1").unwrap().into(), | |
v8::Integer::new(scope, 0), | |
v8::Integer::new(scope, 0), | |
v8::Boolean::new(scope, false), | |
v8::Integer::new(scope, 0), | |
v8::null(scope).into(), | |
v8::Boolean::new(scope, false), | |
v8::Boolean::new(scope, false), | |
v8::Boolean::new(scope, true) | |
)); | |
let module = v8::script_compiler::compile_module(scope, source).unwrap(); | |
module.instantiate_module(scope, my_resolve_callback).unwrap(); | |
scope.escape(module) | |
}; | |
let context = v8::Context::new(scope); | |
let scope = &mut v8::ContextScope::new(scope, context); | |
module.evaluate(scope).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment