1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(ext/ffi): Make External pointers keep reference to V8 buffer (#17955)

This commit is contained in:
Aapo Alasuutari 2023-03-05 10:01:23 +02:00 committed by GitHub
parent 0910be4d64
commit da201d9ea5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View file

@ -26,6 +26,7 @@ const {
SafeMap, SafeMap,
SafeArrayIterator, SafeArrayIterator,
SymbolFor, SymbolFor,
WeakMap,
} = primordials; } = primordials;
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId"); const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
@ -179,6 +180,7 @@ class UnsafePointerView {
const OUT_BUFFER = new Uint32Array(2); const OUT_BUFFER = new Uint32Array(2);
const OUT_BUFFER_64 = new BigInt64Array(OUT_BUFFER.buffer); const OUT_BUFFER_64 = new BigInt64Array(OUT_BUFFER.buffer);
const POINTER_TO_BUFFER_WEAK_MAP = new WeakMap();
class UnsafePointer { class UnsafePointer {
static create(value) { static create(value) {
return ops.op_ffi_ptr_create(value); return ops.op_ffi_ptr_create(value);
@ -195,7 +197,11 @@ class UnsafePointer {
if (ObjectPrototypeIsPrototypeOf(UnsafeCallbackPrototype, value)) { if (ObjectPrototypeIsPrototypeOf(UnsafeCallbackPrototype, value)) {
return value.pointer; return value.pointer;
} }
return ops.op_ffi_ptr_of(value); const pointer = ops.op_ffi_ptr_of(value);
if (pointer) {
POINTER_TO_BUFFER_WEAK_MAP.set(pointer, value);
}
return pointer;
} }
static offset(value, offset) { static offset(value, offset) {

View file

@ -354,8 +354,6 @@ Deno.bench("nop_f64()", () => {
const { nop_buffer } = dylib.symbols; const { nop_buffer } = dylib.symbols;
const buffer = new Uint8Array(8).fill(5); const buffer = new Uint8Array(8).fill(5);
// Make sure the buffer does not get collected
globalThis.buffer = buffer;
Deno.bench("nop_buffer()", () => { Deno.bench("nop_buffer()", () => {
nop_buffer(buffer); nop_buffer(buffer);
}); });
@ -565,8 +563,6 @@ Deno.bench("return_buffer_nonblocking()", async () => {
const { nop_many_parameters } = dylib.symbols; const { nop_many_parameters } = dylib.symbols;
const buffer2 = new Uint8Array(8).fill(25); const buffer2 = new Uint8Array(8).fill(25);
// Make sure the buffer does not get collected
globalThis.buffer2 = buffer2;
Deno.bench("nop_many_parameters()", () => { Deno.bench("nop_many_parameters()", () => {
nop_many_parameters( nop_many_parameters(
135, 135,
@ -635,8 +631,6 @@ Deno.bench("Deno.UnsafePointer.of", () => {
}); });
const cstringBuffer = new TextEncoder().encode("Best believe it!\0"); const cstringBuffer = new TextEncoder().encode("Best believe it!\0");
// Make sure the buffer does not get collected
globalThis.cstringBuffer = cstringBuffer;
const cstringPointerView = new Deno.UnsafePointerView( const cstringPointerView = new Deno.UnsafePointerView(
Deno.UnsafePointer.of(cstringBuffer), Deno.UnsafePointer.of(cstringBuffer),
); );