2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-05-03 13:45:57 -04:00
|
|
|
use crate::error::format_file_name;
|
2021-04-25 13:23:22 -04:00
|
|
|
use crate::error::type_error;
|
2022-10-09 10:49:25 -04:00
|
|
|
use crate::io::BufMutView;
|
|
|
|
use crate::io::BufView;
|
2023-03-17 14:22:15 -04:00
|
|
|
use crate::ops_builtin_v8;
|
2021-10-10 11:20:30 -04:00
|
|
|
use crate::ops_metrics::OpMetrics;
|
2021-04-25 13:23:22 -04:00
|
|
|
use crate::resources::ResourceId;
|
2023-06-22 17:37:56 -04:00
|
|
|
use crate::JsBuffer;
|
2021-04-25 13:23:22 -04:00
|
|
|
use crate::OpState;
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
use crate::Resource;
|
2021-11-16 09:02:28 -05:00
|
|
|
use anyhow::Error;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_ops::op;
|
2023-06-24 07:54:10 -04:00
|
|
|
use deno_ops::op2;
|
2023-06-22 17:37:56 -04:00
|
|
|
use serde_v8::ToJsBuffer;
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
use std::cell::RefCell;
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::io::stderr;
|
|
|
|
use std::io::stdout;
|
|
|
|
use std::io::Write;
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
use std::rc::Rc;
|
2021-04-25 13:23:22 -04:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
crate::extension!(
|
|
|
|
core,
|
|
|
|
ops = [
|
|
|
|
op_close,
|
|
|
|
op_try_close,
|
|
|
|
op_print,
|
|
|
|
op_resources,
|
|
|
|
op_wasm_streaming_feed,
|
|
|
|
op_wasm_streaming_set_url,
|
|
|
|
op_void_sync,
|
2023-04-30 04:50:24 -04:00
|
|
|
op_error_async,
|
|
|
|
op_error_async_deferred,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_void_async,
|
2023-04-16 15:47:06 -04:00
|
|
|
op_void_async_deferred,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_add,
|
2023-04-30 04:50:24 -04:00
|
|
|
op_add_async,
|
2023-03-17 14:22:15 -04:00
|
|
|
// TODO(@AaronO): track IO metrics for builtin streams
|
|
|
|
op_read,
|
|
|
|
op_read_all,
|
|
|
|
op_write,
|
2023-04-05 18:14:16 -04:00
|
|
|
op_read_sync,
|
|
|
|
op_write_sync,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_write_all,
|
|
|
|
op_shutdown,
|
|
|
|
op_metrics,
|
|
|
|
op_format_file_name,
|
|
|
|
op_is_proxy,
|
|
|
|
op_str_byte_length,
|
|
|
|
ops_builtin_v8::op_ref_op,
|
|
|
|
ops_builtin_v8::op_unref_op,
|
|
|
|
ops_builtin_v8::op_set_promise_reject_callback,
|
|
|
|
ops_builtin_v8::op_run_microtasks,
|
|
|
|
ops_builtin_v8::op_has_tick_scheduled,
|
|
|
|
ops_builtin_v8::op_set_has_tick_scheduled,
|
|
|
|
ops_builtin_v8::op_eval_context,
|
|
|
|
ops_builtin_v8::op_queue_microtask,
|
|
|
|
ops_builtin_v8::op_create_host_object,
|
|
|
|
ops_builtin_v8::op_encode,
|
|
|
|
ops_builtin_v8::op_decode,
|
|
|
|
ops_builtin_v8::op_serialize,
|
|
|
|
ops_builtin_v8::op_deserialize,
|
|
|
|
ops_builtin_v8::op_set_promise_hooks,
|
|
|
|
ops_builtin_v8::op_get_promise_details,
|
|
|
|
ops_builtin_v8::op_get_proxy_details,
|
2023-04-30 03:24:13 -04:00
|
|
|
ops_builtin_v8::op_get_non_index_property_names,
|
|
|
|
ops_builtin_v8::op_get_constructor_name,
|
2023-03-17 14:22:15 -04:00
|
|
|
ops_builtin_v8::op_memory_usage,
|
|
|
|
ops_builtin_v8::op_set_wasm_streaming_callback,
|
|
|
|
ops_builtin_v8::op_abort_wasm_streaming,
|
|
|
|
ops_builtin_v8::op_destructure_error,
|
|
|
|
ops_builtin_v8::op_dispatch_exception,
|
|
|
|
ops_builtin_v8::op_op_names,
|
|
|
|
ops_builtin_v8::op_apply_source_map,
|
|
|
|
ops_builtin_v8::op_set_format_exception_callback,
|
|
|
|
ops_builtin_v8::op_event_loop_has_more_work,
|
|
|
|
ops_builtin_v8::op_store_pending_promise_rejection,
|
|
|
|
ops_builtin_v8::op_remove_pending_promise_rejection,
|
|
|
|
ops_builtin_v8::op_has_pending_promise_rejection,
|
|
|
|
ops_builtin_v8::op_arraybuffer_was_detached,
|
|
|
|
],
|
|
|
|
js = ["00_primordials.js", "01_core.js", "02_error.js"],
|
2023-04-13 20:41:32 -04:00
|
|
|
customizer = |ext: &mut crate::ExtensionBuilder| {
|
|
|
|
ext.deno_core();
|
|
|
|
}
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2023-03-09 19:22:27 -05:00
|
|
|
|
2021-04-25 13:23:22 -04:00
|
|
|
/// Return map of resources with id as key
|
|
|
|
/// and string representation as value.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-12 13:13:25 -04:00
|
|
|
pub fn op_resources(state: &mut OpState) -> Vec<(ResourceId, String)> {
|
|
|
|
state
|
2021-04-25 13:23:22 -04:00
|
|
|
.resource_table
|
|
|
|
.names()
|
|
|
|
.map(|(rid, name)| (rid, name.to_string()))
|
2022-05-12 13:13:25 -04:00
|
|
|
.collect()
|
2021-04-25 13:23:22 -04:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:54:10 -04:00
|
|
|
#[op2(core, fast)]
|
2022-08-21 08:07:53 -04:00
|
|
|
fn op_add(a: i32, b: i32) -> i32 {
|
|
|
|
a + b
|
|
|
|
}
|
|
|
|
|
2023-04-30 04:50:24 -04:00
|
|
|
#[op]
|
|
|
|
pub async fn op_add_async(a: i32, b: i32) -> i32 {
|
|
|
|
a + b
|
|
|
|
}
|
|
|
|
|
2022-08-21 08:07:53 -04:00
|
|
|
#[op(fast)]
|
2022-05-12 13:13:25 -04:00
|
|
|
pub fn op_void_sync() {}
|
2022-03-14 13:44:15 -04:00
|
|
|
|
|
|
|
#[op]
|
2022-05-12 13:13:25 -04:00
|
|
|
pub async fn op_void_async() {}
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2023-04-30 04:50:24 -04:00
|
|
|
#[op]
|
|
|
|
pub async fn op_error_async() -> Result<(), Error> {
|
|
|
|
Err(Error::msg("error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op(deferred)]
|
|
|
|
pub async fn op_error_async_deferred() -> Result<(), Error> {
|
|
|
|
Err(Error::msg("error"))
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:47:06 -04:00
|
|
|
#[op(deferred)]
|
|
|
|
pub async fn op_void_async_deferred() {}
|
|
|
|
|
2021-04-25 13:23:22 -04:00
|
|
|
/// Remove a resource from the resource table.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-04-25 13:23:22 -04:00
|
|
|
pub fn op_close(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: Option<ResourceId>,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<(), Error> {
|
2021-09-10 18:47:09 -04:00
|
|
|
// TODO(@AaronO): drop Option after improving type-strictness balance in
|
|
|
|
// serde_v8
|
2021-04-25 13:23:22 -04:00
|
|
|
let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?;
|
2021-08-15 07:29:19 -04:00
|
|
|
state.resource_table.close(rid)?;
|
2021-09-10 18:47:09 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-25 13:23:22 -04:00
|
|
|
|
2021-09-10 18:47:09 -04:00
|
|
|
/// Try to remove a resource from the resource table. If there is no resource
|
|
|
|
/// with the specified `rid`, this is a no-op.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-09-10 18:47:09 -04:00
|
|
|
pub fn op_try_close(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: Option<ResourceId>,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<(), Error> {
|
2021-09-10 18:47:09 -04:00
|
|
|
// TODO(@AaronO): drop Option after improving type-strictness balance in
|
|
|
|
// serde_v8.
|
|
|
|
let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?;
|
|
|
|
let _ = state.resource_table.close(rid);
|
2021-04-25 13:23:22 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-02 19:30:03 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-12 13:13:25 -04:00
|
|
|
pub fn op_metrics(state: &mut OpState) -> (OpMetrics, Vec<OpMetrics>) {
|
2022-03-14 13:44:15 -04:00
|
|
|
let aggregate = state.tracker.aggregate();
|
|
|
|
let per_op = state.tracker.per_op();
|
2022-05-12 13:13:25 -04:00
|
|
|
(aggregate, per_op)
|
2022-03-14 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 19:30:03 -04:00
|
|
|
/// Builtin utility to print to stdout/stderr
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2023-03-03 08:34:10 -05:00
|
|
|
pub fn op_print(msg: &str, is_err: bool) -> Result<(), Error> {
|
2021-05-02 19:30:03 -04:00
|
|
|
if is_err {
|
2021-06-21 22:39:59 -04:00
|
|
|
stderr().write_all(msg.as_bytes())?;
|
2021-05-03 08:51:32 -04:00
|
|
|
stderr().flush().unwrap();
|
2021-05-02 19:30:03 -04:00
|
|
|
} else {
|
2021-06-21 22:39:59 -04:00
|
|
|
stdout().write_all(msg.as_bytes())?;
|
2021-05-02 19:30:03 -04:00
|
|
|
stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
|
2021-10-27 17:26:15 -04:00
|
|
|
pub struct WasmStreamingResource(pub(crate) RefCell<v8::WasmStreaming>);
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
|
|
|
|
impl Resource for WasmStreamingResource {
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
// At this point there are no clones of Rc<WasmStreamingResource> on the
|
|
|
|
// resource table, and no one should own a reference outside of the stack.
|
|
|
|
// Therefore, we can be sure `self` is the only reference.
|
|
|
|
if let Ok(wsr) = Rc::try_unwrap(self) {
|
|
|
|
wsr.0.into_inner().finish();
|
|
|
|
} else {
|
|
|
|
panic!("Couldn't consume WasmStreamingResource.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Feed bytes to WasmStreamingResource.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
pub fn op_wasm_streaming_feed(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
2022-09-07 06:51:47 -04:00
|
|
|
bytes: &[u8],
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<(), Error> {
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
let wasm_streaming =
|
|
|
|
state.resource_table.get::<WasmStreamingResource>(rid)?;
|
|
|
|
|
2022-09-07 06:51:47 -04:00
|
|
|
wasm_streaming.0.borrow_mut().on_bytes_received(bytes);
|
refactor(core): Turn the `wasm_streaming_feed` binding into ops (#11985)
Async WebAssembly compilation was implemented by adding two
bindings: `set_wasm_streaming_callback`, which registered a callback to
be called whenever a streaming wasm compilation was started, and
`wasm_streaming_feed`, which let the JS callback modify the state of the
v8 wasm compiler.
`set_wasm_streaming_callback` cannot currently be implemented as
anything other than a binding, but `wasm_streaming_feed` does not really
need to use anything specific to bindings, and could indeed be
implemented as one or more ops. This PR does that, resulting in a
simplification of the relevant code.
There are three operations on the state of the v8 wasm compiler that
`wasm_streaming_feed` allowed: feeding new bytes into the compiler,
letting it know that there are no more bytes coming from the network,
and aborting the compilation. This PR provides `op_wasm_streaming_feed`
to feed new bytes into the compiler, and `op_wasm_streaming_abort` to
abort the compilation. It doesn't provide an op to let v8 know that the
response is finished, but closing the resource with `Deno.core.close()`
will achieve that.
2021-09-13 08:27:54 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-10-10 10:03:23 -04:00
|
|
|
pub fn op_wasm_streaming_set_url(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
2023-03-03 08:34:10 -05:00
|
|
|
url: &str,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<(), Error> {
|
2021-10-10 10:03:23 -04:00
|
|
|
let wasm_streaming =
|
|
|
|
state.resource_table.get::<WasmStreamingResource>(rid)?;
|
|
|
|
|
2023-03-03 08:34:10 -05:00
|
|
|
wasm_streaming.0.borrow_mut().set_url(url);
|
2021-10-10 10:03:23 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-10 11:20:30 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-11-09 13:26:17 -05:00
|
|
|
async fn op_read(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
2023-06-22 17:37:56 -04:00
|
|
|
buf: JsBuffer,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<u32, Error> {
|
2021-11-09 13:26:17 -05:00
|
|
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
2022-10-09 10:49:25 -04:00
|
|
|
let view = BufMutView::from(buf);
|
|
|
|
resource.read_byob(view).await.map(|(n, _)| n as u32)
|
2021-11-09 13:26:17 -05:00
|
|
|
}
|
|
|
|
|
2022-10-04 09:48:50 -04:00
|
|
|
#[op]
|
|
|
|
async fn op_read_all(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, Error> {
|
2022-10-04 09:48:50 -04:00
|
|
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
|
|
|
|
2022-10-09 10:49:25 -04:00
|
|
|
// The number of bytes we attempt to grow the buffer by each time it fills
|
|
|
|
// up and we have more data to read. We start at 64 KB. The grow_len is
|
|
|
|
// doubled if the nread returned from a single read is equal or greater than
|
|
|
|
// the grow_len. This allows us to reduce allocations for resources that can
|
|
|
|
// read large chunks of data at a time.
|
|
|
|
let mut grow_len: usize = 64 * 1024;
|
|
|
|
|
|
|
|
let (min, maybe_max) = resource.size_hint();
|
|
|
|
// Try to determine an optimial starting buffer size for this resource based
|
|
|
|
// on the size hint.
|
|
|
|
let initial_size = match (min, maybe_max) {
|
|
|
|
(min, Some(max)) if min == max => min as usize,
|
|
|
|
(_min, Some(max)) if (max as usize) < grow_len => max as usize,
|
|
|
|
(min, _) if (min as usize) < grow_len => grow_len,
|
|
|
|
(min, _) => min as usize,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut buf = BufMutView::new(initial_size);
|
2022-10-04 09:48:50 -04:00
|
|
|
loop {
|
2022-10-09 10:49:25 -04:00
|
|
|
// if the buffer does not have much remaining space, we may have to grow it.
|
|
|
|
if buf.len() < grow_len {
|
|
|
|
let vec = buf.get_mut_vec();
|
|
|
|
match maybe_max {
|
|
|
|
Some(max) if vec.len() >= max as usize => {
|
|
|
|
// no need to resize the vec, because the vec is already large enough
|
2022-11-24 09:22:39 -05:00
|
|
|
// to accommodate the maximum size of the read data.
|
2022-10-09 10:49:25 -04:00
|
|
|
}
|
|
|
|
Some(max) if (max as usize) < vec.len() + grow_len => {
|
|
|
|
// grow the vec to the maximum size of the read data
|
|
|
|
vec.resize(max as usize, 0);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// grow the vec by grow_len
|
|
|
|
vec.resize(vec.len() + grow_len, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let (n, new_buf) = resource.clone().read_byob(buf).await?;
|
|
|
|
buf = new_buf;
|
|
|
|
buf.advance_cursor(n);
|
|
|
|
if n == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if n >= grow_len {
|
|
|
|
// we managed to read more or equal data than fits in a single grow_len in
|
|
|
|
// a single go, so let's attempt to read even more next time. this reduces
|
|
|
|
// allocations for resources that can read large chunks of data at a time.
|
|
|
|
grow_len *= 2;
|
2022-10-04 09:48:50 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-09 10:49:25 -04:00
|
|
|
|
|
|
|
let nread = buf.reset_cursor();
|
|
|
|
let mut vec = buf.unwrap_vec();
|
|
|
|
// If the buffer is larger than the amount of data read, shrink it to the
|
|
|
|
// amount of data read.
|
|
|
|
if nread < vec.len() {
|
|
|
|
vec.truncate(nread);
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
Ok(ToJsBuffer::from(vec))
|
2022-10-04 09:48:50 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-11-09 13:26:17 -05:00
|
|
|
async fn op_write(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
2023-06-22 17:37:56 -04:00
|
|
|
buf: JsBuffer,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<u32, Error> {
|
2021-11-09 13:26:17 -05:00
|
|
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
2022-10-09 10:49:25 -04:00
|
|
|
let view = BufView::from(buf);
|
|
|
|
let resp = resource.write(view).await?;
|
|
|
|
Ok(resp.nwritten() as u32)
|
2021-11-09 13:26:17 -05:00
|
|
|
}
|
|
|
|
|
2023-04-05 18:14:16 -04:00
|
|
|
#[op(fast)]
|
|
|
|
fn op_read_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
data: &mut [u8],
|
|
|
|
) -> Result<u32, Error> {
|
|
|
|
let resource = state.resource_table.get_any(rid)?;
|
|
|
|
resource.read_byob_sync(data).map(|n| n as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_write_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<u32, Error> {
|
|
|
|
let resource = state.resource_table.get_any(rid)?;
|
|
|
|
let nwritten = resource.write_sync(data)?;
|
|
|
|
Ok(nwritten as u32)
|
|
|
|
}
|
|
|
|
|
2022-10-10 04:28:35 -04:00
|
|
|
#[op]
|
|
|
|
async fn op_write_all(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
2023-06-22 17:37:56 -04:00
|
|
|
buf: JsBuffer,
|
2022-10-10 04:28:35 -04:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
|
|
|
let view = BufView::from(buf);
|
|
|
|
resource.write_all(view).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-11-09 13:26:17 -05:00
|
|
|
async fn op_shutdown(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<(), Error> {
|
2021-11-09 13:26:17 -05:00
|
|
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
|
|
|
resource.shutdown().await
|
|
|
|
}
|
2022-05-03 13:45:57 -04:00
|
|
|
|
|
|
|
#[op]
|
2022-05-12 13:13:25 -04:00
|
|
|
fn op_format_file_name(file_name: String) -> String {
|
|
|
|
format_file_name(&file_name)
|
2022-05-03 13:45:57 -04:00
|
|
|
}
|
2022-06-07 05:25:10 -04:00
|
|
|
|
2022-08-30 05:01:36 -04:00
|
|
|
#[op(fast)]
|
2022-06-07 05:25:10 -04:00
|
|
|
fn op_is_proxy(value: serde_v8::Value) -> bool {
|
|
|
|
value.v8_value.is_proxy()
|
|
|
|
}
|
2022-09-07 06:51:30 -04:00
|
|
|
|
|
|
|
#[op(v8)]
|
|
|
|
fn op_str_byte_length(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
value: serde_v8::Value,
|
|
|
|
) -> u32 {
|
|
|
|
if let Ok(string) = v8::Local::<v8::String>::try_from(value.v8_value) {
|
|
|
|
string.utf8_length(scope) as u32
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|