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;
|
2021-04-30 21:08:29 -04:00
|
|
|
use crate::include_js_files;
|
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;
|
2021-04-30 21:08:29 -04:00
|
|
|
use crate::Extension;
|
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;
|
|
|
|
use crate::ZeroCopyBuf;
|
2021-11-16 09:02:28 -05:00
|
|
|
use anyhow::Error;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_ops::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
|
|
|
use std::cell::RefCell;
|
2021-05-03 08:51:32 -04:00
|
|
|
use std::io::{stderr, stdout, 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
|
|
|
|
2021-04-30 21:08:29 -04:00
|
|
|
pub(crate) fn init_builtins() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.js(include_js_files!(
|
|
|
|
prefix "deno:core",
|
2021-07-02 06:18:30 -04:00
|
|
|
"00_primordials.js",
|
|
|
|
"01_core.js",
|
|
|
|
"02_error.js",
|
2021-04-30 21:08:29 -04:00
|
|
|
))
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_close::decl(),
|
|
|
|
op_try_close::decl(),
|
|
|
|
op_print::decl(),
|
|
|
|
op_resources::decl(),
|
|
|
|
op_wasm_streaming_feed::decl(),
|
|
|
|
op_wasm_streaming_set_url::decl(),
|
|
|
|
op_void_sync::decl(),
|
|
|
|
op_void_async::decl(),
|
|
|
|
// // TODO(@AaronO): track IO metrics for builtin streams
|
|
|
|
op_read::decl(),
|
|
|
|
op_write::decl(),
|
|
|
|
op_shutdown::decl(),
|
|
|
|
op_metrics::decl(),
|
2022-05-03 13:45:57 -04:00
|
|
|
op_format_file_name::decl(),
|
2021-04-30 21:08:29 -04:00
|
|
|
])
|
|
|
|
.build()
|
|
|
|
}
|
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]
|
2021-04-25 13:23:22 -04:00
|
|
|
pub fn op_resources(
|
|
|
|
state: &mut OpState,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Result<Vec<(ResourceId, String)>, Error> {
|
2021-04-25 13:23:22 -04:00
|
|
|
let serialized_resources = state
|
|
|
|
.resource_table
|
|
|
|
.names()
|
|
|
|
.map(|(rid, name)| (rid, name.to_string()))
|
|
|
|
.collect();
|
|
|
|
Ok(serialized_resources)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-15 19:33:46 -04:00
|
|
|
pub fn op_void_sync() -> Result<(), Error> {
|
2022-03-14 13:44:15 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2022-03-20 09:54:38 -04:00
|
|
|
pub async fn op_void_async() -> Result<(), Error> {
|
2022-03-14 13:44:15 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
pub fn op_metrics(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<(OpMetrics, Vec<OpMetrics>), Error> {
|
|
|
|
let aggregate = state.tracker.aggregate();
|
|
|
|
let per_op = state.tracker.per_op();
|
|
|
|
Ok((aggregate, per_op))
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:30:03 -04:00
|
|
|
/// Builtin utility to print to stdout/stderr
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-15 19:33:46 -04:00
|
|
|
pub fn op_print(msg: String, 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,
|
|
|
|
bytes: ZeroCopyBuf,
|
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)?;
|
|
|
|
|
|
|
|
wasm_streaming.0.borrow_mut().on_bytes_received(&bytes);
|
|
|
|
|
|
|
|
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,
|
|
|
|
url: String,
|
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)?;
|
|
|
|
|
|
|
|
wasm_streaming.0.borrow_mut().set_url(&url);
|
|
|
|
|
|
|
|
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,
|
|
|
|
buf: ZeroCopyBuf,
|
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)?;
|
|
|
|
resource.read(buf).await.map(|n| n as u32)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
buf: ZeroCopyBuf,
|
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)?;
|
|
|
|
resource.write(buf).await.map(|n| n as u32)
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
fn op_format_file_name(file_name: String) -> Result<String, Error> {
|
|
|
|
Ok(format_file_name(&file_name))
|
|
|
|
}
|