porffor 0.58.14 → 0.58.16

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.
@@ -1880,27 +1880,6 @@ const getNodeType = (scope, node) => {
1880
1880
  const generateLiteral = (scope, decl, global, name) => {
1881
1881
  if (decl.value === null) return [ number(NULL) ];
1882
1882
 
1883
- if (decl.regex) {
1884
- // todo/opt: separate aot compiling regex engine for compile-time known regex (literals, known RegExp args)
1885
- return generate(scope, {
1886
- type: 'CallExpression',
1887
- callee: {
1888
- type: 'Identifier',
1889
- name: 'RegExp'
1890
- },
1891
- arguments: [
1892
- {
1893
- type: 'Literal',
1894
- value: decl.regex.pattern
1895
- },
1896
- {
1897
- type: 'Literal',
1898
- value: decl.regex.flags
1899
- }
1900
- ]
1901
- });
1902
- }
1903
-
1904
1883
  switch (typeof decl.value) {
1905
1884
  case 'number':
1906
1885
  return [ number(decl.value) ];
@@ -1935,6 +1914,27 @@ const generateLiteral = (scope, decl, global, name) => {
1935
1914
  });
1936
1915
  }
1937
1916
 
1917
+ if (decl.regex) {
1918
+ // todo/opt: separate aot compiling regex engine for compile-time known regex (literals, known RegExp args)
1919
+ return generate(scope, {
1920
+ type: 'CallExpression',
1921
+ callee: {
1922
+ type: 'Identifier',
1923
+ name: 'RegExp'
1924
+ },
1925
+ arguments: [
1926
+ {
1927
+ type: 'Literal',
1928
+ value: decl.regex.pattern
1929
+ },
1930
+ {
1931
+ type: 'Literal',
1932
+ value: decl.regex.flags
1933
+ }
1934
+ ]
1935
+ });
1936
+ }
1937
+
1938
1938
  return todo(scope, `cannot generate literal of type ${typeof decl.value}`, true);
1939
1939
  };
1940
1940
 
@@ -4583,6 +4583,7 @@ const generateConditional = (scope, decl) => {
4583
4583
  );
4584
4584
 
4585
4585
  out.push([ Opcodes.end ]);
4586
+ depth.pop();
4586
4587
  inferBranchEnd(scope);
4587
4588
 
4588
4589
  return out;
@@ -5559,7 +5560,11 @@ const compileBytes = (val, itemType) => {
5559
5560
 
5560
5561
  const makeData = (scope, elements, page = null, itemType = 'i8') => {
5561
5562
  // if data for page already exists, abort
5562
- if (page && data.find(x => x.page === page)) return;
5563
+ if (page) {
5564
+ data.existsForPage ??= new Map();
5565
+ if (data.existsForPage.has(page)) return;
5566
+ data.existsForPage.set(page, true);
5567
+ }
5563
5568
 
5564
5569
  const length = elements.length;
5565
5570
 
@@ -5608,22 +5613,21 @@ const byteStringable = str => {
5608
5613
  return true;
5609
5614
  };
5610
5615
 
5611
- const makeString = (scope, str, forceBytestring = undefined) => {
5616
+ const makeString = (scope, str, bytestring = true) => {
5612
5617
  if (str.length === 0) return [ number(0) ];
5613
5618
 
5619
+ if (globalThis.precompile) return [
5620
+ [ 'str', Opcodes.const, str, bytestring ]
5621
+ ];
5622
+
5614
5623
  const elements = new Array(str.length);
5615
- let bytestring = forceBytestring !== false;
5616
5624
  for (let i = 0; i < str.length; i++) {
5617
5625
  const c = str.charCodeAt(i);
5618
5626
  elements[i] = c;
5619
5627
 
5620
- if (bytestring && c > 0xFF) bytestring = false;
5628
+ if (c > 0xFF) bytestring = false;
5621
5629
  }
5622
5630
 
5623
- if (globalThis.precompile) return [
5624
- [ 'str', Opcodes.const, str, forceBytestring ]
5625
- ];
5626
-
5627
5631
  const ptr = allocStr({ scope, pages }, str, bytestring);
5628
5632
  makeData(scope, elements, str, bytestring ? 'i8' : 'i16');
5629
5633
 
@@ -6351,13 +6355,6 @@ const generateClass = (scope, decl) => {
6351
6355
  // always generate class constructor funcs
6352
6356
  func.generate();
6353
6357
 
6354
- let constrInsertIndex = func.wasm.findIndex(x => x.at(-1) === 'super marker');
6355
- if (constrInsertIndex != -1) {
6356
- func.wasm.splice(constrInsertIndex, 1);
6357
- } else {
6358
- constrInsertIndex = 0;
6359
- }
6360
-
6361
6358
  if (decl.superClass) {
6362
6359
  const superTmp = localTmp(scope, '#superclass');
6363
6360
  const superTypeTmp = localTmp(scope, '#superclass#type', Valtype.i32);
@@ -6417,10 +6414,15 @@ const generateClass = (scope, decl) => {
6417
6414
  );
6418
6415
  }
6419
6416
 
6420
- scope.overrideThis = generate(scope, root);
6421
- scope.overrideThisType = TYPES.function;
6417
+ // opt: avoid mass generate calls for many class fields
6418
+ const rootWasm = scope.overrideThis = generate(scope, root);
6419
+ const rootType = scope.overrideThisType = [ [ Opcodes.i32_const, TYPES.function ] ];
6420
+ const protoWasm = generate(scope, proto);
6421
+ const protoType = getNodeType(scope, proto);
6422
+ const thisWasm = generate(func, { type: 'ThisExpression', _noGlobalThis: true });
6423
+ const thisType = getNodeType(func, { type: 'ThisExpression', _noGlobalThis: true });
6422
6424
 
6423
- let constrAdd = [];
6425
+ const batchedNonStaticPropWasm = [];
6424
6426
  for (const x of body) {
6425
6427
  let { type, value, kind, static: _static, computed } = x;
6426
6428
  if (kind === 'constructor') continue;
@@ -6441,13 +6443,11 @@ const generateClass = (scope, decl) => {
6441
6443
  }
6442
6444
 
6443
6445
  const key = getProperty(x, true);
6444
- let object = _static ? root : proto;
6445
-
6446
- let initKind = type === 'MethodDefinition' ? 'method' : 'value';
6447
- if (kind === 'get' || kind === 'set') initKind = kind;
6448
6446
 
6449
- // default value to undefined
6450
- value ??= DEFAULT_VALUE();
6447
+ value ??= {
6448
+ type: 'Identifier',
6449
+ name: 'undefined'
6450
+ };
6451
6451
 
6452
6452
  if (isFuncType(value.type)) {
6453
6453
  let id = value.id;
@@ -6468,51 +6468,53 @@ const generateClass = (scope, decl) => {
6468
6468
 
6469
6469
  if (type === 'PropertyDefinition' && !_static) {
6470
6470
  // define in construction instead
6471
- object = {
6472
- type: 'ThisExpression',
6473
- _noGlobalThis: true
6474
- };
6475
-
6476
- let computedTmp;
6477
6471
  if (computed) {
6478
- // compute now, reference in construction
6479
- computedTmp = allocVar(scope, `#class_computed_prop${uniqId()}`, true, true, false, true);
6472
+ // compute key now, reference in construction
6473
+ const computedTmp = allocVar(scope, `#class_computed_prop${uniqId()}`, true, true, false, true);
6480
6474
 
6481
6475
  out.push(
6482
6476
  ...toPropertyKey(scope, generate(scope, key), getNodeType(scope, key), computed, true),
6483
6477
  [ Opcodes.global_set, computedTmp + 1 ],
6484
6478
  [ Opcodes.global_set, computedTmp ]
6485
6479
  );
6486
- }
6487
6480
 
6488
- const constrWasm = [
6489
- ...generate(func, object),
6490
- Opcodes.i32_to_u,
6491
- ...getNodeType(func, object),
6481
+ batchedNonStaticPropWasm.push(
6482
+ ...thisWasm,
6483
+ Opcodes.i32_to_u,
6484
+ ...thisType,
6492
6485
 
6493
- ...(computed ? [
6494
6486
  [ Opcodes.global_get, computedTmp ],
6495
6487
  [ Opcodes.global_get, computedTmp + 1 ],
6496
- ] : [
6497
- ...generate(func, key),
6488
+
6489
+ ...generate(func, value),
6490
+ ...getNodeType(func, value),
6491
+
6492
+ [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_value`).index ]
6493
+ );
6494
+ } else {
6495
+ batchedNonStaticPropWasm.push(
6496
+ ...thisWasm,
6498
6497
  Opcodes.i32_to_u,
6499
- ...getNodeType(func, key)
6500
- ]),
6498
+ ...thisType,
6501
6499
 
6502
- ...generate(func, value),
6503
- ...(initKind !== 'value' && initKind !== 'method' ? [ Opcodes.i32_to_u ] : []),
6504
- ...getNodeType(func, value),
6500
+ ...generate(func, key),
6501
+ Opcodes.i32_to_u,
6502
+ ...getNodeType(func, key),
6505
6503
 
6506
- [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_${initKind}`).index ]
6507
- ];
6504
+ ...generate(func, value),
6505
+ ...getNodeType(func, value),
6508
6506
 
6509
- func.wasm.splice(constrInsertIndex, 0, ...constrWasm);
6510
- constrInsertIndex += constrWasm.length;
6507
+ [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_value`).index ]
6508
+ );
6509
+ }
6511
6510
  } else {
6511
+ let initKind = type === 'MethodDefinition' ? 'method' : 'value';
6512
+ if (kind === 'get' || kind === 'set') initKind = kind;
6513
+
6512
6514
  out.push(
6513
- ...generate(scope, object),
6515
+ ...(_static ? rootWasm : protoWasm),
6514
6516
  Opcodes.i32_to_u,
6515
- ...getNodeType(scope, object),
6517
+ ...(_static ? rootType : protoType),
6516
6518
 
6517
6519
  ...toPropertyKey(scope, generate(scope, key), getNodeType(scope, key), computed, true),
6518
6520
 
@@ -6525,6 +6527,14 @@ const generateClass = (scope, decl) => {
6525
6527
  }
6526
6528
  }
6527
6529
 
6530
+ const constrInsertIndex = func.wasm.findIndex(x => x.at(-1) === 'super marker');
6531
+ if (constrInsertIndex != -1) {
6532
+ func.wasm.splice(constrInsertIndex, 1);
6533
+ func.wasm.splice(constrInsertIndex, 0, ...batchedNonStaticPropWasm);
6534
+ } else {
6535
+ func.wasm = batchedNonStaticPropWasm.concat(func.wasm);
6536
+ }
6537
+
6528
6538
  delete scope.overrideThis;
6529
6539
  delete scope.overrideThisType;
6530
6540