porffor 0.19.13 → 0.19.14

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
@@ -628,8 +628,8 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
628
628
  }
629
629
 
630
630
  if (!cified.has(func.name) && func.name !== f.name) {
631
- cify(func);
632
631
  cified.add(func.name);
632
+ cify(func);
633
633
  }
634
634
 
635
635
  let args = [];
@@ -35,7 +35,9 @@ export class StaticAllocator {
35
35
  }
36
36
 
37
37
  alloc({ scope, pages }, name, { itemType }) {
38
- const reason = `${this.allocType(itemType)}: ${Prefs.scopedPageNames ? (scope.name + '/') : ''}${name}`;
38
+ let scopeName = scope.name;
39
+ if (globalThis.precompile && scopeName === 'main') scopeName = globalThis.precompile;
40
+ const reason = `${this.allocType(itemType)}: ${Prefs.scopedPageNames ? (scopeName + '/') : ''}${name}`;
39
41
 
40
42
  if (pages.has(reason)) return number(this.ptr(pages.get(reason).ind), Valtype.i32);
41
43
 
@@ -137,5 +137,16 @@ ${[...s1].map((x, i) => ` Porffor.wasm.i32.store16(outPtr, ${x.charCodeAt(0)},
137
137
  arg('anchor', 'a', 'name');
138
138
  arg('link', 'a', 'href');
139
139
 
140
+ const prototypeAlias = (regular, annex) => `export const __String_prototype_${annex} = (_this: string) => {
141
+ return __String_prototype_${regular}(_this);
142
+ };
143
+ export const __ByteString_prototype_${annex} = (_this: bytestring) => {
144
+ return __ByteString_prototype_${regular}(_this);
145
+ };
146
+ `;
147
+
148
+ prototypeAlias('trimStart', 'trimLeft');
149
+ prototypeAlias('trimEnd', 'trimRight');
150
+
140
151
  return out;
141
152
  };
@@ -62,15 +62,17 @@ export const __Porffor_print = (arg: any, colors: boolean = true) => {
62
62
  return;
63
63
 
64
64
  case Porffor.TYPES.array:
65
- printStatic('[ ');
66
-
67
65
  const arrLen: i32 = arg.length - 1;
68
- for (let i: i32 = 0; i <= arrLen; i++) {
69
- __Porffor_print(arg[i], colors);
70
- if (i != arrLen) printStatic(', ');
66
+ if (arrLen == -1) {
67
+ printStatic('[]');
68
+ } else {
69
+ printStatic('[ ');
70
+ for (let i: i32 = 0; i <= arrLen; i++) {
71
+ __Porffor_print(arg[i], colors);
72
+ if (i != arrLen) printStatic(', ');
73
+ }
74
+ printStatic(' ]');
71
75
  }
72
-
73
- printStatic(' ]');
74
76
  return;
75
77
 
76
78
  case Porffor.TYPES.empty:
@@ -341,16 +343,17 @@ export const __Porffor_consolePrint = (arg: any) => {
341
343
  };
342
344
 
343
345
  export const __console_group = (label: bytestring) => {
344
- if (Porffor.rawType(label) != Porffor.TYPES.undefined) __Porffor_consolePrint(label);
346
+ if (Porffor.rawType(label) != Porffor.TYPES.undefined) {
347
+ __Porffor_consoleIndent();
348
+ __Porffor_consolePrint(label);
349
+ }
345
350
 
346
351
  tabLevel++;
347
352
  };
348
353
 
349
354
  export const __console_groupCollapsed = (label: bytestring) => __console_group(label);
350
355
 
351
- export const __console_groupEnd = (label: bytestring) => {
352
- if (Porffor.rawType(label) != Porffor.TYPES.undefined) __Porffor_consolePrint(label);
353
-
356
+ export const __console_groupEnd = () => {
354
357
  tabLevel--;
355
358
  if (tabLevel < 0) tabLevel = 0;
356
359
  };
@@ -1,30 +1,33 @@
1
1
  import type {} from './porffor.d.ts';
2
2
 
3
- export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
4
- const ptr: bytestring = '';
5
-
6
- if (op) { // write
7
- const size: number = Porffor.wasm.i32.load(ptr, 0, 0);
8
- Porffor.wasm.i32.store(ptr, size + 1, 0, 0)
9
-
10
- // reuse set internals to store description
11
- Porffor.set.write(ptr, size, value);
12
- return size;
13
- } else { // read
14
- return Porffor.set.read(ptr, value);
15
- }
16
- };
3
+ const descStore: any[] = new Array(0);
17
4
 
5
+ // 20.4.1.1 Symbol ([ description ])
6
+ // https://tc39.es/ecma262/#sec-symbol-description
18
7
  export const Symbol = (description: any): Symbol => {
8
+ // 1. If NewTarget is not undefined, throw a TypeError exception.
9
+ // This is an arrow function so happens implicitly
10
+
11
+ // 2. If description is undefined, let descString be undefined.
12
+ let descString: any = undefined;
13
+
14
+ // 3. Else, let descString be ? ToString(description).
15
+ if (Porffor.rawType(description) != Porffor.TYPES.undefined) {
16
+ descString = ecma262.ToString(description);
17
+ }
18
+
19
+ // 4. Return a new Symbol whose [[Description]] is descString.
20
+ let len: i32 = descStore.length;
21
+ descStore[len] = descString;
22
+ descStore.length = ++len;
23
+
19
24
  // 1-based so always truthy as numeric value
20
- const ptr: Symbol = __Porffor_symbol_descStore(true, description) + 1;
21
- return ptr;
25
+ const sym: Symbol = len;
26
+ return sym;
22
27
  };
23
28
 
24
29
  export const __Symbol_prototype_description$get = (_this: Symbol) => {
25
- const description: bytestring =
26
- __Porffor_symbol_descStore(false, Porffor.wasm`local.get ${_this}` - 1);
27
- return description;
30
+ return descStore[Porffor.wasm`local.get ${_this}` - 1];
28
31
  };
29
32
 
30
33
  export const __Symbol_prototype_toString = (_this: Symbol) => {
@@ -39,15 +42,20 @@ export const __Symbol_prototype_toString = (_this: Symbol) => {
39
42
  Porffor.wasm.i32.store8(out, 108, 0, 9);
40
43
  Porffor.wasm.i32.store8(out, 40, 0, 10);
41
44
 
42
- const description: bytestring =
43
- __Porffor_symbol_descStore(false, Porffor.wasm`local.get ${_this}` - 1);
45
+ const description: any = descStore[Porffor.wasm`local.get ${_this}` - 1];
46
+ let descLen: i32 = 0;
47
+ if (description !== undefined) {
48
+ descLen = description.length;
49
+
50
+ print(descLen);
44
51
 
45
- const descLen: i32 = description.length;
46
- let outPtr: i32 = Porffor.wasm`local.get ${out}` + 7;
47
- let descPtr: i32 = Porffor.wasm`local.get ${description}`;
48
- const descPtrEnd: i32 = descPtr + descLen;
49
- while (descPtr < descPtrEnd) {
50
- Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(descPtr++, 0, 4), 0, 4);
52
+ // todo: support regular string
53
+ let outPtr: i32 = Porffor.wasm`local.get ${out}` + 7;
54
+ let descPtr: i32 = Porffor.wasm`local.get ${description}`;
55
+ const descPtrEnd: i32 = descPtr + descLen;
56
+ while (descPtr < descPtrEnd) {
57
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(descPtr++, 0, 4), 0, 4);
58
+ }
51
59
  }
52
60
 
53
61
  // )
@@ -64,7 +72,7 @@ export const __Symbol_prototype_valueOf = (_this: Symbol) => {
64
72
  return _this;
65
73
  };
66
74
 
67
- const forStore = new Map();
75
+ const forStore: Map = new Map();
68
76
  export const __Symbol_for = (key: any): Symbol => {
69
77
  if (forStore.has(key)) return forStore.get(key);
70
78
 
@@ -38,7 +38,9 @@ export const __ecma262_ToString = (argument: unknown): bytestring => {
38
38
  if (type == Porffor.TYPES.symbol) throw new TypeError('Cannot convert a Symbol value to a string');
39
39
 
40
40
  // 3. If argument is undefined, return "undefined".
41
- if (type == Porffor.TYPES.undefined) return out = 'undefined';
41
+ if (Porffor.fastOr(
42
+ type == Porffor.TYPES.undefined,
43
+ type == Porffor.TYPES.empty)) return out = 'undefined';
42
44
 
43
45
  // 4. If argument is null, return "null".
44
46
  if (Porffor.fastAnd(
@@ -261,170 +261,6 @@ export const BuiltinFuncs = function() {
261
261
  constr: true
262
262
  };
263
263
 
264
-
265
- this.__Porffor_print = {
266
- params: [ globalThis.precompile ? Valtype.f64 : valtypeBinary, Valtype.i32 ],
267
- typedParams: true,
268
- locals: [ Valtype.i32, Valtype.i32 ],
269
- returns: [],
270
- wasm: (scope, { typeSwitch, builtin }) => [
271
- ...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
272
- [TYPES.number]: [
273
- [ Opcodes.local_get, 0 ],
274
- [ Opcodes.call, importedFuncs.print ],
275
- ],
276
- [TYPES.boolean]: [
277
- [ Opcodes.local_get, 0 ],
278
- Opcodes.i32_to_u,
279
- [ Opcodes.if, Blocktype.void ],
280
- ...printStaticStr('true'),
281
- [ Opcodes.else ],
282
- ...printStaticStr('false'),
283
- [ Opcodes.end ]
284
- ],
285
- [TYPES.string]: [
286
- // simply print a string :))
287
- // cache input pointer as i32
288
- [ Opcodes.local_get, 0 ],
289
- Opcodes.i32_to_u,
290
- [ Opcodes.local_tee, 2 ],
291
-
292
- // make end pointer
293
- [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
294
- ...number(ValtypeSize.i16, Valtype.i32),
295
- [ Opcodes.i32_mul ],
296
-
297
- [ Opcodes.local_get, 2 ],
298
- [ Opcodes.i32_add ],
299
- [ Opcodes.local_set, 3 ],
300
-
301
- [ Opcodes.loop, Blocktype.void ],
302
-
303
- // print current char
304
- [ Opcodes.local_get, 2 ],
305
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
306
- Opcodes.i32_from_u,
307
- [ Opcodes.call, importedFuncs.printChar ],
308
-
309
- // increment pointer by sizeof i16
310
- [ Opcodes.local_get, 2 ],
311
- ...number(ValtypeSize.i16, Valtype.i32),
312
- [ Opcodes.i32_add ],
313
- [ Opcodes.local_tee, 2 ],
314
-
315
- // if pointer != end pointer, loop
316
- [ Opcodes.local_get, 3 ],
317
- [ Opcodes.i32_ne ],
318
- [ Opcodes.br_if, 0 ],
319
-
320
- [ Opcodes.end ]
321
- ],
322
- [TYPES.bytestring]: [
323
- // simply print a (byte)string :))
324
- // cache input pointer as i32
325
- [ Opcodes.local_get, 0 ],
326
- Opcodes.i32_to_u,
327
- [ Opcodes.local_tee, 2 ],
328
-
329
- // make end pointer
330
- [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
331
- [ Opcodes.local_get, 2 ],
332
- [ Opcodes.i32_add ],
333
- [ Opcodes.local_set, 3 ],
334
-
335
- [ Opcodes.loop, Blocktype.void ],
336
-
337
- // print current char
338
- [ Opcodes.local_get, 2 ],
339
- [ Opcodes.i32_load8_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
340
- Opcodes.i32_from_u,
341
- [ Opcodes.call, importedFuncs.printChar ],
342
-
343
- // increment pointer
344
- [ Opcodes.local_get, 2 ],
345
- [ Opcodes.i32_const, 1 ],
346
- [ Opcodes.i32_add ],
347
- [ Opcodes.local_tee, 2 ],
348
-
349
- // if pointer != end pointer, loop
350
- [ Opcodes.local_get, 3 ],
351
- [ Opcodes.i32_ne ],
352
- [ Opcodes.br_if, 0 ],
353
-
354
- [ Opcodes.end ]
355
- ],
356
- [TYPES.array]: [
357
- ...printStaticStr('[ '),
358
-
359
- // cache input pointer as i32
360
- [ Opcodes.local_get, 0 ],
361
- Opcodes.i32_to_u,
362
- [ Opcodes.local_tee, 2 ],
363
-
364
- // make end pointer
365
- [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
366
- ...number(ValtypeSize[valtype] + 1, Valtype.i32),
367
- [ Opcodes.i32_mul ],
368
-
369
- [ Opcodes.local_get, 2 ],
370
- [ Opcodes.i32_add ],
371
- [ Opcodes.local_set, 3 ],
372
-
373
- [ Opcodes.loop, Blocktype.void ],
374
-
375
- // print current array element
376
- [ Opcodes.local_get, 2 ],
377
- [ Opcodes.load, 0, ValtypeSize.i32 ],
378
-
379
- [ Opcodes.local_get, 2 ],
380
- [ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
381
-
382
- [ Opcodes.call, builtin('__Porffor_print') ],
383
-
384
- // increment pointer by sizeof valtype
385
- [ Opcodes.local_get, 2 ],
386
- ...number(ValtypeSize[valtype] + 1, Valtype.i32),
387
- [ Opcodes.i32_add ],
388
- [ Opcodes.local_tee, 2 ],
389
-
390
- // if pointer != end pointer, print separator and loop
391
- [ Opcodes.local_get, 3 ],
392
- [ Opcodes.i32_ne ],
393
- [ Opcodes.if, Blocktype.void ],
394
- ...printStaticStr(', '),
395
- [ Opcodes.br, 1 ],
396
- [ Opcodes.end ],
397
-
398
- [ Opcodes.end ],
399
-
400
- ...printStaticStr(' ]'),
401
- ],
402
- [TYPES.undefined]: [
403
- ...printStaticStr('undefined')
404
- ],
405
- [TYPES.function]: [
406
- ...printStaticStr('function () {}')
407
- ],
408
- [TYPES.object]: [
409
- [ Opcodes.local_get, 0 ],
410
- Opcodes.i32_to_u,
411
- [ Opcodes.if, Blocktype.void ],
412
- ...printStaticStr('{}'),
413
- [ Opcodes.else ],
414
- ...printStaticStr('null'),
415
- [ Opcodes.end ]
416
- ],
417
- default: [
418
- [ Opcodes.local_get, 0 ],
419
- [ Opcodes.call, importedFuncs.print ],
420
- ]
421
- }, Blocktype.void)
422
- ]
423
- };
424
-
425
- // todo: add more console funcs
426
-
427
-
428
264
  this.isNaN = {
429
265
  floatOnly: true,
430
266
  params: [ valtypeBinary ],