From 1619932a651a189d590bd579f334faac3c6b4397 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Thu, 5 Oct 2023 07:35:21 -0600 Subject: chore(ext/ffi): migrate from op -> op2 for ffi (#20509) Migrate to op2. Making a few decisions to get this across the line: - Empty slices, no matter where the come from, are null pointers. The v8 bugs (https://bugs.chromium.org/p/v8/issues/detail?id=13489) and (https://bugs.chromium.org/p/v8/issues/detail?id=13488) make passing around zero-length slice pointers too dangerous as they might be uninitialized or null data. - Offsets and lengths are `#[number] isize` and `#[number] usize` respectively -- 53 bits should be enough for anyone - Pointers are bigints. This is a u64 in the fastcall world, and can accept Integer/Int32/Number/BigInt v8 types in the slow world. --- test_ffi/tests/integration_tests.rs | 11 ++++++----- test_ffi/tests/test.js | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'test_ffi/tests') diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index a4f233e45..ad00465e6 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use pretty_assertions::assert_eq; use std::process::Command; use test_util::deno_cmd; @@ -54,11 +55,11 @@ fn basic() { [ 4, 5, 6 ]\n\ Hello from pointer!\n\ pointer!\n\ - false\n\ - true\n\ - false\n\ - true\n\ - false\n\ + false false\n\ + true true\n\ + false false\n\ + true true\n\ + false false\n\ 579\n\ true\n\ 579\n\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index f4fc96a1b..80c566398 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -341,13 +341,13 @@ const stringPtr = Deno.UnsafePointer.of(string); const stringPtrview = new Deno.UnsafePointerView(stringPtr); console.log(stringPtrview.getCString()); console.log(stringPtrview.getCString(11)); -console.log(dylib.symbols.is_null_ptr(ptr0)); -console.log(dylib.symbols.is_null_ptr(null)); -console.log(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into))); +console.log("false", dylib.symbols.is_null_ptr(ptr0)); +console.log("true", dylib.symbols.is_null_ptr(null)); +console.log("false", dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into))); const emptyBuffer = new Uint8Array(0); -console.log(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(emptyBuffer))); +console.log("true", dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(emptyBuffer))); const emptySlice = into.subarray(6); -console.log(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(emptySlice))); +console.log("false", dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(emptySlice))); const { is_null_buf } = symbols; function isNullBuffer(buffer) { return is_null_buf(buffer); }; @@ -386,10 +386,9 @@ const externalOneBuffer = new Uint8Array(Deno.UnsafePointerView.getArrayBuffer(p assertEquals(isNullBuffer(externalOneBuffer), false, "isNullBuffer(externalOneBuffer) !== false"); assertEquals(isNullBufferDeopt(externalOneBuffer), false, "isNullBufferDeopt(externalOneBuffer) !== false"); -// Due to ops macro using `Local->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), null, "Deno.UnsafePointer.of(externalZeroBuffer) !== null"); +// UnsafePointer.of uses an exact-pointer fallback for zero-length buffers and slices to ensure that it always gets +// the underlying pointer right. +assertNotEquals(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(); @@ -726,7 +725,9 @@ assertEquals(view.getUint32(), 55); 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); + const zeroPointer = Deno.UnsafePointer.offset(offsetPointer, -6); + assertEquals(Deno.UnsafePointer.value(zeroPointer), 0); + assertEquals(zeroPointer, null); } // Test non-UTF-8 characters -- cgit v1.2.3