summaryrefslogtreecommitdiff
path: root/ops
diff options
context:
space:
mode:
Diffstat (limited to 'ops')
-rw-r--r--ops/fast_call.rs4
-rw-r--r--ops/lib.rs34
-rw-r--r--ops/optimizer.rs97
-rw-r--r--ops/optimizer_tests/cow_str.expected11
-rw-r--r--ops/optimizer_tests/cow_str.out87
-rw-r--r--ops/optimizer_tests/cow_str.rs3
-rw-r--r--ops/optimizer_tests/op_blob_revoke_object_url.expected12
-rw-r--r--ops/optimizer_tests/op_blob_revoke_object_url.out2
-rw-r--r--ops/optimizer_tests/op_blob_revoke_object_url.rs3
-rw-r--r--ops/optimizer_tests/op_print.expected11
-rw-r--r--ops/optimizer_tests/op_print.out82
-rw-r--r--ops/optimizer_tests/op_print.rs7
-rw-r--r--ops/optimizer_tests/owned_string.expected11
-rw-r--r--ops/optimizer_tests/owned_string.out99
-rw-r--r--ops/optimizer_tests/owned_string.rs3
-rw-r--r--ops/optimizer_tests/strings.expected11
-rw-r--r--ops/optimizer_tests/strings.out100
-rw-r--r--ops/optimizer_tests/strings.rs3
-rw-r--r--ops/optimizer_tests/strings_result.expected11
-rw-r--r--ops/optimizer_tests/strings_result.out71
-rw-r--r--ops/optimizer_tests/strings_result.rs4
21 files changed, 659 insertions, 7 deletions
diff --git a/ops/fast_call.rs b/ops/fast_call.rs
index 12ff9e446..0ddf5c878 100644
--- a/ops/fast_call.rs
+++ b/ops/fast_call.rs
@@ -430,7 +430,8 @@ fn q_fast_ty(v: &FastValue) -> Quote {
FastValue::V8Value => q!({ v8::Local<v8::Value> }),
FastValue::Uint8Array
| FastValue::Uint32Array
- | FastValue::Float64Array => unreachable!(),
+ | FastValue::Float64Array
+ | FastValue::SeqOneByteString => unreachable!(),
}
}
@@ -450,6 +451,7 @@ fn q_fast_ty_variant(v: &FastValue) -> Quote {
FastValue::Uint8Array => q!({ TypedArray(CType::Uint8) }),
FastValue::Uint32Array => q!({ TypedArray(CType::Uint32) }),
FastValue::Float64Array => q!({ TypedArray(CType::Float64) }),
+ FastValue::SeqOneByteString => q!({ SeqOneByteString }),
}
}
diff --git a/ops/lib.rs b/ops/lib.rs
index 28cb3c320..19667ab45 100644
--- a/ops/lib.rs
+++ b/ops/lib.rs
@@ -423,7 +423,12 @@ fn codegen_arg(
return quote! { let #ident = (); };
}
// Fast path for `String`
- if is_string(&**ty) {
+ if let Some(is_ref) = is_string(&**ty) {
+ let ref_block = if is_ref {
+ quote! { let #ident = #ident.as_ref(); }
+ } else {
+ quote! {}
+ };
return quote! {
let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) {
Ok(v8_string) => #core::serde_v8::to_utf8(v8_string, scope),
@@ -431,6 +436,18 @@ fn codegen_arg(
return #core::_ops::throw_type_error(scope, format!("Expected string at position {}", #idx));
}
};
+ #ref_block
+ };
+ }
+ // Fast path for `Cow<'_, str>`
+ if is_cow_str(&**ty) {
+ return quote! {
+ let #ident = match #core::v8::Local::<#core::v8::String>::try_from(args.get(#idx as i32)) {
+ Ok(v8_string) => ::std::borrow::Cow::Owned(#core::serde_v8::to_utf8(v8_string, scope)),
+ Err(_) => {
+ return #core::_ops::throw_type_error(scope, format!("Expected string at position {}", #idx));
+ }
+ };
};
}
// Fast path for `Option<String>`
@@ -701,14 +718,25 @@ fn is_result(ty: impl ToTokens) -> bool {
}
}
-fn is_string(ty: impl ToTokens) -> bool {
- tokens(ty) == "String"
+fn is_string(ty: impl ToTokens) -> Option<bool> {
+ let toks = tokens(ty);
+ if toks == "String" {
+ return Some(false);
+ }
+ if toks == "& str" {
+ return Some(true);
+ }
+ None
}
fn is_option_string(ty: impl ToTokens) -> bool {
tokens(ty) == "Option < String >"
}
+fn is_cow_str(ty: impl ToTokens) -> bool {
+ tokens(&ty).starts_with("Cow <") && tokens(&ty).ends_with("str >")
+}
+
enum SliceType {
U8,
U8Mut,
diff --git a/ops/optimizer.rs b/ops/optimizer.rs
index 115011008..5a2be7a01 100644
--- a/ops/optimizer.rs
+++ b/ops/optimizer.rs
@@ -38,12 +38,20 @@ pub(crate) enum BailoutReason {
}
#[derive(Debug, PartialEq)]
+enum StringType {
+ Cow,
+ Ref,
+ Owned,
+}
+
+#[derive(Debug, PartialEq)]
enum TransformKind {
// serde_v8::Value
V8Value,
SliceU32(bool),
SliceU8(bool),
SliceF64(bool),
+ SeqOneByteString(StringType),
PtrU8,
PtrVoid,
WasmMemory,
@@ -78,6 +86,13 @@ impl Transform {
}
}
+ fn seq_one_byte_string(index: usize, is_ref: StringType) -> Self {
+ Transform {
+ kind: TransformKind::SeqOneByteString(is_ref),
+ index,
+ }
+ }
+
fn wasm_memory(index: usize) -> Self {
Transform {
kind: TransformKind::WasmMemory,
@@ -180,6 +195,21 @@ impl Transform {
};
})
}
+ // &str
+ TransformKind::SeqOneByteString(str_ty) => {
+ *ty = parse_quote! { *const #core::v8::fast_api::FastApiOneByteString };
+ match str_ty {
+ StringType::Ref => q!(Vars { var: &ident }, {
+ let var = unsafe { &*var }.as_str();
+ }),
+ StringType::Cow => q!(Vars { var: &ident }, {
+ let var = ::std::borrow::Cow::Borrowed(unsafe { &*var }.as_str());
+ }),
+ StringType::Owned => q!(Vars { var: &ident }, {
+ let var = unsafe { &*var }.as_str().to_owned();
+ }),
+ }
+ }
TransformKind::WasmMemory => {
// Note: `ty` is correctly set to __opts by the fast call tier.
// U8 slice is always byte-aligned.
@@ -252,6 +282,7 @@ pub(crate) enum FastValue {
Uint8Array,
Uint32Array,
Float64Array,
+ SeqOneByteString,
}
impl FastValue {
@@ -388,6 +419,13 @@ impl Optimizer {
self.analyze_param_type(index, param)?;
}
+ // TODO(@littledivy): https://github.com/denoland/deno/issues/17159
+ if self.returns_result
+ && self.fast_parameters.contains(&FastValue::SeqOneByteString)
+ {
+ self.fast_compatible = false;
+ }
+
Ok(())
}
@@ -661,10 +699,58 @@ impl Optimizer {
}
}
}
+ // Cow<'_, str>
+ PathSegment {
+ ident, arguments, ..
+ } if ident == "Cow" => {
+ if let PathArguments::AngleBracketed(
+ AngleBracketedGenericArguments { args, .. },
+ ) = arguments
+ {
+ assert_eq!(args.len(), 2);
+
+ let ty = &args[1];
+ match ty {
+ GenericArgument::Type(Type::Path(TypePath {
+ path: Path { segments, .. },
+ ..
+ })) => {
+ let segment = single_segment(segments)?;
+ match segment {
+ PathSegment { ident, .. } if ident == "str" => {
+ self.fast_parameters.push(FastValue::SeqOneByteString);
+ assert!(self
+ .transforms
+ .insert(
+ index,
+ Transform::seq_one_byte_string(
+ index,
+ StringType::Cow
+ )
+ )
+ .is_none());
+ }
+ _ => return Err(BailoutReason::FastUnsupportedParamType),
+ }
+ }
+ _ => return Err(BailoutReason::FastUnsupportedParamType),
+ }
+ }
+ }
// Is `T` a fast scalar?
PathSegment { ident, .. } => {
if let Some(val) = get_fast_scalar(ident.to_string().as_str()) {
self.fast_parameters.push(val);
+ } else if ident == "String" {
+ // Is `T` an owned String?
+ self.fast_parameters.push(FastValue::SeqOneByteString);
+ assert!(self
+ .transforms
+ .insert(
+ index,
+ Transform::seq_one_byte_string(index, StringType::Owned)
+ )
+ .is_none());
} else {
return Err(BailoutReason::FastUnsupportedParamType);
}
@@ -687,6 +773,17 @@ impl Optimizer {
{
self.has_ref_opstate = true;
}
+ // Is `T` a str?
+ PathSegment { ident, .. } if ident == "str" => {
+ self.fast_parameters.push(FastValue::SeqOneByteString);
+ assert!(self
+ .transforms
+ .insert(
+ index,
+ Transform::seq_one_byte_string(index, StringType::Ref)
+ )
+ .is_none());
+ }
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
diff --git a/ops/optimizer_tests/cow_str.expected b/ops/optimizer_tests/cow_str.expected
new file mode 100644
index 000000000..8b28965ba
--- /dev/null
+++ b/ops/optimizer_tests/cow_str.expected
@@ -0,0 +1,11 @@
+=== Optimizer Dump ===
+returns_result: false
+has_ref_opstate: false
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(Void)
+fast_parameters: [V8Value, SeqOneByteString]
+transforms: {0: Transform { kind: SeqOneByteString(Cow), index: 0 }}
+is_async: false
+fast_compatible: true
diff --git a/ops/optimizer_tests/cow_str.out b/ops/optimizer_tests/cow_str.out
new file mode 100644
index 000000000..da405f451
--- /dev/null
+++ b/ops/optimizer_tests/cow_str.out
@@ -0,0 +1,87 @@
+#[allow(non_camel_case_types)]
+///Auto-generated by `deno_ops`, i.e: `#[op]`
+///
+///Use `op_cow_str::decl()` to get an op-declaration
+///you can include in a `deno_core::Extension`.
+pub struct op_cow_str;
+#[doc(hidden)]
+impl op_cow_str {
+ pub fn name() -> &'static str {
+ stringify!(op_cow_str)
+ }
+ 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_cow_str_fast {
+ _phantom: ::std::marker::PhantomData,
+ }),
+ ),
+ is_async: false,
+ is_unstable: false,
+ is_v8: false,
+ argc: 1usize,
+ }
+ }
+ #[inline]
+ #[allow(clippy::too_many_arguments)]
+ fn call(c: Cow<'_, str>) {}
+ 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 = match deno_core::v8::Local::<
+ deno_core::v8::String,
+ >::try_from(args.get(0usize as i32)) {
+ Ok(v8_string) => {
+ ::std::borrow::Cow::Owned(deno_core::serde_v8::to_utf8(v8_string, scope))
+ }
+ Err(_) => {
+ return deno_core::_ops::throw_type_error(
+ scope,
+ format!("Expected string at position {}", 0usize),
+ );
+ }
+ };
+ let result = Self::call(arg_0);
+ let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
+ op_state.tracker.track_sync(ctx.id);
+ }
+}
+struct op_cow_str_fast {
+ _phantom: ::std::marker::PhantomData<()>,
+}
+impl<'scope> deno_core::v8::fast_api::FastFunction for op_cow_str_fast {
+ fn function(&self) -> *const ::std::ffi::c_void {
+ op_cow_str_fast_fn as *const ::std::ffi::c_void
+ }
+ fn args(&self) -> &'static [deno_core::v8::fast_api::Type] {
+ use deno_core::v8::fast_api::Type::*;
+ use deno_core::v8::fast_api::CType;
+ &[V8Value, SeqOneByteString]
+ }
+ fn return_type(&self) -> deno_core::v8::fast_api::CType {
+ deno_core::v8::fast_api::CType::Void
+ }
+}
+fn op_cow_str_fast_fn<'scope>(
+ _: deno_core::v8::Local<deno_core::v8::Object>,
+ c: *const deno_core::v8::fast_api::FastApiOneByteString,
+) -> () {
+ use deno_core::v8;
+ use deno_core::_ops;
+ let c = ::std::borrow::Cow::Borrowed(unsafe { &*c }.as_str());
+ let result = op_cow_str::call(c);
+ result
+}
diff --git a/ops/optimizer_tests/cow_str.rs b/ops/optimizer_tests/cow_str.rs
new file mode 100644
index 000000000..b7214bdc7
--- /dev/null
+++ b/ops/optimizer_tests/cow_str.rs
@@ -0,0 +1,3 @@
+fn op_cow_str(c: Cow<'_, str>) {
+ // ...
+}
diff --git a/ops/optimizer_tests/op_blob_revoke_object_url.expected b/ops/optimizer_tests/op_blob_revoke_object_url.expected
index 0fae25e86..00a896433 100644
--- a/ops/optimizer_tests/op_blob_revoke_object_url.expected
+++ b/ops/optimizer_tests/op_blob_revoke_object_url.expected
@@ -1 +1,11 @@
-MustBeSingleSegment \ No newline at end of file
+=== Optimizer Dump ===
+returns_result: true
+has_ref_opstate: true
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(Void)
+fast_parameters: [V8Value, SeqOneByteString]
+transforms: {1: Transform { kind: SeqOneByteString(Owned), index: 1 }}
+is_async: false
+fast_compatible: false
diff --git a/ops/optimizer_tests/op_blob_revoke_object_url.out b/ops/optimizer_tests/op_blob_revoke_object_url.out
index 72bc75e05..ac51c0de7 100644
--- a/ops/optimizer_tests/op_blob_revoke_object_url.out
+++ b/ops/optimizer_tests/op_blob_revoke_object_url.out
@@ -27,7 +27,7 @@ impl op_blob_revoke_object_url {
}
#[inline]
#[allow(clippy::too_many_arguments)]
- pub fn call(state: &mut deno_core::OpState, url: String) -> Result<(), AnyError> {
+ pub fn call(state: &mut OpState, url: String) -> Result<(), AnyError> {
let url = Url::parse(&url)?;
let blob_store = state.borrow::<BlobStore>();
blob_store.remove_object_url(&url);
diff --git a/ops/optimizer_tests/op_blob_revoke_object_url.rs b/ops/optimizer_tests/op_blob_revoke_object_url.rs
index 8e139da06..4ef79386e 100644
--- a/ops/optimizer_tests/op_blob_revoke_object_url.rs
+++ b/ops/optimizer_tests/op_blob_revoke_object_url.rs
@@ -1,7 +1,8 @@
pub fn op_blob_revoke_object_url(
- state: &mut deno_core::OpState,
+ state: &mut OpState,
url: String,
) -> Result<(), AnyError> {
+ // TODO(@littledivy): fast compatible https://github.com/denoland/deno/issues/17159
let url = Url::parse(&url)?;
let blob_store = state.borrow::<BlobStore>();
blob_store.remove_object_url(&url);
diff --git a/ops/optimizer_tests/op_print.expected b/ops/optimizer_tests/op_print.expected
new file mode 100644
index 000000000..0390be396
--- /dev/null
+++ b/ops/optimizer_tests/op_print.expected
@@ -0,0 +1,11 @@
+=== Optimizer Dump ===
+returns_result: true
+has_ref_opstate: true
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(Void)
+fast_parameters: [V8Value, SeqOneByteString, Bool]
+transforms: {1: Transform { kind: SeqOneByteString(Ref), index: 1 }}
+is_async: false
+fast_compatible: false
diff --git a/ops/optimizer_tests/op_print.out b/ops/optimizer_tests/op_print.out
new file mode 100644
index 000000000..bafa0b9f8
--- /dev/null
+++ b/ops/optimizer_tests/op_print.out
@@ -0,0 +1,82 @@
+#[allow(non_camel_case_types)]
+///Auto-generated by `deno_ops`, i.e: `#[op]`
+///
+///Use `op_print::decl()` to get an op-declaration
+///you can include in a `deno_core::Extension`.
+pub struct op_print;
+#[doc(hidden)]
+impl op_print {
+ pub fn name() -> &'static str {
+ stringify!(op_print)
+ }
+ 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: None,
+ is_async: false,
+ is_unstable: false,
+ is_v8: false,
+ argc: 2usize,
+ }
+ }
+ #[inline]
+ #[allow(clippy::too_many_arguments)]
+ fn call(state: &mut OpState, msg: &str, is_err: bool) -> Result<(), AnyError> {}
+ 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 = match deno_core::v8::Local::<
+ deno_core::v8::String,
+ >::try_from(args.get(0usize as i32)) {
+ Ok(v8_string) => deno_core::serde_v8::to_utf8(v8_string, scope),
+ Err(_) => {
+ return deno_core::_ops::throw_type_error(
+ scope,
+ format!("Expected string at position {}", 0usize),
+ );
+ }
+ };
+ let arg_0 = arg_0.as_ref();
+ let arg_1 = args.get(1usize as i32);
+ let arg_1 = match deno_core::serde_v8::from_v8(scope, arg_1) {
+ 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 result = Self::call(
+ &mut std::cell::RefCell::borrow_mut(&ctx.state),
+ arg_0,
+ arg_1,
+ );
+ let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
+ op_state.tracker.track_sync(ctx.id);
+ match result {
+ Ok(result) => {}
+ Err(err) => {
+ let exception = deno_core::error::to_v8_error(
+ scope,
+ op_state.get_error_class_fn,
+ &err,
+ );
+ scope.throw_exception(exception);
+ }
+ };
+ }
+}
diff --git a/ops/optimizer_tests/op_print.rs b/ops/optimizer_tests/op_print.rs
new file mode 100644
index 000000000..776ee8418
--- /dev/null
+++ b/ops/optimizer_tests/op_print.rs
@@ -0,0 +1,7 @@
+fn op_print(
+ state: &mut OpState,
+ msg: &str,
+ is_err: bool,
+) -> Result<(), AnyError> {
+ // TODO(@littledivy): fast compatible https://github.com/denoland/deno/issues/17159
+}
diff --git a/ops/optimizer_tests/owned_string.expected b/ops/optimizer_tests/owned_string.expected
new file mode 100644
index 000000000..a15275412
--- /dev/null
+++ b/ops/optimizer_tests/owned_string.expected
@@ -0,0 +1,11 @@
+=== Optimizer Dump ===
+returns_result: false
+has_ref_opstate: false
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(U32)
+fast_parameters: [V8Value, SeqOneByteString]
+transforms: {0: Transform { kind: SeqOneByteString(Owned), index: 0 }}
+is_async: false
+fast_compatible: true
diff --git a/ops/optimizer_tests/owned_string.out b/ops/optimizer_tests/owned_string.out
new file mode 100644
index 000000000..4892221f8
--- /dev/null
+++ b/ops/optimizer_tests/owned_string.out
@@ -0,0 +1,99 @@
+#[allow(non_camel_case_types)]
+///Auto-generated by `deno_ops`, i.e: `#[op]`
+///
+///Use `op_string_length::decl()` to get an op-declaration
+///you can include in a `deno_core::Extension`.
+pub struct op_string_length;
+#[doc(hidden)]
+impl op_string_length {
+ pub fn name() -> &'static str {
+ stringify!(op_string_length)
+ }
+ 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_string_length_fast {
+ _phantom: ::std::marker::PhantomData,
+ }),
+ ),
+ is_async: false,
+ is_unstable: false,
+ is_v8: false,
+ argc: 1usize,
+ }
+ }
+ #[inline]
+ #[allow(clippy::too_many_arguments)]
+ fn call(string: String) -> u32 {
+ string.len() as u32
+ }
+ 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 = match deno_core::v8::Local::<
+ deno_core::v8::String,
+ >::try_from(args.get(0usize as i32)) {
+ Ok(v8_string) => deno_core::serde_v8::to_utf8(v8_string, scope),
+ Err(_) => {
+ return deno_core::_ops::throw_type_error(
+ scope,
+ format!("Expected string at position {}", 0usize),
+ );
+ }
+ };
+ let result = Self::call(arg_0);
+ let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
+ op_state.tracker.track_sync(ctx.id);
+ match deno_core::serde_v8::to_v8(scope, result) {
+ Ok(ret) => rv.set(ret),
+ Err(err) => {
+ deno_core::_ops::throw_type_error(
+ scope,
+ format!(
+ "Error serializing return: {}",
+ deno_core::anyhow::Error::from(err)
+ ),
+ )
+ }
+ };
+ }
+}
+struct op_string_length_fast {
+ _phantom: ::std::marker::PhantomData<()>,
+}
+impl<'scope> deno_core::v8::fast_api::FastFunction for op_string_length_fast {
+ fn function(&self) -> *const ::std::ffi::c_void {
+ op_string_length_fast_fn as *const ::std::ffi::c_void
+ }
+ fn args(&self) -> &'static [deno_core::v8::fast_api::Type] {
+ use deno_core::v8::fast_api::Type::*;
+ use deno_core::v8::fast_api::CType;
+ &[V8Value, SeqOneByteString]
+ }
+ fn return_type(&self) -> deno_core::v8::fast_api::CType {
+ deno_core::v8::fast_api::CType::Uint32
+ }
+}
+fn op_string_length_fast_fn<'scope>(
+ _: deno_core::v8::Local<deno_core::v8::Object>,
+ string: *const deno_core::v8::fast_api::FastApiOneByteString,
+) -> u32 {
+ use deno_core::v8;
+ use deno_core::_ops;
+ let string = unsafe { &*string }.as_str().to_owned();
+ let result = op_string_length::call(string);
+ result
+}
diff --git a/ops/optimizer_tests/owned_string.rs b/ops/optimizer_tests/owned_string.rs
new file mode 100644
index 000000000..102cf00fb
--- /dev/null
+++ b/ops/optimizer_tests/owned_string.rs
@@ -0,0 +1,3 @@
+fn op_string_length(string: String) -> u32 {
+ string.len() as u32
+}
diff --git a/ops/optimizer_tests/strings.expected b/ops/optimizer_tests/strings.expected
new file mode 100644
index 000000000..ead741054
--- /dev/null
+++ b/ops/optimizer_tests/strings.expected
@@ -0,0 +1,11 @@
+=== Optimizer Dump ===
+returns_result: false
+has_ref_opstate: false
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(U32)
+fast_parameters: [V8Value, SeqOneByteString]
+transforms: {0: Transform { kind: SeqOneByteString(Ref), index: 0 }}
+is_async: false
+fast_compatible: true
diff --git a/ops/optimizer_tests/strings.out b/ops/optimizer_tests/strings.out
new file mode 100644
index 000000000..41f09c4f8
--- /dev/null
+++ b/ops/optimizer_tests/strings.out
@@ -0,0 +1,100 @@
+#[allow(non_camel_case_types)]
+///Auto-generated by `deno_ops`, i.e: `#[op]`
+///
+///Use `op_string_length::decl()` to get an op-declaration
+///you can include in a `deno_core::Extension`.
+pub struct op_string_length;
+#[doc(hidden)]
+impl op_string_length {
+ pub fn name() -> &'static str {
+ stringify!(op_string_length)
+ }
+ 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_string_length_fast {
+ _phantom: ::std::marker::PhantomData,
+ }),
+ ),
+ is_async: false,
+ is_unstable: false,
+ is_v8: false,
+ argc: 1usize,
+ }
+ }
+ #[inline]
+ #[allow(clippy::too_many_arguments)]
+ fn call(string: &str) -> u32 {
+ string.len() as u32
+ }
+ 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 = match deno_core::v8::Local::<
+ deno_core::v8::String,
+ >::try_from(args.get(0usize as i32)) {
+ Ok(v8_string) => deno_core::serde_v8::to_utf8(v8_string, scope),
+ Err(_) => {
+ return deno_core::_ops::throw_type_error(
+ scope,
+ format!("Expected string at position {}", 0usize),
+ );
+ }
+ };
+ let arg_0 = arg_0.as_ref();
+ let result = Self::call(arg_0);
+ let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
+ op_state.tracker.track_sync(ctx.id);
+ match deno_core::serde_v8::to_v8(scope, result) {
+ Ok(ret) => rv.set(ret),
+ Err(err) => {
+ deno_core::_ops::throw_type_error(
+ scope,
+ format!(
+ "Error serializing return: {}",
+ deno_core::anyhow::Error::from(err)
+ ),
+ )
+ }
+ };
+ }
+}
+struct op_string_length_fast {
+ _phantom: ::std::marker::PhantomData<()>,
+}
+impl<'scope> deno_core::v8::fast_api::FastFunction for op_string_length_fast {
+ fn function(&self) -> *const ::std::ffi::c_void {
+ op_string_length_fast_fn as *const ::std::ffi::c_void
+ }
+ fn args(&self) -> &'static [deno_core::v8::fast_api::Type] {
+ use deno_core::v8::fast_api::Type::*;
+ use deno_core::v8::fast_api::CType;
+ &[V8Value, SeqOneByteString]
+ }
+ fn return_type(&self) -> deno_core::v8::fast_api::CType {
+ deno_core::v8::fast_api::CType::Uint32
+ }
+}
+fn op_string_length_fast_fn<'scope>(
+ _: deno_core::v8::Local<deno_core::v8::Object>,
+ string: *const deno_core::v8::fast_api::FastApiOneByteString,
+) -> u32 {
+ use deno_core::v8;
+ use deno_core::_ops;
+ let string = unsafe { &*string }.as_str();
+ let result = op_string_length::call(string);
+ result
+}
diff --git a/ops/optimizer_tests/strings.rs b/ops/optimizer_tests/strings.rs
new file mode 100644
index 000000000..860f1e8ec
--- /dev/null
+++ b/ops/optimizer_tests/strings.rs
@@ -0,0 +1,3 @@
+fn op_string_length(string: &str) -> u32 {
+ string.len() as u32
+}
diff --git a/ops/optimizer_tests/strings_result.expected b/ops/optimizer_tests/strings_result.expected
new file mode 100644
index 000000000..68c753cab
--- /dev/null
+++ b/ops/optimizer_tests/strings_result.expected
@@ -0,0 +1,11 @@
+=== Optimizer Dump ===
+returns_result: true
+has_ref_opstate: false
+has_rc_opstate: false
+has_fast_callback_option: false
+needs_fast_callback_option: false
+fast_result: Some(U32)
+fast_parameters: [V8Value, SeqOneByteString]
+transforms: {0: Transform { kind: SeqOneByteString(Ref), index: 0 }}
+is_async: false
+fast_compatible: false
diff --git a/ops/optimizer_tests/strings_result.out b/ops/optimizer_tests/strings_result.out
new file mode 100644
index 000000000..8446a603b
--- /dev/null
+++ b/ops/optimizer_tests/strings_result.out
@@ -0,0 +1,71 @@
+#[allow(non_camel_case_types)]
+///Auto-generated by `deno_ops`, i.e: `#[op]`
+///
+///Use `op_string_length::decl()` to get an op-declaration
+///you can include in a `deno_core::Extension`.
+pub struct op_string_length;
+#[doc(hidden)]
+impl op_string_length {
+ pub fn name() -> &'static str {
+ stringify!(op_string_length)
+ }
+ 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: None,
+ is_async: false,
+ is_unstable: false,
+ is_v8: false,
+ argc: 1usize,
+ }
+ }
+ #[inline]
+ #[allow(clippy::too_many_arguments)]
+ fn call(string: &str) -> Result<u32, AnyError> {
+ Ok(string.len() as u32)
+ }
+ 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 = match deno_core::v8::Local::<
+ deno_core::v8::String,
+ >::try_from(args.get(0usize as i32)) {
+ Ok(v8_string) => deno_core::serde_v8::to_utf8(v8_string, scope),
+ Err(_) => {
+ return deno_core::_ops::throw_type_error(
+ scope,
+ format!("Expected string at position {}", 0usize),
+ );
+ }
+ };
+ let arg_0 = arg_0.as_ref();
+ let result = Self::call(arg_0);
+ let op_state = ::std::cell::RefCell::borrow(&*ctx.state);
+ op_state.tracker.track_sync(ctx.id);
+ match result {
+ Ok(result) => {
+ rv.set_uint32(result as u32);
+ }
+ Err(err) => {
+ let exception = deno_core::error::to_v8_error(
+ scope,
+ op_state.get_error_class_fn,
+ &err,
+ );
+ scope.throw_exception(exception);
+ }
+ };
+ }
+}
diff --git a/ops/optimizer_tests/strings_result.rs b/ops/optimizer_tests/strings_result.rs
new file mode 100644
index 000000000..f89efaab1
--- /dev/null
+++ b/ops/optimizer_tests/strings_result.rs
@@ -0,0 +1,4 @@
+// https://github.com/denoland/deno/issues/16979
+fn op_string_length(string: &str) -> Result<u32, AnyError> {
+ Ok(string.len() as u32)
+}