porffor 0.2.0-fde989a → 0.2.0-fdf0fc5

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 (58) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/LICENSE +20 -20
  3. package/README.md +123 -85
  4. package/asur/README.md +2 -0
  5. package/asur/index.js +1262 -0
  6. package/byg/index.js +237 -0
  7. package/compiler/2c.js +1 -1
  8. package/compiler/{sections.js → assemble.js} +63 -15
  9. package/compiler/builtins/annexb_string.js +72 -0
  10. package/compiler/builtins/annexb_string.ts +18 -0
  11. package/compiler/builtins/array.ts +149 -0
  12. package/compiler/builtins/base64.ts +76 -0
  13. package/compiler/builtins/boolean.ts +20 -0
  14. package/compiler/builtins/crypto.ts +120 -0
  15. package/compiler/builtins/date.ts +2070 -0
  16. package/compiler/builtins/escape.ts +141 -0
  17. package/compiler/builtins/function.ts +7 -0
  18. package/compiler/builtins/int.ts +147 -0
  19. package/compiler/builtins/number.ts +534 -0
  20. package/compiler/builtins/object.ts +6 -0
  21. package/compiler/builtins/porffor.d.ts +59 -0
  22. package/compiler/builtins/set.ts +5 -0
  23. package/compiler/builtins/string.ts +1080 -0
  24. package/compiler/builtins.js +435 -279
  25. package/compiler/{codeGen.js → codegen.js} +1034 -404
  26. package/compiler/decompile.js +0 -1
  27. package/compiler/embedding.js +22 -22
  28. package/compiler/encoding.js +108 -10
  29. package/compiler/generated_builtins.js +1526 -0
  30. package/compiler/index.js +25 -34
  31. package/compiler/log.js +6 -3
  32. package/compiler/opt.js +50 -36
  33. package/compiler/parse.js +33 -23
  34. package/compiler/precompile.js +128 -0
  35. package/compiler/prefs.js +27 -0
  36. package/compiler/prototype.js +27 -42
  37. package/compiler/types.js +38 -0
  38. package/compiler/wasmSpec.js +28 -8
  39. package/compiler/wrap.js +51 -46
  40. package/package.json +9 -5
  41. package/porf +2 -0
  42. package/rhemyn/compile.js +46 -27
  43. package/rhemyn/parse.js +322 -320
  44. package/rhemyn/test/parse.js +58 -58
  45. package/runner/compare.js +34 -34
  46. package/runner/debug.js +122 -0
  47. package/runner/index.js +78 -11
  48. package/runner/profiler.js +102 -0
  49. package/runner/repl.js +42 -9
  50. package/runner/sizes.js +37 -37
  51. package/compiler/builtins/base64.js +0 -92
  52. package/filesize.cmd +0 -2
  53. package/runner/info.js +0 -89
  54. package/runner/profile.js +0 -46
  55. package/runner/results.json +0 -1
  56. package/runner/transform.js +0 -15
  57. package/tmp.c +0 -661
  58. 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
@@ -114,7 +114,7 @@ const todo = msg => {
114
114
  }
115
115
  }
116
116
 
117
- throw new TodoError(`todo: ${msg}`);
117
+ throw new TodoError(msg);
118
118
  };
119
119
 
120
120
  const removeBrackets = str => {
@@ -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,18 @@
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
+
3
+ export const __String_prototype_trimLeft = (_this: string) => {
4
+ return __String_prototype_trimStart(_this);
5
+ };
6
+
7
+ export const __ByteString_prototype_trimLeft = (_this: string) => {
8
+ return __ByteString_prototype_trimStart(_this);
9
+ };
10
+
11
+
12
+ export const __String_prototype_trimRight = (_this: string) => {
13
+ return __String_prototype_trimEnd(_this);
14
+ };
15
+
16
+ export const __ByteString_prototype_trimEnd = (_this: string) => {
17
+ return __ByteString_prototype_trimRight(_this);
18
+ };
@@ -0,0 +1,149 @@
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
+ };
146
+
147
+ export const __Array_prototype_valueOf = (_this: any[]) => {
148
+ return _this;
149
+ };
@@ -0,0 +1,76 @@
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
+
3
+ export const btoa = (input: bytestring): bytestring => {
4
+ const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
5
+ const keyStrPtr: i32 = Porffor.wasm`local.get ${keyStr}`;
6
+
7
+ let len: i32 = input.length;
8
+ let output: bytestring = '';
9
+ output.length = 4 * (len / 3 + !!(len % 3));
10
+
11
+ let i: i32 = Porffor.wasm`local.get ${input}`,
12
+ j: i32 = Porffor.wasm`local.get ${output}`;
13
+
14
+ // todo/perf: add some per 6 char variant using bitwise magic
15
+
16
+ const endPtr = i + len;
17
+ while (i < endPtr) {
18
+ const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
19
+ const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
20
+ const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
21
+
22
+ const enc1: i32 = chr1 >> 2;
23
+ const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
24
+ let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
25
+ let enc4: i32 = chr3 & 63;
26
+
27
+ if (chr2 == -1) {
28
+ enc3 = 64;
29
+ enc4 = 64;
30
+ } else if (chr3 == -1) {
31
+ enc4 = 64;
32
+ }
33
+
34
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
35
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
36
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
37
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
38
+ }
39
+
40
+ return output;
41
+ };
42
+
43
+ // todo: impl atob by converting below to "porf ts"
44
+ /* var atob = function (input) {
45
+ const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
46
+
47
+ let output = "";
48
+ let chr1, chr2, chr3;
49
+ let enc1, enc2, enc3, enc4;
50
+ let i = 0;
51
+
52
+ while (i < input.length) {
53
+ enc1 = keyStr.indexOf(input.charAt(i++));
54
+ enc2 = keyStr.indexOf(input.charAt(i++));
55
+ enc3 = keyStr.indexOf(input.charAt(i++));
56
+ enc4 = keyStr.indexOf(input.charAt(i++));
57
+
58
+ chr1 = (enc1 << 2) | (enc2 >> 4);
59
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
60
+ chr3 = ((enc3 & 3) << 6) | enc4;
61
+
62
+ // output += String.fromCharCode(chr1);
63
+ Porffor.bytestring.appendCharCode(output, chr1);
64
+
65
+ if (enc3 != 64) {
66
+ // output += String.fromCharCode(chr2);
67
+ Porffor.bytestring.appendCharCode(output, chr2);
68
+ }
69
+ if (enc4 != 64) {
70
+ // output += String.fromCharCode(chr3);
71
+ Porffor.bytestring.appendCharCode(output, chr3);
72
+ }
73
+ }
74
+
75
+ return output;
76
+ }; */