porffor 0.17.0-b92c546cf → 0.17.0-c2af76d41

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.
@@ -4,6 +4,7 @@ export const __Array_isArray = (x: unknown): boolean =>
4
4
  // Porffor.wasm`local.get ${x+1}` == Porffor.TYPES.array;
5
5
  Porffor.rawType(x) == Porffor.TYPES.array;
6
6
 
7
+
7
8
  export const __Array_prototype_slice = (_this: any[], start: number, end: number) => {
8
9
  const len: i32 = _this.length;
9
10
  if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
@@ -22,7 +23,7 @@ export const __Array_prototype_slice = (_this: any[], start: number, end: number
22
23
  }
23
24
  if (end > len) end = len;
24
25
 
25
- let out: any[] = [];
26
+ let out: any[] = Porffor.allocate();
26
27
 
27
28
  if (start > end) return out;
28
29
 
@@ -42,10 +43,10 @@ export const __Array_prototype_slice = (_this: any[], start: number, end: number
42
43
  }
43
44
 
44
45
  out.length = end - start;
45
-
46
46
  return out;
47
47
  };
48
48
 
49
+ // @porf-typed-array
49
50
  export const __Array_prototype_indexOf = (_this: any[], searchElement: any, position: number) => {
50
51
  const len: i32 = _this.length;
51
52
  if (position > 0) {
@@ -60,6 +61,7 @@ export const __Array_prototype_indexOf = (_this: any[], searchElement: any, posi
60
61
  return -1;
61
62
  };
62
63
 
64
+ // @porf-typed-array
63
65
  export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any, position: number) => {
64
66
  const len: i32 = _this.length;
65
67
  if (position > 0) {
@@ -74,6 +76,7 @@ export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any,
74
76
  return -1;
75
77
  };
76
78
 
79
+ // @porf-typed-array
77
80
  export const __Array_prototype_includes = (_this: any[], searchElement: any, position: number) => {
78
81
  const len: i32 = _this.length;
79
82
  if (position > 0) {
@@ -88,6 +91,7 @@ export const __Array_prototype_includes = (_this: any[], searchElement: any, pos
88
91
  return false;
89
92
  };
90
93
 
94
+ // @porf-typed-array
91
95
  export const __Array_prototype_with = (_this: any[], index: number, value: any) => {
92
96
  const len: i32 = _this.length;
93
97
  if (index < 0) {
@@ -101,8 +105,7 @@ export const __Array_prototype_with = (_this: any[], index: number, value: any)
101
105
  throw new RangeError('Invalid index');
102
106
  }
103
107
 
104
- // todo: allocator is bad here?
105
- let out: any[] = [];
108
+ let out: any[] = Porffor.allocate();
106
109
 
107
110
  Porffor.clone(_this, out);
108
111
 
@@ -111,6 +114,7 @@ export const __Array_prototype_with = (_this: any[], index: number, value: any)
111
114
  return out;
112
115
  };
113
116
 
117
+ // @porf-typed-array
114
118
  export const __Array_prototype_reverse = (_this: any[]) => {
115
119
  const len: i32 = _this.length;
116
120
 
@@ -126,14 +130,14 @@ export const __Array_prototype_reverse = (_this: any[]) => {
126
130
  return _this;
127
131
  };
128
132
 
129
- // todo: this has memory/allocation bugs so sometimes crashes :(
133
+ // @porf-typed-array
130
134
  export const __Array_prototype_toReversed = (_this: any[]) => {
131
135
  const len: i32 = _this.length;
132
136
 
133
137
  let start: i32 = 0;
134
138
  let end: i32 = len - 1;
135
139
 
136
- let out: any[] = [];
140
+ let out: any[] = Porffor.allocate();
137
141
  out.length = len;
138
142
 
139
143
  while (start < end) {
@@ -144,11 +148,13 @@ export const __Array_prototype_toReversed = (_this: any[]) => {
144
148
  return out;
145
149
  };
146
150
 
151
+ // @porf-typed-array
147
152
  export const __Array_prototype_valueOf = (_this: any[]) => {
148
153
  return _this;
149
154
  };
150
155
 
151
156
 
157
+ // @porf-typed-array
152
158
  export const __Array_prototype_forEach = (_this: any[], callbackFn: any) => {
153
159
  const len: i32 = _this.length;
154
160
  let i: i32 = 0;
@@ -157,22 +163,26 @@ export const __Array_prototype_forEach = (_this: any[], callbackFn: any) => {
157
163
  }
158
164
  };
159
165
 
166
+ // @porf-typed-array
160
167
  export const __Array_prototype_filter = (_this: any[], callbackFn: any) => {
161
- const out: any[] = [];
168
+ const out: any[] = Porffor.allocate();
162
169
 
163
170
  const len: i32 = _this.length;
164
171
  let i: i32 = 0;
172
+ let j: i32 = 0;
165
173
  while (i < len) {
166
174
  const el: any = _this[i];
167
- if (Boolean(callbackFn(el, i++, _this))) out.push(el);
175
+ if (Boolean(callbackFn(el, i++, _this))) out[j++] = el;
168
176
  }
169
177
 
178
+ out.length = j;
170
179
  return out;
171
180
  };
172
181
 
182
+ // @porf-typed-array
173
183
  export const __Array_prototype_map = (_this: any[], callbackFn: any) => {
174
184
  const len: i32 = _this.length;
175
- const out: any[] = [];
185
+ const out: any[] = Porffor.allocate();
176
186
  out.length = len;
177
187
 
178
188
  let i: i32 = 0;
@@ -183,6 +193,7 @@ export const __Array_prototype_map = (_this: any[], callbackFn: any) => {
183
193
  return out;
184
194
  };
185
195
 
196
+ // @porf-typed-array
186
197
  export const __Array_prototype_find = (_this: any[], callbackFn: any) => {
187
198
  const len: i32 = _this.length;
188
199
  let i: i32 = 0;
@@ -192,6 +203,7 @@ export const __Array_prototype_find = (_this: any[], callbackFn: any) => {
192
203
  }
193
204
  };
194
205
 
206
+ // @porf-typed-array
195
207
  export const __Array_prototype_findLast = (_this: any[], callbackFn: any) => {
196
208
  let i: i32 = _this.length;
197
209
  while (i > 0) {
@@ -200,6 +212,7 @@ export const __Array_prototype_findLast = (_this: any[], callbackFn: any) => {
200
212
  }
201
213
  };
202
214
 
215
+ // @porf-typed-array
203
216
  export const __Array_prototype_findIndex = (_this: any[], callbackFn: any) => {
204
217
  const len: i32 = _this.length;
205
218
  let i: i32 = 0;
@@ -208,6 +221,7 @@ export const __Array_prototype_findIndex = (_this: any[], callbackFn: any) => {
208
221
  }
209
222
  };
210
223
 
224
+ // @porf-typed-array
211
225
  export const __Array_prototype_findLastIndex = (_this: any[], callbackFn: any) => {
212
226
  let i: i32 = _this.length;
213
227
  while (i > 0) {
@@ -215,6 +229,7 @@ export const __Array_prototype_findLastIndex = (_this: any[], callbackFn: any) =
215
229
  }
216
230
  };
217
231
 
232
+ // @porf-typed-array
218
233
  export const __Array_prototype_every = (_this: any[], callbackFn: any) => {
219
234
  const len: i32 = _this.length;
220
235
  let i: i32 = 0;
@@ -225,6 +240,7 @@ export const __Array_prototype_every = (_this: any[], callbackFn: any) => {
225
240
  return true;
226
241
  };
227
242
 
243
+ // @porf-typed-array
228
244
  export const __Array_prototype_some = (_this: any[], callbackFn: any) => {
229
245
  const len: i32 = _this.length;
230
246
  let i: i32 = 0;
@@ -235,6 +251,7 @@ export const __Array_prototype_some = (_this: any[], callbackFn: any) => {
235
251
  return false;
236
252
  };
237
253
 
254
+ // @porf-typed-array
238
255
  export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialValue: any) => {
239
256
  let acc: any = initialValue ?? _this[0];
240
257
 
@@ -247,6 +264,7 @@ export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialV
247
264
  return acc;
248
265
  };
249
266
 
267
+ // @porf-typed-array
250
268
  export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, initialValue: any) => {
251
269
  const len: i32 = _this.length;
252
270
  let acc: any = initialValue ?? _this[len - 1];
@@ -259,6 +277,7 @@ export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, ini
259
277
  return acc;
260
278
  };
261
279
 
280
+ // @porf-typed-array
262
281
  export const __Array_prototype_toString = (_this: any[]) => {
263
282
  // todo: this is bytestring only!
264
283
 
@@ -283,6 +302,7 @@ export const __Array_prototype_toString = (_this: any[]) => {
283
302
  return out;
284
303
  };
285
304
 
305
+ // @porf-typed-array
286
306
  export const __Array_prototype_join = (_this: any[], _separator: any) => {
287
307
  // todo: this is bytestring only!
288
308
  // todo/perf: optimize single char separators
@@ -1,6 +1,9 @@
1
- export default () => {
1
+ export default async () => {
2
2
  let out = '';
3
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
+
4
7
  const constr = name => out += `export const ${name} = () => {
5
8
  throw new TypeError("Constructor ${name} requires 'new'");
6
9
  };
@@ -31,6 +34,8 @@ export const ${name}$constructor = (arg: any): ${name} => {
31
34
  export const __${name}_prototype_byteLength$get = (_this: ${name}) => {
32
35
  return _this.length * ${name}.BYTES_PER_ELEMENT;
33
36
  };
37
+
38
+ ${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}
34
39
  `;
35
40
 
36
41
  constr('Uint8Array');
@@ -1026,6 +1026,7 @@ const asmFuncToAsm = (func, scope) => {
1026
1026
  idx = funcIndex[n];
1027
1027
  }
1028
1028
 
1029
+ if (idx == null) throw new Error(`builtin('${n}') failed to find a func (inside ${scope.name})`);
1029
1030
  return idx;
1030
1031
  }
1031
1032
  });
@@ -1064,7 +1065,10 @@ const asmFunc = (name, { wasm, params, locals: localTypes, globals: globalTypes
1064
1065
  funcs.push(func);
1065
1066
  funcIndex[name] = func.index;
1066
1067
 
1067
- if (typeof wasm === 'function') wasm = asmFuncToAsm(wasm, func);
1068
+ if (typeof wasm === 'function') {
1069
+ if (globalThis.precompile) wasm = [];
1070
+ else wasm = asmFuncToAsm(wasm, func);
1071
+ }
1068
1072
 
1069
1073
  let baseGlobalIdx, i = 0;
1070
1074
  for (const type of globalTypes) {