unpeek 1.6.1 → 1.6.2

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.
@@ -128,15 +128,100 @@ function analyzeAstHotPaths(ast) {
128
128
  const coldFunctionNodes = /* @__PURE__ */ new WeakSet();
129
129
  const hotExemptNodes = /* @__PURE__ */ new WeakSet();
130
130
  const directHotNodes = /* @__PURE__ */ new Set();
131
+ const serializedFunctionNodes = /* @__PURE__ */ new Set();
132
+ const serializerHelperNodes = /* @__PURE__ */ new Set();
131
133
  const allFunctions = [];
132
134
  const fnByName = /* @__PURE__ */ new Map();
133
135
  const scopeFunctions = /* @__PURE__ */ new Map();
134
136
  const fnScopeChain = /* @__PURE__ */ new Map();
137
+ const fnParams = /* @__PURE__ */ new Map();
138
+ const fnVarAliases = /* @__PURE__ */ new Map();
135
139
  const callerToCallees = /* @__PURE__ */ new Map();
140
+ const pendingSerializedRefs = [];
141
+ const allCallSites = [];
142
+ function ensureFnParams(fnNode) {
143
+ let paramSet = fnParams.get(fnNode);
144
+ if (!paramSet) {
145
+ paramSet = /* @__PURE__ */ new Set();
146
+ for (const p of fnNode.params || []) {
147
+ if (p?.type === "Identifier") {
148
+ paramSet.add(p.name);
149
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
150
+ paramSet.add(p.left.name);
151
+ }
152
+ }
153
+ fnParams.set(fnNode, paramSet);
154
+ }
155
+ return paramSet;
156
+ }
157
+ function recordVarAlias(scopeNode, varName, rhsName) {
158
+ let map = fnVarAliases.get(scopeNode);
159
+ if (!map) {
160
+ map = /* @__PURE__ */ new Map();
161
+ fnVarAliases.set(scopeNode, map);
162
+ }
163
+ map.set(varName, rhsName);
164
+ }
165
+ function resolveAliasTarget(scopeNode, identName) {
166
+ const map = fnVarAliases.get(scopeNode);
167
+ let cur = identName;
168
+ const seen = /* @__PURE__ */ new Set();
169
+ while (map && map.has(cur) && !seen.has(cur)) {
170
+ seen.add(cur);
171
+ cur = map.get(cur);
172
+ }
173
+ return cur;
174
+ }
175
+ function markSerializedFunction(node) {
176
+ serializedFunctionNodes.add(node);
177
+ hotFunctionNodes.add(node);
178
+ directHotNodes.add(node);
179
+ hotExemptNodes.add(node);
180
+ }
181
+ function findEnclosingFunction(ancestors, selfNode) {
182
+ for (let i = ancestors.length - 1; i >= 0; i--) {
183
+ if (ancestors[i] !== selfNode && FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
184
+ return ancestors[i];
185
+ }
186
+ }
187
+ return null;
188
+ }
189
+ function findOutermostModuleFunction(ancestors) {
190
+ const fnList = [];
191
+ for (let i = 0; i < ancestors.length; i++) {
192
+ if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
193
+ fnList.push(ancestors[i]);
194
+ }
195
+ }
196
+ if (fnList.length === 0) return null;
197
+ if (fnList.length === 1) return fnList[0];
198
+ return fnList[1];
199
+ }
200
+ function isZeroArgIifeFactoryFunction(node) {
201
+ if (!node || node.params && node.params.length > 0) return false;
202
+ if (!node.body || node.body.type !== "BlockStatement") return false;
203
+ const stmts = (node.body.body || []).filter(
204
+ (s) => !(s.type === "ExpressionStatement" && s.directive)
205
+ );
206
+ if (stmts.length !== 1 || stmts[0].type !== "ReturnStatement") return false;
207
+ const retArg = stmts[0].argument;
208
+ if (!retArg || retArg.type !== "CallExpression") return false;
209
+ const callee = retArg.callee;
210
+ return callee && (callee.type === "FunctionExpression" || callee.type === "ArrowFunctionExpression") && callee.body?.type === "BlockStatement";
211
+ }
136
212
  function registerFunctionNode(node, ancestors) {
137
213
  const name = getFunctionLabel(node, ancestors);
138
214
  allFunctions.push({ node, name });
139
215
  fnByName.set(name, node);
216
+ const paramSet = /* @__PURE__ */ new Set();
217
+ for (const p of node.params || []) {
218
+ if (p?.type === "Identifier") {
219
+ paramSet.add(p.name);
220
+ } else if (p?.type === "AssignmentPattern" && p.left?.type === "Identifier") {
221
+ paramSet.add(p.left.name);
222
+ }
223
+ }
224
+ fnParams.set(node, paramSet);
140
225
  const enclosingScope = findEnclosingScopeNode(ancestors, node);
141
226
  let localMap = scopeFunctions.get(enclosingScope);
142
227
  if (!localMap) {
@@ -145,6 +230,22 @@ function analyzeAstHotPaths(ast) {
145
230
  }
146
231
  localMap.set(name, node);
147
232
  const parent = ancestors[ancestors.length - 2];
233
+ if (parent?.type === "VariableDeclarator" && parent.id?.type === "Identifier") {
234
+ fnByName.set(parent.id.name, node);
235
+ localMap.set(parent.id.name, node);
236
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "Identifier") {
237
+ fnByName.set(parent.left.name, node);
238
+ localMap.set(parent.left.name, node);
239
+ } else if (parent?.type === "AssignmentExpression" && parent.left?.type === "MemberExpression" && !parent.left.computed && parent.left.property?.type === "Identifier") {
240
+ const propName = parent.left.property.name;
241
+ fnByName.set(propName, node);
242
+ localMap.set(propName, node);
243
+ if (parent.left.object?.type === "Identifier") {
244
+ const qual = `${parent.left.object.name}.${propName}`;
245
+ fnByName.set(qual, node);
246
+ localMap.set(qual, node);
247
+ }
248
+ }
148
249
  const grandParent = ancestors[ancestors.length - 3];
149
250
  const greatGrandParent = ancestors[ancestors.length - 4];
150
251
  if (parent?.type === "Property" && parent.key?.type === "Identifier" && grandParent?.type === "ObjectExpression" && greatGrandParent?.type === "VariableDeclarator" && greatGrandParent.id?.type === "Identifier") {
@@ -154,7 +255,7 @@ function analyzeAstHotPaths(ast) {
154
255
  }
155
256
  const chain = [];
156
257
  for (let i = ancestors.length - 1; i >= 0; i--) {
157
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program") {
258
+ if (ancestors[i] !== node && (FUNCTION_NODE_TYPES.has(ancestors[i].type) || ancestors[i].type === "Program")) {
158
259
  chain.push(ancestors[i]);
159
260
  }
160
261
  }
@@ -163,8 +264,13 @@ function analyzeAstHotPaths(ast) {
163
264
  hotFunctionNodes.add(node);
164
265
  directHotNodes.add(node);
165
266
  }
267
+ if (isZeroArgIifeFactoryFunction(node)) {
268
+ markSerializedFunction(node);
269
+ }
166
270
  }
167
271
  function resolveCalleeNode(callerNode, calleeKey) {
272
+ const directLocal = scopeFunctions.get(callerNode)?.get(calleeKey);
273
+ if (directLocal) return directLocal;
168
274
  const chain = fnScopeChain.get(callerNode);
169
275
  if (chain) {
170
276
  for (const scopeNode of chain) {
@@ -175,6 +281,25 @@ function analyzeAstHotPaths(ast) {
175
281
  }
176
282
  return fnByName.get(calleeKey);
177
283
  }
284
+ function queueSerializedTarget(target, enclosingScope) {
285
+ if (!target) return;
286
+ if (FUNCTION_NODE_TYPES.has(target.type)) {
287
+ markSerializedFunction(target);
288
+ } else if (target.type === "Identifier") {
289
+ pendingSerializedRefs.push({ scopeNode: enclosingScope, name: target.name });
290
+ } else if (target.type === "MemberExpression" && !target.computed && target.property?.type === "Identifier") {
291
+ pendingSerializedRefs.push({
292
+ scopeNode: enclosingScope,
293
+ name: target.property.name
294
+ });
295
+ if (target.object?.type === "Identifier") {
296
+ pendingSerializedRefs.push({
297
+ scopeNode: enclosingScope,
298
+ name: `${target.object.name}.${target.property.name}`
299
+ });
300
+ }
301
+ }
302
+ }
178
303
  walk.ancestor(ast, {
179
304
  FunctionDeclaration(node, _state, ancestors) {
180
305
  registerFunctionNode(node, ancestors);
@@ -185,6 +310,59 @@ function analyzeAstHotPaths(ast) {
185
310
  ArrowFunctionExpression(node, _state, ancestors) {
186
311
  registerFunctionNode(node, ancestors);
187
312
  },
313
+ VariableDeclarator(node, _state, ancestors) {
314
+ if (node.id?.type === "Identifier" && node.init?.type === "Identifier") {
315
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
316
+ recordVarAlias(scopeNode, node.id.name, node.init.name);
317
+ }
318
+ },
319
+ AssignmentExpression(node, _state, ancestors) {
320
+ const left = node.left;
321
+ if (left?.type === "Identifier" && node.right?.type === "Identifier") {
322
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
323
+ recordVarAlias(scopeNode, left.name, node.right.name);
324
+ }
325
+ const isBareOnMessage = left?.type === "Identifier" && left.name === "onmessage";
326
+ const isSelfOnMessage = left?.type === "MemberExpression" && !left.computed && left.object?.type === "Identifier" && left.object.name === "self" && left.property?.type === "Identifier" && left.property.name === "onmessage";
327
+ if (isBareOnMessage || isSelfOnMessage) {
328
+ const enclosingFn = findEnclosingFunction(ancestors);
329
+ if (enclosingFn) {
330
+ markSerializedFunction(enclosingFn);
331
+ }
332
+ const outerFn = findOutermostModuleFunction(ancestors);
333
+ if (outerFn) {
334
+ markSerializedFunction(outerFn);
335
+ }
336
+ const scopeNode = enclosingFn ?? ast;
337
+ queueSerializedTarget(node.right, scopeNode);
338
+ }
339
+ },
340
+ ObjectExpression(node, _state, ancestors) {
341
+ const props = node.properties || [];
342
+ const propMap = /* @__PURE__ */ new Map();
343
+ for (const p of props) {
344
+ if (p?.type === "Property" && !p.computed) {
345
+ const keyName = p.key?.type === "Identifier" ? p.key.name : p.key?.type === "Literal" ? String(p.key.value) : "";
346
+ if (keyName) {
347
+ propMap.set(keyName, p.value);
348
+ }
349
+ }
350
+ }
351
+ const hasWorkerDescriptorShape = propMap.has("init") && propMap.has("dependencies") || propMap.has("isWorkerModule") || propMap.has("getTransferables") && (propMap.has("init") || propMap.has("dependencies"));
352
+ if (hasWorkerDescriptorShape) {
353
+ const scopeNode = findEnclosingFunction(ancestors) ?? ast;
354
+ for (const key of ["init", "getTransferables", "worker"]) {
355
+ const val = propMap.get(key);
356
+ if (val) queueSerializedTarget(val, scopeNode);
357
+ }
358
+ const deps = propMap.get("dependencies");
359
+ if (deps?.type === "ArrayExpression") {
360
+ for (const el of deps.elements || []) {
361
+ if (el) queueSerializedTarget(el, scopeNode);
362
+ }
363
+ }
364
+ }
365
+ },
188
366
  CallExpression(node, _state, ancestors) {
189
367
  const callee = node.callee;
190
368
  let calleeName = "";
@@ -201,38 +379,76 @@ function analyzeAstHotPaths(ast) {
201
379
  qualifiedCall = memberProp;
202
380
  }
203
381
  }
382
+ const enclosingFn = findEnclosingFunction(ancestors);
383
+ const currentScope = enclosingFn ?? ast;
204
384
  if (qualifiedCall || memberProp) {
205
- for (let i = ancestors.length - 1; i >= 0; i--) {
206
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
207
- let set = callerToCallees.get(ancestors[i]);
208
- if (!set) {
209
- set = /* @__PURE__ */ new Set();
210
- callerToCallees.set(ancestors[i], set);
211
- }
212
- if (qualifiedCall) set.add(qualifiedCall);
213
- if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
214
- set.add(memberProp);
215
- }
216
- break;
385
+ allCallSites.push({
386
+ scopeNode: currentScope,
387
+ calleeKey: qualifiedCall || memberProp,
388
+ args: node.arguments || []
389
+ });
390
+ if (enclosingFn) {
391
+ let set = callerToCallees.get(enclosingFn);
392
+ if (!set) {
393
+ set = /* @__PURE__ */ new Set();
394
+ callerToCallees.set(enclosingFn, set);
395
+ }
396
+ if (qualifiedCall) set.add(qualifiedCall);
397
+ if (memberProp && !ARRAY_HOF_METHODS.has(memberProp)) {
398
+ set.add(memberProp);
217
399
  }
218
400
  }
219
401
  }
402
+ if (memberProp === "toString" && (!node.arguments || node.arguments.length === 0) && callee?.type === "MemberExpression") {
403
+ const targetObj = callee.object;
404
+ queueSerializedTarget(targetObj, currentScope);
405
+ if (targetObj?.type === "Identifier" && enclosingFn) {
406
+ const resolvedName = resolveAliasTarget(enclosingFn, targetObj.name);
407
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
408
+ serializerHelperNodes.add(enclosingFn);
409
+ }
410
+ if (resolvedName !== targetObj.name) {
411
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
412
+ }
413
+ }
414
+ }
415
+ if ((memberProp === "call" || memberProp === "apply") && callee?.type === "MemberExpression" && callee.object?.type === "MemberExpression" && callee.object.property?.type === "Identifier" && callee.object.property.name === "toString" && node.arguments?.length > 0) {
416
+ const firstArg = node.arguments[0];
417
+ queueSerializedTarget(firstArg, currentScope);
418
+ if (firstArg?.type === "Identifier" && enclosingFn) {
419
+ const resolvedName = resolveAliasTarget(enclosingFn, firstArg.name);
420
+ if (ensureFnParams(enclosingFn).has(resolvedName)) {
421
+ serializerHelperNodes.add(enclosingFn);
422
+ }
423
+ if (resolvedName !== firstArg.name) {
424
+ queueSerializedTarget({ type: "Identifier", name: resolvedName }, currentScope);
425
+ }
426
+ }
427
+ }
428
+ if (calleeName === "importScripts" || memberProp === "importScripts" && callee?.object?.type === "Identifier" && callee.object.name === "self") {
429
+ if (enclosingFn) markSerializedFunction(enclosingFn);
430
+ const outerFn = findOutermostModuleFunction(ancestors);
431
+ if (outerFn) markSerializedFunction(outerFn);
432
+ }
220
433
  if (ARRAY_HOF_METHODS.has(memberProp) || calleeName === "requestAnimationFrame" || memberProp === "requestAnimationFrame" || calleeName === "cancelAnimationFrame" || memberProp === "cancelAnimationFrame") {
221
434
  for (const arg of node.arguments || []) {
222
435
  if (FUNCTION_NODE_TYPES.has(arg.type)) {
223
436
  hotFunctionNodes.add(arg);
224
437
  directHotNodes.add(arg);
225
438
  hotExemptNodes.add(arg);
439
+ } else if (arg?.type === "Identifier") {
440
+ allCallSites.push({
441
+ scopeNode: currentScope,
442
+ calleeKey: arg.name,
443
+ args: [arg]
444
+ });
226
445
  }
227
446
  }
228
447
  }
229
448
  if (UI_HOOK_OR_RENDER_CALLS.has(calleeName) || UI_HOOK_OR_RENDER_CALLS.has(memberProp)) {
230
- for (let i = ancestors.length - 1; i >= 0; i--) {
231
- if (FUNCTION_NODE_TYPES.has(ancestors[i].type)) {
232
- hotFunctionNodes.add(ancestors[i]);
233
- directHotNodes.add(ancestors[i]);
234
- break;
235
- }
449
+ if (enclosingFn) {
450
+ hotFunctionNodes.add(enclosingFn);
451
+ directHotNodes.add(enclosingFn);
236
452
  }
237
453
  }
238
454
  },
@@ -257,10 +473,41 @@ function analyzeAstHotPaths(ast) {
257
473
  markEnclosingFunctionHot(ancestors, hotFunctionNodes, directHotNodes);
258
474
  }
259
475
  });
476
+ for (const callSite of allCallSites) {
477
+ const resolvedCallee = resolveCalleeNode(callSite.scopeNode, callSite.calleeKey);
478
+ if (resolvedCallee && serializerHelperNodes.has(resolvedCallee)) {
479
+ for (const arg of callSite.args) {
480
+ queueSerializedTarget(arg, callSite.scopeNode);
481
+ }
482
+ }
483
+ }
484
+ for (const ref of pendingSerializedRefs) {
485
+ const targetNode = resolveCalleeNode(ref.scopeNode, ref.name);
486
+ if (targetNode) {
487
+ markSerializedFunction(targetNode);
488
+ }
489
+ }
260
490
  const forwardHotSet = new Set(directHotNodes);
261
491
  let forwardChanged = true;
262
492
  while (forwardChanged) {
263
493
  forwardChanged = false;
494
+ for (const fn of allFunctions) {
495
+ if (forwardHotSet.has(fn.node)) continue;
496
+ const chain = fnScopeChain.get(fn.node);
497
+ if (!chain) continue;
498
+ for (const anc of chain) {
499
+ if (anc !== fn.node && (serializedFunctionNodes.has(anc) || forwardHotSet.has(anc))) {
500
+ forwardHotSet.add(fn.node);
501
+ hotFunctionNodes.add(fn.node);
502
+ hotExemptNodes.add(fn.node);
503
+ if (serializedFunctionNodes.has(anc)) {
504
+ serializedFunctionNodes.add(fn.node);
505
+ }
506
+ forwardChanged = true;
507
+ break;
508
+ }
509
+ }
510
+ }
264
511
  for (const callerNode of Array.from(forwardHotSet)) {
265
512
  const calleeNames = callerToCallees.get(callerNode);
266
513
  if (!calleeNames) continue;
@@ -270,6 +517,9 @@ function analyzeAstHotPaths(ast) {
270
517
  forwardHotSet.add(targetNode);
271
518
  hotFunctionNodes.add(targetNode);
272
519
  hotExemptNodes.add(targetNode);
520
+ if (serializedFunctionNodes.has(callerNode)) {
521
+ serializedFunctionNodes.add(targetNode);
522
+ }
273
523
  forwardChanged = true;
274
524
  }
275
525
  }
@@ -948,8 +1198,13 @@ function createOpaqueGuardStatement(seed, fnIndex) {
948
1198
  right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
949
1199
  },
950
1200
  consequent: {
951
- type: "ReturnStatement",
952
- argument: null
1201
+ type: "ExpressionStatement",
1202
+ expression: {
1203
+ type: "UnaryExpression",
1204
+ operator: "void",
1205
+ prefix: true,
1206
+ argument: { type: "Literal", value: 0, raw: "0" }
1207
+ }
953
1208
  },
954
1209
  alternate: null
955
1210
  };