diff options
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index bea9908ed..cb8b242e9 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -3052,6 +3052,82 @@ assertEquals(1, notify_return_value); } #[test] + fn test_op_detached_buffer() { + use serde_v8::DetachedBuffer; + + #[op] + fn op_sum_take(b: DetachedBuffer) -> Result<u64, anyhow::Error> { + Ok(b.as_ref().iter().clone().map(|x| *x as u64).sum()) + } + + #[op] + fn op_boomerang( + b: DetachedBuffer, + ) -> Result<DetachedBuffer, anyhow::Error> { + Ok(b) + } + + let ext = Extension::builder() + .ops(vec![op_sum_take::decl(), op_boomerang::decl()]) + .build(); + + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![ext], + ..Default::default() + }); + + runtime + .execute_script( + "test.js", + r#" + const a1 = new Uint8Array([1,2,3]); + const a1b = a1.subarray(0, 3); + const a2 = new Uint8Array([5,10,15]); + const a2b = a2.subarray(0, 3); + + + if (!(a1.length > 0 && a1b.length > 0)) { + throw new Error("a1 & a1b should have a length"); + } + let sum = Deno.core.opSync('op_sum_take', a1b); + if (sum !== 6) { + throw new Error(`Bad sum: ${sum}`); + } + if (a1.length > 0 || a1b.length > 0) { + throw new Error("expecting a1 & a1b to be detached"); + } + + const a3 = Deno.core.opSync('op_boomerang', a2b); + if (a3.byteLength != 3) { + throw new Error(`Expected a3.byteLength === 3, got ${a3.byteLength}`); + } + if (a3[0] !== 5 || a3[1] !== 10) { + throw new Error(`Invalid a3: ${a3[0]}, ${a3[1]}`); + } + if (a2.byteLength > 0 || a2b.byteLength > 0) { + throw new Error("expecting a2 & a2b to be detached, a3 re-attached"); + } + + const wmem = new WebAssembly.Memory({ initial: 1, maximum: 2 }); + const w32 = new Uint32Array(wmem.buffer); + w32[0] = 1; w32[1] = 2; w32[2] = 3; + const assertWasmThrow = (() => { + try { + let sum = Deno.core.opSync('op_sum_take', w32.subarray(0, 2)); + return false; + } catch(e) { + return e.message.includes('ExpectedDetachable'); + } + }); + if (!assertWasmThrow()) { + throw new Error("expected wasm mem to not be detachable"); + } + "#, + ) + .unwrap(); + } + + #[test] fn test_op_unstable_disabling() { #[op] fn op_foo() -> Result<i64, anyhow::Error> { |