mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-25 15:29:43 -05:00
Add support for Fast calls with Uint8Array (#1047)
This commit is contained in:
parent
33e426bfe4
commit
4b3b081c03
3 changed files with 81 additions and 1 deletions
|
@ -79,6 +79,7 @@ pub enum SequenceType {
|
||||||
pub enum CType {
|
pub enum CType {
|
||||||
Void = 0,
|
Void = 0,
|
||||||
Bool,
|
Bool,
|
||||||
|
Uint8,
|
||||||
Int32,
|
Int32,
|
||||||
Uint32,
|
Uint32,
|
||||||
Int64,
|
Int64,
|
||||||
|
|
|
@ -7239,6 +7239,85 @@ fn test_fast_calls_arraybuffer() {
|
||||||
assert_eq!("fast", unsafe { WHO });
|
assert_eq!("fast", unsafe { WHO });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fast_calls_typedarray() {
|
||||||
|
static mut WHO: &str = "none";
|
||||||
|
fn fast_fn(
|
||||||
|
_recv: v8::Local<v8::Object>,
|
||||||
|
data: *const fast_api::FastApiTypedArray<u8>,
|
||||||
|
) -> u32 {
|
||||||
|
unsafe { WHO = "fast" };
|
||||||
|
let first = unsafe { &*data }.get(0);
|
||||||
|
let second = unsafe { &*data }.get(1);
|
||||||
|
let third = unsafe { &*data }.get(2);
|
||||||
|
assert_eq!(first, 4);
|
||||||
|
assert_eq!(second, 5);
|
||||||
|
assert_eq!(third, 6);
|
||||||
|
let sum = first + second + third;
|
||||||
|
sum.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FastTest;
|
||||||
|
impl fast_api::FastFunction for FastTest {
|
||||||
|
fn args(&self) -> &'static [fast_api::Type] {
|
||||||
|
&[
|
||||||
|
fast_api::Type::V8Value,
|
||||||
|
fast_api::Type::TypedArray(fast_api::CType::Uint8),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn return_type(&self) -> fast_api::CType {
|
||||||
|
fast_api::CType::Uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn function(&self) -> *const c_void {
|
||||||
|
fast_fn as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slow_fn(
|
||||||
|
scope: &mut v8::HandleScope,
|
||||||
|
_: v8::FunctionCallbackArguments,
|
||||||
|
mut rv: v8::ReturnValue,
|
||||||
|
) {
|
||||||
|
unsafe { WHO = "slow" };
|
||||||
|
rv.set(v8::Boolean::new(scope, false).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 global = context.global(scope);
|
||||||
|
|
||||||
|
let template =
|
||||||
|
v8::FunctionTemplate::builder(slow_fn).build_fast(scope, &FastTest, None);
|
||||||
|
|
||||||
|
let name = v8::String::new(scope, "func").unwrap();
|
||||||
|
let value = template.get_function(scope).unwrap();
|
||||||
|
global.set(scope, name.into(), value.into()).unwrap();
|
||||||
|
let source = r#"
|
||||||
|
function f(data) { return func(data); }
|
||||||
|
%PrepareFunctionForOptimization(f);
|
||||||
|
const arr = new Uint8Array([4, 5, 6]);
|
||||||
|
f(arr);
|
||||||
|
"#;
|
||||||
|
eval(scope, source).unwrap();
|
||||||
|
assert_eq!("slow", unsafe { WHO });
|
||||||
|
|
||||||
|
let source = r#"
|
||||||
|
%OptimizeFunctionOnNextCall(f);
|
||||||
|
const result = f(arr);
|
||||||
|
if (result != 15) {
|
||||||
|
throw new Error("wrong result");
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
eval(scope, source).unwrap();
|
||||||
|
assert_eq!("fast", unsafe { WHO });
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fast_calls_reciever() {
|
fn test_fast_calls_reciever() {
|
||||||
const V8_WRAPPER_TYPE_INDEX: i32 = 0;
|
const V8_WRAPPER_TYPE_INDEX: i32 = 0;
|
||||||
|
|
2
v8
2
v8
|
@ -1 +1 @@
|
||||||
Subproject commit d50c91cc9e5baab72fd876b7c05acb002810e0d6
|
Subproject commit 151c55f530a172751dcb719b3b31cee7fdde0029
|
Loading…
Reference in a new issue