porffor 0.17.0-cab4904b8 → 0.17.0-cbb73d209

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.
@@ -1,10 +1,8 @@
1
- // todo: support non-bytestring properly
2
- export const String = (value: any): bytestring => {
3
- if (Porffor.rawType(value) == Porffor.TYPES.symbol) return __Symbol_prototype_toString(value);
4
- return __ecma262_ToString(value);
5
- };
1
+ import type {} from './porffor.d.ts';
6
2
 
3
+ // todo: support non-bytestring properly
7
4
  // todo: support constructor/string objects properly
8
- export const String$constructor = (value: any): bytestring => {
5
+ export const String = function (value: any): bytestring {
6
+ if (!new.target && Porffor.rawType(value) == Porffor.TYPES.symbol) return __Symbol_prototype_toString(value);
9
7
  return __ecma262_ToString(value);
10
8
  };
@@ -17,7 +17,8 @@ export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
17
17
 
18
18
  export const Symbol = (description: any): Symbol => {
19
19
  // 1-based so always truthy as numeric value
20
- return __Porffor_symbol_descStore(true, description) + 1;
20
+ const ptr: Symbol = __Porffor_symbol_descStore(true, description) + 1;
21
+ return ptr;
21
22
  };
22
23
 
23
24
  export const __Symbol_prototype_description$get = (_this: Symbol) => {
@@ -0,0 +1,94 @@
1
+ export default async () => {
2
+ let out = '';
3
+
4
+ const arrayCode = (await import('node:fs')).readFileSync(globalThis.precompileCompilerPath + '/builtins/array.ts', 'utf8');
5
+ const typedArrayFuncs = [...arrayCode.matchAll(/\/\/ @porf-typed-array[\s\S]+?^};$/gm)].map(x => x[0]);
6
+
7
+ const constr = name => out += `export const ${name} = function (arg: any): ${name} {
8
+ if (!new.target) throw new TypeError("Constructor ${name} requires 'new'");
9
+
10
+ const out: ${name} = Porffor.allocate();
11
+ let len: i32 = 0;
12
+
13
+ const type: i32 = Porffor.rawType(arg);
14
+ if (Porffor.fastOr(
15
+ type == Porffor.TYPES.array,
16
+ type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
17
+ type == Porffor.TYPES.set
18
+ )) {
19
+ let i: i32 = 0;
20
+ for (const x of arg) {
21
+ out[i++] = x;
22
+ }
23
+ len = i;
24
+ } else if (type == Porffor.TYPES.number) {
25
+ len = arg;
26
+ }
27
+
28
+ out.length = len;
29
+ return out;
30
+ };
31
+
32
+ export const __${name}_prototype_byteLength$get = (_this: ${name}) => {
33
+ return _this.length * ${name}.BYTES_PER_ELEMENT;
34
+ };
35
+
36
+ export const __${name}_prototype_at = (_this: ${name}, index: number) => {
37
+ const len: i32 = _this.length;
38
+ index |= 0;
39
+ if (index < 0) {
40
+ index = len + index;
41
+ if (index < 0) return undefined;
42
+ }
43
+ if (index >= len) return undefined;
44
+
45
+ return _this[index];
46
+ };
47
+
48
+ export const __${name}_prototype_slice = (_this: ${name}, start: number, end: number) => {
49
+ const len: i32 = _this.length;
50
+ if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
51
+
52
+ start |= 0;
53
+ end |= 0;
54
+
55
+ if (start < 0) {
56
+ start = len + start;
57
+ if (start < 0) start = 0;
58
+ }
59
+ if (start > len) start = len;
60
+ if (end < 0) {
61
+ end = len + end;
62
+ if (end < 0) end = 0;
63
+ }
64
+ if (end > len) end = len;
65
+
66
+ let out: ${name} = Porffor.allocate();
67
+
68
+ if (start > end) return out;
69
+
70
+ let i: i32 = start;
71
+ let j: i32 = 0;
72
+ while (i < end) {
73
+ out[j++] = _this[i++];
74
+ }
75
+
76
+ out.length = end - start;
77
+ return out;
78
+ };
79
+
80
+ ${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}
81
+ `;
82
+
83
+ constr('Uint8Array');
84
+ constr('Int8Array');
85
+ constr('Uint8ClampedArray');
86
+ constr('Uint16Array');
87
+ constr('Int16Array');
88
+ constr('Uint32Array');
89
+ constr('Int32Array');
90
+ constr('Float32Array');
91
+ constr('Float64Array');
92
+
93
+ return out;
94
+ };
@@ -1,4 +1,5 @@
1
1
  // general widely used ecma262/spec functions
2
+ import type {} from './porffor.d.ts';
2
3
 
3
4
  // 7.1.5 ToIntegerOrInfinity (argument)
4
5
  // https://tc39.es/ecma262/#sec-tointegerorinfinity
@@ -160,6 +160,16 @@ export const BuiltinVars = function() {
160
160
  this.__performance_timeOrigin = [
161
161
  [ Opcodes.call, importedFuncs.timeOrigin ]
162
162
  ];
163
+
164
+ this.__Uint8Array_BYTES_PER_ELEMENT = number(1);
165
+ this.__Int8Array_BYTES_PER_ELEMENT = number(1);
166
+ this.__Uint8ClampedArray_BYTES_PER_ELEMENT = number(1);
167
+ this.__Uint16Array_BYTES_PER_ELEMENT = number(2);
168
+ this.__Int16Array_BYTES_PER_ELEMENT = number(2);
169
+ this.__Uint32Array_BYTES_PER_ELEMENT = number(4);
170
+ this.__Int32Array_BYTES_PER_ELEMENT = number(4);
171
+ this.__Float32Array_BYTES_PER_ELEMENT = number(4);
172
+ this.__Float64Array_BYTES_PER_ELEMENT = number(8);
163
173
  };
164
174
 
165
175
  export const BuiltinFuncs = function() {
@@ -194,7 +204,8 @@ export const BuiltinFuncs = function() {
194
204
  ...number(TYPES.number, Valtype.i32),
195
205
  [ Opcodes.local_get, 1 ],
196
206
  ...number(TYPES.number, Valtype.i32),
197
- [ Opcodes.call, builtin('__Math_pow') ]
207
+ [ Opcodes.call, builtin('__Math_pow') ],
208
+ [ Opcodes.drop ],
198
209
  ]
199
210
  };
200
211