porffor 0.2.0-623cdf0 → 0.2.0-67434d5

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.
Files changed (53) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +154 -89
  3. package/asur/README.md +2 -0
  4. package/asur/index.js +1262 -0
  5. package/byg/index.js +237 -0
  6. package/compiler/2c.js +12 -12
  7. package/compiler/{sections.js → assemble.js} +63 -15
  8. package/compiler/builtins/annexb_string.js +72 -0
  9. package/compiler/builtins/annexb_string.ts +19 -0
  10. package/compiler/builtins/array.ts +145 -0
  11. package/compiler/builtins/base64.ts +151 -0
  12. package/compiler/builtins/crypto.ts +120 -0
  13. package/compiler/builtins/date.ts +1370 -0
  14. package/compiler/builtins/escape.ts +141 -0
  15. package/compiler/builtins/int.ts +147 -0
  16. package/compiler/builtins/number.ts +527 -0
  17. package/compiler/builtins/porffor.d.ts +42 -0
  18. package/compiler/builtins/string.ts +1055 -0
  19. package/compiler/builtins/tostring.ts +45 -0
  20. package/compiler/builtins.js +455 -278
  21. package/compiler/{codeGen.js → codegen.js} +930 -349
  22. package/compiler/embedding.js +22 -22
  23. package/compiler/encoding.js +108 -10
  24. package/compiler/generated_builtins.js +1262 -0
  25. package/compiler/index.js +25 -34
  26. package/compiler/log.js +6 -3
  27. package/compiler/opt.js +50 -36
  28. package/compiler/parse.js +36 -28
  29. package/compiler/precompile.js +123 -0
  30. package/compiler/prefs.js +27 -0
  31. package/compiler/prototype.js +13 -28
  32. package/compiler/types.js +37 -0
  33. package/compiler/wasmSpec.js +28 -8
  34. package/compiler/wrap.js +51 -46
  35. package/fib.js +10 -0
  36. package/package.json +9 -5
  37. package/porf +4 -0
  38. package/rhemyn/compile.js +46 -27
  39. package/rhemyn/parse.js +322 -320
  40. package/rhemyn/test/parse.js +58 -58
  41. package/runner/compare.js +34 -34
  42. package/runner/debug.js +122 -0
  43. package/runner/index.js +76 -11
  44. package/runner/profiler.js +102 -0
  45. package/runner/repl.js +42 -9
  46. package/runner/sizes.js +37 -37
  47. package/compiler/builtins/base64.js +0 -92
  48. package/filesize.cmd +0 -2
  49. package/runner/info.js +0 -89
  50. package/runner/profile.js +0 -46
  51. package/runner/results.json +0 -1
  52. package/runner/transform.js +0 -15
  53. package/util/enum.js +0 -20
