porffor 0.41.6 → 0.42.0

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.
@@ -121,9 +121,10 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => {
121
121
 
122
122
  const nameSection = Prefs.d ? customSection('name', encodeNames(funcs)) : [];
123
123
 
124
+ const directFuncs = funcs.filter(x => !x.indirect);
124
125
  const tableSection = !funcs.table ? [] : createSection(
125
126
  Section.table,
126
- encodeVector([ [ Reftype.funcref, 0x00, ...unsignedLEB128(funcs.length) ] ])
127
+ encodeVector([ [ Reftype.funcref, 0x00, ...unsignedLEB128(directFuncs.length) ] ])
127
128
  );
128
129
  time('table section');
129
130
 
@@ -132,13 +133,12 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => {
132
133
  encodeVector([ [
133
134
  0x00,
134
135
  Opcodes.i32_const, 0, Opcodes.end,
135
- ...encodeVector(funcs.map(x => unsignedLEB128(x.asmIndex)))
136
+ ...encodeVector(directFuncs.map(x => unsignedLEB128((x.wrapperFunc ?? x).asmIndex)))
136
137
  ] ])
137
138
  );
138
139
  time('element section');
139
140
 
140
141
  if (pages.has('func lut')) {
141
- const offset = pages.get('func lut').ind * pageSize;
142
142
  if (data.addedFuncArgcLut) {
143
143
  // remove existing data
144
144
  data = data.filter(x => x.page !== 'func lut');
@@ -146,21 +146,20 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => {
146
146
 
147
147
  // generate func lut data
148
148
  const bytes = [];
149
- for (let i = 0; i < funcs.length; i++) {
150
- const func = funcs[i];
149
+ for (let i = 0; i < directFuncs.length; i++) {
150
+ const func = directFuncs[i];
151
151
  let name = func.name;
152
152
 
153
- // real argc
154
- let argc = func.params.length;
155
- if (func.constr) argc -= 4;
156
- if (!func.internal || func.typedParams) argc = Math.floor(argc / 2);
157
-
158
- bytes.push(argc % 256, (argc / 256 | 0) % 256);
159
-
160
153
  // userland exposed .length
161
- let length = func.jsLength ?? argc;
162
- // remove _this from internal prototype funcs
163
- if (func.internal && name.includes('_prototype_')) length--;
154
+ let length = func.jsLength;
155
+ if (length == null) {
156
+ length = func.params.length;
157
+ if (func.constr) length -= 4;
158
+ if (!func.internal || func.typedParams) length = Math.floor(length / 2);
159
+
160
+ // remove _this from internal prototype funcs
161
+ if (func.internal && name.includes('_prototype_')) length--;
162
+ }
164
163
 
165
164
  bytes.push(length % 256, (length / 256 | 0) % 256);
166
165
 
@@ -174,9 +173,9 @@ export default (funcs, globals, tags, pages, data, noTreeshake = false) => {
174
173
  // eg: __String_prototype_toLowerCase -> toLowerCase
175
174
  if (name.startsWith('__')) name = name.split('_').pop();
176
175
 
177
- bytes.push(...new Uint8Array(new Int32Array([ name.length ]).buffer));
176
+ bytes.push(...new Uint8Array(new Int32Array([ Math.min(name.length, 48 - 5 - 4) ]).buffer));
178
177
 
179
- for (let i = 0; i < (64 - 5 - 4); i++) {
178
+ for (let i = 0; i < (48 - 3 - 4); i++) {
180
179
  const c = name.charCodeAt(i);
181
180
  bytes.push((c || 0) % 256);
182
181
  }
@@ -280,24 +280,7 @@ i32.const 128
280
280
  return`;
281
281
  }
282
282
 
283
- const funcFlags: i32 = __Porffor_funcLut_flags(get);
284
- if (funcFlags & 0b10) {
285
- // constructor func, add new.target, this args
286
- Porffor.wasm`
287
- f64.const 0
288
- i32.const 0
289
- local.get ${obj}
290
- f64.convert_i32_u
291
- local.get ${obj+1}
292
- local.get ${get}
293
- call_indirect 2 0
294
- return`;
295
- } else {
296
- Porffor.wasm`
297
- local.get ${get}
298
- call_indirect 0 0
299
- return`;
300
- }
283
+ return get.call(obj);
301
284
  }
302
285
 
303
286
  // data descriptor
@@ -364,27 +347,7 @@ export const __Porffor_object_set = (obj: any, key: any, value: any): any => {
364
347
  return value;
365
348
  }
366
349
 
367
- const funcFlags: i32 = __Porffor_funcLut_flags(set);
368
- if (funcFlags & 0b10) {
369
- // constructor func, add new.target, this args
370
- Porffor.wasm`
371
- f64.const 0
372
- i32.const 0
373
- local.get ${obj}
374
- f64.convert_i32_u
375
- i32.const 7
376
- local.get ${value}
377
- local.get ${value+1}
378
- local.get ${set}
379
- call_indirect 3 0`;
380
- } else {
381
- Porffor.wasm`
382
- local.get ${value}
383
- local.get ${value+1}
384
- local.get ${set}
385
- call_indirect 1 0`;
386
- }
387
-
350
+ set.call(obj, value);
388
351
  return value;
389
352
  }
390
353
 
@@ -450,27 +413,7 @@ export const __Porffor_object_setStrict = (obj: any, key: any, value: any): any
450
413
  throw new TypeError('Cannot set property with no setter of object');
451
414
  }
452
415
 
453
- const funcFlags: i32 = __Porffor_funcLut_flags(set);
454
- if (funcFlags & 0b10) {
455
- // constructor func, add new.target, this args
456
- Porffor.wasm`
457
- f64.const 0
458
- i32.const 0
459
- local.get ${obj}
460
- f64.convert_i32_u
461
- i32.const 7
462
- local.get ${value}
463
- local.get ${value+1}
464
- local.get ${set}
465
- call_indirect 3 0`;
466
- } else {
467
- Porffor.wasm`
468
- local.get ${value}
469
- local.get ${value+1}
470
- local.get ${set}
471
- call_indirect 1 0`;
472
- }
473
-
416
+ set.call(obj, value);
474
417
  return value;
475
418
  }
476
419
 
@@ -1002,32 +1002,30 @@ export const BuiltinFuncs = function() {
1002
1002
  ]
1003
1003
  };
1004
1004
 
1005
- this.__Porffor_funcLut_flags = {
1005
+ this.__Porffor_funcLut_length = {
1006
1006
  params: [ Valtype.i32 ],
1007
1007
  returns: [ Valtype.i32 ],
1008
1008
  returnType: TYPES.number,
1009
1009
  wasm: (scope, { allocPage }) => [
1010
1010
  [ Opcodes.local_get, 0 ],
1011
- ...number(64, Valtype.i32),
1011
+ ...number(48, Valtype.i32),
1012
1012
  [ Opcodes.i32_mul ],
1013
- ...number(4, Valtype.i32),
1014
- [ Opcodes.i32_add ],
1015
- [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut')) ]
1013
+ [ Opcodes.i32_load16_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut')) ]
1016
1014
  ],
1017
1015
  table: true
1018
1016
  };
1019
1017
 
1020
- this.__Porffor_funcLut_length = {
1018
+ this.__Porffor_funcLut_flags = {
1021
1019
  params: [ Valtype.i32 ],
1022
1020
  returns: [ Valtype.i32 ],
1023
1021
  returnType: TYPES.number,
1024
1022
  wasm: (scope, { allocPage }) => [
1025
1023
  [ Opcodes.local_get, 0 ],
1026
- ...number(64, Valtype.i32),
1024
+ ...number(48, Valtype.i32),
1027
1025
  [ Opcodes.i32_mul ],
1028
1026
  ...number(2, Valtype.i32),
1029
1027
  [ Opcodes.i32_add ],
1030
- [ Opcodes.i32_load16_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut')) ]
1028
+ [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut')) ]
1031
1029
  ],
1032
1030
  table: true
1033
1031
  };
@@ -1038,9 +1036,9 @@ export const BuiltinFuncs = function() {
1038
1036
  returnType: TYPES.bytestring,
1039
1037
  wasm: (scope, { allocPage }) => [
1040
1038
  [ Opcodes.local_get, 0 ],
1041
- ...number(64, Valtype.i32),
1039
+ ...number(48, Valtype.i32),
1042
1040
  [ Opcodes.i32_mul ],
1043
- ...number(5, Valtype.i32),
1041
+ ...number(3, Valtype.i32),
1044
1042
  [ Opcodes.i32_add ],
1045
1043
  ...number(allocPage(scope, 'func lut'), Valtype.i32),
1046
1044
  [ Opcodes.i32_add ]