From d991c7c1602cf7e021d6a09cdb23abb4b345a827 Mon Sep 17 00:00:00 2001 From: Oliver Medhurst Date: Sat, 19 Oct 2024 20:54:23 +0100 Subject: [PATCH] compiler: fix functions named main breaking things oops! fixes #204. --- compiler/2c.js | 10 +++++----- compiler/allocator.js | 2 +- compiler/assemble.js | 2 +- compiler/codegen.js | 19 +++++++++---------- compiler/pgo.js | 2 +- compiler/precompile.js | 4 ++-- package.json | 2 +- runner/index.js | 2 +- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/compiler/2c.js b/compiler/2c.js index 372889cb..1f111f10 100644 --- a/compiler/2c.js +++ b/compiler/2c.js @@ -343,7 +343,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => { const typedReturns = f.returnType == null; const shouldInline = false; // f.internal; - if (f.name === 'main') out += `int main(${prependMain.has('argv') ? 'int argc, char* argv[]' : ''}) {\n`; + if (f.name === '#main') out += `int main(${prependMain.has('argv') ? 'int argc, char* argv[]' : ''}) {\n`; else out += `${!typedReturns ? (returns ? CValtype[f.returns[0]] : 'void') : 'struct ReturnValue'} ${shouldInline ? 'inline ' : ''}${sanitize(f.name)}(${f.params.map((x, i) => `${CValtype[x]} ${invLocals[i]}`).join(', ')}) {\n`; if (f.name === '__Porffor_promise_runJobs') { @@ -352,7 +352,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => { return; } - if (f.name === 'main') { + if (f.name === '#main') { out += ' ' + [...prependMain.values()].join('\n '); if (prependMain.size > 0) out += '\n\n'; } @@ -881,7 +881,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`); line(`return ${vals.pop()}`); } - if (f.name === 'main') { + if (f.name === '#main') { out += '\n'; line(`return 0`); } @@ -891,14 +891,14 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`); globalThis.out = globalThis.out + out; }; - cify(funcs.find(x => x.name === 'main')); + cify(funcs.find(x => x.name === '#main')); const rawParams = f => { if (ffiFuncs[f.name]) return ffiFuncs[f.name].parameters; return f.params; }; - prepend.set('func decls', funcs.filter(x => x.name !== 'main' && cified.has(x.name)).map(f => { + prepend.set('func decls', funcs.filter(x => x.name !== '#main' && cified.has(x.name)).map(f => { const returns = f.returns.length > 0; const typedReturns = f.returnType == null; diff --git a/compiler/allocator.js b/compiler/allocator.js index 8168244e..05a1ecd9 100644 --- a/compiler/allocator.js +++ b/compiler/allocator.js @@ -8,7 +8,7 @@ const pagePtr = ind => { export const nameToReason = (scope, name) => { let scopeName = scope.name; - if (globalThis.precompile && scopeName === 'main') scopeName = globalThis.precompile; + if (globalThis.precompile && scopeName === '#main') scopeName = globalThis.precompile; return `${Prefs.scopedPageNames ? (scopeName + '/') : ''}${name}`; }; diff --git a/compiler/assemble.js b/compiler/assemble.js index 1881863f..834a54ec 100644 --- a/compiler/assemble.js +++ b/compiler/assemble.js @@ -226,7 +226,7 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => { ); time('memory section'); - const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, ...unsignedLEB128(x.asmIndex) ]); + const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === '#main' ? 'm' : x.name), ExportDesc.func, ...unsignedLEB128(x.asmIndex) ]); // export memory if used if (usesMemory) exports.unshift([ ...encodeString('$'), ExportDesc.mem, 0x00 ]); diff --git a/compiler/codegen.js b/compiler/codegen.js index 52611147..ff5a6513 100644 --- a/compiler/codegen.js +++ b/compiler/codegen.js @@ -406,7 +406,7 @@ const generateIdent = (scope, decl) => { } if (local?.idx === undefined) { - if (name === 'arguments' && scope.name !== 'main' && !scope.arrow) { + if (name === 'arguments' && scope.name !== '#main' && !scope.arrow) { // todo: not compliant let len = countLength(scope); const names = new Array(len); @@ -1352,7 +1352,7 @@ const getType = (scope, name, failEarly = false) => { return fallback; } - if (name === 'arguments' && scope.name !== 'main' && !scope.arrow) { + if (name === 'arguments' && scope.name !== '#main' && !scope.arrow) { return number(TYPES.array, Valtype.i32); } @@ -3075,14 +3075,13 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => { } } - const topLevel = scope.name === 'main'; - if (typeof pattern === 'string') { pattern = { type: 'Identifier', name: pattern }; } // todo: handle globalThis.foo = ... + const topLevel = scope.name === '#main'; if (pattern.type === 'Identifier') { let out = []; const name = pattern.name; @@ -3314,7 +3313,7 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => { const generateVar = (scope, decl) => { let out = []; - const topLevel = scope.name === 'main'; + const topLevel = scope.name === '#main'; // global variable if in top scope (main) or if internally wanted const global = decl._global ?? (topLevel || decl._bare); @@ -4175,7 +4174,7 @@ const generateForOf = (scope, decl) => { setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true); } else { // todo: verify this is correct - const global = scope.name === 'main' && decl.left.kind === 'var'; + const global = scope.name === '#main' && decl.left.kind === 'var'; setVar = generateVarDstr(scope, decl.left.kind, decl.left?.declarations?.[0]?.id ?? decl.left, { type: 'Identifier', name: tmpName }, undefined, global); } @@ -4537,7 +4536,7 @@ const generateForIn = (scope, decl) => { setVar = generateVarDstr(scope, 'var', decl.left, { type: 'Identifier', name: tmpName }, undefined, true); } else { // todo: verify this is correct - const global = scope.name === 'main' && decl.left.kind === 'var'; + const global = scope.name === '#main' && decl.left.kind === 'var'; setVar = generateVarDstr(scope, decl.left.kind, decl.left?.declarations?.[0]?.id ?? decl.left, { type: 'Identifier', name: tmpName }, undefined, global); } @@ -6245,7 +6244,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => { ensureTag(); } - if (name === 'main') { + if (name === '#main') { func.gotLastType = true; func.export = true; @@ -6358,7 +6357,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => { func.jsLength = jsLength; // force generate for main - if (name === 'main') func.generate(); + if (name === '#main') func.generate(); // force generate all for precompile if (globalThis.precompile) func.generate(); @@ -6537,7 +6536,7 @@ export default program => { const getObjectName = x => x.startsWith('__') && x.slice(2, x.indexOf('_', 2)); objectHackers = ['assert', 'compareArray', 'Test262Error', ...new Set(Object.keys(builtinFuncs).map(getObjectName).concat(Object.keys(builtinVars).map(getObjectName)).filter(x => x))]; - program.id = { name: 'main' }; + program.id = { name: '#main' }; program.body = { type: 'BlockStatement', diff --git a/compiler/pgo.js b/compiler/pgo.js index c8eaa451..98149aeb 100644 --- a/compiler/pgo.js +++ b/compiler/pgo.js @@ -214,7 +214,7 @@ export const run = obj => { log = ''; for (const x of funcs) { // skip pgo opt for main() - if (x.name === 'main') continue; + if (x.name === '#main') continue; const wasmFunc = wasmFuncs.find(y => y.name === x.name); diff --git a/compiler/precompile.js b/compiler/precompile.js index 56516e6b..16bbffea 100644 --- a/compiler/precompile.js +++ b/compiler/precompile.js @@ -70,8 +70,8 @@ const compile = async (file, _funcs) => { return acc; }, {}); - const main = funcs.find(x => x.name === 'main'); - const exports = funcs.filter(x => x.export && x.name !== 'main'); + const main = funcs.find(x => x.name === '#main'); + const exports = funcs.filter(x => x.export && x.name !== '#main'); for (const x of exports) { if (x.data) { x.data = x.data.reduce((acc, x) => { acc[data[x].page] = data[x].bytes; return acc; }, {}); diff --git a/package.json b/package.json index 7612f5d7..1ee9b545 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "porffor", "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js", - "version": "0.49.2", + "version": "0.49.3", "author": "CanadaHonk", "license": "MIT", "scripts": {}, diff --git a/runner/index.js b/runner/index.js index bd775456..0779f970 100644 --- a/runner/index.js +++ b/runner/index.js @@ -1,6 +1,6 @@ #!/usr/bin/env node import fs from 'node:fs'; -globalThis.version = '0.49.2'; +globalThis.version = '0.49.3'; // deno compat if (typeof process === 'undefined' && typeof Deno !== 'undefined') {