porffor 0.14.0-a24ac8cf2 → 0.14.0-dcfc43df0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,4 +15,4 @@ export const __Boolean_prototype_toString = (_this: boolean) => {
15
15
  export const __Boolean_prototype_valueOf = (_this: boolean) => {
16
16
  // 1. Return ? ThisBooleanValue(this value).
17
17
  return _this;
18
- };
18
+ };
@@ -0,0 +1,61 @@
1
+ export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
2
+ const ptr: bytestring = '';
3
+
4
+ if (op) { // write
5
+ const size: number = Porffor.wasm.i32.load(ptr, 0, 0);
6
+ Porffor.wasm.i32.store(ptr, size + 1, 0, 0)
7
+
8
+ // reuse set internals to store description
9
+ __Porffor_set_write(ptr, size, value);
10
+ return size;
11
+ } else { // read
12
+ return __Porffor_set_read(ptr, value);
13
+ }
14
+ };
15
+
16
+ export const Symbol = (description: any): Symbol => {
17
+ // 1-based so always truthy as numeric value
18
+ return __Porffor_symbol_descStore(true, description) + 1;
19
+ };
20
+
21
+ // todo: this should be a getter somehow not a method
22
+ export const __Symbol_prototype_description = (_this: Symbol) => {
23
+ const description: bytestring = __Porffor_symbol_descStore(false,
24
+ Porffor.wasm`local.get ${_this}` - 1);
25
+ return description;
26
+ };
27
+
28
+ export const __Symbol_prototype_toString = (_this: Symbol) => {
29
+ let out: bytestring = '';
30
+
31
+ // Symbol(
32
+ Porffor.wasm.i32.store8(out, 83, 0, 4);
33
+ Porffor.wasm.i32.store8(out, 121, 0, 5);
34
+ Porffor.wasm.i32.store8(out, 109, 0, 6);
35
+ Porffor.wasm.i32.store8(out, 98, 0, 7);
36
+ Porffor.wasm.i32.store8(out, 111, 0, 8);
37
+ Porffor.wasm.i32.store8(out, 108, 0, 9);
38
+ Porffor.wasm.i32.store8(out, 40, 0, 10);
39
+
40
+ const description: bytestring = __Porffor_symbol_descStore(false,
41
+ Porffor.wasm`local.get ${_this}` - 1);
42
+
43
+ const descLen: i32 = description.length;
44
+ let outPtr: i32 = Porffor.wasm`local.get ${out}` + 7;
45
+ let descPtr: i32 = Porffor.wasm`local.get ${description}`;
46
+ const descPtrEnd: i32 = descPtr + descLen;
47
+ while (descPtr < descPtrEnd) {
48
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(descPtr++, 0, 4), 0, 4);
49
+ }
50
+
51
+ // )
52
+ Porffor.wasm.i32.store8(Porffor.wasm`local.get ${out}` + descLen, 41, 0, 11);
53
+
54
+ out.length = 8 + descLen;
55
+
56
+ return out;
57
+ };
58
+
59
+ export const __Symbol_prototype_valueOf = (_this: Symbol) => {
60
+ return _this;
61
+ };
@@ -198,7 +198,8 @@ export const BuiltinFuncs = function() {
198
198
  returns: [ valtypeBinary ],
199
199
  wasm: [
200
200
  [ Opcodes.local_get, 0 ]
201
- ]
201
+ ],
202
+ constr: true
202
203
  };
203
204
 
204
205
  // just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
@@ -210,7 +211,8 @@ export const BuiltinFuncs = function() {
210
211
  wasm: [
211
212
  // [ Opcodes.local_get, 0 ]
212
213
  ...number(1)
213
- ]
214
+ ],
215
+ constr: true
214
216
  };
215
217
 
216
218
 
@@ -1901,6 +1901,12 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1901
1901
  const arg = args[i];
1902
1902
  out = out.concat(generate(scope, arg));
1903
1903
 
1904
+ if (i >= paramCount) {
1905
+ // over param count of func, drop arg
1906
+ out.push([ Opcodes.drop ]);
1907
+ continue;
1908
+ }
1909
+
1904
1910
  if (valtypeBinary !== Valtype.i32 && (
1905
1911
  (builtinFuncs[name] && builtinFuncs[name].params[i * (typedParams ? 2 : 1)] === Valtype.i32) ||
1906
1912
  (importedFuncs[name] && name.startsWith('profile'))
@@ -1949,6 +1955,11 @@ const generateNew = (scope, decl, _global, _name) => {
1949
1955
  }, _global, _name);
1950
1956
  }
1951
1957
 
