porffor 0.17.0-b7ba5b83d → 0.17.0-b92c546cf

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/compiler/2c.js CHANGED
@@ -182,7 +182,15 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
182
182
  }, {});
183
183
  const invGlobals = inv(globals, x => x.idx);
184
184
 
185
- const sanitize = str => str.replace(/[^0-9a-zA-Z_]/g, _ => String.fromCharCode(97 + _.charCodeAt(0) % 32));
185
+ const codeToSanitizedStr = code => {
186
+ let out = '';
187
+ while (code > 0) {
188
+ out += String.fromCharCode(97 + code % 26);
189
+ code -= 26;
190
+ }
191
+ return out;
192
+ };
193
+ const sanitize = str => str.replace(/[^0-9a-zA-Z_]/g, _ => codeToSanitizedStr(_.charCodeAt(0)));
186
194
 
187
195
  for (const x in invGlobals) {
188
196
  invGlobals[x] = sanitize(invGlobals[x]);
@@ -273,10 +281,11 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
273
281
  }
274
282
 
275
283
  const returns = f.returns.length > 0;
284
+ const typedReturns = f.returnType == null;
276
285
 
277
286
  const shouldInline = false; // f.internal;
278
287
  if (f.name === 'main') out += `int main(${prependMain.has('argv') ? 'int argc, char* argv[]' : ''}) {\n`;
279
- else out += `${f.internal ? (returns ? 'f64' : 'void') : 'struct ReturnValue'} ${shouldInline ? 'inline ' : ''}${sanitize(f.name)}(${f.params.map((x, i) => `${CValtype[x]} ${invLocals[i]}`).join(', ')}) {\n`;
288
+ else out += `${!typedReturns ? (returns ? CValtype[f.returns[0]] : 'void') : 'struct ReturnValue'} ${shouldInline ? 'inline ' : ''}${sanitize(f.name)}(${f.params.map((x, i) => `${CValtype[x]} ${invLocals[i]}`).join(', ')}) {\n`;
280
289
 
