char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport wasmstreaming extension to typescript067c81e

main
3.2 KiB69 linesraw
1use std::cell::RefCell;
2use std::io;
3use std::rc::Rc;
4
5use avril_core::serde_v8::{JsBuffer, from_v8};
6use avril_core::{Extension, Op, WasmStreamingRegistry, v8};
7
8pub fn init() -> Extension {
9    Extension {
10        name: "ext:wasmstreaming",
11        dependencies: &["ext:http_types", "ext:webstreams"],
12        ops: vec![
13            Op::try_raw("op_wasm_streaming_register", |scope, args| {
14                let callback = v8::Local::<v8::Function>::try_from(args.get(0))
15                    .map_err(|_| io::Error::other("Expected a Wasm streaming callback"))?;
16                let registry = scope
17                    .get_slot::<Rc<RefCell<WasmStreamingRegistry>>>()
18                    .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?;
19                registry
20                    .borrow_mut()
21                    .register(v8::Global::new(scope, callback))?;
22                Ok::<_, io::Error>(v8::undefined(scope).into())
23            }),
24            Op::try_raw("op_wasm_streaming_write", |scope, args| {
25                let id = from_v8(scope, args.get(0)).map_err(io::Error::other)?;
26                let bytes: JsBuffer = from_v8(scope, args.get(1)).map_err(io::Error::other)?;
27                let registry = scope
28                    .get_slot::<Rc<RefCell<WasmStreamingRegistry>>>()
29                    .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?;
30                registry
31                    .borrow_mut()
32                    .stream_mut(id)?
33                    .on_bytes_received(bytes.as_ref());
34                Ok::<_, io::Error>(v8::undefined(scope).into())
35            }),
36            Op::with_scope(
37                "op_wasm_streaming_url",
38                |scope, (id, url): (u64, String)| {
39                    let registry = scope
40                        .get_slot::<Rc<RefCell<WasmStreamingRegistry>>>()
41                        .ok_or_else(|| {
42                            io::Error::other("Wasm streaming registry is unavailable")
43                        })?;
44                    registry.borrow_mut().stream_mut(id)?.set_url(&url);
45                    Ok::<_, io::Error>(())
46                },
47            ),
48            Op::with_scope("op_wasm_streaming_finish", |scope, id: u64| {
49                let registry = scope
50                    .get_slot::<Rc<RefCell<WasmStreamingRegistry>>>()
51                    .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?;
52                registry.borrow_mut().finish(id)
53            }),
54            Op::try_raw("op_wasm_streaming_abort", |scope, args| {
55                let id = from_v8(scope, args.get(0)).map_err(io::Error::other)?;
56                let registry = scope
57                    .get_slot::<Rc<RefCell<WasmStreamingRegistry>>>()
58                    .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?;
59                registry.borrow_mut().abort(id, args.get(1))?;
60                Ok::<_, io::Error>(v8::undefined(scope).into())
61            }),
62        ],
63        modules: &[(
64            "ext:wasmstreaming/main.js",
65            include_str!(concat!(env!("OUT_DIR"), "/main.js")),
66        )],
67        entry_point: Some("ext:wasmstreaming/main.js"),
68    }
69}