Repository navigation
Replace legacy Array.prototype.slice.call idioms. NFC - #28139
Merged
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors several JavaScript files to replace verbose Array.prototype.slice.call patterns with modern alternatives like Array.from and direct .slice() calls, resulting in minor code size reductions across the codebase. However, a potential runtime issue was identified in src/runtime_debug.js where calling .slice() on an object with a byteLength property (such as a DataView or ArrayBuffer) can lead to a TypeError or incorrect stringification. It is recommended to verify that the object also has a numeric length property before calling .slice().
Array.prototype.slice.call idiomsArray.prototype.slice.call idioms
Array.prototype.slice.call idiomsArray.prototype.slice.call idioms. NFC
Historically, `Array.prototype.slice.call` was used in two places in our JS runtime and library code: 1. On `TypedArray` instances (in `libglemu.js`, `libmemfs.js`, and `runtime_debug.js`). Before ES2015 incorporated `TypedArray` into the ECMAScript standard, the Khronos WebGL `TypedArray` specification lacked `%TypedArray%.prototype.slice()` for shallow copying as well as `%TypedArray%.prototype.toString()` for comma-separated formatting (calling `.toString()` inherited `Object.prototype.toString` and produced `"[object Float32Array]"`). Converting to a plain `Array` via `Array.prototype.slice.call` was therefore necessary both to clone matrices in `glPushMatrix` and to format typed arrays in debug logs. 2. On `arguments` objects (in `jsifier.mjs` and `libcore.js`) before `Array.from()` was introduced in ES2015. `%TypedArray%.prototype.slice()`, `%TypedArray%.prototype.toString()`, and `Array.from()` have all been supported since Chrome 45, Firefox 38, Safari 10, and Node.js 4.0 (2015-2016), well before Emscripten's minimum supported engine versions (Chrome 85, Firefox 79, Safari 15, Node 18.3). In `runtime_debug.js`, calling `arg.slice(0, 400)` directly also avoids copying the entire typed array before slicing to 400 elements, and in `glPushMatrix` it preserves the `Float32Array` type on the matrix stack.
sbc100
enabled auto-merge (squash)
November 2, 2026 23:44
kripken
approved these changes
Nov 3, 2026
sbc100
disabled auto-merge
November 3, 2026 19:27
sbc100
added a commit
that referenced
this pull request
Nov 5, 2026
Follow-up to #28129 and #28139 - Enable `prefer-exponentiation-operator`, `prefer-object-spread`, `prefer-rest-params`, `prefer-spread`, and `logical-assignment-operators` in `eslint.config.mjs`. - Use `Object.values`, direct integer indexing, or `??=` in places where `$hasOwn` is not needed. - Use `Map` instead of plain `{}` objects for lookup tables in `acorn-optimizer.mjs` and `libglfw.js`. - Replace legacy `Array.prototype.slice.call`, `.apply`, `.indexOf`, `Math.pow`, and `arr[arr.length - 1]` idioms with `.slice`, `Array.from`, spread syntax, `.includes`, `**` / `Math.hypot`, and `.at(-1)`. Note: We are not yet using `.at(-1)` in runtime code since we still support engines that do not yet support its (and even we we re-introduced babel, its not able to lower this one away).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Historically,
Array.prototype.slice.callwas used in two places in our JS runtime and library code:TypedArrayinstances (inlibglemu.js,libmemfs.js, andruntime_debug.js). Before ES2015 incorporatedTypedArrayinto the ECMAScript standard, the Khronos WebGLTypedArrayspecification lacked%TypedArray%.prototype.slice()for shallow copying as well as%TypedArray%.prototype.toString()for comma-separated formatting (calling.toString()inheritedObject.prototype.toStringand produced"[object Float32Array]"). Converting to a plainArrayviaArray.prototype.slice.callwas therefore necessary both to clone matrices inglPushMatrixand to format typed arrays in debug logs.argumentsobjects (injsifier.mjsandlibcore.js) beforeArray.from()was introduced in ES2015.%TypedArray%.prototype.slice(),%TypedArray%.prototype.toString(), andArray.from()have all been supported since Chrome 45, Firefox 38, Safari 10, and Node.js 4.0 (2015-2016), well before Emscripten's minimum supported engine versions (Chrome 85, Firefox 79, Safari 15, Node 18.3).