summaryrefslogtreecommitdiff
path: root/test_ffi/tests
diff options
context:
space:
mode:
Diffstat (limited to 'test_ffi/tests')
-rw-r--r--test_ffi/tests/test.js47
1 files changed, 19 insertions, 28 deletions
diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js
index 1568abcbd..0caa416a1 100644
--- a/test_ffi/tests/test.js
+++ b/test_ffi/tests/test.js
@@ -13,6 +13,10 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
const resourcesPre = Deno.resources();
+function ptr(v) {
+ return Deno.UnsafePointer.of(v);
+}
+
// dlopen shouldn't panic
assertThrows(() => {
Deno.dlopen("cli/src/main.rs", {});
@@ -188,9 +192,9 @@ const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const buffer2 = new Uint8Array([9, 10]);
dylib.symbols.print_buffer(buffer, buffer.length);
dylib.symbols.print_buffer2(buffer, buffer.length, buffer2, buffer2.length);
-const ptr = dylib.symbols.return_buffer();
-dylib.symbols.print_buffer(ptr, 8);
-const ptrView = new Deno.UnsafePointerView(ptr);
+const ptr0 = dylib.symbols.return_buffer();
+dylib.symbols.print_buffer(ptr0, 8);
+const ptrView = new Deno.UnsafePointerView(ptr0);
const into = new Uint8Array(6);
const into2 = new Uint8Array(3);
const into2ptr = Deno.UnsafePointer.of(into2);
@@ -210,7 +214,7 @@ const stringPtr = Deno.UnsafePointer.of(string);
const stringPtrview = new Deno.UnsafePointerView(stringPtr);
console.log(stringPtrview.getCString());
console.log(stringPtrview.getCString(11));
-console.log(Boolean(dylib.symbols.is_null_ptr(ptr)));
+console.log(Boolean(dylib.symbols.is_null_ptr(ptr0)));
console.log(Boolean(dylib.symbols.is_null_ptr(null)));
console.log(Boolean(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into))));
@@ -232,20 +236,7 @@ await sleepNonBlocking.call(100);
console.log(performance.now() - before >= 100);
console.log(dylib.symbols.add_u32(123, 456));
-assertThrows(
- () => {
- dylib.symbols.add_u32(-1, 100);
- },
- TypeError,
- "Expected FFI argument to be an unsigned integer, but got '-1'",
-);
-assertThrows(
- () => {
- dylib.symbols.add_u32(null, 100);
- },
- TypeError,
- "Expected FFI argument to be an unsigned integer, but got 'null'",
-);
+
console.log(dylib.symbols.add_i32(123, 456));
console.log(dylib.symbols.add_u64(0xffffffffn, 0xffffffffn));
console.log(dylib.symbols.add_i64(-0xffffffffn, -0xffffffffn));
@@ -345,7 +336,7 @@ const logManyParametersCallback = new Deno.UnsafeCallback({
],
result: "void",
}, (u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, pointer) => {
- const view = new Deno.UnsafePointerView(new Deno.UnsafePointer(pointer));
+ const view = new Deno.UnsafePointerView(pointer);
const copy_buffer = new Uint8Array(8);
view.copyInto(copy_buffer);
console.log(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, ...copy_buffer);
@@ -373,19 +364,19 @@ const throwCallback = new Deno.UnsafeCallback({
assertThrows(
() => {
- dylib.symbols.call_fn_ptr(throwCallback);
+ dylib.symbols.call_fn_ptr(ptr(throwCallback));
},
TypeError,
"hi",
);
-dylib.symbols.call_fn_ptr(logCallback);
-dylib.symbols.call_fn_ptr_many_parameters(logManyParametersCallback);
-dylib.symbols.call_fn_ptr_return_u8(returnU8Callback);
-dylib.symbols.call_fn_ptr_return_buffer(returnBufferCallback);
-dylib.symbols.store_function(logCallback);
+dylib.symbols.call_fn_ptr(ptr(logCallback));
+dylib.symbols.call_fn_ptr_many_parameters(ptr(logManyParametersCallback));
+dylib.symbols.call_fn_ptr_return_u8(ptr(returnU8Callback));
+dylib.symbols.call_fn_ptr_return_buffer(ptr(returnBufferCallback));
+dylib.symbols.store_function(ptr(logCallback));
dylib.symbols.call_stored_function();
-dylib.symbols.store_function_2(add10Callback);
+dylib.symbols.store_function_2(ptr(add10Callback));
dylib.symbols.call_stored_function_2(20);
const nestedCallback = new Deno.UnsafeCallback(
@@ -394,7 +385,7 @@ const nestedCallback = new Deno.UnsafeCallback(
dylib.symbols.call_stored_function_2(10);
},
);
-dylib.symbols.store_function(nestedCallback);
+dylib.symbols.store_function(ptr(nestedCallback));
dylib.symbols.store_function(null);
dylib.symbols.store_function_2(null);
@@ -404,7 +395,7 @@ console.log("Static u32:", dylib.symbols.static_u32);
console.log("Static i64:", dylib.symbols.static_i64);
console.log(
"Static ptr:",
- dylib.symbols.static_ptr instanceof Deno.UnsafePointer,
+ typeof dylib.symbols.static_ptr === "bigint",
);
const view = new Deno.UnsafePointerView(dylib.symbols.static_ptr);
console.log("Static ptr value:", view.getUint32());