oxc-parser 0.123.0 → 0.125.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -270,7 +270,7 @@ function createBuffer() {
270
270
  const arrayBuffer = new ArrayBuffer(ARRAY_BUFFER_SIZE);
271
271
  const offset = getBufferOffset(new Uint8Array(arrayBuffer));
272
272
  const buffer = new Uint8Array(arrayBuffer, offset, BUFFER_SIZE);
273
- buffer.uint32 = new Uint32Array(arrayBuffer, offset, BUFFER_SIZE / 4);
273
+ buffer.int32 = new Int32Array(arrayBuffer, offset, BUFFER_SIZE / 4);
274
274
  buffer.float64 = new Float64Array(arrayBuffer, offset, BUFFER_SIZE / 8);
275
275
  return buffer;
276
276
  }
@@ -162,10 +162,11 @@ const TOKEN_TYPES = [
162
162
  "JSXIdentifier",
163
163
  ];
164
164
 
165
+ // Mask for active bits in `ESTreeKind` discriminants
166
+ const TOKEN_KIND_MASK = 15;
167
+
165
168
  // Details of Rust `Token` type
166
169
  const TOKEN_SIZE = 16;
167
- const KIND_FIELD_OFFSET = 8;
168
- const IS_ESCAPED_FIELD_OFFSET = 10;
169
170
 
170
171
  /**
171
172
  * Deserialize tokens from buffer.
@@ -175,15 +176,15 @@ const IS_ESCAPED_FIELD_OFFSET = 10;
175
176
  * @returns {Object[]} - Array of token objects
176
177
  */
177
178
  function deserializeTokens(buffer, sourceText, isJs) {
178
- const { uint32 } = buffer;
179
+ const { int32 } = buffer;
179
180
 
180
- let pos = uint32[TOKENS_OFFSET_POS_32];
181
- const len = uint32[TOKENS_LEN_POS_32];
181
+ let pos = int32[TOKENS_OFFSET_POS_32];
182
+ const len = int32[TOKENS_LEN_POS_32];
182
183
  const endPos = pos + len * TOKEN_SIZE;
183
184
 
184
185
  const tokens = [];
185
186
  while (pos < endPos) {
186
- tokens.push(deserializeToken(pos, buffer, sourceText, isJs));
187
+ tokens.push(deserializeToken(pos, int32, sourceText, isJs));
187
188
  pos += TOKEN_SIZE;
188
189
  }
189
190
  return tokens;
@@ -192,21 +193,24 @@ function deserializeTokens(buffer, sourceText, isJs) {
192
193
  /**
193
194
  * Deserialize a token from buffer at position `pos`.
194
195
  * @param {number} pos - Position in buffer containing Rust `Token` type
195
- * @param {Uint8Array} buffer - Buffer containing AST in raw form
196
+ * @param {Int32Array} int32 - Buffer containing AST in raw form as an `Int32Array`
196
197
  * @param {string} sourceText - Source for the file
197
198
  * @param {boolean} isJs - `true` if parsing in JS mode
198
199
  * @returns {Object} - Token object
199
200
  */
200
- function deserializeToken(pos, buffer, sourceText, isJs) {
201
- const { uint32 } = buffer;
202
-
203
- const pos32 = pos >> 2;
204
- const start = uint32[pos32],
205
- end = uint32[pos32 + 1];
201
+ function deserializeToken(pos, int32, sourceText, isJs) {
202
+ const pos32 = pos >> 2,
203
+ start = int32[pos32],
204
+ end = int32[pos32 + 1],
205
+ kindAndFlags = int32[pos32 + 2];
206
206
 
207
207
  let value = sourceText.slice(start, end);
208
208
 
209
- const kind = buffer[pos + KIND_FIELD_OFFSET];
209
+ // `Kind` is byte at index 8 in `Token`.
210
+ // `Kind` has 12 variants numbered from 0 to 11.
211
+ // We have to mask the bottom byte (`& 0xFF`), so may as well mask off bits which can't be set in `Kind` at same time.
212
+ // This may allow V8 to generate more efficient code for `TOKEN_TYPES[kind]`.
213
+ const kind = kindAndFlags & TOKEN_KIND_MASK;
210
214
 
211
215
  if (kind === REGEXP_KIND) {
212
216
  const patternEnd = value.lastIndexOf("/");
@@ -225,8 +229,9 @@ function deserializeToken(pos, buffer, sourceText, isJs) {
225
229
  // Strip leading `#` from private identifiers
226
230
  if (kind === PRIVATE_IDENTIFIER_KIND) value = value.slice(1);
227
231
 
228
- // Unescape identifiers, keywords, and private identifiers in JS mode
229
- if (isJs && kind <= PRIVATE_IDENTIFIER_KIND && buffer[pos + IS_ESCAPED_FIELD_OFFSET] === 1) {
232
+ // Unescape identifiers, keywords, and private identifiers in JS mode.
233
+ // `is_escaped` flag is in byte 10 of `Token`, and is a `bool`.
234
+ if (isJs && kind <= PRIVATE_IDENTIFIER_KIND && (kindAndFlags & 0x10000) !== 0) {
230
235
  value = unescapeIdentifier(value);
231
236
  }
232
237
 
@@ -98,7 +98,7 @@ function construct(buffer, sourceText, sourceByteLen, _options) {
98
98
  bufferRecycleRegistry.register(ast, buffer, ast);
99
99
 
100
100
  // Get root data class instance
101
- const rawDataPos = buffer.uint32[DATA_POINTER_POS_32];
101
+ const rawDataPos = buffer.int32[DATA_POINTER_POS_32];
102
102
  const data = new RawTransferData(rawDataPos, ast);
103
103
 
104
104
  return {