package/byg/index.js ADDED
@@ -0,0 +1,237 @@
1
+ import fs from 'node:fs';
2
+
3
+ const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
4
+ const printLine = (line, number, breakpoint = false, current = false, selected = false) => {
5
+ // console.log(`\x1b[${breakpoint ? (selected ? '106' : '46') : (selected ? '47' : '100')}m\x1b[${selected ? '30' : '97'}m${number.toFixed(0).padStart(4, ' ')}\x1b[${breakpoint ? (selected ? '96' : '36') : (selected ? '37' : '90')}m\x1b[${current ? '47' : '40'}m▌ \x1b[${current ? '47' : '40'}m\x1b[${current ? '30' : '37'}m${current ? noAnsi(line) : line}\x1b[0K`);
6
+ console.log(`\x1b[${breakpoint ? (selected ? '43' : '103') : (selected ? '47' : '100')}m\x1b[${selected || breakpoint ? '30' : '97'}m${number.toFixed(0).padStart(4, ' ')}\x1b[${breakpoint ? (selected ? '33' : '93') : (selected ? '37' : '90')}m\x1b[${current ? '47' : '40'}m▌ \x1b[${current ? '47' : '40'}m\x1b[${current ? '30' : '37'}m${current ? noAnsi(line) : line}\x1b[0K`);
7
+ };
8
+
9
+ const box = (x, y, width, height, title = '', content = [], color = ['90', '100', '37', '35', '45'], padding = true) => {
10
+ if (padding) {
11
+ width += 1;
12
+ height += 1;
13
+
14
+ // top
15
+ process.stdout.write(`\x1b[48m\x1b[${y + 1};${x + 1}H\x1b[${color[0]}m` + '▄'.repeat(width));
16
+
17
+ // bottom
18
+ process.stdout.write(`\x1b[${y + height + 1};${x + 1}H▝` + '▀'.repeat(width - 1) + '▘');
19
+
20
+ // left
21
+ process.stdout.write(`\x1b[${y + 1};${x + 1}H▗` + '\x1b[1B\x1b[1D▐'.repeat(height - 1));
22
+
23
+ // right
24
+ process.stdout.write(`\x1b[${y + 1};${x + width + 1}H▖` + '\x1b[1B\x1b[1D▌'.repeat(height - 1));
25
+
26
+ x += 1;
27
+ y += 1;
28
+ width -= 1;
29
+ height -= 1;
30
+ }
31
+
32
+ // bg
33
+ process.stdout.write(`\x1b[${y + 1};${x + 1}H\x1b[${color[1]}m` + ' '.repeat(width) + (`\x1b[1B\x1b[${width}D` + ' '.repeat(width)).repeat(Math.max(0, height - 1)));
34
+
35
+ // title
36
+ // if (title) process.stdout.write(`\x1b[${y + 1};${x + ((width - title.length) / 2 | 0) + 1}H\x1b[${color[1]}m\x1b[${color[2]}m\x1b[1m${title}\x1b[22m`);
37
+ // if (title) process.stdout.write(`\x1b[${y};${x}H\x1b[${color[3]}▗\x1b[${color[4]}m\x1b[${color[2]}m\x1b[1m${' '.repeat((width - title.length) / 2 | 0)}${title}${' '.repeat(width - (((width - title.length) / 2 | 0)) - title.length)}\x1b[0m\x1b[${color[4]}m▖`);
38
+ if (title) process.stdout.write(`\x1b[${y};${x}H\x1b[0m\x1b[${color[3]}m▐\x1b[${color[4]}m\x1b[${color[2]}m\x1b[1m${' '.repeat((width - title.length) / 2 | 0)}${title}${' '.repeat(width - (((width - title.length) / 2 | 0)) - title.length)}\x1b[0m\x1b[${color[3]}m▌`);
39
+
40
+ // content
41
+ process.stdout.write(`\x1b[${y + (title ? 1 : 1)};${x + 1}H\x1b[${color[1]}m\x1b[${color[2]}m${content.join(`\x1b[1B\x1b[${x + 1}G`)}`);
42
+ };
43
+
44
+ const controls = {
45
+ 'ret': 'resume ',
46
+ 'b': 'breakpoint ',
47
+ 's': 'step over',
48
+ 'i': 'step in',
49
+ 'o': 'step out ',
50
+ };
51
+
52
+ const controlInfo = Object.keys(controls).reduce((acc, x, i) => acc + `\x1B[45m\x1B[97m${x}\x1b[105m\x1b[37m ${controls[x]} `, '');
53
+ const plainControlInfo = noAnsi(controlInfo);
54
+
55
+ globalThis.termWidth = process.stdout.columns || 80;
56
+ globalThis.termHeight = process.stdout.rows || 24;
57
+
58
+ export default ({ lines, pause, breakpoint }) => {
59
+ process.stdin.setRawMode(true);
60
+ process.stdin.resume();
61
+ process.stdin.setEncoding('utf8');
62
+
63
+ process.stdin.on('data', s => {
64
+ // ctrl c
65
+ if (s === '\u0003') {
66
+ process.exit();
67
+ }
68
+
69
+ // process.stdout.write(s);
70
+
71
+ if (!paused) pause();
72
+ });
73
+
74
+ const stdin = fs.openSync('/dev/stdin', 'r+');
75
+ const readCharSync = () => {
76
+ const buffer = Buffer.alloc(1);
77
+ fs.readSync(stdin, buffer, 0, 1);
78
+ return buffer.toString('utf8');
79
+ };
80
+
81
+ const tooManyLines = lines.length > (termHeight - 1);
82
+ const breakpoints = {};
83
+
84
+ process.on('exit', () => {
85
+ process.stdout.write('\x1b[0m');
86
+ // console.clear();
87
+ });
88
+
89
+ console.clear();
90
+
91
+ let paused = true;
92
+ return (_paused, currentLine, text, boxes = []) => {
93
+ paused = _paused;
94
+
95
+ let scrollOffset = 0;
96
+ let currentLinePos = currentLine;
97
+
98
+ const draw = () => {
99
+ console.clear();
100
+ process.stdout.write(`\x1b[1;1H`);
101
+
102
+ if (tooManyLines) {
103
+ const edgePadding = (termHeight / 2) - 1;
104
+ let beforePadding = currentLine - edgePadding;
105
+ let afterPadding = currentLine + edgePadding + 1;
106
+
107
+ if (beforePadding < 0) {
108
+ afterPadding += Math.abs(beforePadding);
109
+ beforePadding = 0;
110
+ }
111
+
112
+ beforePadding += scrollOffset;
113
+ afterPadding += scrollOffset;
114
+
115
+ if (afterPadding > lines.length) {
116
+ beforePadding -= afterPadding - lines.length;
117
+ afterPadding = lines.length;
118
+ }
119
+
120
+ for (let i = Math.max(0, beforePadding); i < Math.max(0, beforePadding) + (termHeight - 1); i++) {
121
+ printLine(lines[i], i + 1, !!breakpoints[i], currentLine === i, currentLine + scrollOffset === i);
122
+ }
123
+
124
+ currentLinePos = currentLine - beforePadding;
125
+ } else {
126
+ for (let i = 0; i < lines.length; i++) {
127
+ printLine(lines[i], i + 1, !!breakpoints[i], currentLine === i, currentLine + scrollOffset === i);
128
+ }
129
+ }
130
+
131
+ for (const x of boxes) {
132
+ const y = x.y({ currentLinePos });
133
+ const height = x.height;
134
+ if (y < 0 || y >= termHeight) continue;
135
+
136
+ // crop box if > termHeight
137
+ // if (y + height >= termHeight) {
138
+ // const excess = y + height - termHeight;
139
+ // height -= excess;
140
+
141
+ // content = content.slice(0, height - 2);
142
+ // }
143
+
144
+ box(x.x, y, x.width, height, x.title, x.content);
145
+ }
146
+
147
+ // text += ` | rss: ${(process.memoryUsage.rss() / 1024 / 1024).toFixed(2)}mb`;
148
+
149
+ process.stdout.write(`\x1b[${termHeight};1H\x1b[105m\x1b[37m${text}${' '.repeat(termWidth - plainControlInfo.length - noAnsi(text).length - 1)}${controlInfo} \x1b[0m`);
150
+ };
151
+
152
+ draw();
153
+
154
+ let lastSpecial = false;
155
+ while (true) {
156
+ const char = readCharSync();
157
+
158
+ if (char === '[') {
159
+ lastSpecial = true;
160
+ continue;
161
+ }
162
+
163
+ switch (char.toLowerCase()) {
164
+ case '\r': {
165
+ paused = false;
166
+ return 'resume';
167
+ }
168
+
169
+ case 's': {
170
+ return 'stepOver';
171
+ }
172
+
173
+ case 'i': {
174
+ return 'stepIn';
175
+ }
176
+
177
+ case 'o': {
178
+ return 'stepOut';
179
+ }
180
+
181
+ case 'b': {
182
+ if (!lastSpecial) { // b pressed normally
183
+ breakpoints[currentLine + scrollOffset] = !breakpoints[currentLine + scrollOffset];
184
+ draw();
185
+
186
+ breakpoint(currentLine + scrollOffset, breakpoints[currentLine + scrollOffset]);
187
+ break;
188
+ }
189
+
190
+ // arrow down
191
+ // if (screenOffset + termHeight <= lines.length) scrollOffset++;
192
+ if (scrollOffset < lines.length - currentLine - 1) scrollOffset++;
193
+ draw();
194
+ break;
195
+ }
196
+
197
+ case 'a': {
198
+ if (!lastSpecial) break;
199
+
200
+ // arrow up
201
+ // if (screenOffset > 0) scrollOffset--;
202
+ if (scrollOffset > -currentLine) scrollOffset--;
203
+ draw();
204
+
205
+ break;
206
+ }
207
+
208
+ case '5': {
209
+ if (!lastSpecial) break;
210
+
211
+ // page up
212
+ // scrollOffset -= Math.min(screenOffset, termHeight - 1);
213
+ scrollOffset -= Math.min(scrollOffset + currentLine, termHeight - 1);
214
+ draw();
215
+ break;
216
+ }
217
+
218
+ case '6': {
219
+ if (!lastSpecial) break;
220
+
221
+ // page down
222
+ // scrollOffset += Math.min((lines.length + 1) - (screenOffset + termHeight), termHeight - 1);
223
+ scrollOffset += Math.min(lines.length - (scrollOffset + currentLine) - 1, termHeight - 1);
224
+ draw();
225
+ break;
226
+ }
227
+
228
+ case 'q':
229
+ case '\u0003': {
230
+ process.exit();
231
+ }
232
+ }
233
+
234
+ lastSpecial = false;
235
+ }
236
+ };
237
+ };
package/compiler/2c.js CHANGED
@@ -17,12 +17,12 @@ const CValtype = {
17
17
  undefined: 'void'
18
18
  };
19
19
 
20
- const alwaysPreface = `typedef unsigned char i8;
21
- typedef unsigned short i16;
22
- typedef long i32;
23
- typedef unsigned long u32;
24
- typedef long long i64;
25
- typedef unsigned long long u64;
20
+ const alwaysPreface = `typedef uint8_t i8;
21
+ typedef uint16_t i16;
22
+ typedef int32_t i32;
23
+ typedef uint32_t u32;
24
+ typedef int64_t i64;
25
+ typedef uint64_t u64;
26
26
  typedef float f32;
27
27
  typedef double f64;
28
28
 
@@ -31,8 +31,7 @@ f64 NAN = 0e+0/0e+0;
31
31
  struct ReturnValue {
32
32
  ${CValtype.f64} value;
33
33
  ${CValtype.i32} type;
34
- };
35
- \n`;
34
+ };\n\n`;
36
35
 
37
36
  // todo: is memcpy/etc safe with host endianness?
38
37
 
@@ -115,7 +114,7 @@ const todo = msg => {
115
114
  }
116
115
  }
117
116
 
118
- throw new TodoError(`todo: ${msg}`);
117
+ throw new TodoError(msg);
119
118
  };
