Skip to content

Replace legacy Array.prototype.slice.call idioms. NFC - #28139

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:array_slice
Nov 3, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:array_slice

Conversation

@sbc100

@sbc100 sbc100 commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

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).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Comment thread src/runtime_debug.js Resolved
@sbc100
sbc100 requested a review from kripken November 2, 2026 20:14
@sbc100 sbc100 changed the title [JS] Replace legacy Array.prototype.slice.call idioms Replace legacy Array.prototype.slice.call idioms Nov 2, 2026
@sbc100 sbc100 changed the title Replace legacy Array.prototype.slice.call idioms Replace legacy Array.prototype.slice.call idioms. NFC Nov 2, 2026
@sbc100
sbc100 requested a review from dschuff November 2, 2026 22:36
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
sbc100 enabled auto-merge (squash) November 2, 2026 23:44
@sbc100
sbc100 disabled auto-merge November 3, 2026 19:27
@sbc100
sbc100 merged commit 916ef30 into emscripten-core:main Nov 3, 2026
42 checks passed
@sbc100
sbc100 deleted the array_slice branch November 3, 2026 19:28
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants