2019-12-04 00:57:06 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
|
|
|
use rusty_v8 as v8;
|
2019-12-10 22:43:22 -05:00
|
|
|
use rusty_v8::{new_null, FunctionCallbackInfo, Local};
|
2019-12-04 08:12:27 -05:00
|
|
|
use std::default::Default;
|
2019-12-04 00:57:06 -05:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref INIT_LOCK: Mutex<u32> = Mutex::new(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestGuard {}
|
|
|
|
|
|
|
|
impl Drop for TestGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// TODO shutdown process cleanly.
|
|
|
|
/*
|
|
|
|
*g -= 1;
|
|
|
|
if *g == 0 {
|
|
|
|
unsafe { v8::V8::dispose() };
|
|
|
|
v8::V8::shutdown_platform();
|
|
|
|
}
|
|
|
|
drop(g);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:03:18 -05:00
|
|
|
fn setup() -> TestGuard {
|
2019-12-04 00:57:06 -05:00
|
|
|
let mut g = INIT_LOCK.lock().unwrap();
|
|
|
|
*g += 1;
|
|
|
|
if *g == 1 {
|
|
|
|
v8::V8::initialize_platform(v8::platform::new_default_platform());
|
|
|
|
v8::V8::initialize();
|
|
|
|
}
|
|
|
|
drop(g);
|
|
|
|
TestGuard {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn handle_scope_nested() {
|
|
|
|
let g = setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
v8::HandleScope::enter(scope, |_scope| {});
|
|
|
|
});
|
|
|
|
drop(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(clippy::float_cmp)]
|
|
|
|
fn handle_scope_numbers() {
|
|
|
|
let g = setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let l1 = v8::Integer::new(scope, -123);
|
|
|
|
let l2 = v8::Integer::new_from_unsigned(scope, 456);
|
|
|
|
v8::HandleScope::enter(scope, |scope2| {
|
|
|
|
let l3 = v8::Number::new(scope2, 78.9);
|
|
|
|
assert_eq!(l1.value(), -123);
|
|
|
|
assert_eq!(l2.value(), 456);
|
|
|
|
assert_eq!(l3.value(), 78.9);
|
|
|
|
assert_eq!(v8::Number::value(&l1), -123f64);
|
|
|
|
assert_eq!(v8::Number::value(&l2), 456f64);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
drop(g);
|
|
|
|
}
|
|
|
|
|
2019-12-04 02:03:17 -05:00
|
|
|
#[test]
|
|
|
|
fn test_string() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let reference = "Hello 🦕 world!";
|
|
|
|
let local =
|
2019-12-04 10:10:15 -05:00
|
|
|
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
|
2019-12-05 12:28:20 -05:00
|
|
|
assert_eq!(15, local.length());
|
|
|
|
assert_eq!(17, local.utf8_length(scope));
|
2019-12-04 02:03:17 -05:00
|
|
|
assert_eq!(reference, local.to_rust_string_lossy(scope));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:57:06 -05:00
|
|
|
#[test]
|
|
|
|
fn isolate_new() {
|
|
|
|
let g = setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
v8::Isolate::new(params);
|
|
|
|
drop(g);
|
|
|
|
}
|
|
|
|
|
2019-12-04 08:12:27 -05:00
|
|
|
#[test]
|
|
|
|
fn script_compile_and_run() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
|
|
|
|
v8::HandleScope::enter(&mut locker, |s| {
|
|
|
|
let mut context = v8::Context::new(s);
|
|
|
|
context.enter();
|
|
|
|
let source =
|
|
|
|
v8::String::new(s, "'Hello ' + 13 + 'th planet'", Default::default())
|
|
|
|
.unwrap();
|
|
|
|
let mut script = v8::Script::compile(s, context, source, None).unwrap();
|
|
|
|
source.to_rust_string_lossy(s);
|
|
|
|
let result = script.run(s, context).unwrap();
|
|
|
|
// TODO: safer casts.
|
|
|
|
let result: v8::Local<v8::String> =
|
|
|
|
unsafe { std::mem::transmute_copy(&result) };
|
|
|
|
assert_eq!(result.to_rust_string_lossy(s), "Hello 13th planet");
|
2019-12-05 12:55:46 -05:00
|
|
|
context.exit();
|
2019-12-04 08:12:27 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:46:36 -05:00
|
|
|
#[test]
|
|
|
|
fn script_origin() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
|
|
|
|
v8::HandleScope::enter(&mut locker, |s| {
|
|
|
|
let mut context = v8::Context::new(s);
|
|
|
|
context.enter();
|
|
|
|
|
|
|
|
let resource_name =
|
|
|
|
v8::String::new(s, "foo.js", Default::default()).unwrap();
|
|
|
|
let resource_line_offset = v8::Integer::new(s, 4);
|
|
|
|
let resource_column_offset = v8::Integer::new(s, 5);
|
|
|
|
let resource_is_shared_cross_origin = v8::new_true(s);
|
|
|
|
let script_id = v8::Integer::new(s, 123);
|
|
|
|
let source_map_url =
|
|
|
|
v8::String::new(s, "source_map_url", Default::default()).unwrap();
|
|
|
|
let resource_is_opaque = v8::new_true(s);
|
|
|
|
let is_wasm = v8::new_false(s);
|
|
|
|
let is_module = v8::new_false(s);
|
|
|
|
|
|
|
|
let script_origin = v8::ScriptOrigin::new(
|
|
|
|
resource_name.into(),
|
|
|
|
resource_line_offset,
|
|
|
|
resource_column_offset,
|
|
|
|
resource_is_shared_cross_origin,
|
|
|
|
script_id,
|
|
|
|
source_map_url.into(),
|
|
|
|
resource_is_opaque,
|
|
|
|
is_wasm,
|
|
|
|
is_module,
|
|
|
|
);
|
|
|
|
|
|
|
|
let source = v8::String::new(s, "1+2", Default::default()).unwrap();
|
|
|
|
let mut script =
|
|
|
|
v8::Script::compile(s, context, source, Some(&script_origin)).unwrap();
|
|
|
|
source.to_rust_string_lossy(s);
|
|
|
|
let _result = script.run(s, context).unwrap();
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:57:06 -05:00
|
|
|
#[test]
|
|
|
|
fn get_version() {
|
|
|
|
assert!(v8::V8::get_version().len() > 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_flags_from_command_line() {
|
|
|
|
let r = v8::V8::set_flags_from_command_line(vec![
|
|
|
|
"binaryname".to_string(),
|
|
|
|
"--log-colour".to_string(),
|
|
|
|
"--should-be-ignored".to_string(),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r,
|
|
|
|
vec!["binaryname".to_string(), "--should-be-ignored".to_string()]
|
|
|
|
);
|
|
|
|
}
|
2019-12-04 10:31:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inspector_string_view() {
|
|
|
|
let chars = b"Hello world!";
|
|
|
|
let view = v8::inspector::StringView::from(&chars[..]);
|
|
|
|
|
|
|
|
assert_eq!(chars.len(), view.into_iter().len());
|
|
|
|
assert_eq!(chars.len(), view.len());
|
|
|
|
for (c1, c2) in chars.iter().copied().map(u16::from).zip(&view) {
|
|
|
|
assert_eq!(c1, c2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inspector_string_buffer() {
|
|
|
|
let chars = b"Hello Venus!";
|
|
|
|
let mut buf = {
|
|
|
|
let src_view = v8::inspector::StringView::from(&chars[..]);
|
|
|
|
v8::inspector::StringBuffer::create(&src_view)
|
|
|
|
};
|
|
|
|
let view = buf.as_mut().unwrap().string();
|
|
|
|
|
|
|
|
assert_eq!(chars.len(), view.into_iter().len());
|
|
|
|
assert_eq!(chars.len(), view.len());
|
|
|
|
for (c1, c2) in chars.iter().copied().map(u16::from).zip(view) {
|
|
|
|
assert_eq!(c1, c2);
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 17:03:18 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_primitives() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
2019-12-06 09:36:34 -05:00
|
|
|
let null = v8::new_null(scope);
|
|
|
|
assert!(!null.is_undefined());
|
|
|
|
assert!(null.is_null());
|
|
|
|
assert!(null.is_null_or_undefined());
|
|
|
|
|
|
|
|
let undefined = v8::new_undefined(scope);
|
|
|
|
assert!(undefined.is_undefined());
|
|
|
|
assert!(!undefined.is_null());
|
|
|
|
assert!(undefined.is_null_or_undefined());
|
|
|
|
|
|
|
|
let true_ = v8::new_true(scope);
|
|
|
|
assert!(!true_.is_undefined());
|
|
|
|
assert!(!true_.is_null());
|
|
|
|
assert!(!true_.is_null_or_undefined());
|
|
|
|
|
|
|
|
let false_ = v8::new_false(scope);
|
|
|
|
assert!(!false_.is_undefined());
|
|
|
|
assert!(!false_.is_null());
|
|
|
|
assert!(!false_.is_null_or_undefined());
|
2019-12-05 17:03:18 -05:00
|
|
|
});
|
|
|
|
}
|
2019-12-08 20:26:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exception() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
isolate.enter();
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
|
|
|
let reference = "This is a test error";
|
|
|
|
let local =
|
|
|
|
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
|
|
|
|
v8::Exception::RangeError(local);
|
|
|
|
v8::Exception::ReferenceError(local);
|
|
|
|
v8::Exception::SyntaxError(local);
|
|
|
|
v8::Exception::TypeError(local);
|
|
|
|
let exception = v8::Exception::Error(local);
|
|
|
|
let mut msg = v8::Exception::CreateMessage(scope, exception);
|
|
|
|
let msg_string = msg.get();
|
|
|
|
let rust_msg_string = msg_string.to_rust_string_lossy(scope);
|
2019-12-09 19:14:07 -05:00
|
|
|
assert_eq!(
|
|
|
|
"Uncaught Error: This is a test error".to_string(),
|
|
|
|
rust_msg_string
|
|
|
|
);
|
2019-12-08 20:26:58 -05:00
|
|
|
assert!(v8::Exception::GetStackTrace(exception).is_none());
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
isolate.exit();
|
|
|
|
}
|
2019-12-09 17:11:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn json() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |s| {
|
|
|
|
let mut context = v8::Context::new(s);
|
|
|
|
context.enter();
|
|
|
|
let json_string =
|
2019-12-09 19:14:07 -05:00
|
|
|
v8::String::new(s, "{\"a\": 1, \"b\": 2}", Default::default()).unwrap();
|
2019-12-09 17:11:31 -05:00
|
|
|
let maybe_value = v8::JSON::Parse(context, json_string);
|
|
|
|
assert!(maybe_value.is_some());
|
|
|
|
let value = maybe_value.unwrap();
|
|
|
|
let maybe_stringified = v8::JSON::Stringify(context, value);
|
|
|
|
assert!(maybe_stringified.is_some());
|
|
|
|
let stringified = maybe_stringified.unwrap();
|
|
|
|
let rust_str = stringified.to_rust_string_lossy(s);
|
|
|
|
assert_eq!("{\"a\":1,\"b\":2}".to_string(), rust_str);
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
2019-12-09 19:14:07 -05:00
|
|
|
|
|
|
|
// TODO Safer casts https://github.com/denoland/rusty_v8/issues/51
|
|
|
|
fn cast<U, T>(local: v8::Local<T>) -> v8::Local<U> {
|
|
|
|
let cast_local: v8::Local<U> = unsafe { std::mem::transmute_copy(&local) };
|
|
|
|
cast_local
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn object() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
2019-12-13 21:40:54 -05:00
|
|
|
let null: v8::Local<v8::Value> = new_null(scope).into();
|
2019-12-09 19:14:07 -05:00
|
|
|
let s1 = v8::String::new(scope, "a", v8::NewStringType::Normal).unwrap();
|
|
|
|
let s2 = v8::String::new(scope, "b", v8::NewStringType::Normal).unwrap();
|
|
|
|
let name1: Local<v8::Name> = cast(s1);
|
|
|
|
let name2: Local<v8::Name> = cast(s2);
|
|
|
|
let names = vec![name1, name2];
|
2019-12-13 21:40:54 -05:00
|
|
|
let v1: v8::Local<v8::Value> = v8::Number::new(scope, 1.0).into();
|
|
|
|
let v2: v8::Local<v8::Value> = v8::Number::new(scope, 2.0).into();
|
2019-12-09 19:14:07 -05:00
|
|
|
let values = vec![v1, v2];
|
|
|
|
let object = v8::Object::new(scope, null, names, values, 2);
|
|
|
|
assert!(!object.is_null_or_undefined());
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
2019-12-10 22:43:22 -05:00
|
|
|
|
2019-12-13 20:18:30 -05:00
|
|
|
#[test]
|
|
|
|
fn promise_resolved() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
|
|
|
let maybe_resolver = v8::PromiseResolver::new(context);
|
|
|
|
assert!(maybe_resolver.is_some());
|
|
|
|
let mut resolver = maybe_resolver.unwrap();
|
|
|
|
let mut promise = resolver.get_promise(scope);
|
|
|
|
assert!(!promise.has_handler());
|
|
|
|
assert_eq!(promise.state(), v8::PromiseState::Pending);
|
|
|
|
let str =
|
|
|
|
v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap();
|
|
|
|
let value: Local<v8::Value> = cast(str);
|
|
|
|
resolver.resolve(context, value);
|
|
|
|
assert_eq!(promise.state(), v8::PromiseState::Fulfilled);
|
|
|
|
let result = promise.result(scope);
|
|
|
|
let result_str: v8::Local<v8::String> = cast(result);
|
|
|
|
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
|
|
|
|
// Resolve again with different value, since promise is already in `Fulfilled` state
|
|
|
|
// it should be ignored.
|
|
|
|
let str =
|
|
|
|
v8::String::new(scope, "test2", v8::NewStringType::Normal).unwrap();
|
|
|
|
let value: Local<v8::Value> = cast(str);
|
|
|
|
resolver.resolve(context, value);
|
|
|
|
let result = promise.result(scope);
|
|
|
|
let result_str: v8::Local<v8::String> = cast(result);
|
|
|
|
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:43:22 -05:00
|
|
|
extern "C" fn callback(info: &FunctionCallbackInfo) {
|
|
|
|
assert_eq!(info.length(), 0);
|
|
|
|
let mut locker = v8::Locker::new(info.get_isolate());
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
|
|
|
let s =
|
|
|
|
v8::String::new(scope, "Hello callback!", v8::NewStringType::Normal)
|
|
|
|
.unwrap();
|
2019-12-13 21:40:54 -05:00
|
|
|
let value: Local<v8::Value> = s.into();
|
2019-12-17 13:19:40 -05:00
|
|
|
let rv = info.get_return_value();
|
|
|
|
let rv_value = rv.get(scope);
|
|
|
|
assert!(rv_value.is_undefined());
|
|
|
|
rv.set(value);
|
2019-12-10 22:43:22 -05:00
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:18:30 -05:00
|
|
|
#[test]
|
|
|
|
fn promise_rejected() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
|
|
|
let maybe_resolver = v8::PromiseResolver::new(context);
|
|
|
|
assert!(maybe_resolver.is_some());
|
|
|
|
let mut resolver = maybe_resolver.unwrap();
|
|
|
|
let mut promise = resolver.get_promise(scope);
|
|
|
|
assert!(!promise.has_handler());
|
|
|
|
assert_eq!(promise.state(), v8::PromiseState::Pending);
|
|
|
|
let str =
|
|
|
|
v8::String::new(scope, "test", v8::NewStringType::Normal).unwrap();
|
|
|
|
let value: Local<v8::Value> = cast(str);
|
|
|
|
let rejected = resolver.reject(context, value);
|
|
|
|
assert!(rejected);
|
|
|
|
assert_eq!(promise.state(), v8::PromiseState::Rejected);
|
|
|
|
let result = promise.result(scope);
|
|
|
|
let result_str: v8::Local<v8::String> = cast(result);
|
|
|
|
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
|
|
|
|
// Reject again with different value, since promise is already in `Rejected` state
|
|
|
|
// it should be ignored.
|
|
|
|
let str =
|
|
|
|
v8::String::new(scope, "test2", v8::NewStringType::Normal).unwrap();
|
|
|
|
let value: Local<v8::Value> = cast(str);
|
|
|
|
resolver.reject(context, value);
|
|
|
|
let result = promise.result(scope);
|
|
|
|
let result_str: v8::Local<v8::String> = cast(result);
|
|
|
|
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:43:22 -05:00
|
|
|
#[test]
|
|
|
|
fn function() {
|
|
|
|
setup();
|
|
|
|
let mut params = v8::Isolate::create_params();
|
|
|
|
params.set_array_buffer_allocator(
|
|
|
|
v8::array_buffer::Allocator::new_default_allocator(),
|
|
|
|
);
|
|
|
|
let mut isolate = v8::Isolate::new(params);
|
|
|
|
let mut locker = v8::Locker::new(&mut isolate);
|
|
|
|
v8::HandleScope::enter(&mut locker, |scope| {
|
|
|
|
let mut context = v8::Context::new(scope);
|
|
|
|
context.enter();
|
|
|
|
let global = context.global();
|
2019-12-13 21:40:54 -05:00
|
|
|
let recv: Local<v8::Value> = global.into();
|
2019-12-10 22:43:22 -05:00
|
|
|
// create function using template
|
|
|
|
let mut fn_template = v8::FunctionTemplate::new(scope, callback);
|
|
|
|
let mut function = fn_template
|
|
|
|
.get_function(context)
|
|
|
|
.expect("Unable to create function");
|
|
|
|
let _value = v8::Function::call(&mut *function, context, recv, 0, vec![]);
|
|
|
|
// create function without a template
|
|
|
|
let mut function =
|
|
|
|
v8::Function::new(context, callback).expect("Unable to create function");
|
|
|
|
let maybe_value =
|
|
|
|
v8::Function::call(&mut *function, context, recv, 0, vec![]);
|
|
|
|
let value = maybe_value.unwrap();
|
|
|
|
let value_str: v8::Local<v8::String> = cast(value);
|
|
|
|
let rust_str = value_str.to_rust_string_lossy(scope);
|
|
|
|
assert_eq!(rust_str, "Hello callback!".to_string());
|
|
|
|
context.exit();
|
|
|
|
});
|
|
|
|
}
|