Skip to content

Commit

Permalink
codegen: support optional calls
Browse files Browse the repository at this point in the history
test262: 55.10% | πŸ§ͺ 50254 | 🀠 27692 | ❌ 7207 | πŸ’€ 14008 (+1) | πŸ—οΈ 32 | πŸ’₯ 176 | ⏰ 140 (-1) | πŸ“ 999
  • Loading branch information
CanadaHonk committed Dec 15, 2024
1 parent 00874bd commit 5ba1f62
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
type: 'CallExpression',
callee: target,
arguments: decl.arguments.slice(1),
optional: decl.optional,
_thisWasm: [
...generate(scope, decl.arguments[0] ?? DEFAULT_VALUE()),
[ Opcodes.local_tee, valTmp ],
Expand Down Expand Up @@ -2269,6 +2270,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {

protoBC[type] = () => generate(scope, {
type: 'CallExpression',
optional: decl.optional,
callee: {
type: 'Identifier',
name: x
Expand Down Expand Up @@ -2370,7 +2372,8 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
...protoBC,

// TODO: error better
default: internalThrow(scope, 'TypeError', `'${protoName}' proto func tried to be called on a type without an impl`, !unusedValue)
default: decl.optional ? withType(scope, [ number(UNDEFINED) ], TYPES.undefined)
: internalThrow(scope, 'TypeError', `'${protoName}' proto func tried to be called on a type without an impl`, !unusedValue)
}, unusedValue ? Blocktype.void : valtypeBinary),
...(unusedValue ? [ number(UNDEFINED) ] : [])
];
Expand All @@ -2392,7 +2395,8 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
...protoBC,

// TODO: error better
default: def
default: decl.optional ? withType(scope, [ number(UNDEFINED) ], TYPES.undefined)
: def
}, valtypeBinary)
];
}
Expand Down Expand Up @@ -2580,7 +2584,8 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
...setLastType(scope)
],

default: internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, Valtype.f64)
default: decl.optional ? withType(scope, [ number(UNDEFINED) ], TYPES.undefined)
: internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, Valtype.f64)
}, Valtype.f64),
...(valtypeBinary === Valtype.i32 && scope.returns[0] !== Valtype.f64 ? [ [ Opcodes.f64_convert_i32_s ] ] : [])
];
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.55.5",
"version": "0.55.6",
"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.55.5';
globalThis.version = '0.55.6';

// 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 5ba1f62

Please sign in to comment.