120
119
 
121
120
  const removeBrackets = str => {
@@ -148,7 +147,8 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
148
147
  const includes = new Map(), unixIncludes = new Map(), winIncludes = new Map();
149
148
  const prepend = new Map(), prependMain = new Map();
150
149
 
151
- // presume all <i32 work is unsigned
150
+ includes.set('stdint.h', true);
151
+
152
152
  let out = ``;
153
153
 
154
154
  for (const x in globals) {
@@ -164,7 +164,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
164
164
  }
165
165
 
166
166
  if (data.length > 0) {
167
- prependMain.set('_data', data.map(x => `memcpy(_memory + ${x.offset}, (char[]){${x.bytes.join(',')}}, ${x.bytes.length});`).join('\n'));
167
+ prependMain.set('_data', data.map(x => `memcpy(_memory + ${x.offset}, (unsigned char[]){${x.bytes.join(',')}}, ${x.bytes.length});`).join('\n'));
168
168
  }
169
169
 
170
170
  // for (const [ x, p ] of pages) {
@@ -624,7 +624,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
624
624
 
625
625
  const makeIncludes = includes => [...includes.keys()].map(x => `#include <${x}>\n`).join('');
626
626
 
627
- out = alwaysPreface + platformSpecific(makeIncludes(winIncludes), makeIncludes(unixIncludes), false) + '\n' + makeIncludes(includes) + '\n' + [...prepend.values()].join('\n') + '\n\n' + out;
627
+ out = platformSpecific(makeIncludes(winIncludes), makeIncludes(unixIncludes), false) + '\n' + makeIncludes(includes) + '\n' + alwaysPreface + [...prepend.values()].join('\n') + '\n\n' + out;
628
628
 
629
629
  return out.trim();
630
630
  };
