From 889094fdbc9a521d7572b17082a182ac887ad70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 7 Dec 2024 18:24:28 +0000 Subject: [PATCH] lib: handle Float16Array in node:v8 serdes PR-URL: https://github.com/nodejs/node/pull/55996 Fixes: https://github.com/nodejs/node/issues/55574 Reviewed-By: Ben Noordhuis Reviewed-By: Antoine du Hamel Reviewed-By: LiviaMedeiros --- lib/eslint.config_partial.mjs | 5 +++++ lib/v8.js | 6 ++++++ test/parallel/test-v8-serdes.js | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/eslint.config_partial.mjs b/lib/eslint.config_partial.mjs index 78d9f409a37956..520df40aecbc90 100644 --- a/lib/eslint.config_partial.mjs +++ b/lib/eslint.config_partial.mjs @@ -347,6 +347,11 @@ export default [ name: 'SubtleCrypto', message: "Use `const { SubtleCrypto } = require('internal/crypto/webcrypto');` instead of the global.", }, + // Float16Array is not available in primordials because it's only available with --js-float16array CLI flag. + { + name: 'Float16Array', + message: 'Use `const { Float16Array } = globalThis;` instead of the global.', + }, ], 'no-restricted-modules': [ 'error', diff --git a/lib/v8.js b/lib/v8.js index 7a8979887bab49..e379233ea49e0d 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -31,6 +31,9 @@ const { Uint32Array, Uint8Array, Uint8ClampedArray, + globalThis: { + Float16Array, + }, } = primordials; const { Buffer } = require('buffer'); @@ -63,6 +66,7 @@ const { } = require('internal/heap_utils'); const promiseHooks = require('internal/promise_hooks'); const { getOptionValue } = require('internal/options'); + /** * Generates a snapshot of the current V8 heap * and writes it to a JSON file. @@ -289,6 +293,7 @@ function arrayBufferViewTypeToIndex(abView) { // Index 10 is FastBuffer. if (type === '[object BigInt64Array]') return 11; if (type === '[object BigUint64Array]') return 12; + if (type === '[object Float16Array]') return 13; return -1; } @@ -306,6 +311,7 @@ function arrayBufferViewIndexToType(index) { if (index === 10) return FastBuffer; if (index === 11) return BigInt64Array; if (index === 12) return BigUint64Array; + if (index === 13) return Float16Array; return undefined; } diff --git a/test/parallel/test-v8-serdes.js b/test/parallel/test-v8-serdes.js index 296e076a9f760d..c87ed89353af3b 100644 --- a/test/parallel/test-v8-serdes.js +++ b/test/parallel/test-v8-serdes.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --expose-internals --js-float16array 'use strict'; @@ -7,6 +7,9 @@ const { internalBinding } = require('internal/test/binding'); const assert = require('assert'); const v8 = require('v8'); const os = require('os'); +// TODO(bartlomieju): once `Float16Array` is available in stable V8, +// remove this line and `--js-float16array` flag up top +const { Float16Array } = globalThis; const circular = {}; circular.circular = circular; @@ -26,6 +29,7 @@ const objects = [ Buffer.from([1, 2, 3, 4]), new BigInt64Array([42n]), new BigUint64Array([42n]), + new Float16Array([1, 2, 3, 4]), undefined, null, 42,