Skip to content

Commit

Permalink
compiler: fix generators
Browse files Browse the repository at this point in the history
fix a few bad bugs making them broken: incorrect property name for next; return not giving return value; array.from being incorrectly inserted into prototype functions.

test262: 56.45% (+0.87) | 🧪 48625 | 🤠 27448 (+421) | ❌ 6924 (-440) | 💀 12960 (+19) | 🏗️ 21 | 💥 165 | ⏰ 135 | 📝 972
  • Loading branch information
CanadaHonk committed Dec 11, 2024
1 parent ec10df6 commit f060d45
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 373 deletions.
1 change: 1 addition & 0 deletions compiler/builtins/_internal_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export const __Porffor_object_writeKey = (ptr: i32, key: any): void => {
Porffor.wasm.i32.store(ptr, keyEnc, 0, 0);
};

// todo: check prototype for setters
export const __Porffor_object_set = (obj: any, key: any, value: any): any => {
if (Porffor.wasm`local.get ${obj+1}` != Porffor.TYPES.object) {
obj = __Porffor_object_underlying(obj);
Expand Down
20 changes: 18 additions & 2 deletions compiler/builtins/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ export const __Porffor_Generator_yield = (vals: any[], value: any): void => {
vals.length = len + 1;
};

export const __Porffor_Generator_return = (vals: any[], value: any): __Porffor_Generator => {
vals.length = 1;
vals[0] = value;

const gen: __Porffor_Generator = vals;
return gen;
};

export const __Porffor_Generator_prototype_next = (vals: any[]): object => {
const obj: object = {};
obj.next = vals.shift();
obj.value = vals.shift();
obj.done = vals.length == 0;

return obj;
Expand Down Expand Up @@ -45,9 +53,17 @@ export const __Porffor_AsyncGenerator_yield = (vals: any[], value: any): void =>
vals.length = len + 1;
};

export const __Porffor_AsyncGenerator_return = (vals: any[], value: any): __Porffor_AsyncGenerator => {
vals.length = 1;
vals[0] = value;

const gen: __Porffor_AsyncGenerator = vals;
return gen;
};

export const __Porffor_AsyncGenerator_prototype_next = async (vals: any[]): object => {
const obj: object = {};
obj.next = await vals.shift();
obj.value = await vals.shift();
obj.done = vals.length == 0;

return obj;
Expand Down
732 changes: 372 additions & 360 deletions compiler/builtins_precompiled.js

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,8 @@ const generateReturn = (scope, decl) => {
...generate(scope, arg),
...getNodeType(scope, arg),

[ Opcodes.call, includeBuiltin(scope, scope.async ? '__Porffor_AsyncGenerator_prototype_return' : '__Porffor_Generator_prototype_return').index ],
[ Opcodes.drop ],
[ Opcodes.drop ],

// return generator
[ Opcodes.local_get, scope.locals['#generator_out'].idx ],
number(scope.async ? TYPES.__porffor_asyncgenerator : TYPES.__porffor_generator, Valtype.i32),
[ Opcodes.call, includeBuiltin(scope, scope.async ? '__Porffor_AsyncGenerator_return' : '__Porffor_Generator_return').index ],
// returns generator
[ Opcodes.return ]
];
}
Expand Down Expand Up @@ -6553,7 +6548,7 @@ const generateFunc = (scope, decl, forceNoExpr = false) => {
addVarMetadata(func, name, false, typeAnno);

// automatically add throws if unexpected this type to builtins
if (globalThis.precompile && i === 0 && func.name.includes('_prototype_')) {
if (globalThis.precompile && i === 0 && func.name.includes('_prototype_') && !func.name.startsWith('__Porffor_')) {
if (typeAnno.type === TYPES.array) {
// Array.from
wasm.push(
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.2",
"version": "0.55.3",
"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.2';
globalThis.version = '0.55.3';

// 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 f060d45

Please sign in to comment.