use std::cell::RefCell; use std::io; use std::rc::Rc; use avril_core::serde_v8::{JsBuffer, from_v8}; use avril_core::{Extension, Op, WasmStreamingRegistry, v8}; pub fn init() -> Extension { Extension { name: "ext:wasmstreaming", dependencies: &["ext:http_types", "ext:webstreams"], ops: vec![ Op::try_raw("op_wasm_streaming_register", |scope, args| { let callback = v8::Local::::try_from(args.get(0)) .map_err(|_| io::Error::other("Expected a Wasm streaming callback"))?; let registry = scope .get_slot::>>() .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?; registry .borrow_mut() .register(v8::Global::new(scope, callback))?; Ok::<_, io::Error>(v8::undefined(scope).into()) }), Op::try_raw("op_wasm_streaming_write", |scope, args| { let id = from_v8(scope, args.get(0)).map_err(io::Error::other)?; let bytes: JsBuffer = from_v8(scope, args.get(1)).map_err(io::Error::other)?; let registry = scope .get_slot::>>() .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?; registry .borrow_mut() .stream_mut(id)? .on_bytes_received(bytes.as_ref()); Ok::<_, io::Error>(v8::undefined(scope).into()) }), Op::with_scope( "op_wasm_streaming_url", |scope, (id, url): (u64, String)| { let registry = scope .get_slot::>>() .ok_or_else(|| { io::Error::other("Wasm streaming registry is unavailable") })?; registry.borrow_mut().stream_mut(id)?.set_url(&url); Ok::<_, io::Error>(()) }, ), Op::with_scope("op_wasm_streaming_finish", |scope, id: u64| { let registry = scope .get_slot::>>() .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?; registry.borrow_mut().finish(id) }), Op::try_raw("op_wasm_streaming_abort", |scope, args| { let id = from_v8(scope, args.get(0)).map_err(io::Error::other)?; let registry = scope .get_slot::>>() .ok_or_else(|| io::Error::other("Wasm streaming registry is unavailable"))?; registry.borrow_mut().abort(id, args.get(1))?; Ok::<_, io::Error>(v8::undefined(scope).into()) }), ], modules: &[( "ext:wasmstreaming/main.js", include_str!(concat!(env!("OUT_DIR"), "/main.js")), )], entry_point: Some("ext:wasmstreaming/main.js"), } }