porffor 0.40.1 → 0.40.3

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.
@@ -293,21 +293,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
293
293
  }
294
294
  };
295
295
 
296
- const mapName = x => {
297
- if (!x) return x;
298
-
299
- if (x.startsWith('__globalThis_')) {
300
- const key = x.slice('__globalThis_'.length);
301
- // hack: this will not work properly
302
- return key.includes('_') ? ('__' + key) : key;
303
- }
304
-
305
- return x;
306
- };
307
-
308
- const lookupName = (scope, _name) => {
309
- const name = mapName(_name);
310
-
296
+ const lookupName = (scope, name) => {
311
297
  if (Object.hasOwn(scope.locals, name)) return [ scope.locals[name], false ];
312
298
  if (Object.hasOwn(globals, name)) return [ globals[name], true ];
313
299
 
@@ -335,9 +321,8 @@ const internalThrow = (scope, constructor, message, expectsValue = Prefs.alwaysV
335
321
  ];
336
322
 
337
323
  const generateIdent = (scope, decl) => {
338
- const lookup = (rawName, failEarly = false) => {
339
- const name = mapName(rawName);
340
- let local = scope.locals[rawName];
324
+ const lookup = (name, failEarly = false) => {
325
+ let local = scope.locals[name];
341
326
 
342
327
  if (Object.hasOwn(builtinVars, name)) {
343
328
  if (builtinVars[name].floatOnly && valtype[0] === 'i') throw new Error(`Cannot use ${unhackName(name)} with integer valtype`);
@@ -371,9 +356,9 @@ const generateIdent = (scope, decl) => {
371
356
  if (Object.hasOwn(funcIndex, name)) return funcRef(funcByName(name));
372
357
  }
373
358
 
374
- if (local?.idx === undefined && rawName.startsWith('__')) {
359
+ if (local?.idx === undefined && name.startsWith('__')) {
375
360
  // return undefined if unknown key in already known var
376
- let parent = rawName.slice(2).split('_').slice(0, -1).join('_');
361
+ let parent = name.slice(2).split('_').slice(0, -1).join('_');
377
362
  if (parent.includes('_')) parent = '__' + parent;
378
363
 
379
364
  const parentLookup = lookup(parent, true);
@@ -385,7 +370,7 @@ const generateIdent = (scope, decl) => {
385
370
 
386
371
  return [ [ null, () => {
387
372
  // try generating again at the end
388
- return lookup(rawName, true);
373
+ return lookup(name, true);
389
374
  }, 1 ] ];
390
375
  }
391
376
 
@@ -1170,13 +1155,11 @@ const isExistingProtoFunc = name => {
1170
1155
  return false;
1171
1156
  };
1172
1157
 
1173
- const getType = (scope, _name, failEarly = false) => {
1158
+ const getType = (scope, name, failEarly = false) => {
1174
1159
  const fallback = failEarly ? number(TYPES.undefined, Valtype.i32) : [ [ null, () => {
1175
- return getType(scope, _name, true);
1160
+ return getType(scope, name, true);
1176
1161
  }, 1 ] ];
1177
1162
 
1178
- const name = mapName(_name);
1179
-
1180
1163
  if (Object.hasOwn(builtinVars, name)) return number(builtinVars[name].type ?? TYPES.number, Valtype.i32);
1181
1164
 
1182
1165
  if (Object.hasOwn(scope.locals, name)) {
@@ -1212,11 +1195,9 @@ const getType = (scope, _name, failEarly = false) => {
1212
1195
  return fallback;
1213
1196
  };
1214
1197
 
1215
- const setType = (scope, _name, type) => {
1198
+ const setType = (scope, name, type) => {
1216
1199
  typeUsed(scope, knownType(scope, type));
1217
1200
 
1218
- const name = mapName(_name);
1219
-
1220
1201
  const out = typeof type === 'number' ? number(type, Valtype.i32) : type;
1221
1202
 
1222
1203
  if (Object.hasOwn(scope.locals, name)) {
@@ -1749,7 +1730,7 @@ const aliasPrimObjsBC = bc => {
1749
1730
  };
1750
1731
 
1751
1732
  const createThisArg = (scope, decl) => {
1752
- const name = mapName(decl.callee?.name);
1733
+ const name = decl.callee?.name;
1753
1734
  if (decl._new) {
1754
1735
  // if precompiling or builtin func, just make it null as unused
1755
1736
  if (globalThis.precompile || Object.hasOwn(builtinFuncs, name)) return [
@@ -1823,7 +1804,7 @@ const createThisArg = (scope, decl) => {
1823
1804
 
1824
1805
  const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1825
1806
  let out = [];
1826
- let name = mapName(decl.callee.name);
1807
+ let name = decl.callee.name;
1827
1808
 
1828
1809
  // opt: virtualize iifes
1829
1810
  if (isFuncType(decl.callee.type)) {
@@ -1883,8 +1864,8 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1883
1864
  target.name = spl.slice(0, -1).join('_');
1884
1865
 
1885
1866
  if (builtinFuncs['__' + target.name + '_' + protoName]) protoName = null;
1886
- else if (!lookupName(scope, target.name)[0] && !builtinFuncs[target.name]) {
1887
- if (lookupName(scope, '__' + target.name)[0] || builtinFuncs['__' + target.name]) target.name = '__' + target.name;
1867
+ else if (lookupName(scope, target.name)[0] == null && !builtinFuncs[target.name]) {
1868
+ if (lookupName(scope, '__' + target.name)[0] != null || builtinFuncs['__' + target.name]) target.name = '__' + target.name;
1888
1869
  else protoName = null;
1889
1870
  }
1890
1871
  }
@@ -3075,9 +3056,11 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
3075
3056
  pattern = { type: 'Identifier', name: pattern };
3076
3057
  }
3077
3058
 
3059
+ // todo: handle globalThis.foo = ...
3060
+
3078
3061
  if (pattern.type === 'Identifier') {
3079
3062
  let out = [];
3080
- const name = mapName(pattern.name);
3063
+ const name = pattern.name;
3081
3064
 
3082
3065
  if (init && isFuncType(init.type)) {
3083
3066
  // hack for let a = function () { ... }
@@ -3353,6 +3336,19 @@ const getProperty = (decl, forceValueStr = false) => {
3353
3336
  return prop;
3354
3337
  };
3355
3338
 
3339
+ const isIdentAssignable = (scope, name, op = '=') => {
3340
+ // not in strict mode and op is =, so ignore
3341
+ if (!scope.strict && op === '=') return true;
3342
+
3343
+ // local exists
3344
+ if (lookupName(scope, name)[0] != null) return true;
3345
+
3346
+ // function with name exists and is not current function
3347
+ if (hasFuncWithName(name) && scope.name !== name) return true;
3348
+
3349
+ return false;
3350
+ };
3351
+
3356
3352
  // todo: optimize this func for valueUnused
3357
3353
  const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
3358
3354
  const { type, name } = decl.left;
@@ -3678,7 +3674,7 @@ const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
3678
3674
 
3679
3675
  if (local === undefined) {
3680
3676
  // only allow = for this, or if in strict mode always throw
3681
- if (op !== '=' || scope.strict) return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
3677
+ if (!isIdentAssignable(scope, name, op)) return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
3682
3678
 
3683
3679
  if (type != 'Identifier') {
3684
3680
  const tmpName = '#rhs' + uniqId();
@@ -4095,7 +4091,7 @@ const generateForOf = (scope, decl) => {
4095
4091
  // setup local for left
4096
4092
  let setVar;
4097
4093
  if (decl.left.type === 'Identifier') {
4098
- if (scope.strict) return internalThrow(scope, 'ReferenceError', `${decl.left.name} is not defined`);
4094
+ if (!isIdentAssignable(scope, decl.left.name)) return internalThrow(scope, 'ReferenceError', `${decl.left.name} is not defined`);
4099
4095
  setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true);
4100
4096
  } else {
4101
4097
  // todo: verify this is correct
@@ -4453,7 +4449,7 @@ const generateForIn = (scope, decl) => {
4453
4449
 
4454
4450
  let setVar;
4455
4451
  if (decl.left.type === 'Identifier') {
4456
- if (scope.strict) return internalThrow(scope, 'ReferenceError', `${decl.left.name} is not defined`);
4452
+ if (!isIdentAssignable(scope, decl.left.name)) return internalThrow(scope, 'ReferenceError', `${decl.left.name} is not defined`);
4457
4453
  setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true);
4458
4454
  } else {
4459
4455
  // todo: verify this is correct
@@ -5331,6 +5327,8 @@ const generateMember = (scope, decl, _global, _name, _objectWasm = undefined) =>
5331
5327
  let final = [], finalEnd, extraBC = {};
5332
5328
  const name = decl.object.name;
5333
5329
 
5330
+ // todo: handle globalThis.foo
5331
+
5334
5332
  // hack: .name
5335
5333
  if (decl.property.name === 'name' && hasFuncWithName(name) && !scope.noFastFuncMembers) {
5336
5334
  let nameProp = name;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.40.1+5e139c5e1",
4
+ "version": "0.40.3+5f310db4d",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/runner/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from 'node:fs';
3
- globalThis.version = '0.40.1+5e139c5e1';
3
+ globalThis.version = '0.40.3+5f310db4d';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {