Skip to content

Commit

Permalink
chore(ops): increase codegen tests coverage (denoland#16834)
Browse files Browse the repository at this point in the history
Upgrade fast_call tests to full (both tier) codegen tests.
  • Loading branch information
littledivy authored Nov 27, 2022
1 parent 28b5a7e commit 95fb4b8
Show file tree
Hide file tree
Showing 17 changed files with 1,370 additions and 46 deletions.
46 changes: 0 additions & 46 deletions ops/fast_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,49 +422,3 @@ fn exclude_lifetime_params(
where_clause: None,
})
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{Attributes, Op};
use std::path::PathBuf;

#[testing_macros::fixture("optimizer_tests/**/*.rs")]
fn test_fast_call_codegen(input: PathBuf) {
let update_expected = std::env::var("UPDATE_EXPECTED").is_ok();
let core = quote!(deno_core);

let source =
std::fs::read_to_string(&input).expect("Failed to read test file");

let mut attrs = Attributes::default();
if source.contains("// @test-attr:fast") {
attrs.must_be_fast = true;
}

let item = syn::parse_str(&source).expect("Failed to parse test file");
let mut op = Op::new(item, attrs);
let mut optimizer = Optimizer::new();
if optimizer.analyze(&mut op).is_err() {
// Tested by optimizer::test tests.
return;
}

let expected = std::fs::read_to_string(input.with_extension("out"))
.expect("Failed to read expected file");

let FastImplItems {
impl_and_fn: actual,
..
} = generate(&core, &mut optimizer, &op.item);
// Validate syntax tree.
let tree = syn::parse2(actual).unwrap();
let actual = prettyplease::unparse(&tree);
if update_expected {
std::fs::write(input.with_extension("out"), actual)
.expect("Failed to write expected file");
} else {
assert_eq!(actual, expected);
}
}
}
36 changes: 36 additions & 0 deletions ops/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,39 @@ fn exclude_lifetime_params(
.cloned()
.collect::<Punctuated<GenericParam, Comma>>()
}