@@ -1,8 +1,9 @@
1
1
  import { Valtype, FuncType, Empty, ExportDesc, Section, Magic, ModuleVersion, Opcodes, PageSize } from './wasmSpec.js';
2
- import { encodeVector, encodeString, encodeLocal, unsignedLEB128, signedLEB128 } from './encoding.js';
3
- import { number } from './embedding.js';
2
+ import { encodeVector, encodeString, encodeLocal, unsignedLEB128, signedLEB128, ieee754_binary64, unsignedLEB128_into, signedLEB128_into, ieee754_binary64_into } from './encoding.js';
3
+ // import { number } from './embedding.js';
4
4
  import { importedFuncs } from './builtins.js';
5
5
  import { log } from "./log.js";
6
+ import Prefs from './prefs.js';
6
7
 
7
8
  const createSection = (type, data) => [
8
9
  type,
@@ -26,12 +27,12 @@ export default (funcs, globals, tags, pages, data, flags) => {
26
27
 
27
28
  const optLevel = parseInt(process.argv.find(x => x.startsWith('-O'))?.[2] ?? 1);
28
29
 
29
- const compileHints = process.argv.includes('-compile-hints');
30
- if (compileHints) log.warning('sections', 'compile hints is V8 only w/ experimental arg! (you used -compile-hints)');
30
+ const compileHints = Prefs.compileHints;
31
+ if (compileHints) log.warning('assemble', 'compile hints is V8 only w/ experimental arg! (you used -compile-hints)');
31
32
 
32
33
  const getType = (params, returns) => {
33
34
  const hash = `${params.join(',')}_${returns.join(',')}`;
34
- if (optLog) log('sections', `getType(${JSON.stringify(params)}, ${JSON.stringify(returns)}) -> ${hash} | cache: ${typeCache[hash]}`);
35
+ if (Prefs.optLog) log('assemble', `getType(${JSON.stringify(params)}, ${JSON.stringify(returns)}) -> ${hash} | cache: ${typeCache[hash]}`);
35
36
  if (optLevel >= 1 && typeCache[hash] !== undefined) return typeCache[hash];
36
37
 
37
38
  const type = [ FuncType, ...encodeVector(params), ...encodeVector(returns) ];
@@ -44,7 +45,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
44
45
 
45
46
  let importFuncs = [];
46
47
 
47
- if (optLevel < 1) {
48
+ if (optLevel < 1 || !Prefs.treeshakeWasmImports) {
48
49
  importFuncs = importedFuncs;
49
50
  } else {
50
51
  let imports = new Map();
@@ -79,11 +80,11 @@ export default (funcs, globals, tags, pages, data, flags) => {
79
80
  }
80
81
  globalThis.importFuncs = importFuncs;
81
82
 
82
- if (optLog) log('sections', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);
83
+ if (Prefs.optLog) log('assemble', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);
83
84
 
84
85
  const importSection = importFuncs.length === 0 ? [] : createSection(
85
86
  Section.import,
86
- encodeVector(importFuncs.map(x => [ 0, ...encodeString(x.import), ExportDesc.func, getType(new Array(x.params).fill(valtypeBinary), new Array(x.returns).fill(valtypeBinary)) ]))
87
+ encodeVector(importFuncs.map(x => [ 0, ...encodeString(x.import), ExportDesc.func, getType(new Array(x.params).fill(x.name.startsWith('profile') ? Valtype.i32 : valtypeBinary), new Array(x.returns).fill(valtypeBinary)) ]))
87
88
  );
88
89
 
89
90
  const funcSection = createSection(
@@ -95,18 +96,65 @@ export default (funcs, globals, tags, pages, data, flags) => {
95
96
  // https://github.com/WebAssembly/design/issues/1473#issuecomment-1431274746
96
97
  const chSection = !compileHints ? [] : customSection(
97
98
  'compilationHints',
98
- // for now just do everything as optimise eager
99
+ // for now just do everything as optimize eager
99
100
  encodeVector(funcs.map(_ => chHint(0x02, 0x02, 0x02)))
100
101
  );
101
102
 
102
- const globalSection = Object.keys(globals).length === 0 ? [] : createSection(
103
- Section.global,
104
- encodeVector(Object.keys(globals).map(x => [ globals[x].type, 0x01, ...number(globals[x].init ?? 0, globals[x].type).flat(), Opcodes.end ]))
105
- );
103
+ // const t0 = performance.now();
104
+
105
+ // specially optimized assembly for globals as this version is much (>5x) faster than traditional createSection(...)
106
+ const globalsValues = Object.values(globals);
107
+
108
+ let globalSection = [];
109
+ if (globalsValues.length > 0) {
110
+ let data = unsignedLEB128(globalsValues.length);
111
+ for (let i = 0; i < globalsValues.length; i++) {
112
+ const global = globalsValues[i];
113
+
114
+ switch (global.type) {
115
+ case Valtype.i32:
116
+ if (i > 0) data.push(Opcodes.end, Valtype.i32, 0x01, Opcodes.i32_const);
117
+ else data.push(Valtype.i32, 0x01, Opcodes.i32_const);
118
+
119
+ signedLEB128_into(global.init ?? 0, data);
120
+ break;
121
+
122
+ case Valtype.i64:
123
+ if (i > 0) data.push(Opcodes.end, Valtype.i64, 0x01, Opcodes.i64_const);
124
+ else data.push(Valtype.i64, 0x01, Opcodes.i64_const);
125
+
126
+ signedLEB128_into(global.init ?? 0, data);
127
+ break;
128
+
129
+ case Valtype.f64:
130
+ if (i > 0) data.push(Opcodes.end, Valtype.f64, 0x01, Opcodes.f64_const);
131
+ else data.push(Valtype.f64, 0x01, Opcodes.f64_const);
132
+
133
+ ieee754_binary64_into(global.init ?? 0, data);
134
+ break;
135
+ }
136
+ }
137
+
138
+ data.push(Opcodes.end);
139
+
140
+ globalSection.push(Section.global);
141
+
142
+ unsignedLEB128_into(data.length, globalSection);
143
+ globalSection = globalSection.concat(data);
144
+ }
145
+
146
+ // if (Prefs.profileCompiler) {
147
+ // const log = console.log;
148
+ // console.log = function () {
149
+ // log.apply(this, arguments);
150
+ // console.log = log;
151
+ // console.log(` a. assembled global section in ${(performance.now() - t0).toFixed(2)}ms\n`);
152
+ // };
153
+ // }
106
154
 
107
155
  const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, x.index ]);
108
156
 
109
- if (process.argv.includes('-always-memory') && pages.size === 0) pages.set('-always-memory', 0);
157
+ if (Prefs.alwaysMemory && pages.size === 0) pages.set('--always-memory', 0);
110
158
  if (optLevel === 0) pages.set('O0 precaution', 0);
111
159
 
112
160
  const usesMemory = pages.size > 0;
@@ -169,7 +217,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
169
217
  unsignedLEB128(data.length)
170
218
  );
171
219
 
172
- if (process.argv.includes('-sections')) console.log({
220
+ if (Prefs.sections) console.log({
173
221
  typeSection: typeSection.map(x => x.toString(16)),
174
222
  importSection: importSection.map(x => x.toString(16)),
175
223
  funcSection: funcSection.map(x => x.toString(16)),
@@ -0,0 +1,72 @@
1
+ export default () => {
2
+ let out = `// @porf -funsafe-no-unlikely-proto-checks -valtype=i32
3
+ `;
4
+
5
+ const annexB_noArgs = (a0, a1) => out += `
6
+ export const __String_prototype_${a0} = (_this: string) => {
7
+ let out: string = Porffor.s\`<${a1}>\`;
8
+
9
+ let outPtr: i32 = Porffor.wasm\`local.get \${out}\` + ${(2 + a1.length) * 2};
10
+
11
+ let thisPtr: i32 = Porffor.wasm\`local.get \${_this}\`;
12
+ let thisLen: i32 = _this.length;
13
+ let endPtr: i32 = thisPtr + thisLen * 2;
14
+
15
+ while (thisPtr < endPtr) {
16
+ let chr: i32 = Porffor.wasm.i32.load16_u(thisPtr, 0, 4);
17
+ Porffor.wasm.i32.store16(outPtr, chr, 0, 4);
18
+
19
+ thisPtr += 2;
20
+ outPtr += 2;
21
+ }
22
+
23
+ Porffor.wasm.i32.store16(outPtr, 60, 0, 4); // <
24
+ Porffor.wasm.i32.store16(outPtr, 47, 0, 6); // /
25
+
26
+ ${[...a1].map((x, i) => ` Porffor.wasm.i32.store16(outPtr, ${x.charCodeAt(0)}, 0, ${8 + i * 2}); // ${x}`).join('\n')}
27
+
28
+ Porffor.wasm.i32.store16(outPtr, 62, 0, ${8 + a1.length * 2}); // >
29
+
30
+ out.length = thisLen + ${a1.length * 2 + 2 + 3};
31
+
32
+ return out;
33
+ };
34
+ export const ___bytestring_prototype_${a0} = (_this: bytestring) => {
35
+ let out: bytestring = Porffor.bs\`<${a1}>\`;
36
+
37
+ let outPtr: i32 = Porffor.wasm\`local.get \${out}\` + ${2 + a1.length};
38
+
39
+ let thisPtr: i32 = Porffor.wasm\`local.get \${_this}\`;
40
+ let thisLen: i32 = _this.length;
41
+ let endPtr: i32 = thisPtr + thisLen;
42
+
43
+ while (thisPtr < endPtr) {
44
+ let chr: i32 = Porffor.wasm.i32.load8_u(thisPtr++, 0, 4);
45
+ Porffor.wasm.i32.store8(outPtr++, chr, 0, 4);
46
+ }
47
+
48
+ Porffor.wasm.i32.store8(outPtr, 60, 0, 4); // <
49
+ Porffor.wasm.i32.store8(outPtr, 47, 0, 5); // /
50
+
51
+ ${[...a1].map((x, i) => ` Porffor.wasm.i32.store8(outPtr, ${x.charCodeAt(0)}, 0, ${6 + i}); // ${x}`).join('\n')}
52
+
53
+ Porffor.wasm.i32.store8(outPtr, 62, 0, ${6 + a1.length}); // >
54
+
55
+ out.length = thisLen + ${a1.length * 2 + 2 + 3};
56
+
57
+ return out;
58
+ };
59
+ `;
60
+
61
+ annexB_noArgs('big', 'big');
62
+ annexB_noArgs('blink', 'blink');
63
+ annexB_noArgs('bold', 'b');
64
+ annexB_noArgs('fixed', 'tt');
65
+ annexB_noArgs('italics', 'i');
66
+ annexB_noArgs('small', 'small');
67
+ annexB_noArgs('strike', 'strike');
68
+ annexB_noArgs('sub', 'sub');
69
+ annexB_noArgs('sup', 'sup');
70
+
71
+ return out;
72
+ };
@@ -0,0 +1,19 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+
3
+ // todo: trimLeft, trimRight
4
+ export const __String_prototype_trimLeft = (_this: string) => {
5
+ return __String_prototype_trimStart(_this);
6
+ };
7
+
8
+ export const ___bytestring_prototype_trimLeft = (_this: string) => {
9
+ return ___bytestring_prototype_trimStart(_this);
10
+ };
11
+
12
+
13
+ export const __String_prototype_trimRight = (_this: string) => {
14
+ return __String_prototype_trimEnd(_this);
15
+ };
16
+
17
+ export const ___bytestring_prototype_trimEnd = (_this: string) => {
18
+ return ___bytestring_prototype_trimRight(_this);
19
+ };
@@ -0,0 +1,145 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks
2
+
3
+ export const __Array_isArray = (x: unknown): boolean =>
4
+ // Porffor.wasm`local.get ${x+1}` == Porffor.TYPES._array;
5
+ Porffor.rawType(x) == Porffor.TYPES._array;
6
+
7
+ export const ___array_prototype_slice = (_this: any[], start: number, end: number) => {
8
+ const len: i32 = _this.length;
9
+ if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
10
+
11
+ start |= 0;
12
+ end |= 0;
13
+
14
+ if (start < 0) {
15
+ start = len + start;
16
+ if (start < 0) start = 0;
17
+ }
18
+ if (start > len) start = len;
19
+ if (end < 0) {
20
+ end = len + end;
21
+ if (end < 0) end = 0;
22
+ }
23
+ if (end > len) end = len;
24
+
25
+ let out: any[] = [];
26
+
27
+ if (start > end) return out;
28
+
29
+ let outPtr: i32 = Porffor.wasm`local.get ${out}`;
30
+ let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
31
+
32
+ const thisPtrEnd: i32 = thisPtr + end * 8;
33
+
34
+ thisPtr += start * 8;
35
+
36
+ while (thisPtr < thisPtrEnd) {
37
+ Porffor.wasm.f64.store(outPtr, Porffor.wasm.f64.load(thisPtr, 0, 4), 0, 4);
38
+ thisPtr += 8;
39
+ outPtr += 8;
40
+ }
41
+
42
+ out.length = end - start;
43
+
44
+ return out;
45
+ };
46
+
47
+ export const ___array_prototype_indexOf = (_this: any[], searchElement: any, position: number) => {
48
+ const len: i32 = _this.length;
49
+ if (position > 0) {
50
+ if (position > len) position = len;
51
+ else position |= 0;
52
+ } else position = 0;
53
+
54
+ for (let i: i32 = position; i < len; i++) {
55
+ if (_this[i] == searchElement) return i;
56
+ }
57
+
58
+ return -1;
59
+ };
60
+
61
+ export const ___array_prototype_lastIndexOf = (_this: any[], searchElement: any, position: number) => {
62
+ const len: i32 = _this.length;
63
+ if (position > 0) {
64
+ if (position > len) position = len;
65
+ else position |= 0;
66
+ } else position = 0;
67
+
68
+ for (let i: i32 = len - 1; i >= position; i--) {
69
+ if (_this[i] == searchElement) return i;
70
+ }
71
+
72
+ return -1;
73
+ };
74
+
75
+ export const ___array_prototype_includes = (_this: any[], searchElement: any, position: number) => {
76
+ const len: i32 = _this.length;
77
+ if (position > 0) {
78
+ if (position > len) position = len;
79
+ else position |= 0;
80
+ } else position = 0;
81
+
82
+ for (let i: i32 = position; i < len; i++) {
83
+ if (_this[i] == searchElement) return true;
84
+ }
85
+
86
+ return false;
87
+ };
88
+
89
+ export const ___array_prototype_with = (_this: any[], index: number, value: any) => {
90
+ const len: i32 = _this.length;
91
+ if (index < 0) {
92
+ index = len + index;
93
+ if (index < 0) {
94
+ // todo: throw RangeError: Invalid index
95
+ return null;
96
+ }
97
+ }
98
+
99
+ if (index > len) {
100
+ // todo: throw RangeError: Invalid index
101
+ return null;
102
+ }
103
+
104
+ // todo: allocator is bad here?
105
+ let out: any[] = [];
106
+
107
+ Porffor.clone(_this, out);
108
+
109
+ out[index] = value;
110
+
111
+ return out;
112
+ };
113
+
114
+ export const ___array_prototype_reverse = (_this: any[]) => {
115
+ const len: i32 = _this.length;
116
+
117
+ let start: i32 = 0;
118
+ let end: i32 = len - 1;
119
+
120
+ while (start < end) {
121
+ const tmp: i32 = _this[start];
122
+ _this[start++] = _this[end];
123
+ _this[end--] = tmp;
124
+ }
125
+
126
+ return _this;
127
+ };
128
+
129
+ // todo: this has memory/allocation bugs so sometimes crashes :(
130
+ export const ___array_prototype_toReversed = (_this: any[]) => {
131
+ const len: i32 = _this.length;
132
+
133
+ let start: i32 = 0;
134
+ let end: i32 = len - 1;
135
+
136
+ let out: any[] = [];
137
+ out.length = len;
138
+
139
+ while (start < end) {
140
+ out[start] = _this[end];
141
+ out[end--] = _this[start++];
142
+ }
143
+
144
+ return out;
145
+ };