porffor 0.2.0-e62542f → 0.2.0-e69a2a2

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.
package/CONTRIBUTING.md CHANGED
@@ -139,6 +139,7 @@ Here we define a built-in for Porffor. Notably:
139
139
  - We do not use `a.b.c`, instead we use `__a_b_c`
140
140
  - We use a `_this` argument, as `this` does not exist in Porffor yet
141
141
  - We use an arrow function
142
+ - We do not set a return type as prototype methods cannot use them currently or errors can happen.
142
143
 
143
144
  ---
144
145
 
@@ -201,6 +202,7 @@ Store the character code into the `out` pointer variable, and increment it.
201
202
  - You might spot `Porffor.fastOr`/`Porffor.fastAnd`, these are non-short circuiting versions of `||`/`&&`, taking any number of conditions as arguments. You shouldn't don't need to use or worry about these.
202
203
  - **There are ~no objects, you cannot use them/literals.**
203
204
  - Attempt to avoid string/array-heavy code and use more variables instead if possible, easier on memory and CPU/perf.
205
+ - Do not set a return type for prototype methods, it can cause errors/unexpected results.
204
206
 
205
207
  <br>
206
208
 
@@ -0,0 +1,6 @@
1
+ // 20.3.3.3 Boolean.prototype.valueOf ()
2
+ // https://tc39.es/ecma262/#sec-boolean.prototype.valueof
3
+ export const __Boolean_prototype_valueOf = (_this: boolean) => {
4
+ // 1. Return ? ThisBooleanValue(this value).
5
+ return _this;
6
+ };
@@ -2051,8 +2051,8 @@ export const __Date_prototype_toLocaleTimeString = (_this: Date, reserved1: any,
2051
2051
  return __Date_prototype_toTimeString(_this);
2052
2052
  };
2053
2053
 
2054
-
2055
2054
  // 21.4.4.44 Date.prototype.valueOf ()
