Repository: denoland/deno_core
Pull request #70 (2023-07-22 02:41:46)

refactor: Migrate some ops to op2 with buffers


Patch:
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs
index a1f4444..baee838 100644
--- a/core/ops_builtin.rs
+++ b/core/ops_builtin.rs
@@ -11,7 +11,6 @@ use crate::Resource;
 use anyhow::Error;
 use deno_ops::op;
 use deno_ops::op2;
-use serde_v8::ToJsBuffer;
 use std::cell::RefCell;
 use std::io::stderr;
 use std::io::stdout;
@@ -113,13 +112,13 @@ pub async fn op_error_async() -> Result<(), Error> {
   Err(Error::msg("error"))
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support
+// TODO(bartlomieju): migration to op2 blocked by deferred support
 #[op(deferred)]
 pub async fn op_error_async_deferred() -> Result<(), Error> {
   Err(Error::msg("error"))
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support
+// TODO(bartlomieju): migration to op2 blocked by deferred support
 #[op(deferred)]
 pub async fn op_void_async_deferred() {}
 
@@ -205,26 +204,23 @@ pub fn op_wasm_streaming_set_url(
   Ok(())
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support and buffer
-// support
-#[op]
+#[op2(async, core)]
 async fn op_read(
   state: Rc<RefCell<OpState>>,
-  rid: ResourceId,
-  buf: JsBuffer,
+  #[smi] rid: ResourceId,
+  #[buffer] buf: JsBuffer,
 ) -> Result<u32, Error> {
   let resource = state.borrow().resource_table.get_any(rid)?;
   let view = BufMutView::from(buf);
   resource.read_byob(view).await.map(|(n, _)| n as u32)
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support and buffer
-// support
-#[op]
+#[op2(async, core)]
+#[buffer]
 async fn op_read_all(
   state: Rc<RefCell<OpState>>,
-  rid: ResourceId,
-) -> Result<ToJsBuffer, Error> {
+  #[smi] rid: ResourceId,
+) -> Result<Vec<u8>, Error> {
   let resource = state.borrow().resource_table.get_any(rid)?;
 
   // The number of bytes we attempt to grow the buffer by each time it fills
@@ -286,16 +282,14 @@ async fn op_read_all(
     vec.truncate(nread);
   }
 
-  Ok(ToJsBuffer::from(vec))
+  Ok(vec)
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support and buffer
-// support
-#[op]
+#[op2(async, core)]
 async fn op_write(
   state: Rc<RefCell<OpState>>,
-  rid: ResourceId,
-  buf: JsBuffer,
+  #[smi] rid: ResourceId,
+  #[buffer] buf: JsBuffer,
 ) -> Result<u32, Error> {
   let resource = state.borrow().resource_table.get_any(rid)?;
   let view = BufView::from(buf);
@@ -324,13 +318,11 @@ fn op_write_sync(
   Ok(nwritten as u32)
 }
 
-// TODO(bartlomieju): migration to op2 blocked by async fn support and buffer
-// support
-#[op]
+#[op2(async, core)]
 async fn op_write_all(
   state: Rc<RefCell<OpState>>,
-  rid: ResourceId,
-  buf: JsBuffer,
+  #[smi] rid: ResourceId,
+  #[buffer] buf: JsBuffer,
 ) -> Result<(), Error> {
   let resource = state.borrow().resource_table.get_any(rid)?;
   let view = BufView::from(buf);
diff --git a/core/ops_builtin_v8.rs b/core/ops_builtin_v8.rs
index d2303c1..5d45787 100644
--- a/core/ops_builtin_v8.rs
+++ b/core/ops_builtin_v8.rs
@@ -7,14 +7,11 @@ use crate::error::JsError;
 use crate::ops_builtin::WasmStreamingResource;
 use crate::resolve_url;
 use crate::runtime::script_origin;
-use crate::serde_v8::from_v8;
 use crate::source_map::apply_source_map;
 use crate::JsBuffer;
 use crate::JsRealm;
 use crate::JsRuntime;
-use crate::ToJsBuffer;
 use anyhow::Error;
-use deno_ops::op;
 use deno_ops::op2;
 use serde::Deserialize;
 use serde::Serialize;
@@ -161,12 +158,11 @@ pub fn op_encode<'a>(
   Ok(u8array)
 }
 
-// TODO(bartlomieju): migration to op2 blocked by buffer support
-#[op(v8)]
-fn op_decode<'a>(
+#[op2(core)]
+pub fn op_decode<'a>(
   scope: &mut v8::HandleScope<'a>,
-  zero_copy: &[u8],
-) -> Result<serde_v8::Value<'a>, Error> {
+  #[buffer] zero_copy: &[u8],
+) -> Result<v8::Local<'a, v8::String>, Error> {
   let buf = &zero_copy;
 
   // Strip BOM
@@ -186,7 +182,7 @@ fn op_decode<'a>(
   // - https://github.com/denoland/deno/issues/6649
   // - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
   match v8::String::new_from_utf8(scope, buf, v8::NewStringType::Normal) {
-    Some(text) => Ok(from_v8(scope, text.into())?),
+    Some(text) => Ok(text),
     None => Err(range_error("string too long")),
   }
 }
@@ -345,25 +341,25 @@ impl<'a> v8::ValueDeserializerImpl for SerializeDeserialize<'a> {
 
 #[derive(Default, Deserialize)]
 #[serde(rename_all = "camelCase")]
-struct SerializeDeserializeOptions<'a> {
+pub struct SerializeDeserializeOptions<'a> {
   host_objects: Option<serde_v8::Value<'a>>,
   transferred_array_buffers: Option<serde_v8::Value<'a>>,
   #[serde(default)]
   for_storage: bool,
 }
 
-// TODO(bartlomieju): migration to op2 blocked by buffer support
-#[op(v8)]
-fn op_serialize(
+#[op2(core)]
+#[buffer]
+pub fn op_serialize(
   scope: &mut v8::HandleScope,
-  value: serde_v8::Value,
-  options: Option<SerializeDeserializeOptions>,
-  error_callback: Option<serde_v8::Value>,
-) -> Result<ToJsBuffer, Error> {
+  value: v8::Local<v8::Value>,
+  #[serde] options: Option<SerializeDeserializeOptions>,
+  error_callback: Option<v8::Local<v8::Value>>,
+) -> Result<Vec<u8>, Error> {
   let options = options.unwrap_or_default();
   let error_callback = match error_callback {
     Some(cb) => Some(
-      v8::Local::<v8::Function>::try_from(cb.v8_value)
+      v8::Local::<v8::Function>::try_from(cb)
         .map_err(|_| type_error("Invalid error callback"))?,
     ),
     None => None,
@@ -427,27 +423,25 @@ fn op_serialize(
   }
 
   let scope = &mut v8::TryCatch::new(scope);
-  let ret =
-    value_serializer.write_value(scope.get_current_context(), value.v8_value);
+  let ret = value_serializer.write_value(scope.get_current_context(), value);
   if scope.has_caught() || scope.has_terminated() {
     scope.rethrow();
     // Dummy value, this result will be discarded because an error was thrown.
-    Ok(ToJsBuffer::empty())
+    Ok(vec![])
   } else if let Some(true) = ret {
     let vector = value_serializer.release();
-    Ok(vector.into())
+    Ok(vector)
   } else {
     Err(type_error("Failed to serialize response"))
   }
 }
 
-// TODO(bartlomieju): migration to op2 blocked by buffer support
-#[op(v8)]
-fn op_deserialize<'a>(
+#[op2(core)]
+pub fn op_deserialize<'a>(
   scope: &mut v8::HandleScope<'a>,
-  zero_copy: JsBuffer,
-  options: Option<SerializeDeserializeOptions>,
-) -> Result<serde_v8::Value<'a>, Error> {
+  #[buffer] zero_copy: JsBuffer,
+  #[serde] options: Option<SerializeDeserializeOptions>,
+) -> Result<v8::Local<'a, v8::Value>, Error> {
   let options = options.unwrap_or_default();
   let host_objects = match options.host_objects {
     Some(value) => Some(
@@ -509,7 +503,7 @@ fn op_deserialize<'a>(
 
   let value = value_deserializer.read_value(scope.get_current_context());
   match value {
-    Some(deserialized) => Ok(deserialized.into()),
+    Some(deserialized) => Ok(deserialized),
     None => Err(range_error("could not deserialize value")),
   }
 }
diff --git a/ops/op2/dispatch_slow.rs b/ops/op2/dispatch_slow.rs
index e50e0b4..c70bcb9 100644
--- a/ops/op2/dispatch_slow.rs
+++ b/ops/op2/dispatch_slow.rs
@@ -736,7 +736,7 @@ fn throw_type_error(
   Ok(gs_quote!(generator_state(deno_core, scope) => {
     #maybe_scope
     let msg = #deno_core::v8::String::new_from_one_byte(&mut #scope, #message.as_bytes(), #deno_core::v8::NewStringType::Normal).unwrap();
-    let exc = #deno_core::v8::Exception::error(&mut #scope, msg);
+    let exc = #deno_core::v8::Exception::type_error(&mut #scope, msg);
     #scope.throw_exception(exc);
     return;
   }))


Test patch:
diff --git a/ops/op2/test_cases/async/async_arg_return.out b/ops/op2/test_cases/async/async_arg_return.out
index d3efb53..56a063b 100644
--- a/ops/op2/test_cases/async/async_arg_return.out
+++ b/ops/op2/test_cases/async/async_arg_return.out
@@ -44,7 +44,7 @@ impl op_async {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/async/async_arg_return_result.out b/ops/op2/test_cases/async/async_arg_return_result.out
index dcdfc32..2bfe2a2 100644
--- a/ops/op2/test_cases/async/async_arg_return_result.out
+++ b/ops/op2/test_cases/async/async_arg_return_result.out
@@ -44,7 +44,7 @@ impl op_async {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/async/async_result_impl.out b/ops/op2/test_cases/async/async_result_impl.out
index 7e7fcee..7021f0f 100644
--- a/ops/op2/test_cases/async/async_result_impl.out
+++ b/ops/op2/test_cases/async/async_result_impl.out
@@ -44,7 +44,7 @@ impl op_async_result_impl {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/async/async_v8_global.out b/ops/op2/test_cases/async/async_v8_global.out
index 4b783de..f694015 100644
--- a/ops/op2/test_cases/async/async_v8_global.out
+++ b/ops/op2/test_cases/async/async_v8_global.out
@@ -46,7 +46,7 @@ impl op_async_v8_global {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/add.out b/ops/op2/test_cases/sync/add.out
index 186fa58..d853684 100644
--- a/ops/op2/test_cases/sync/add.out
+++ b/ops/op2/test_cases/sync/add.out
@@ -59,7 +59,7 @@ impl op_add {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -73,7 +73,7 @@ impl op_add {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/add_options.out b/ops/op2/test_cases/sync/add_options.out
index 72647da..86c4d5e 100644
--- a/ops/op2/test_cases/sync/add_options.out
+++ b/ops/op2/test_cases/sync/add_options.out
@@ -39,7 +39,7 @@ impl op_test_add_option {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -56,7 +56,7 @@ impl op_test_add_option {
                             deno_core::v8::NewStringType::Normal,
                         )
                         .unwrap();
-                    let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                    let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                     scope.throw_exception(exc);
                     return;
                 };
diff --git a/ops/op2/test_cases/sync/nofast.out b/ops/op2/test_cases/sync/nofast.out
index 5a7a554..630e8ec 100644
--- a/ops/op2/test_cases/sync/nofast.out
+++ b/ops/op2/test_cases/sync/nofast.out
@@ -39,7 +39,7 @@ impl op_nofast {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -53,7 +53,7 @@ impl op_nofast {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/smi.out b/ops/op2/test_cases/sync/smi.out
index 0b6d0d8..c0681e7 100644
--- a/ops/op2/test_cases/sync/smi.out
+++ b/ops/op2/test_cases/sync/smi.out
@@ -59,7 +59,7 @@ impl op_add {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -73,7 +73,7 @@ impl op_add {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/v8_global.out b/ops/op2/test_cases/sync/v8_global.out
index 7f0c247..b1331a6 100644
--- a/ops/op2/test_cases/sync/v8_global.out
+++ b/ops/op2/test_cases/sync/v8_global.out
@@ -46,7 +46,7 @@ impl op_global {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/v8_handlescope.out b/ops/op2/test_cases/sync/v8_handlescope.out
index a26aa60..5801d34 100644
--- a/ops/op2/test_cases/sync/v8_handlescope.out
+++ b/ops/op2/test_cases/sync/v8_handlescope.out
@@ -41,7 +41,7 @@ impl op_handlescope {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/v8_lifetime.out b/ops/op2/test_cases/sync/v8_lifetime.out
index e9002d1..d508606 100644
--- a/ops/op2/test_cases/sync/v8_lifetime.out
+++ b/ops/op2/test_cases/sync/v8_lifetime.out
@@ -41,7 +41,7 @@ impl op_v8_lifetime {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/v8_ref_option.out b/ops/op2/test_cases/sync/v8_ref_option.out
index 7649c9e..22e6e79 100644
--- a/ops/op2/test_cases/sync/v8_ref_option.out
+++ b/ops/op2/test_cases/sync/v8_ref_option.out
@@ -81,7 +81,7 @@ impl op_v8_lifetime {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -100,7 +100,7 @@ impl op_v8_lifetime {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
diff --git a/ops/op2/test_cases/sync/v8_string.out b/ops/op2/test_cases/sync/v8_string.out
index 9f4d927..99273fa 100644
--- a/ops/op2/test_cases/sync/v8_string.out
+++ b/ops/op2/test_cases/sync/v8_string.out
@@ -41,7 +41,7 @@ impl op_v8_string {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };
@@ -57,7 +57,7 @@ impl op_v8_string {
                         deno_core::v8::NewStringType::Normal,
                     )
                     .unwrap();
-                let exc = deno_core::v8::Exception::error(&mut scope, msg);
+                let exc = deno_core::v8::Exception::type_error(&mut scope, msg);
                 scope.throw_exception(exc);
                 return;
             };