Skip to content

fix(napi/parser): lazy deser: remove outdated comments #11699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions napi/parser/raw-transfer/node-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
const { TOKEN, constructorError } = require('./lazy-common.js');

// Mapping from a proxy to the `NodeArray` that it wraps.
// Used by `slice` method.
// Used by `slice`, `values`, `key` and `elements` methods.
//
// TODO: Is there any way to avoid this?
// Seems necessary because `this` in methods is a proxy, so accessing `this.#internal` throws.
const nodeArrays = new WeakMap();

// An array of AST nodes where elements are deserialized lazily upon access.
//
// Extends `Array` to make `Array.isArray` return `true` for a `NodeArray`.
//
// TODO: Other methods could maybe be more optimal, avoiding going via proxy multiple times.
// e.g. `some`, `indexOf`
// TODO: Other methods could maybe be more optimal, avoiding going via proxy multiple times
// e.g. `some`, `indexOf`.
class NodeArray extends Array {
#internal;

Expand Down Expand Up @@ -56,6 +59,8 @@ class NodeArray extends Array {
// TODO: Benchmark to check that this is actually faster.
keys() {
// Get actual `NodeArray`. `this` is a proxy.
// TODO: `this.length` would work here.
// Not sure which is more expensive - property lookup via proxy, or `WeakMap` lookup.
const arr = nodeArrays.get(this);
return new NodeArrayKeysIterator(arr.length);
}
Expand All @@ -73,11 +78,6 @@ class NodeArray extends Array {
[Symbol.iterator]() {}

// Override `slice` method to return a `NodeArray`.
//
// The new `NodeArray` references this `NodeArray` so element accesses on the slice will
// read/write to this `NodeArray`.
//
// TODO: Is there any way to avoid the `WeakMap`? Seems necessary because `this` here is a proxy.
slice(start, end) {
// Get actual `NodeArray`. `this` is a proxy.
const arr = nodeArrays.get(this);
Expand Down Expand Up @@ -140,7 +140,7 @@ NodeArray.prototype[Symbol.iterator] = NodeArray.prototype.values;
module.exports = NodeArray;

// Iterator over values of a `NodeArray`.
// Returned by `values` method, and also used as iterator for `for (const value of nodeArray) {}`.
// Returned by `values` method, and also used as iterator for `for (const node of nodeArray) {}`.
class NodeArrayValuesIterator {
#internal;

Expand Down Expand Up @@ -175,8 +175,8 @@ class NodeArrayKeysIterator {
#internal;

constructor(length) {
// Don't bother gating constructor with `TOKEN` since this iterator doesn't access buffer,
// and so is harmless
// Don't bother gating constructor with `TOKEN` check.
// This iterator doesn't access the buffer, so is harmless.
this.#internal = { index: 0, length };
}

Expand Down Expand Up @@ -227,7 +227,7 @@ class NodeArrayEntriesIterator {
}
}

// Class used for `[Symbol.for('nodejs.util.inspect.custom')]` method (`console.log`).
// Class used for `[Symbol.for('nodejs.util.inspect.custom')]` method (for `console.log`).
const DebugNodeArray = class NodeArray extends Array {};

// Proxy handlers.
Expand Down Expand Up @@ -284,7 +284,6 @@ const PROXY_HANDLERS = {

// Get keys, including element indexes.
ownKeys(arr) {
// `NodeArray`s which are slices don't have their own elements. Act as if they do.
const keys = [];
for (let i = 0; i < arr.length; i++) {
keys.push(i + '');
Expand All @@ -296,7 +295,7 @@ const PROXY_HANDLERS = {

/**
* Check if a key is a valid array index.
* Only strings comprising of plain integers are valid indexes.
* Only strings comprising a plain integer are valid indexes.
* e.g. `"-1"`, `"01"`, `"0xFF"`, `"1e1"`, `"1 "` are not valid indexes.
*
* @param {*} - Key used for property lookup.
Expand Down
Loading