1958
+ if (
1959
+ (builtinFuncs[name] && !builtinFuncs[name].constr) ||
1960
+ (internalConstrs[name] && builtinFuncs[name].notConstr)
1961
+ ) return internalThrow(scope, 'TypeError', `${name} is not a constructor`);
1962
+
1952
1963
  if (!builtinFuncs[name]) return todo(scope, `new statement is not supported yet`); // return todo(scope, `new statement is not supported yet (new ${unhackName(name)})`);
1953
1964
 
1954
1965
  return generateCall(scope, decl, _global, _name);
@@ -2523,6 +2534,7 @@ const generateUnary = (scope, decl) => {
2523
2534
  [TYPES.string]: makeString(scope, 'string', false, '#typeof_result'),
2524
2535
  [TYPES.undefined]: makeString(scope, 'undefined', false, '#typeof_result'),
2525
2536
  [TYPES.function]: makeString(scope, 'function', false, '#typeof_result'),
2537
+ [TYPES.symbol]: makeString(scope, 'symbol', false, '#typeof_result'),
2526
2538
 
2527
2539
  [TYPES.bytestring]: makeString(scope, 'string', false, '#typeof_result'),
2528
2540
 
@@ -119,7 +119,7 @@ export const highlightAsm = asm => asm
119
119
  .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
120
120
  .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
121
121
  .replace(/(i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `\x1B[36m${_}\x1B[0m`)
122
- .replace(/(return_call|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
122
+ .replace(/(return_call|call_indirect|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
123
123
  .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
124
124
  .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
125
125
  .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
@@ -1622,4 +1622,49 @@ export const BuiltinFuncs = function() {
1622
1622
  locals: [],
1623
1623
  localNames: ["_this","_this#type"],
1624
1624
  };
1625
+ this.__Porffor_symbol_descStore = {
1626
+ wasm: (scope, {allocPage,builtin,}) => [...number(allocPage(scope, 'bytestring: __Porffor_symbol_descStore/ptr', 'i8') * pageSize, 124),[33,4],[32,0],[252,3],[4,64],[32,4],[252,2],[40,0,0],[183],[33,5],[32,4],[252,2],[32,5],[68,0,0,0,0,0,0,240,63],[160],[252,2],[54,0,0],[32,4],[65,18],[32,5],[65,0],[32,2],[32,3],[16, builtin('__Porffor_set_write')],[26],[32,5],[65,0],[15],[5],[32,4],[65,18],[32,2],[32,3],[16, builtin('__Porffor_set_read')],[34,6],[15],[11],[68,0,0,0,0,0,0,0,0],[65,3],[15]],
1627
+ params: [124,127,124,127],
1628
+ typedParams: true,
1629
+ returns: [124,127],
1630
+ typedReturns: true,
1631
+ locals: [124,124,127],
1632
+ localNames: ["op","op#type","value","value#type","ptr","size","#last_type"],
1633
+ };
1634
+ this.Symbol = {
1635
+ wasm: (scope, {builtin,}) => [[68,0,0,0,0,0,0,240,63],[65,1],[32,0],[32,1],[16, builtin('__Porffor_symbol_descStore')],[33,2],[68,0,0,0,0,0,0,240,63],[160],[15]],
1636
+ params: [124,127],
1637
+ typedParams: true,
1638
+ returns: [124],
1639
+ returnType: 6,
1640
+ locals: [127],
1641
+ localNames: ["description","description#type","#last_type"],
1642
+ };
1643
+ this.__Symbol_prototype_description = {
1644
+ wasm: (scope, {builtin,}) => [[68,0,0,0,0,0,0,0,0],[65,1],[32,0],[68,0,0,0,0,0,0,240,63],[161],[65,0],[16, builtin('__Porffor_symbol_descStore')],[33,3],[34,2],[65,18],[15]],
1645
+ params: [124,127],
1646
+ typedParams: true,
1647
+ returns: [124,127],
1648
+ typedReturns: true,
1649
+ locals: [124,127],
1650
+ localNames: ["_this","_this#type","description","#last_type"],
1651
+ };
1652
+ this.__Symbol_prototype_toString = {
1653
+ wasm: (scope, {allocPage,builtin,}) => [...number(allocPage(scope, 'bytestring: __Symbol_prototype_toString/out', 'i8') * pageSize, 124),[34,2],[252,2],[65,211,0],[58,0,4],[32,2],[252,2],[65,249,0],[58,0,5],[32,2],[252,2],[65,237,0],[58,0,6],[32,2],[252,2],[65,226,0],[58,0,7],[32,2],[252,2],[65,239,0],[58,0,8],[32,2],[252,2],[65,236,0],[58,0,9],[32,2],[252,2],[65,40],[58,0,10],[68,0,0,0,0,0,0,0,0],[65,1],[32,0],[68,0,0,0,0,0,0,240,63],[161],[65,0],[16, builtin('__Porffor_symbol_descStore')],[33,4],[34,3],[252,3],[40,1,0],[184],[33,5],[32,2],[68,0,0,0,0,0,0,28,64],[160],[33,6],[32,3],[34,7],[32,5],[160],[33,8],[3,64],[32,7],[32,8],[99],[4,64],[32,6],[32,6],[68,0,0,0,0,0,0,240,63],[160],[33,6],[252,2],[32,7],[32,7],[68,0,0,0,0,0,0,240,63],[160],[33,7],[252,2],[45,0,4],[58,0,4],[12,1],[11],[11],[32,2],[32,5],[160],[252,2],[65,41],[58,0,11],[32,2],[252,3],[68,0,0,0,0,0,0,32,64],[32,5],[160],[34,9],[252,3],[54,1,0],[32,2],[65,18],[15]],
1654
+ params: [124,127],
1655
+ typedParams: true,
1656
+ returns: [124,127],
1657
+ typedReturns: true,
1658
+ locals: [124,124,127,124,124,124,124,124],
1659
+ localNames: ["_this","_this#type","out","description","#last_type","descLen","outPtr","descPtr","descPtrEnd","__length_setter_tmp"],
1660
+ };
1661
+ this.__Symbol_prototype_valueOf = {
1662
+ wasm: (scope, {}) => [[32,0],[65,6],[15]],
1663
+ params: [124,127],
1664
+ typedParams: true,
1665
+ returns: [124,127],
1666
+ typedReturns: true,
1667
+ locals: [],
1668
+ localNames: ["_this","_this#type"],
1669
+ };
1625
1670
  };
package/compiler/prefs.js CHANGED
@@ -6,7 +6,7 @@ const obj = new Proxy({}, {
6
6
  // intentionally misses with undefined values cached
7
7
  if (cache[p]) return cache[p];
8
8
 
9
- return cache[p] = (() => {
9
+ const ret = (() => {
10
10
  // fooBar -> foo-bar
11
11
  const name = p[0] === '_' ? p : p.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
12
12
  const prefix = name.length === 1 ? '-' : '--';
@@ -19,6 +19,10 @@ const obj = new Proxy({}, {
19
19
  if (onByDefault.includes(p)) return true;
20
20
  return undefined;
21
21
  })();
22
+
23
+ // do not cache in web demo as args are changed live
24
+ if (!globalThis.document) cache[p] = ret;
25
+ return ret;
22
26
  }
23
27
  });
24
28
 
package/compiler/wrap.js CHANGED
@@ -8,7 +8,7 @@ import Prefs from './prefs.js';
8
8
 
9
9
  const bold = x => `\u001b[1m${x}\u001b[0m`;
10
10
 
11
- const porfToJSValue = (memory, funcs, value, type) => {
11
+ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
12
12
  switch (type) {
13
13
  case TYPES.boolean: return Boolean(value);
14
14
  case TYPES.undefined: return undefined;
@@ -66,12 +66,24 @@ const porfToJSValue = (memory, funcs, value, type) => {
66
66
  // console.log(' memory:', Array.from(new Uint8Array(memory.buffer, offset, 9)).map(x => x.toString(16).padStart(2, '0')).join(' '));
67
67
  // console.log(' read:', { value: v, type: t }, '\n');
68
68
 
69
- out.add(porfToJSValue(memory, funcs, v, t));
69
+ out.add(porfToJSValue({ memory, funcs, pages }, v, t));
70
70
  }
71
71
 
72
72
  return out;
73
73
  }
74
74
 
75
+ case TYPES.symbol: {
76
+ const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
77
+ const offset = descStore + 4 + ((value - 1) * 9);
78
+
79
+ const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
80
+ const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
81
+
82
+ const desc = porfToJSValue({ memory, funcs, pages }, v, t);
83
+
84
+ return Symbol(desc);
85
+ }
86
+
75
87
  default: return value;
76
88
  }
77
89
  };
@@ -218,10 +230,11 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
218
230
  exports[func.name] = function() {
219
231
  try {
220
232
  const ret = exp.apply(this, arguments);
221
-
222
233
  if (ret == null) return undefined;
223
234
 
224
- return porfToJSValue(memory, funcs, ret[0], ret[1]);
235
+ if (Prefs.rawValue) return { value: ret[0], type: ret[1] };
236
+
237
+ return porfToJSValue({ memory, funcs, pages }, ret[0], ret[1]);
225
238
  } catch (e) {
226
239
  if (e.is && e.is(exceptTag)) {
227
240
  const exceptId = e.getArg(exceptTag, 0);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.14.0-a24ac8cf2",
4
+ "version": "0.14.0-dcfc43df0",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {