1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

fix(ext/ffi): use anybuffer for op_ffi_ptr_of (#20820)

Fixes #20817
This commit is contained in:
Matt Mastracci 2023-10-08 14:02:07 +09:00 committed by GitHub
parent effb5e1ce4
commit 6cb5d8eb86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View file

@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertThrows } from "./test_util.ts"; import { assertEquals, assertThrows } from "./test_util.ts";
Deno.test({ permissions: { ffi: true } }, function dlopenInvalidArguments() { Deno.test({ permissions: { ffi: true } }, function dlopenInvalidArguments() {
const filename = "/usr/lib/libc.so.6"; const filename = "/usr/lib/libc.so.6";
@ -77,3 +77,24 @@ Deno.test({ permissions: { ffi: false } }, function ffiPermissionDenied() {
ptrView.getFloat64(); ptrView.getFloat64();
}, Deno.errors.PermissionDenied); }, Deno.errors.PermissionDenied);
}); });
Deno.test({ permissions: { ffi: true } }, function pointerOf() {
const buffer = new ArrayBuffer(1024);
const baseAddress = Deno.UnsafePointer.value(Deno.UnsafePointer.of(buffer));
const uint8Address = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Uint8Array(buffer)),
);
assertEquals(baseAddress, uint8Address);
const float64Address = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Float64Array(buffer)),
);
assertEquals(baseAddress, float64Address);
const uint8AddressOffset = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Uint8Array(buffer, 100)),
);
assertEquals(Number(baseAddress) + 100, uint8AddressOffset);
const float64AddressOffset = Deno.UnsafePointer.value(
Deno.UnsafePointer.of(new Float64Array(buffer, 80)),
);
assertEquals(Number(baseAddress) + 80, float64AddressOffset);
});

View file

@ -47,7 +47,7 @@ where
#[op2(fast)] #[op2(fast)]
pub fn op_ffi_ptr_of<FP>( pub fn op_ffi_ptr_of<FP>(
state: &mut OpState, state: &mut OpState,
#[buffer] buf: *const u8, #[anybuffer] buf: *const u8,
) -> Result<*mut c_void, AnyError> ) -> Result<*mut c_void, AnyError>
where where
FP: FfiPermissions + 'static, FP: FfiPermissions + 'static,