diff options
Diffstat (limited to 'test_ffi/tests')
-rw-r--r-- | test_ffi/tests/ffi_types.ts | 43 | ||||
-rw-r--r-- | test_ffi/tests/integration_tests.rs | 15 | ||||
-rw-r--r-- | test_ffi/tests/test.js | 69 |
3 files changed, 68 insertions, 59 deletions
diff --git a/test_ffi/tests/ffi_types.ts b/test_ffi/tests/ffi_types.ts index d7c66203c..c04c13d79 100644 --- a/test_ffi/tests/ffi_types.ts +++ b/test_ffi/tests/ffi_types.ts @@ -140,30 +140,31 @@ remote.symbols.method14(0); remote.symbols.method15("foo"); // @ts-expect-error: Invalid argument remote.symbols.method15(new Uint16Array(1)); -remote.symbols.method15(0n); +remote.symbols.method15(null); +remote.symbols.method15({} as Deno.PointerValue); const result = remote.symbols.method16(); // @ts-expect-error: Invalid argument let r_0: string = result; -let r_1: Deno.PointerValue = result; +let r_1: number | bigint = result; const result2 = remote.symbols.method17(); // @ts-expect-error: Invalid argument result2.then((_0: string) => {}); -result2.then((_1: Deno.PointerValue) => {}); +result2.then((_1: number | bigint) => {}); const result3 = remote.symbols.method18(); // @ts-expect-error: Invalid argument let r3_0: Deno.BufferSource = result3; -let r3_1: Deno.UnsafePointer = result3; +let r3_1: null | Deno.UnsafePointer = result3; const result4 = remote.symbols.method19(); // @ts-expect-error: Invalid argument result4.then((_0: Deno.BufferSource) => {}); -result4.then((_1: Deno.UnsafePointer) => {}); +result4.then((_1: null | Deno.UnsafePointer) => {}); const fnptr = new Deno.UnsafeFnPointer( - 0n, + {} as NonNullable<Deno.PointerValue>, { parameters: ["u32", "pointer"], result: "void", @@ -210,7 +211,7 @@ const unsafe_callback_right1 = new Deno.UnsafeCallback( parameters: ["u8", "u32", "pointer"], result: "void", }, - (_1: number, _2: number, _3: Deno.PointerValue) => {}, + (_1: number, _2: number, _3: null | Deno.PointerValue) => {}, ); const unsafe_callback_right2 = new Deno.UnsafeCallback( { @@ -232,14 +233,14 @@ const unsafe_callback_right4 = new Deno.UnsafeCallback( parameters: ["u8", "u32", "pointer"], result: "u8", }, - (_1: number, _2: number, _3: Deno.PointerValue) => 3, + (_1: number, _2: number, _3: null | Deno.PointerValue) => 3, ); const unsafe_callback_right5 = new Deno.UnsafeCallback( { parameters: ["u8", "i32", "pointer"], result: "void", }, - (_1: number, _2: number, _3: Deno.PointerValue) => {}, + (_1: number, _2: number, _3: null | Deno.PointerValue) => {}, ); // @ts-expect-error: Must pass callback @@ -255,9 +256,9 @@ remote.symbols.method23(new Uint32Array(1)); remote.symbols.method23(new Uint8Array(1)); // @ts-expect-error: Cannot pass pointer values as buffer. -remote.symbols.method23(0); +remote.symbols.method23({}); // @ts-expect-error: Cannot pass pointer values as buffer. -remote.symbols.method23(0n); +remote.symbols.method23({}); remote.symbols.method23(null); // @ts-expect-error: Cannot pass number as bool. @@ -278,16 +279,16 @@ let r42_1: number = remote.symbols.method24(true); // @ts-expect-error: Invalid member type const static1_wrong: null = remote.symbols.static1; -const static1_right: Deno.PointerValue = remote.symbols.static1; +const static1_right: number | bigint = remote.symbols.static1; // @ts-expect-error: Invalid member type const static2_wrong: null = remote.symbols.static2; -const static2_right: Deno.UnsafePointer = remote.symbols.static2; +const static2_right: null | Deno.UnsafePointer = remote.symbols.static2; // @ts-expect-error: Invalid member type const static3_wrong: null = remote.symbols.static3; -const static3_right: Deno.PointerValue = remote.symbols.static3; +const static3_right: number | bigint = remote.symbols.static3; // @ts-expect-error: Invalid member type const static4_wrong: null = remote.symbols.static4; -const static4_right: Deno.PointerValue = remote.symbols.static4; +const static4_right: number | bigint = remote.symbols.static4; // @ts-expect-error: Invalid member type const static5_wrong: null = remote.symbols.static5; const static5_right: number = remote.symbols.static5; @@ -299,7 +300,7 @@ const static7_wrong: null = remote.symbols.static7; const static7_right: number = remote.symbols.static7; // @ts-expect-error: Invalid member type const static8_wrong: null = remote.symbols.static8; -const static8_right: Deno.PointerValue = remote.symbols.static8; +const static8_right: number | bigint = remote.symbols.static8; // @ts-expect-error: Invalid member type const static9_wrong: null = remote.symbols.static9; const static9_right: number = remote.symbols.static9; @@ -311,7 +312,7 @@ const static11_wrong: null = remote.symbols.static11; const static11_right: number = remote.symbols.static11; // @ts-expect-error: Invalid member type const static12_wrong: null = remote.symbols.static12; -const static12_right: Deno.PointerValue = remote.symbols.static12; +const static12_right: number | bigint = remote.symbols.static12; // @ts-expect-error: Invalid member type const static13_wrong: null = remote.symbols.static13; const static13_right: number = remote.symbols.static13; @@ -376,8 +377,8 @@ type __Tests__ = [ symbols: { pushBuf: ( buf: BufferSource | null, - ptr: Deno.PointerValue | null, - func: Deno.PointerValue | null, + ptr: Deno.PointerValue, + func: Deno.PointerValue, ) => Deno.PointerValue; }; close(): void; @@ -395,8 +396,8 @@ type __Tests__ = [ { symbols: { foo: ( - ...args: (Deno.PointerValue | null)[] - ) => Deno.PointerValue; + ...args: (number | Deno.PointerValue | null)[] + ) => number | bigint; }; close(): void; }, diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index c213f8295..fdfb5d895 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -97,12 +97,8 @@ fn basic() { -9007199254740992n\n\ 579.9119873046875\n\ 579.912\n\ - After sleep_blocking\n\ - true\n\ Before\n\ - true\n\ After\n\ - true\n\ logCallback\n\ 1 -1 2 -2 3 -3 4 -4 0.5 -0.5 1 2 3 4 5 6 7 8\n\ u8: 8\n\ @@ -119,23 +115,12 @@ fn basic() { 78\n\ STORED_FUNCTION cleared\n\ STORED_FUNCTION_2 cleared\n\ - Thread safe call counter: 0\n\ logCallback\n\ - Thread safe call counter: 1\n\ u8: 8\n\ - Static u32: 42\n\ - Static i64: -1242464576485\n\ - Static ptr: true\n\ - Static ptr value: 42\n\ Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\ Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\ Rect { x: 20.0, y: 20.0, w: 100.0, h: 200.0 }\n\ Mixed { u8: 3, f32: 12.515, rect: Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }, usize: 12456789, array: [8, 32] }\n\ - arrayBuffer.byteLength: 4\n\ - uint32Array.length: 1\n\ - uint32Array[0]: 42\n\ - uint32Array[0] after mutation: 55\n\ - Static ptr value after mutation: 55\n\ 2264956937\n\ 2264956937\n\ Correct number of resources\n"; diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 15900e72c..788faa93e 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -1,12 +1,15 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // deno-lint-ignore-file -// Run using cargo test or `--v8-options=--allow-natives-syntax` +// Run using cargo test or `--v8-flags=--allow-natives-syntax` -import { assertEquals, assertInstanceOf, assertNotEquals } from "https://deno.land/std@0.149.0/testing/asserts.ts"; import { assertThrows, assert, + assertNotEquals, + assertInstanceOf, + assertEquals, + assertFalse, } from "../../test_util/std/testing/asserts.ts"; const targetDir = Deno.execPath().replace(/[^\/\\]+$/, ""); @@ -368,8 +371,8 @@ assertEquals(isNullBufferDeopt(externalOneBuffer), false, "isNullBufferDeopt(ext // Due to ops macro using `Local<ArrayBuffer>->Data()` to get the pointer for the slice that is then used to get // the pointer of an ArrayBuffer / TypedArray, the same effect can be seen where a zero byte length buffer returns // a null pointer as its pointer value. -assertEquals(Deno.UnsafePointer.of(externalZeroBuffer), 0, "Deno.UnsafePointer.of(externalZeroBuffer) !== 0"); -assertNotEquals(Deno.UnsafePointer.of(externalOneBuffer), 0, "Deno.UnsafePointer.of(externalOneBuffer) !== 0"); +assertEquals(Deno.UnsafePointer.of(externalZeroBuffer), null, "Deno.UnsafePointer.of(externalZeroBuffer) !== null"); +assertNotEquals(Deno.UnsafePointer.of(externalOneBuffer), null, "Deno.UnsafePointer.of(externalOneBuffer) === null"); const addU32Ptr = dylib.symbols.get_add_u32_ptr(); const addU32 = new Deno.UnsafeFnPointer(addU32Ptr, { @@ -486,16 +489,15 @@ await promise; let start = performance.now(); dylib.symbols.sleep_blocking(100); -console.log("After sleep_blocking"); -console.log(performance.now() - start >= 100); +assert(performance.now() - start >= 100); start = performance.now(); const promise_2 = dylib.symbols.sleep_nonblocking(100).then(() => { console.log("After"); - console.log(performance.now() - start >= 100); + assert(performance.now() - start >= 100); }); console.log("Before"); -console.log(performance.now() - start < 100); +assert(performance.now() - start < 100); // Await to make sure `sleep_nonblocking` calls and logs before we proceed await promise_2; @@ -532,7 +534,7 @@ const returnU8Callback = new Deno.UnsafeCallback( ); const returnBufferCallback = new Deno.UnsafeCallback({ parameters: [], - result: "pointer", + result: "buffer", }, () => { return buffer; }); @@ -617,27 +619,29 @@ const addToFooCallback = new Deno.UnsafeCallback({ }, () => counter++); // Test thread safe callbacks -console.log("Thread safe call counter:", counter); +assertEquals(counter, 0); addToFooCallback.ref(); await dylib.symbols.call_fn_ptr_thread_safe(addToFooCallback.pointer); addToFooCallback.unref(); logCallback.ref(); await dylib.symbols.call_fn_ptr_thread_safe(logCallback.pointer); logCallback.unref(); -console.log("Thread safe call counter:", counter); +assertEquals(counter, 1); returnU8Callback.ref(); await dylib.symbols.call_fn_ptr_return_u8_thread_safe(returnU8Callback.pointer); // Purposefully do not unref returnU8Callback: Instead use it to test close() unrefing. // Test statics -console.log("Static u32:", dylib.symbols.static_u32); -console.log("Static i64:", dylib.symbols.static_i64); -console.log( - "Static ptr:", - typeof dylib.symbols.static_ptr === "number", +assertEquals(dylib.symbols.static_u32, 42); +assertEquals(dylib.symbols.static_i64, -1242464576485); +assert( + typeof dylib.symbols.static_ptr === "object" +); +assertEquals( + Object.keys(dylib.symbols.static_ptr).length, 0 ); const view = new Deno.UnsafePointerView(dylib.symbols.static_ptr); -console.log("Static ptr value:", view.getUint32()); +assertEquals(view.getUint32(), 42); // Test struct returning const rect_sync = dylib.symbols.make_rect(10, 20, 100, 200); @@ -656,7 +660,7 @@ assertEquals(rect_async.length, 4 * 8); assertEquals(Array.from(new Float64Array(rect_async.buffer)), [10, 20, 100, 200]); // Test complex, mixed struct returning and passing -const mixedStruct = dylib.symbols.create_mixed(3, 12.515000343322754, rect_async, 12456789, new Uint32Array([8, 32])); +const mixedStruct = dylib.symbols.create_mixed(3, 12.515000343322754, rect_async, Deno.UnsafePointer.create(12456789), new Uint32Array([8, 32])); assertEquals(mixedStruct.length, 56); assertEquals(Array.from(mixedStruct.subarray(0, 4)), [3, 0, 0, 0]); assertEquals(new Float32Array(mixedStruct.buffer, 4, 1)[0], 12.515000343322754); @@ -681,12 +685,31 @@ cb.close(); const arrayBuffer = view.getArrayBuffer(4); const uint32Array = new Uint32Array(arrayBuffer); -console.log("arrayBuffer.byteLength:", arrayBuffer.byteLength); -console.log("uint32Array.length:", uint32Array.length); -console.log("uint32Array[0]:", uint32Array[0]); +assertEquals(arrayBuffer.byteLength, 4); +assertEquals(uint32Array.length, 1); +assertEquals(uint32Array[0], 42); uint32Array[0] = 55; // MUTATES! -console.log("uint32Array[0] after mutation:", uint32Array[0]); -console.log("Static ptr value after mutation:", view.getUint32()); +assertEquals(uint32Array[0], 55); +assertEquals(view.getUint32(), 55); + + +{ + // Test UnsafePointer APIs + assertEquals(Deno.UnsafePointer.create(0), null); + const createdPointer = Deno.UnsafePointer.create(1); + assertNotEquals(createdPointer, null); + assertEquals(typeof createdPointer, "object"); + assertEquals(Deno.UnsafePointer.value(null), 0); + assertEquals(Deno.UnsafePointer.value(createdPointer), 1); + assert(Deno.UnsafePointer.equals(null, null)); + assertFalse(Deno.UnsafePointer.equals(null, createdPointer)); + assertFalse(Deno.UnsafePointer.equals(Deno.UnsafePointer.create(2), createdPointer)); + // Do not allow offsetting from null, `create` function should be used instead. + assertThrows(() => Deno.UnsafePointer.offset(null, 5)); + const offsetPointer = Deno.UnsafePointer.offset(createdPointer, 5); + assertEquals(Deno.UnsafePointer.value(offsetPointer), 6); + assertEquals(Deno.UnsafePointer.offset(offsetPointer, -6), null); +} // Test non-UTF-8 characters |