porffor 0.25.8 → 0.25.10

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.
@@ -97,13 +97,16 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
97
97
 
98
98
  // fix call indexes for non-imports
99
99
  // also fix call_indirect types
100
+ // also encode call indexes
100
101
  for (const f of funcs) {
101
102
  f.originalIndex = f.index;
102
103
  f.index -= importDelta;
103
104
 
104
105
  for (const inst of f.wasm) {
105
106
  if ((inst[0] === Opcodes.call || inst[0] === Opcodes.return_call) && inst[1] >= importedFuncs.length) {
106
- inst[1] -= importDelta;
107
+ const idx = inst[1] - importDelta;
108
+ inst.pop(); // remove idx
109
+ unsignedLEB128_into(idx, inst); // add unsigned leb128 encoded index to inst
107
110
  }
108
111
 
109
112
  if (inst[0] === Opcodes.call_indirect) {
@@ -455,4 +455,88 @@ export const __Porffor_object_isObjectOrSymbol = (arg: any): boolean => {
455
455
  t != Porffor.TYPES.string,
456
456
  t != Porffor.TYPES.bytestring,
457
457
  );
458
+ };
459
+
460
+
461
+ // used for { foo: 5 }
462
+ export const __Porffor_object_expr_init = (_this: object, key: any, value: any): void => {
463
+ let entryPtr: i32 = __Porffor_object_lookup(_this, key);
464
+ if (entryPtr == -1) {
465
+ // add new entry
466
+ // bump size +1
467
+ const size: i32 = Porffor.wasm.i32.load(_this, 0, 0);
468
+ Porffor.wasm.i32.store(_this, size + 1, 0, 0);
469
+
470
+ // entryPtr = current end of object
471
+ entryPtr = Porffor.wasm`local.get ${_this}` + 5 + size * 14;
472
+
473
+ __Porffor_object_writeKey(entryPtr, key);
474
+ }
475
+
476
+ // write new value value (lol)
477
+ Porffor.wasm.f64.store(entryPtr, value, 0, 4);
478
+
479
+ // write new tail (value type + flags)
480
+ // flags = writable, enumerable, configurable, not accessor
481
+ Porffor.wasm.i32.store16(entryPtr,
482
+ 0b1110 + (Porffor.wasm`local.get ${value+1}` << 8),
483
+ 0, 12);
484
+ };
485
+
486
+ // used for { get foo() {} }
487
+ export const __Porffor_object_expr_get = (_this: object, key: any, get: any): void => {
488
+ let entryPtr: i32 = __Porffor_object_lookup(_this, key);
489
+ let set: any = undefined;
490
+ if (entryPtr == -1) {
491
+ // add new entry
492
+ // bump size +1
493
+ const size: i32 = Porffor.wasm.i32.load(_this, 0, 0);
494
+ Porffor.wasm.i32.store(_this, size + 1, 0, 0);
495
+
496
+ // entryPtr = current end of object
497
+ entryPtr = Porffor.wasm`local.get ${_this}` + 5 + size * 14;
498
+
499
+ __Porffor_object_writeKey(entryPtr, key);
500
+ } else {
501
+ // existing entry, keep set (if exists)
502
+ set = __Porffor_object_accessorSet(entryPtr);
503
+ }
504
+
505
+ // write new value value (lol)
506
+ Porffor.wasm.f64.store(entryPtr, __Porffor_object_packAccessor(get, set), 0, 4);
507
+
508
+ // write new tail (value type + flags)
509
+ // flags = writable, enumerable, configurable, accessor
510
+ Porffor.wasm.i32.store16(entryPtr,
511
+ 0b1111 + (Porffor.TYPES.number << 8),
512
+ 0, 12);
513
+ };
514
+
515
+ // used for { set foo(v) {} }
516
+ export const __Porffor_object_expr_set = (_this: object, key: any, set: any): void => {
517
+ let entryPtr: i32 = __Porffor_object_lookup(_this, key);
518
+ let get: any = undefined;
519
+ if (entryPtr == -1) {
520
+ // add new entry
521
+ // bump size +1
522
+ const size: i32 = Porffor.wasm.i32.load(_this, 0, 0);
523
+ Porffor.wasm.i32.store(_this, size + 1, 0, 0);
524
+
525
+ // entryPtr = current end of object
526
+ entryPtr = Porffor.wasm`local.get ${_this}` + 5 + size * 14;
527
+
528
+ __Porffor_object_writeKey(entryPtr, key);
529
+ } else {
530
+ // existing entry, keep set (if exists)
531
+ get = __Porffor_object_accessorGet(entryPtr);
532
+ }
533
+
534
+ // write new value value (lol)
535
+ Porffor.wasm.f64.store(entryPtr, __Porffor_object_packAccessor(get, set), 0, 4);
536
+
537
+ // write new tail (value type + flags)
538
+ // flags = writable, enumerable, configurable, accessor
539
+ Porffor.wasm.i32.store16(entryPtr,
540
+ 0b1111 + (Porffor.TYPES.number << 8),
541
+ 0, 12);
458
542
  };
@@ -5,6 +5,11 @@ export const __ArrayBuffer_isView = (value: any): boolean => {
5
5
  return false;
6
6
  };
7
7
 
8
+ export const __Porffor_arraybuffer_detach = (buffer: any): void => {
9
+ // mark as detached by setting length = "-1"
10
+ Porffor.wasm.i32.store(buffer, 4294967295, 0, 0);
11
+ };
12
+
8
13
  export const ArrayBuffer = function (length: any): ArrayBuffer {
9
14
  // 1. If NewTarget is undefined, throw a TypeError exception.
10
15
  if (!new.target) throw new TypeError("Constructor ArrayBuffer requires 'new'");
@@ -71,14 +76,14 @@ export const __ArrayBuffer_prototype_resizable$get = (_this: ArrayBuffer) => {
71
76
  return false;
72
77
  };
73
78
 
74
- export const __ArrayBuffer_prototype_slice = (_this: ArrayBuffer, start: number, end: any) => {
79
+ export const __ArrayBuffer_prototype_slice = (_this: ArrayBuffer, start: any, end: any) => {
75
80
  if (_this.detached) throw new TypeError('Called ArrayBuffer.prototype.slice on a detached ArrayBuffer');
76
81
 
77
82
  const len: i32 = Porffor.wasm.i32.load(_this, 0, 0);
78
83
  if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
79
84
 
80
- start |= 0;
81
- end |= 0;
85
+ start = ecma262.ToIntegerOrInfinity(start);
86
+ end = ecma262.ToIntegerOrInfinity(end);
82
87
 
83
88
  if (start < 0) {
84
89
  start = len + start;
@@ -155,8 +160,7 @@ i32.to_u
155
160
 
156
161
  memory.copy 0 0`;
157
162
 
158
- // mark as detached by setting length = "-1"
159
- Porffor.wasm.i32.store(_this, 4294967295, 0, 0);
163
+ __Porffor_arraybuffer_detach(_this);
160
164
 
161
165
  return out;
162
166
  };
@@ -169,16 +173,18 @@ export const __ArrayBuffer_prototype_resize = (_this: ArrayBuffer, newLength: an
169
173
  };
170
174
 
171
175
 
172
- export const SharedArrayBuffer = function (length: number): SharedArrayBuffer {
176
+ export const SharedArrayBuffer = function (length: any): SharedArrayBuffer {
177
+ // 1. If NewTarget is undefined, throw a TypeError exception.
173
178
  if (!new.target) throw new TypeError("Constructor SharedArrayBuffer requires 'new'");
174
179
 
175
- if (length < 0) throw new RangeError('Invalid SharedArrayBuffer length (negative)');
176
- if (length > 4294967295) throw new RangeError('Invalid SharedArrayBuffer length (over 32 bit address space)');
180
+ // 2. Let byteLength be ? ToIndex(length).
181
+ const byteLength: number = ecma262.ToIndex(length);
177
182
 
178
- length |= 0;
183
+ if (byteLength < 0) throw new RangeError('Invalid SharedArrayBuffer length (negative)');
184
+ if (byteLength > 4294967295) throw new RangeError('Invalid SharedArrayBuffer length (over 32 bit address space)');
179
185
 
180
- const out: SharedArrayBuffer = Porffor.allocateBytes(length + 4);
181
- Porffor.wasm.i32.store(out, length, 0, 0);
186
+ const out: SharedArrayBuffer = Porffor.allocateBytes(byteLength + 4);
187
+ Porffor.wasm.i32.store(out, byteLength, 0, 0);
182
188
 
183
189
  return out;
184
190
  };
@@ -196,12 +202,12 @@ export const __SharedArrayBuffer_prototype_growable$get = (_this: SharedArrayBuf
196
202
  };
197
203
 
198
204
 
199
- export const __SharedArrayBuffer_prototype_slice = (_this: SharedArrayBuffer, start: number, end: any) => {
205
+ export const __SharedArrayBuffer_prototype_slice = (_this: SharedArrayBuffer, start: any, end: any) => {
200
206
  const len: i32 = Porffor.wasm.i32.load(_this, 0, 0);
201
207
  if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
202
208
 
203
- start |= 0;
204
- end |= 0;
209
+ start = ecma262.ToIntegerOrInfinity(start);
210
+ end = ecma262.ToIntegerOrInfinity(end);
205
211
 
206
212
  if (start < 0) {
207
213
  start = len + start;
@@ -190,7 +190,7 @@ export const BuiltinFuncs = function() {
190
190
  ...number(TYPES.number, Valtype.i32),
191
191
  [ Opcodes.local_get, 1 ],
192
192
  ...number(TYPES.number, Valtype.i32),
193
- [ Opcodes.call, ...builtin('__Math_pow') ],
193
+ [ Opcodes.call, builtin('__Math_pow') ],
194
194
  [ Opcodes.drop ],
195
195
  ]
196
196
  };
@@ -66,7 +66,7 @@ export default function({ builtinFuncs }, Prefs) {
66
66
  ...number(flags, Valtype.i32),
67
67
  ...number(TYPES.number, Valtype.i32),
68
68
 
69
- [ Opcodes.call, ...builtin('__Porffor_object_define') ],
69
+ [ Opcodes.call, builtin('__Porffor_object_define') ],
70
70
  [ Opcodes.drop ],
71
71
  [ Opcodes.drop ]
72
72
  );
@@ -82,7 +82,7 @@ export default function({ builtinFuncs }, Prefs) {
82
82
 
83
83
 
84
84
  this[name] = (scope, { builtin }) => [
85
- [ Opcodes.call, ...builtin('#get_' + name) ],
85
+ [ Opcodes.call, builtin('#get_' + name) ],
86
86
  Opcodes.i32_from_u
87
87
  ];
88
88
  this[name].type = TYPES.object;