Skip to content

Commit

Permalink
lib: handle Float16Array in node:v8 serdes
Browse files Browse the repository at this point in the history
PR-URL: #55996
Fixes: #55574
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: LiviaMedeiros <[email protected]>
  • Loading branch information
bartlomieju authored and aduh95 committed Dec 10, 2024
1 parent a6f0cfa commit 889094f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const {
Uint32Array,
Uint8Array,
Uint8ClampedArray,
globalThis: {
Float16Array,
},
} = primordials;

const { Buffer } = require('buffer');
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-v8-serdes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --expose-internals
// Flags: --expose-internals --js-float16array

'use strict';

Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 889094f

Please sign in to comment.