Skip to content

Commit

Permalink
builtins/array: impl constructor as ts
Browse files Browse the repository at this point in the history
move to ts from internal constructor hack used previously

test262: 41.52% (+0.04) | πŸ§ͺ 48414 | 🀠 20101 (+21) | ❌ 6460 (-12) | πŸ’€ 15596 (-5) | πŸ—οΈ 64 | πŸ’₯ 301 | ⏰ 27 (-4) | πŸ“ 5865
  • Loading branch information
CanadaHonk committed Aug 28, 2024
1 parent b0fefa2 commit d9a05bc
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 57 deletions.
32 changes: 32 additions & 0 deletions compiler/builtins/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import type {} from './porffor.d.ts';

export const Array = function (...args: any[]): any[] {
const argsLen: number = args.length;
if (argsLen == 0) {
// 0 args, new 0 length array
const out: any[] = Porffor.allocate();
return out;
}

if (argsLen == 1) {
// 1 arg, length (number) or first element (non-number)
const arg: any = args[0];
if (Porffor.rawType(arg) == Porffor.TYPES.number) {
// number so use as length
const n: number = args[0];
if (Porffor.fastOr(
n < 0, // negative
n > 4294967295, // over 2**32 - 1
!Number.isInteger(n) // non-integer/non-finite
)) throw new RangeError('Invalid array length');

const out: any[] = Porffor.allocate();
out.length = arg;
return out;
}

// not number, leave to fallthrough as same as >1
}

// >1 arg, just return args array
return args;
};

export const __Array_isArray = (x: unknown): boolean =>
Porffor.rawType(x) == Porffor.TYPES.array;

Expand Down
2 changes: 0 additions & 2 deletions compiler/builtins/object.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {} from './porffor.d.ts';

export const Object = function (value: any): object {
new.target; // trick compiler into allowing as constructor

if (value == null) {
// if nullish, return new empty object
const obj: object = Porffor.allocate();
Expand Down
47 changes: 27 additions & 20 deletions compiler/builtins_precompiled.js

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6223,38 +6223,6 @@ const generateCode = (scope, decl) => {
};

const internalConstrs = {
Array: {
generate: (scope, decl, global, name) => {
// new Array(i0, i1, ...)
if (decl.arguments.length > 1) return generateArray(scope, {
elements: decl.arguments
}, global, name);

// new Array(n)
const [ out, pointer ] = makeArray(scope, {
rawElements: new Array(0)
}, global, name, true, undefined, true, true);

const arg = decl.arguments[0] ?? DEFAULT_VALUE();

// todo: check in wasm instead of here
const literalValue = arg.value ?? 0;
if (literalValue < 0 || !Number.isFinite(literalValue) || literalValue > 4294967295) return internalThrow(scope, 'RangeError', 'Invalid array length', true);

return [
...out,
...generate(scope, arg, global, name),
Opcodes.i32_to_u,
[ Opcodes.i32_store, Math.log2(ValtypeSize.i32) - 1, 0 ],

...pointer,
Opcodes.i32_from_u
];
},
type: TYPES.array,
length: 1
},

__Array_of: {
// this is not a constructor but best fits internal structure here
generate: (scope, decl, global, name) => {
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.40.3+5f310db4d",
"version": "0.40.4+cc1f25444",
"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.40.3+5f310db4d';
globalThis.version = '0.40.4+cc1f25444';

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

Please sign in to comment.