#[cfg(test)]
mod tests {
use crate::{Attributes, Op};
use std::path::PathBuf;

#[testing_macros::fixture("optimizer_tests/**/*.rs")]
fn test_codegen(input: PathBuf) {
let update_expected = std::env::var("UPDATE_EXPECTED").is_ok();

let source =
std::fs::read_to_string(&input).expect("Failed to read test file");

let mut attrs = Attributes::default();
if source.contains("// @test-attr:fast") {
attrs.must_be_fast = true;
}

let item = syn::parse_str(&source).expect("Failed to parse test file");
let op = Op::new(item, attrs);

let expected = std::fs::read_to_string(input.with_extension("out"))
.expect("Failed to read expected output file");

let actual = op.gen();
// Validate syntax tree.
let tree = syn::parse2(actual).unwrap();
let actual = prettyplease::unparse(&tree);
if update_expected {
std::fs::write(input.with_extension("out"), actual)
.expect("Failed to write expected file");
} else {
assert_eq!(actual, expected);
}
}
}
78 changes: 78 additions & 0 deletions ops/optimizer_tests/async_nop.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
#[allow(non_camel_case_types)]
///Auto-generated by `deno_ops`, i.e: `#[op]`
///
///Use `op_void_async::decl()` to get an op-declaration
///you can include in a `deno_core::Extension`.
pub struct op_void_async;
#[doc(hidden)]
impl op_void_async {
pub fn name() -> &'static str {
stringify!(op_void_async)
}
pub fn v8_fn_ptr<'scope>() -> deno_core::v8::FunctionCallback {
use deno_core::v8::MapFnTo;
Self::v8_func.map_fn_to()
}
pub fn decl<'scope>() -> deno_core::OpDecl {
deno_core::OpDecl {
name: Self::name(),
v8_fn_ptr: Self::v8_fn_ptr(),
enabled: true,
fast_fn: Some(
Box::new(op_void_async_fast {
_phantom: ::std::marker::PhantomData,
}),
),
is_async: true,
is_unstable: false,
is_v8: false,
argc: 0usize,
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
async fn call() {}
pub fn v8_func<'scope>(
scope: &mut deno_core::v8::HandleScope<'scope>,
args: deno_core::v8::FunctionCallbackArguments,
mut rv: deno_core::v8::ReturnValue,
) {
use deno_core::futures::FutureExt;
let ctx = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast(args.data()).value()
as *const deno_core::_ops::OpCtx)
};
let op_id = ctx.id;
let promise_id = args.get(0);
let promise_id = deno_core::v8::Local::<
deno_core::v8::Integer,
>::try_from(promise_id)
.map(|l| l.value() as deno_core::PromiseId)
.map_err(deno_core::anyhow::Error::from);
let promise_id: deno_core::PromiseId = match promise_id {
Ok(promise_id) => promise_id,
Err(err) => {
deno_core::_ops::throw_type_error(
scope,
format!("invalid promise id: {}", err),
);
return;
}
};
let get_class = {
let state = ::std::cell::RefCell::borrow(&ctx.state);
state.tracker.track_async(op_id);
state.get_error_class_fn
};
deno_core::_ops::queue_async_op(
ctx,
scope,
false,
async move {
let result = Self::call().await;
let result = Ok(result);
(promise_id, op_id, deno_core::_ops::to_op_result(get_class, result))
},
);
}
}
struct op_void_async_fast {
_phantom: ::std::marker::PhantomData<()>,
}
Expand Down
137 changes: 137 additions & 0 deletions ops/optimizer_tests/async_result.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,140 @@
#[allow(non_camel_case_types)]
///Auto-generated by `deno_ops`, i.e: `#[op]`
///
///Use `op_read::decl()` to get an op-declaration
///you can include in a `deno_core::Extension`.
pub struct op_read;
#[doc(hidden)]
impl op_read {
pub fn name() -> &'static str {
stringify!(op_read)
}
pub fn v8_fn_ptr<'scope>() -> deno_core::v8::FunctionCallback {
use deno_core::v8::MapFnTo;
Self::v8_func.map_fn_to()
}
pub fn decl<'scope>() -> deno_core::OpDecl {
deno_core::OpDecl {
name: Self::name(),
v8_fn_ptr: Self::v8_fn_ptr(),
enabled: true,
fast_fn: Some(
Box::new(op_read_fast {
_phantom: ::std::marker::PhantomData,
}),
),
is_async: true,
is_unstable: false,
is_v8: false,
argc: 2usize,
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
async fn call(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: &mut [u8],
) -> Result<u32, Error> {}
pub fn v8_func<'scope>(
scope: &mut deno_core::v8::HandleScope<'scope>,
args: deno_core::v8::FunctionCallbackArguments,
mut rv: deno_core::v8::ReturnValue,
) {
use deno_core::futures::FutureExt;
let ctx = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast(args.data()).value()
as *const deno_core::_ops::OpCtx)
};
let op_id = ctx.id;
let promise_id = args.get(0);
let promise_id = deno_core::v8::Local::<
deno_core::v8::Integer,
>::try_from(promise_id)
.map(|l| l.value() as deno_core::PromiseId)
.map_err(deno_core::anyhow::Error::from);
let promise_id: deno_core::PromiseId = match promise_id {
Ok(promise_id) => promise_id,
Err(err) => {
deno_core::_ops::throw_type_error(
scope,
format!("invalid promise id: {}", err),
);
return;
}
};
let arg_0 = args.get(1usize as i32);
let arg_0 = match deno_core::serde_v8::from_v8(scope, arg_0) {
Ok(v) => v,
Err(err) => {
let msg = format!(
"Error parsing args at position {}: {}", 1usize,
deno_core::anyhow::Error::from(err)
);
return deno_core::_ops::throw_type_error(scope, msg);
}
};
let arg_1 = {
let value = args.get(2usize as i32);
match deno_core::v8::Local::<deno_core::v8::ArrayBuffer>::try_from(value) {
Ok(b) => {
let byte_length = b.byte_length();
if let Some(data) = b.data() {
let store = data.cast::<u8>().as_ptr();
unsafe { ::std::slice::from_raw_parts_mut(store, byte_length) }
} else {
&mut []
}
}
Err(_) => {
if let Ok(view)
= deno_core::v8::Local::<
deno_core::v8::ArrayBufferView,
>::try_from(value) {
let len = view.byte_length();
let offset = view.byte_offset();
let buffer = match view.buffer(scope) {
Some(v) => v,
None => {
return deno_core::_ops::throw_type_error(
scope,
format!("Expected ArrayBufferView at position {}", 2usize),
);
}
};
if let Some(data) = buffer.data() {
let store = data.cast::<u8>().as_ptr();
unsafe {
::std::slice::from_raw_parts_mut(store.add(offset), len)
}
} else {
&mut []
}
} else {
return deno_core::_ops::throw_type_error(
scope,
format!("Expected ArrayBufferView at position {}", 2usize),
);
}
}
}
};
let get_class = {
let state = ::std::cell::RefCell::borrow(&ctx.state);
state.tracker.track_async(op_id);
state.get_error_class_fn
};
deno_core::_ops::queue_async_op(
ctx,
scope,
false,
async move {
let result = Self::call(ctx.state.clone(), arg_0, arg_1).await;
(promise_id, op_id, deno_core::_ops::to_op_result(get_class, result))
},
);
}
}
struct op_read_fast {
_phantom: ::std::marker::PhantomData<()>,
}
Expand Down
53 changes: 53 additions & 0 deletions ops/optimizer_tests/callback_options.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
#[allow(non_camel_case_types)]
///Auto-generated by `deno_ops`, i.e: `#[op]`
///
///Use `op_fallback::decl()` to get an op-declaration
///you can include in a `deno_core::Extension`.
pub struct op_fallback;
#[doc(hidden)]
impl op_fallback {
pub fn name() -> &'static str {
stringify!(op_fallback)
}
pub fn v8_fn_ptr<'scope>() -> deno_core::v8::FunctionCallback {
use deno_core::v8::MapFnTo;
Self::v8_func.map_fn_to()
}
pub fn decl<'scope>() -> deno_core::OpDecl {
deno_core::OpDecl {
name: Self::name(),
v8_fn_ptr: Self::v8_fn_ptr(),
enabled: true,
fast_fn: Some(
Box::new(op_fallback_fast {
_phantom: ::std::marker::PhantomData,
}),
),
is_async: false,
is_unstable: false,
is_v8: false,
argc: 1usize,
}
}
#[inline]
#[allow(clippy::too_many_arguments)]
fn call(options: Option<&mut FastApiCallbackOptions>) {
if let Some(options) = options {
options.fallback = true;
}
}
pub fn v8_func<'scope>(
scope: &mut deno_core::v8::HandleScope<'scope>,
args: deno_core::v8::FunctionCallbackArguments,
mut rv: deno_core::v8::ReturnValue,
) {
let ctx = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast(args.data()).value()
as *const deno_core::_ops::OpCtx)
};
let arg_0 = None;
let result = Self::call(arg_0);
let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
op_state.tracker.track_sync(ctx.id);
}
}
struct op_fallback_fast {
_phantom: ::std::marker::PhantomData<()>,
}
Expand Down
Loading

0 comments on commit 95fb4b8

Please sign in to comment.