2055
+ // https://tc39.es/ecma262/#sec-date.prototype.valueof
2056
2056
  export const __Date_prototype_valueOf = (_this: Date) => {
2057
2057
  // 1. Let dateObject be the this value.
2058
2058
  // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
@@ -2060,7 +2060,6 @@ export const __Date_prototype_valueOf = (_this: Date) => {
2060
2060
  return __Porffor_date_read(_this);
2061
2061
  };
2062
2062
 
2063
-
2064
2063
  // 21.4.2.1 Date (...values)
2065
2064
  // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
2066
2065
  export const Date = (): bytestring => {
@@ -1,7 +1,5 @@
1
1
  // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
2
 
3
- import type {} from './porffor';
4
-
5
3
  export const escape = (input: string|bytestring): bytestring => {
6
4
  // we have no byte array yet so use bytestring with 0x00 and 0x01 via escape characters
7
5
  // 0 = should escape, 1 = should not escape
@@ -524,4 +524,11 @@ export const __Number_prototype_toExponential = (_this: number, fractionDigits:
524
524
  out.length = outPtr - Porffor.wasm`local.get ${out}`;
525
525
 
526
526
  return out;
527
- };
527
+ };
528
+
529
+ // 21.1.3.7 Number.prototype.valueOf ()
530
+ // https://tc39.es/ecma262/#sec-number.prototype.valueof
531
+ export const __Number_prototype_valueOf = (_this: number) => {
532
+ // 1. Return ? ThisNumberValue(this value).
533
+ return _this;
534
+ };
@@ -1052,4 +1052,19 @@ export const __String_prototype_trim = (_this: string) => {
1052
1052
  export const __ByteString_prototype_trim = (_this: bytestring) => {
1053
1053
  // todo/perf: optimize and not just reuse
1054
1054
  return __ByteString_prototype_trimStart(__ByteString_prototype_trimEnd(_this));
1055
+ };
1056
+
1057
+
1058
+ // 22.1.3.35 String.prototype.valueOf ()
1059
+ // https://tc39.es/ecma262/#sec-string.prototype.valueof
1060
+ export const __String_prototype_valueOf = (_this: string) => {
1061
+ // 1. Return ? ThisStringValue(this value).
1062
+ return _this;
1063
+ };
1064
+
1065
+ // 22.1.3.35 String.prototype.valueOf ()
1066
+ // https://tc39.es/ecma262/#sec-string.prototype.valueof
1067
+ export const __ByteString_prototype_valueOf = (_this: bytestring) => {
1068
+ // 1. Return ? ThisStringValue(this value).
1069
+ return _this;
1055
1070
  };
@@ -59,6 +59,10 @@ const todo = (scope, msg, expectsValue = undefined) => {
59
59
  };
60
60
 
61
61
  const isFuncType = type => type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression';
62
+ const hasFuncWithName = name => {
63
+ const func = funcs.find(x => x.name === name);
64
+ return !!(func || builtinFuncs[name] || importedFuncs[name] || internalConstrs[name]);
65
+ };
62
66
  const generate = (scope, decl, global = false, name = undefined, valueUnused = false) => {
63
67
  switch (decl.type) {
64
68
  case 'BinaryExpression':
@@ -1177,7 +1181,6 @@ const generateLogicExp = (scope, decl) => {
1177
1181
  // js type: 4 bits
1178
1182
  // internal type: ? bits
1179
1183
  // pointer: 32 bits
1180
-
1181
1184
  // generic
1182
1185
  // 1 23 4 5
1183
1186
  // 0 11111111111 11TTTTIIII??????????PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
@@ -1399,6 +1402,15 @@ const getNodeType = (scope, node) => {
1399
1402
  }
1400
1403
 
1401
1404
  if (node.type === 'MemberExpression') {
1405
+ // hack: if something.name, string type
1406
+ if (node.property.name === 'name') {
1407
+ if (hasFuncWithName(node.object.name)) {
1408
+ return TYPES.bytestring;
1409
+ } else {
1410
+ return TYPES.undefined;
1411
+ }
1412
+ }
1413
+
1402
1414
  // hack: if something.length, number type
1403
1415
  if (node.property.name === 'length') return TYPES.number;
1404
1416
 
@@ -3229,6 +3241,20 @@ export const generateMember = (scope, decl, _global, _name) => {
3229
3241
 
3230
3242
  const aotPointer = Prefs.aotPointerOpt && pointer != null;
3231
3243
 
3244
+ // hack: .name
3245
+ if (decl.property.name === 'name') {
3246
+ if (hasFuncWithName(name)) {
3247
+ let nameProp = name;
3248
+
3249
+ // eg: __String_prototype_toLowerCase -> toLowerCase
3250
+ if (nameProp.startsWith('__')) nameProp = nameProp.split('_').pop();
3251
+
3252
+ return makeString(scope, name, _global, _name, true);
3253
+ } else {
3254
+ return generate(scope, DEFAULT_VALUE);
3255
+ }
3256
+ }
3257
+
3232
3258
  // hack: .length
3233
3259
  if (decl.property.name === 'length') {
3234
3260
  const func = funcs.find(x => x.name === name);
@@ -3238,6 +3264,16 @@ export const generateMember = (scope, decl, _global, _name) => {
3238
3264
  return number(typedParams ? func.params.length / 2 : func.params.length);
3239
3265
  }
3240
3266
 
3267
+ if (builtinFuncs[name + '$constructor']) {
3268
+ const regularFunc = builtinFuncs[name];
3269
+ const regularParams = regularFunc.typedParams ? (regularFunc.params.length / 2) : regularFunc.params.length;
3270
+
3271
+ const constructorFunc = builtinFuncs[name + '$constructor'];
3272
+ const constructorParams = constructorFunc.typedParams ? (constructorFunc.params.length / 2) : constructorFunc.params.length;
3273
+
3274
+ return number(Math.max(regularParams, constructorParams));
3275
+ }
3276
+
3241
3277
  if (builtinFuncs[name]) return number(builtinFuncs[name].typedParams ? (builtinFuncs[name].params.length / 2) : builtinFuncs[name].params.length);
3242
3278
  if (importedFuncs[name]) return number(importedFuncs[name].params);
3243
3279
  if (internalConstrs[name]) return number(internalConstrs[name].length ?? 0);
@@ -3369,8 +3405,8 @@ const objectHack = node => {
3369
3405
 
3370
3406
  if (!objectName) objectName = objectHack(node.object)?.name?.slice?.(2);
3371
3407
 
3372
- // if .length, give up (hack within a hack!)
3373
- if (node.property.name === 'length') {
3408
+ // if .name or .length, give up (hack within a hack!)
3409
+ if (['name', 'length'].includes(node.property.name)) {
3374
3410
  node.object = objectHack(node.object);
3375
3411
  return;
3376
3412
  }
@@ -3654,23 +3690,6 @@ const internalConstrs = {
3654
3690
  }
3655
3691
  };
3656
3692
 
3657
- // const _ = Array.prototype.push;
3658
- // Array.prototype.push = function (a) {
3659
- // const check = arr => {
3660
- // for (const x of arr) {
3661
- // if (x === undefined) {
3662
- // console.trace(arr);
3663
- // process.exit();
3664
- // }
3665
- // if (Array.isArray(x)) check(x);
3666
- // }
3667
- // };
3668
- // if (Array.isArray(a) && !new Error().stack.includes('node:')) check(a);
3669
- // // if (Array.isArray(a)) check(a);
3670
-
3671
- // return _.apply(this, arguments);
3672
- // };
3673
-
3674
3693
  export default program => {
3675
3694
  globals = {};
3676
3695
  globalInd = 0;
@@ -307,6 +307,15 @@ export const BuiltinFuncs = function() {
307
307
  localNames: ["input","input#type","keyStr","keyStrPtr","len","output","__length_setter_tmp","i","j","endPtr","endPtr#type","chr1","chr2","#last_type","chr3","enc1","enc2","enc3","enc4"],
308
308
  data: [{"offset":0,"bytes":[65,0,0,0,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47,61]}],
309
309
  };
310
+ this.__Boolean_prototype_valueOf = {
311
+ wasm: (scope, { allocPage, builtin }) => [[32,0],[65,1],[15]],
312
+ params: [124,127],
313
+ typedParams: true,
314
+ returns: [124,127],
315
+ typedReturns: true,
316
+ locals: [],
317
+ localNames: ["_this","_this#type"],
318
+ };
310
319
  this.__crypto_randomUUID = {
311
320
  wasm: (scope, { allocPage, builtin }) => [...number(allocPage(scope, 'bytestring: __crypto_randomUUID/bytes', 'i8') * pageSize, 127),[34,0],[34,1],[34,2],[65,16],[106],[33,3],[3,64],[32,2],[32,3],[72],[4,64],[32,2],[32,2],[65,1],[106],[33,2],[16, builtin('__Porffor_randomByte')],[58,0,4],[12,1],[11],[11],[32,1],[32,1],[45,0,10],[65,15],[113],[65,192,0],[114],[58,0,10],[32,1],[32,1],[45,0,12],[65,63],[113],[65,128,1],[114],[58,0,12],...number(allocPage(scope, 'bytestring: __crypto_randomUUID/output', 'i8') * pageSize, 127),[34,4],[33,5],[32,1],[33,6],[32,5],[65,8],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,12],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[33,5],[32,4],[15]],
312
321
  params: [],
@@ -1149,6 +1158,15 @@ export const BuiltinFuncs = function() {
1149
1158
  locals: [124,124,127,127,127,124,124,124,124,124,124,124,124,124,124,124],
1150
1159
  localNames: ["_this","_this#type","fractionDigits","fractionDigits#type","out","outPtr","#makearray_pointer_tmp","logictmpi","#last_type","i","digits","l","e","digitsPtr","endPtr","j","intPart","digit","dotPlace","__length_setter_tmp"],
1151
1160
  };
1161
+ this.__Number_prototype_valueOf = {
1162
+ wasm: (scope, { allocPage, builtin }) => [[32,0],[65,0],[15]],
1163
+ params: [124,127],
1164
+ typedParams: true,
1165
+ returns: [124,127],
1166
+ typedReturns: true,
1167
+ locals: [],
1168
+ localNames: ["_this","_this#type"],
1169
+ };
1152
1170
  this.__String_fromCharCode = {
1153
1171
  wasm: (scope, { allocPage, builtin }) => [[32,0],[65,128,2],[72],[4,64],...number(allocPage(scope, 'bytestring: __String_fromCharCode/out', 'i8') * pageSize, 127),[34,2],[32,0],[58,0,4],[32,2],[65,18],[15],[11],[65,0],[34,2],[34,3],[65,1],[54,1,0],[32,3],[65,46],[59,0,4],[32,2],[32,0],[59,0,4],[32,2],[65,2],[15]],
1154
1172
  params: [127,127],
@@ -1431,6 +1449,24 @@ export const BuiltinFuncs = function() {
1431
1449
  locals: [127],
1432
1450
  localNames: ["_this","_this#type","#last_type"],
1433
1451
  };
1452
+ this.__String_prototype_valueOf = {
1453
+ wasm: (scope, { allocPage, builtin }) => [[32,0],[65,2],[15]],
1454
+ params: [127,127],
1455
+ typedParams: true,
1456
+ returns: [127,127],
1457
+ typedReturns: true,
1458
+ locals: [],
1459
+ localNames: ["_this","_this#type"],
1460
+ };
1461
+ this.__ByteString_prototype_valueOf = {
1462
+ wasm: (scope, { allocPage, builtin }) => [[32,0],[65,18],[15]],
1463
+ params: [127,127],
1464
+ typedParams: true,
1465
+ returns: [127,127],
1466
+ typedReturns: true,
1467
+ locals: [],
1468
+ localNames: ["_this","_this#type"],
1469
+ };
1434
1470
  this.__Boolean_prototype_toString = {
1435
1471
  wasm: (scope, { allocPage, builtin }) => [...number(allocPage(scope, 'bytestring: __Boolean_prototype_toString/out', 'i8') * pageSize, 124),[33,2],[32,0],[252,3],[4,64],[32,2],[252,3],[34,3],[65,4],[54,1,0],[32,3],[65,244,0],[58,0,4],[32,3],[65,242,0],[58,0,5],[32,3],[65,245,0],[58,0,6],[32,3],[65,229,0],[58,0,7],[32,3],[184],[33,2],[5],[32,2],[252,3],[34,3],[65,5],[54,1,0],[32,3],[65,230,0],[58,0,4],[32,3],[65,225,0],[58,0,5],[32,3],[65,236,0],[58,0,6],[32,3],[65,243,0],[58,0,7],[32,3],[65,229,0],[58,0,8],[32,3],[184],[33,2],[11],[32,2],[65,18],[15]],
1436
1472
  params: [124,127],
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.2.0-e62542f",
4
+ "version": "0.2.0-e69a2a2",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {