porffor 0.58.13 → 0.58.15

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
 
@@ -5559,7 +5559,11 @@ const compileBytes = (val, itemType) => {
5559
5559
 
5560
5560
  const makeData = (scope, elements, page = null, itemType = 'i8') => {
5561
5561
  // if data for page already exists, abort
5562
- if (page && data.find(x => x.page === page)) return;
5562
+ if (page) {
5563
+ data.existsForPage ??= new Map();
5564
+ if (data.existsForPage.has(page)) return;
5565
+ data.existsForPage.set(page, true);
5566
+ }
5563
5567
 
5564
5568
  const length = elements.length;
5565
5569
 
@@ -5608,22 +5612,21 @@ const byteStringable = str => {
5608
5612
  return true;
5609
5613
  };
5610
5614
 
5611
- const makeString = (scope, str, forceBytestring = undefined) => {
5615
+ const makeString = (scope, str, bytestring = true) => {
5612
5616
  if (str.length === 0) return [ number(0) ];
5613
5617
 
5618
+ if (globalThis.precompile) return [
5619
+ [ 'str', Opcodes.const, str, bytestring ]
5620
+ ];
5621
+
5614
5622
  const elements = new Array(str.length);
5615
- let bytestring = forceBytestring !== false;
5616
5623
  for (let i = 0; i < str.length; i++) {
5617
5624
  const c = str.charCodeAt(i);
5618
5625
  elements[i] = c;
5619
5626
 
5620
- if (bytestring && c > 0xFF) bytestring = false;
5627
+ if (c > 0xFF) bytestring = false;
5621
5628
  }
5622
5629
 
5623
- if (globalThis.precompile) return [
5624
- [ 'str', Opcodes.const, str, forceBytestring ]
5625
- ];
5626
-
5627
5630
  const ptr = allocStr({ scope, pages }, str, bytestring);
5628
5631
  makeData(scope, elements, str, bytestring ? 'i8' : 'i16');
5629
5632
 
@@ -6351,13 +6354,6 @@ const generateClass = (scope, decl) => {
6351
6354
  // always generate class constructor funcs
6352
6355
  func.generate();
6353
6356
 
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
6357
  if (decl.superClass) {
6362
6358
  const superTmp = localTmp(scope, '#superclass');
6363
6359
  const superTypeTmp = localTmp(scope, '#superclass#type', Valtype.i32);
@@ -6417,10 +6413,15 @@ const generateClass = (scope, decl) => {
6417
6413
  );
6418
6414
  }
6419
6415
 
6420
- scope.overrideThis = generate(scope, root);
6421
- scope.overrideThisType = TYPES.function;
6416
+ // opt: avoid mass generate calls for many class fields
6417
+ const rootWasm = scope.overrideThis = generate(scope, root);
6418
+ const rootType = scope.overrideThisType = [ [ Opcodes.i32_const, TYPES.function ] ];
6419
+ const protoWasm = generate(scope, proto);
6420
+ const protoType = getNodeType(scope, proto);
6421
+ const thisWasm = generate(func, { type: 'ThisExpression', _noGlobalThis: true });
6422
+ const thisType = getNodeType(func, { type: 'ThisExpression', _noGlobalThis: true });
6422
6423
 
6423
- let constrAdd = [];
6424
+ const batchedNonStaticPropWasm = [];
6424
6425
  for (const x of body) {
6425
6426
  let { type, value, kind, static: _static, computed } = x;
6426
6427
  if (kind === 'constructor') continue;
@@ -6441,13 +6442,11 @@ const generateClass = (scope, decl) => {
6441
6442
  }
6442
6443
 
6443
6444
  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
6445
 
6449
- // default value to undefined
6450
- value ??= DEFAULT_VALUE();
6446
+ value ??= {
6447
+ type: 'Identifier',
6448
+ name: 'undefined'
6449
+ };
6451
6450
 
6452
6451
  if (isFuncType(value.type)) {
6453
6452
  let id = value.id;
@@ -6468,51 +6467,53 @@ const generateClass = (scope, decl) => {
6468
6467
 
6469
6468
  if (type === 'PropertyDefinition' && !_static) {
6470
6469
  // define in construction instead
6471
- object = {
6472
- type: 'ThisExpression',
6473
- _noGlobalThis: true
6474
- };
6475
-
6476
- let computedTmp;
6477
6470
  if (computed) {
6478
- // compute now, reference in construction
6479
- computedTmp = allocVar(scope, `#class_computed_prop${uniqId()}`, true, true, false, true);
6471
+ // compute key now, reference in construction
6472
+ const computedTmp = allocVar(scope, `#class_computed_prop${uniqId()}`, true, true, false, true);
6480
6473
 
6481
6474
  out.push(
6482
6475
  ...toPropertyKey(scope, generate(scope, key), getNodeType(scope, key), computed, true),
6483
6476
  [ Opcodes.global_set, computedTmp + 1 ],
6484
6477
  [ Opcodes.global_set, computedTmp ]
6485
6478
  );
6486
- }
6487
6479
 
6488
- const constrWasm = [
6489
- ...generate(func, object),
6490
- Opcodes.i32_to_u,
6491
- ...getNodeType(func, object),
6480
+ batchedNonStaticPropWasm.push(
6481
+ ...thisWasm,
6482
+ Opcodes.i32_to_u,
6483
+ ...thisType,
6492
6484
 
6493
- ...(computed ? [
6494
6485
  [ Opcodes.global_get, computedTmp ],
6495
6486
  [ Opcodes.global_get, computedTmp + 1 ],
6496
- ] : [
6497
- ...generate(func, key),
6487
+
6488
+ ...generate(func, value),
6489
+ ...getNodeType(func, value),
6490
+
6491
+ [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_value`).index ]
6492
+ );
6493
+ } else {
6494
+ batchedNonStaticPropWasm.push(
6495
+ ...thisWasm,
6498
6496
  Opcodes.i32_to_u,
6499
- ...getNodeType(func, key)
6500
- ]),
6497
+ ...thisType,
6501
6498
 
6502
- ...generate(func, value),
6503
- ...(initKind !== 'value' && initKind !== 'method' ? [ Opcodes.i32_to_u ] : []),
6504
- ...getNodeType(func, value),
6499
+ ...generate(func, key),
6500
+ Opcodes.i32_to_u,
6501
+ ...getNodeType(func, key),
6505
6502
 
6506
- [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_${initKind}`).index ]
6507
- ];
6503
+ ...generate(func, value),
6504
+ ...getNodeType(func, value),
6508
6505
 
6509
- func.wasm.splice(constrInsertIndex, 0, ...constrWasm);
6510
- constrInsertIndex += constrWasm.length;
6506
+ [ Opcodes.call, includeBuiltin(func, `__Porffor_object_class_value`).index ]
6507
+ );
6508
+ }
6511
6509
  } else {
6510
+ let initKind = type === 'MethodDefinition' ? 'method' : 'value';
6511
+ if (kind === 'get' || kind === 'set') initKind = kind;
6512
+
6512
6513
  out.push(
6513
- ...generate(scope, object),
6514
+ ...(_static ? rootWasm : protoWasm),
6514
6515
  Opcodes.i32_to_u,
6515
- ...getNodeType(scope, object),
6516
+ ...(_static ? rootType : protoType),
6516
6517
 
6517
6518
  ...toPropertyKey(scope, generate(scope, key), getNodeType(scope, key), computed, true),
6518
6519
 
@@ -6525,6 +6526,14 @@ const generateClass = (scope, decl) => {
6525
6526
  }
6526
6527
  }
6527
6528
 
6529
+ const constrInsertIndex = func.wasm.findIndex(x => x.at(-1) === 'super marker');
6530
+ if (constrInsertIndex != -1) {
6531
+ func.wasm.splice(constrInsertIndex, 1);
6532
+ func.wasm.splice(constrInsertIndex, 0, ...batchedNonStaticPropWasm);
6533
+ } else {
6534
+ func.wasm = batchedNonStaticPropWasm.concat(func.wasm);
6535
+ }
6536
+
6528
6537
  delete scope.overrideThis;
6529
6538
  delete scope.overrideThisType;
6530
6539