porffor 0.35.1 → 0.35.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.
- package/compiler/builtins/annexb_string.js +18 -119
- package/compiler/builtins/object.ts +26 -2
- package/compiler/builtins.js +5 -5
- package/compiler/builtins_precompiled.js +116 -124
- package/compiler/codegen.js +87 -29
- package/package.json +1 -1
- package/runner/index.js +1 -1
- package/runner/repl.js +4 -1
package/compiler/codegen.js
CHANGED
@@ -183,6 +183,9 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
183
183
|
case 'AwaitExpression':
|
184
184
|
return cacheAst(decl, generateAwait(scope, decl));
|
185
185
|
|
186
|
+
case 'TemplateLiteral':
|
187
|
+
return cacheAst(decl, generateTemplate(scope, decl));
|
188
|
+
|
186
189
|
case 'ExportNamedDeclaration':
|
187
190
|
if (!decl.declaration) return todo(scope, 'unsupported export declaration');
|
188
191
|
|
@@ -722,8 +725,8 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
722
725
|
return performLogicOp(scope, op, left, right, leftType, rightType);
|
723
726
|
}
|
724
727
|
|
725
|
-
const knownLeft =
|
726
|
-
const knownRight =
|
728
|
+
const knownLeft = knownTypeWithGuess(scope, leftType);
|
729
|
+
const knownRight = knownTypeWithGuess(scope, rightType);
|
727
730
|
|
728
731
|
const eqOp = ['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(op);
|
729
732
|
const strictOp = op === '===' || op === '!==';
|
@@ -881,30 +884,40 @@ const knownNullish = decl => {
|
|
881
884
|
|
882
885
|
const generateBinaryExp = (scope, decl, _global, _name) => {
|
883
886
|
if (decl.operator === 'instanceof') {
|
884
|
-
//
|
885
|
-
// todo: support dynamic right-hand side
|
886
|
-
|
887
|
-
const out = generate(scope, decl.left);
|
888
|
-
disposeLeftover(out);
|
889
|
-
|
887
|
+
// try hacky version for built-ins first
|
890
888
|
const rightName = decl.right.name;
|
891
|
-
if (
|
892
|
-
|
893
|
-
|
894
|
-
|
889
|
+
if (rightName) {
|
890
|
+
const checkType = TYPES[rightName.toLowerCase()];
|
891
|
+
if (checkType != null && rightName === TYPE_NAMES[checkType]) {
|
892
|
+
const out = generate(scope, decl.left);
|
893
|
+
disposeLeftover(out);
|
894
|
+
|
895
|
+
if ([TYPES.number, TYPES.boolean, TYPES.string].includes(checkType)) {
|
896
|
+
out.push(...number(0));
|
897
|
+
} else {
|
898
|
+
out.push(
|
899
|
+
...getNodeType(scope, decl.left),
|
900
|
+
...number(checkType, Valtype.i32),
|
901
|
+
[ Opcodes.i32_eq ],
|
902
|
+
Opcodes.i32_from_u
|
903
|
+
);
|
904
|
+
}
|
895
905
|
|
896
|
-
|
897
|
-
|
898
|
-
} else {
|
899
|
-
out.push(
|
900
|
-
...getNodeType(scope, decl.left),
|
901
|
-
...number(checkType, Valtype.i32),
|
902
|
-
[ Opcodes.i32_eq ],
|
903
|
-
Opcodes.i32_from_u
|
904
|
-
);
|
906
|
+
return out;
|
907
|
+
}
|
905
908
|
}
|
906
909
|
|
907
|
-
return
|
910
|
+
return generate(scope, {
|
911
|
+
type: 'CallExpression',
|
912
|
+
callee: {
|
913
|
+
type: 'Identifier',
|
914
|
+
name: '__Porffor_object_instanceof'
|
915
|
+
},
|
916
|
+
arguments: [
|
917
|
+
decl.left,
|
918
|
+
decl.right
|
919
|
+
]
|
920
|
+
});
|
908
921
|
}
|
909
922
|
|
910
923
|
if (decl.operator === 'in') {
|
@@ -1213,6 +1226,7 @@ const setLastType = (scope, type = []) => [
|
|
1213
1226
|
];
|
1214
1227
|
|
1215
1228
|
const getNodeType = (scope, node) => {
|
1229
|
+
let guess = null;
|
1216
1230
|
const ret = (() => {
|
1217
1231
|
if (node._type) return node._type;
|
1218
1232
|
if (node.type === 'Literal') {
|
@@ -1338,17 +1352,20 @@ const getNodeType = (scope, node) => {
|
|
1338
1352
|
|
1339
1353
|
const leftType = getNodeType(scope, node.left);
|
1340
1354
|
const rightType = getNodeType(scope, node.right);
|
1341
|
-
const knownLeft =
|
1342
|
-
const knownRight =
|
1355
|
+
const knownLeft = knownTypeWithGuess(scope, leftType);
|
1356
|
+
const knownRight = knownTypeWithGuess(scope, rightType);
|
1343
1357
|
|
1344
1358
|
if ((knownLeft != null || knownRight != null) && !(
|
1345
1359
|
(knownLeft === TYPES.string || knownRight === TYPES.string) ||
|
1346
1360
|
(knownLeft === TYPES.bytestring || knownRight === TYPES.bytestring)
|
1347
1361
|
)) return TYPES.number;
|
1348
1362
|
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1363
|
+
if (knownLeft === TYPES.string || knownRight === TYPES.string)
|
1364
|
+
return TYPES.string;
|
1365
|
+
|
1366
|
+
// guess bytestring, could really be bytestring or string
|
1367
|
+
if (knownLeft === TYPES.bytestring || knownRight === TYPES.bytestring)
|
1368
|
+
guess = TYPES.bytestring;
|
1352
1369
|
|
1353
1370
|
if (scope.locals['#last_type']) return getLastType(scope);
|
1354
1371
|
|
@@ -1426,8 +1443,10 @@ const getNodeType = (scope, node) => {
|
|
1426
1443
|
return TYPES.number;
|
1427
1444
|
})();
|
1428
1445
|
|
1429
|
-
|
1430
|
-
|
1446
|
+
const out = typeof ret === 'number' ? number(ret, Valtype.i32) : ret;
|
1447
|
+
if (guess != null) out.guess = typeof guess === 'number' ? number(guess, Valtype.i32) : guess;
|
1448
|
+
|
1449
|
+
return out;
|
1431
1450
|
};
|
1432
1451
|
|
1433
1452
|
const generateLiteral = (scope, decl, global, name) => {
|
@@ -2557,6 +2576,13 @@ const knownType = (scope, type) => {
|
|
2557
2576
|
|
2558
2577
|
return null;
|
2559
2578
|
};
|
2579
|
+
const knownTypeWithGuess = (scope, type) => {
|
2580
|
+
let known = knownType(scope, type);
|
2581
|
+
if (known != null) return known;
|
2582
|
+
|
2583
|
+
if (type.guess != null) return knownType(scope, type.guess);
|
2584
|
+
return known;
|
2585
|
+
};
|
2560
2586
|
|
2561
2587
|
const brTable = (input, bc, returns) => {
|
2562
2588
|
const out = [];
|
@@ -5663,6 +5689,38 @@ const generateClass = (scope, decl) => {
|
|
5663
5689
|
return out;
|
5664
5690
|
};
|
5665
5691
|
|
5692
|
+
export const generateTemplate = (scope, decl) => {
|
5693
|
+
let current = null;
|
5694
|
+
const append = val => {
|
5695
|
+
// console.log(val);
|
5696
|
+
if (!current) {
|
5697
|
+
current = val;
|
5698
|
+
return;
|
5699
|
+
}
|
5700
|
+
|
5701
|
+
current = {
|
5702
|
+
type: 'BinaryExpression',
|
5703
|
+
operator: '+',
|
5704
|
+
left: current,
|
5705
|
+
right: val
|
5706
|
+
};
|
5707
|
+
};
|
5708
|
+
|
5709
|
+
const { expressions, quasis } = decl;
|
5710
|
+
for (let i = 0; i < quasis.length; i++) {
|
5711
|
+
append({
|
5712
|
+
type: 'Literal',
|
5713
|
+
value: quasis[i].value.cooked
|
5714
|
+
});
|
5715
|
+
|
5716
|
+
if (i < expressions.length) {
|
5717
|
+
append(expressions[i]);
|
5718
|
+
}
|
5719
|
+
}
|
5720
|
+
|
5721
|
+
return generate(scope, current);
|
5722
|
+
};
|
5723
|
+
|
5666
5724
|
globalThis._uniqId = 0;
|
5667
5725
|
const uniqId = () => '_' + globalThis._uniqId++;
|
5668
5726
|
|
package/package.json
CHANGED
package/runner/index.js
CHANGED
package/runner/repl.js
CHANGED
@@ -61,7 +61,10 @@ const memoryToString = mem => {
|
|
61
61
|
out += ` \x1B[34m${name}${' '.repeat(longestName - name.length)} \x1B[90m│\x1B[0m \x1B[36m${type}${' '.repeat(longestType - type.length)} \x1B[90m│\x1B[0m `;
|
62
62
|
}
|
63
63
|
|
64
|
-
|
64
|
+
let j = 0;
|
65
|
+
if (i === 0) j = 16;
|
66
|
+
const end = j + 40;
|
67
|
+
for (; j < end; j++) {
|
65
68
|
const val = buf[i * pageSize + j];
|
66
69
|
// if (val === 0) out += '\x1B[2m';
|
67
70
|
if (val === 0) out += '\x1B[90m';
|