281
290
  if (f.name === 'main') {
282
291
  out += ' ' + [...prependMain.values()].join('\n ');
@@ -418,11 +427,14 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
418
427
  continue;
419
428
 
420
429
  case Opcodes.return:
421
- // line(`return${returns ? ` ${removeBrackets(vals.pop())}` : ''}`);
422
- const b = vals.pop();
423
- const a = vals.pop();
430
+ if (!typedReturns) {
431
+ line(`return${returns ? ` ${removeBrackets(vals.pop())}` : ''}`);
432
+ break;
433
+ }
434
+
435
+ const b = returns ? vals.pop() : -1;
436
+ const a = returns ? vals.pop() : -1;
424
437
  line(`return${returns ? ` (struct ReturnValue){ ${removeBrackets(a)}, ${removeBrackets(b)} }` : ''}`);
425
- // line(`return${returns ? ` (struct ReturnValue){ ${removeBrackets(vals.pop())}, ${removeBrackets(vals.pop())} }` : ''}`);
426
438
  break;
427
439
 
428
440
  case Opcodes.if: {
@@ -549,10 +561,15 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
549
561
 
550
562
  prepend.set('__Porffor_readFile',
551
563
  `i32 __Porffor_readFile(u32 pathPtr, u32 outPtr) {
552
- char* path = _memory + pathPtr + 4;
553
- FILE* fp = fopen(path, "r");
554
- if (fp == NULL) {
555
- return -1;
564
+ FILE* fp;
565
+ if (pathPtr == 0) {
566
+ fp = stdin;
567
+ } else {
568
+ char* path = _memory + pathPtr + 4;
569
+ fp = fopen(path, "r");
570
+ if (fp == NULL) {
571
+ return -1;
572
+ }
556
573
  }
557
574
 
558
575
  u32 read = 0;
@@ -585,7 +602,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
585
602
  for (let j = 0; j < func.params.length; j++) args.unshift(removeBrackets(vals.pop()));
586
603
 
587
604
  if (func.returns.length > 0) {
588
- if (func.internal) {
605
+ if (func.returnType != null) {
589
606
  vals.push(`${sanitize(func.name)}(${args.join(', ')})`);
590
607
  } else {
591
608
  const id = retTmpId++;
@@ -225,6 +225,16 @@ export const __Array_prototype_every = (_this: any[], callbackFn: any) => {
225
225
  return true;
226
226
  };
227
227
 
228
+ export const __Array_prototype_some = (_this: any[], callbackFn: any) => {
229
+ const len: i32 = _this.length;
230
+ let i: i32 = 0;
231
+ while (i < len) {
232
+ if (Boolean(callbackFn(_this[i], i++, _this))) return true;
233
+ }
234
+
235
+ return false;
236
+ };
237
+
228
238
  export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialValue: any) => {
229
239
  let acc: any = initialValue ?? _this[0];
230
240
 
@@ -237,6 +247,18 @@ export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialV
237
247
  return acc;
238
248
  };
239
249
 
250
+ export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, initialValue: any) => {
251
+ const len: i32 = _this.length;
252
+ let acc: any = initialValue ?? _this[len - 1];
253
+
254
+ let i: i32 = len;
255
+ while (i > 0) {
256
+ acc = callbackFn(acc, _this[--i], i, _this);
257
+ }
258
+
259
+ return acc;
260
+ };
261
+
240
262
  export const __Array_prototype_toString = (_this: any[]) => {
241
263
  // todo: this is bytestring only!
242
264
 
@@ -16,7 +16,7 @@ export const __Math_exp = (x: number): number => {
16
16
  return 1 / Math.exp(-x);
17
17
  }
18
18
 
19
- const k: number = Math.floor(x / Math.LN2);
19
+ let k: number = Math.floor(x / Math.LN2);
20
20
  const r: number = x - k * Math.LN2;
21
21
 
22
22
  // Taylor series via Horner's method
@@ -30,7 +30,11 @@ export const __Math_exp = (x: number): number => {
30
30
  i++;
31
31
  }
32
32
 
33
- return sum * (1 << k);
33
+ while (k-- > 0) {
34
+ sum *= 2;
35
+ }
36
+
37
+ return sum;
34
38
  };
35
39
 
36
40
  export const __Math_log2 = (y: number): number => {
@@ -170,15 +170,8 @@ export const Set = () => {
170
170
  export const Set$constructor = (iterable: any): Set => {
171
171
  const out: Set = __Porffor_allocate();
172
172
 
173
- const type: number = Porffor.rawType(iterable);
174
- if (Porffor.fastOr(
175
- type == Porffor.TYPES.array,
176
- type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
177
- type == Porffor.TYPES.set
178
- )) {
179
- for (const x of iterable) {
180
- __Set_prototype_add(out, x);
181
- }
173
+ if (Porffor.rawType(iterable) != Porffor.TYPES.undefined) for (const x of iterable) {
174
+ __Set_prototype_add(out, x);
182
175
  }
183
176
 
184
177
  return out;
@@ -0,0 +1,47 @@
1
+ export default () => {
2
+ let out = '';
3
+
4
+ const constr = name => out += `export const ${name} = () => {
5
+ throw new TypeError("Constructor ${name} requires 'new'");
6
+ };
7
+
8
+ export const ${name}$constructor = (arg: any): ${name} => {
9
+ const out: ${name} = Porffor.allocate();
10
+ let len: i32 = 0;
11
+
12
+ const type: i32 = Porffor.rawType(arg);
13
+ if (Porffor.fastOr(
14
+ type == Porffor.TYPES.array,
15
+ type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
16
+ type == Porffor.TYPES.set
17
+ )) {
18
+ let i: i32 = 0;
19
+ for (const x of arg) {
20
+ out[i++] = x;
21
+ }
22
+ len = i;
23
+ } else if (type == Porffor.TYPES.number) {
24
+ len = arg;
25
+ }
26
+
27
+ out.length = len;
28
+ return out;
29
+ };
30
+
31
+ export const __${name}_prototype_byteLength$get = (_this: ${name}) => {
32
+ return _this.length * ${name}.BYTES_PER_ELEMENT;
33
+ };
34
+ `;
35
+
36
+ constr('Uint8Array');
37
+ constr('Int8Array');
38
+ constr('Uint8ClampedArray');
39
+ constr('Uint16Array');
40
+ constr('Int16Array');
41
+ constr('Uint32Array');
42
+ constr('Int32Array');
43
+ constr('Float32Array');
44
+ constr('Float64Array');
45
+
46
+ return out;
47
+ };
@@ -160,6 +160,17 @@ export const BuiltinVars = function() {
160
160
  this.__performance_timeOrigin = [
161
161
  [ Opcodes.call, importedFuncs.timeOrigin ]
162
162
  ];
163
+
164
+
165
+ this.__Uint8Array_BYTES_PER_ELEMENT = number(1);
166
+ this.__Int8Array_BYTES_PER_ELEMENT = number(1);
167
+ this.__Uint8ClampedArray_BYTES_PER_ELEMENT = number(1);
168
+ this.__Uint16Array_BYTES_PER_ELEMENT = number(2);
169
+ this.__Int16Array_BYTES_PER_ELEMENT = number(2);
170
+ this.__Uint32Array_BYTES_PER_ELEMENT = number(4);
171
+ this.__Int32Array_BYTES_PER_ELEMENT = number(4);
172
+ this.__Float32Array_BYTES_PER_ELEMENT = number(4);
173
+ this.__Float64Array_BYTES_PER_ELEMENT = number(8);
163
174
  };
164
175
 
165
176
  export const BuiltinFuncs = function() {
@@ -184,6 +195,20 @@ export const BuiltinFuncs = function() {
184
195
  ]
185
196
  };
186
197
 
198
+ this['f64_**'] = this['i32_**'] = {
199
+ params: [ valtypeBinary, valtypeBinary ],
200
+ locals: [],
201
+ returns: [ valtypeBinary ],
202
+ returnType: TYPES.number,
203
+ wasm: (scope, { builtin }) => [
204
+ [ Opcodes.local_get, 0 ],
205
+ ...number(TYPES.number, Valtype.i32),
206
+ [ Opcodes.local_get, 1 ],
207
+ ...number(TYPES.number, Valtype.i32),
208
+ [ Opcodes.call, builtin('__Math_pow') ]
209
+ ]
210
+ };
211
+
187
212
  // add bitwise ops by converting operands to i32 first
188
213
  for (const [ char, op ] of [ ['&', Opcodes.i32_and], ['|', Opcodes.i32_or], ['^', Opcodes.i32_xor], ['<<', Opcodes.i32_shl], ['>>', Opcodes.i32_shr_s], ['>>>', Opcodes.i32_shr_u] ]) {
189
214
  this[`f64_${char}`] = {