Skip to content

Commit

Permalink
codegen: known value Function hack
Browse files Browse the repository at this point in the history
test262: 50.76% (+0.14) | πŸ§ͺ 48381 | 🀠 24556 (+64) | ❌ 6877 (+30) | πŸ’€ 15397 (-95) | πŸ—οΈ 150 | πŸ’₯ 330 | ⏰ 135 (+1) | πŸ“ 936
  • Loading branch information
CanadaHonk committed Nov 5, 2024
1 parent 016f37b commit 3816b77
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
27 changes: 26 additions & 1 deletion compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {

let parsed;
try {
parsed = parse(code, []);
parsed = parse(code);
} catch (e) {
if (e.name === 'SyntaxError') {
// throw syntax errors of evals at runtime instead
Expand Down Expand Up @@ -2058,6 +2058,31 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
}
}

if (name === 'Function') {
const known = knownValue(scope, decl.arguments[0]);
if (known !== unknownValue) {
// known value literal Function hack
const code = String(known);

let parsed;
try {
parsed = parse(`(function(){${code}})`);
} catch (e) {
if (e.name === 'SyntaxError') {
// throw syntax errors of evals at runtime instead
return internalThrow(scope, 'SyntaxError', e.message, true);
}

throw e;
}

return [
...generate(scope, parsed.body[0].expression),
...setLastType(scope, TYPES.function)
];
}
}

let protoName, target;
// ident.func()
if (!decl._new && name && name.startsWith('__')) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "porffor",
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
"version": "0.50.0",
"version": "0.50.1",
"author": "CanadaHonk",
"license": "MIT",
"scripts": {},
Expand Down
2 changes: 1 addition & 1 deletion runner/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'node:fs';
globalThis.version = '0.50.0';
globalThis.version = '0.50.1';

// deno compat
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion test262/history.json

Large diffs are not rendered by default.

0 comments on commit 3816b77

Please sign in to comment.