porffor 0.17.0-b4e7f7ee0 → 0.17.0-b598eb7bb

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.
@@ -1590,9 +1590,9 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1590
1590
  if (!name && decl.callee.type === 'MemberExpression') {
1591
1591
  // megahack for /regex/.func()
1592
1592
  const funcName = decl.callee.property.name;
1593
- if (decl.callee.object.regex && Object.hasOwn(Rhemyn, funcName)) {
1593
+ if (decl.callee.object.regex && ['test'].includes(funcName)) {
1594
1594
  const regex = decl.callee.object.regex.pattern;
1595
- const rhemynName = `regex_${funcName}_${regex}`;
1595
+ const rhemynName = `regex_${funcName}_${sanitize(regex)}`;
1596
1596
 
1597
1597
  if (!funcIndex[rhemynName]) {
1598
1598
  const func = Rhemyn[funcName](regex, currentFuncIndex++, rhemynName);
@@ -1613,7 +1613,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1613
1613
  [ Opcodes.call, idx ],
1614
1614
  Opcodes.i32_from_u,
1615
1615
 
1616
- ...setLastType(scope, TYPES.boolean)
1616
+ ...setLastType(scope, Rhemyn.types[funcName])
1617
1617
  ];
1618
1618
  }
1619
1619
 
@@ -1622,24 +1622,44 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1622
1622
  target = decl.callee.object;
1623
1623
  }
1624
1624
 
1625
- // if (protoName && baseType === TYPES.string && Rhemyn[protoName]) {
1626
- // const func = Rhemyn[protoName](decl.arguments[0].regex.pattern, currentFuncIndex++);
1625
+ if (protoName) {
1626
+ if (['search'].includes(protoName)) {
1627
+ const regex = decl.arguments[0]?.regex?.pattern;
1628
+ if (!regex) return [
1629
+ // no/bad regex arg, return -1/0 for now
1630
+ ...generate(scope, target),
1631
+ [ Opcodes.drop ],
1627
1632
 
1628
- // funcIndex[func.name] = func.index;
1629
- // funcs.push(func);
1633
+ ...number(Rhemyn.types[protoName] === TYPES.number ? -1 : 0),
1634
+ ...setLastType(scope, Rhemyn.types[protoName])
1635
+ ];
1630
1636
 
1631
- // return [
1632
- // generate(scope, decl.callee.object)
1637
+ const rhemynName = `regex_${protoName}_${sanitize(regex)}`;
1633
1638
 
1634
- // // call regex func
1635
- // [ Opcodes.call, func.index ],
1636
- // Opcodes.i32_from_u
1637
- // ];
1638
- // }
1639
+ if (!funcIndex[rhemynName]) {
1640
+ const func = Rhemyn[protoName](regex, currentFuncIndex++, rhemynName);
1641
+ func.internal = true;
1639
1642
 
1640
- if (protoName) {
1641
- const protoBC = {};
1643
+ funcIndex[func.name] = func.index;
1644
+ funcs.push(func);
1645
+ }
1642
1646
 
1647
+ const idx = funcIndex[rhemynName];
1648
+ return [
1649
+ // make string arg
1650
+ ...generate(scope, target),
1651
+ Opcodes.i32_to_u,
1652
+ ...getNodeType(scope, target),
1653
+
1654
+ // call regex func
1655
+ [ Opcodes.call, idx ],
1656
+ Opcodes.i32_from,
1657
+
1658
+ ...setLastType(scope, Rhemyn.types[protoName])
1659
+ ];
1660
+ }
1661
+
1662
+ const protoBC = {};
1643
1663
  const builtinProtoCands = Object.keys(builtinFuncs).filter(x => x.startsWith('__') && x.endsWith('_prototype_' + protoName));
1644
1664
 
1645
1665
  if (!decl._protoInternalCall && builtinProtoCands.length > 0) {
@@ -2053,6 +2073,16 @@ const DEFAULT_VALUE = {
2053
2073
  name: 'undefined'
2054
2074
  };
2055
2075
 
2076
+ const codeToSanitizedStr = code => {
2077
+ let out = '';
2078
+ while (code > 0) {
2079
+ out += String.fromCharCode(97 + code % 26);
2080
+ code -= 26;
2081
+ }
2082
+ return out;
2083
+ };
2084
+ const sanitize = str => str.replace(/[^0-9a-zA-Z_]/g, _ => codeToSanitizedStr(_.charCodeAt(0)));
2085
+
2056
2086
  const unhackName = name => {
2057
2087
  if (name.startsWith('__')) return name.slice(2).replaceAll('_', '.');
2058
2088
  return name;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.17.0-b4e7f7ee0",
4
+ "version": "0.17.0-b598eb7bb",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/rhemyn/compile.js CHANGED
@@ -6,13 +6,11 @@ import { TYPES } from '../compiler/types.js';
6
6
 
7
7
  // local indexes
8
8
  const BasePointer = 0; // base string pointer
9
- const IterPointer = 1; // this iteration base pointer
10
- const EndPointer = 2; // pointer for the end
11
- const Counter = 3; // what char we are running on
12
- const Pointer = 4; // next char BYTE pointer
13
- const Length = 5;
14
- const Tmp = 6;
15
- const QuantifierTmp = 7; // the temporary variable used for quanitifers
9
+ const Counter = 2; // what char we are running on
10
+ const Pointer = 3; // next char pointer
11
+ const Length = 4;
12
+ const Tmp = 5;
13
+ const QuantifierTmp = 6; // the temporary variable used for quanitifers
16
14
 
17
15
  const generate = (node, negated = false, get = true, stringSize = 2, func = 'test') => {
18
16
  let out = [];
@@ -24,17 +22,13 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
24
22
  [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
25
23
  [ Opcodes.local_set, Length ],
26
24
 
27
- // set iter pointer local as base + sizeof i32 initially
25
+ // pointer = base + sizeof i32
28
26
  [ Opcodes.local_get, BasePointer ],
29
27
  ...number(ValtypeSize.i32, Valtype.i32),
30
28
  [ Opcodes.i32_add ],
31
- [ Opcodes.local_set, IterPointer ],
29
+ [ Opcodes.local_set, Pointer ],
32
30
 
33
31
  [ Opcodes.loop, Blocktype.void ],
34
- // reset pointer as iter pointer
35
- [ Opcodes.local_get, IterPointer ],
36
- [ Opcodes.local_set, Pointer ],
37
-
38
32
  [ Opcodes.block, Blocktype.void ],
39
33
  // generate checks
40
34
  ...node.body.flatMap(x => generate(x, negated, true, stringSize, func)),
@@ -49,12 +43,6 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
49
43
  [ Opcodes.return ],
50
44
  [ Opcodes.end ],
51
45
 
52
- // increment iter pointer by string size
53
- [ Opcodes.local_get, IterPointer ],
54
- ...number(stringSize, Valtype.i32),
55
- [ Opcodes.i32_add ],
56
- [ Opcodes.local_set, IterPointer ],
57
-
58
46
  // increment counter by 1, check if eq length, if not loop
59
47
  [ Opcodes.local_get, Counter ],
60
48
  ...number(1, Valtype.i32),
@@ -67,7 +55,7 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
67
55
  [ Opcodes.br_if, 0 ],
68
56
  [ Opcodes.end ],
69
57
 
70
- // no match, return 0
58
+ // no match
71
59
  ...number(({
72
60
  test: 0,
73
61
  search: -1
@@ -269,7 +257,7 @@ const wrapFunc = (regex, func, name, index) => {
269
257
  const parsed = parse(regex);
270
258
 
271
259
  return outputFunc([
272
- [ Opcodes.local_get, IterPointer ],
260
+ [ Opcodes.local_get, 1 ],
273
261
  ...number(TYPES.string, Valtype.i32),
274
262
  [ Opcodes.i32_eq ],
275
263
  [ Opcodes.if, Valtype.i32 ],
@@ -279,29 +267,33 @@ const wrapFunc = (regex, func, name, index) => {
279
267
  // bytestring
280
268
  ...generate(parsed, false, true, 1, func),
281
269
  [ Opcodes.end ]
282
- ], name, index);
270
+ ], name, index, types[func]);
283
271
  };
284
272
 
285
273
  export const test = (regex, index = 0, name = 'regex_test_' + regex) => wrapFunc(regex, 'test', name, index);
286
274
  export const search = (regex, index = 0, name = 'regex_search_' + regex) => wrapFunc(regex, 'search', name, index);
287
275
 
288
- const outputFunc = (wasm, name, index) => ({
276
+ export const types = {
277
+ test: TYPES.boolean,
278
+ search: TYPES.number
279
+ };
280
+
281
+ const outputFunc = (wasm, name, index, returnType) => ({
289
282
  name,
290
283
  index,
291
284
  wasm,
285
+ returnType,
292
286
 
293
287
  export: true,
294
288
  params: [ Valtype.i32, Valtype.i32 ],
295
289
  returns: [ Valtype.i32 ],
296
- returnType: TYPES.boolean,
297
290
  locals: {
298
291
  basePointer: { idx: 0, type: Valtype.i32 },
299
- iterPointer: { idx: 1, type: Valtype.i32 },
300
- endPointer: { idx: 2, type: Valtype.i32 },
301
- counter: { idx: 3, type: Valtype.i32 },
302
- pointer: { idx: 4, type: Valtype.i32 },
303
- length: { idx: 5, type: Valtype.i32 },
304
- tmp: { idx: 6, type: Valtype.i32 },
305
- quantifierTmp: { idx: 7, type: Valtype.i32 },
292
+ inputType: { idx: 1, type: Valtype.i32 },
293
+ counter: { idx: 2, type: Valtype.i32 },
294
+ pointer: { idx: 3, type: Valtype.i32 },
295
+ length: { idx: 4, type: Valtype.i32 },
296
+ tmp: { idx: 5, type: Valtype.i32 },
297
+ quantifierTmp: { idx: 6, type: Valtype.i32 },
306
298
  }
307
299
  });