porffor 0.2.0-dcc06c8 → 0.2.0-e04e26f

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 (55) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +63 -44
  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 +1 -1
  7. package/compiler/{sections.js → assemble.js} +58 -11
  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 +103 -40
  12. package/compiler/builtins/crypto.ts +120 -0
  13. package/compiler/builtins/date.ts +1128 -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 +33 -1
  18. package/compiler/builtins/string.ts +1055 -0
  19. package/compiler/builtins/tostring.ts +45 -0
  20. package/compiler/builtins.js +452 -238
  21. package/compiler/{codeGen.js → codegen.js} +799 -290
  22. package/compiler/embedding.js +22 -22
  23. package/compiler/encoding.js +108 -10
  24. package/compiler/generated_builtins.js +1133 -0
  25. package/compiler/index.js +16 -14
  26. package/compiler/log.js +6 -3
  27. package/compiler/opt.js +23 -22
  28. package/compiler/parse.js +31 -25
  29. package/compiler/precompile.js +66 -22
  30. package/compiler/prefs.js +5 -1
  31. package/compiler/prototype.js +4 -20
  32. package/compiler/types.js +37 -0
  33. package/compiler/wasmSpec.js +28 -8
  34. package/compiler/wrap.js +51 -47
  35. package/package.json +9 -5
  36. package/porf +2 -0
  37. package/rhemyn/compile.js +3 -2
  38. package/rhemyn/parse.js +323 -320
  39. package/rhemyn/test/parse.js +58 -58
  40. package/runner/compare.js +34 -34
  41. package/runner/debug.js +122 -0
  42. package/runner/index.js +31 -9
  43. package/runner/profiler.js +102 -0
  44. package/runner/repl.js +40 -7
  45. package/runner/sizes.js +37 -37
  46. package/demo.js +0 -3
  47. package/demo.ts +0 -1
  48. package/filesize.cmd +0 -2
  49. package/hello +0 -0
  50. package/runner/info.js +0 -89
  51. package/runner/profile.js +0 -46
  52. package/runner/results.json +0 -1
  53. package/runner/transform.js +0 -15
  54. package/tmp.c +0 -152
  55. 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,6 +1,6 @@
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
6
  import Prefs from './prefs.js';
@@ -28,11 +28,11 @@ export default (funcs, globals, tags, pages, data, flags) => {
28
28
  const optLevel = parseInt(process.argv.find(x => x.startsWith('-O'))?.[2] ?? 1);
29
29
 
30
30
  const compileHints = Prefs.compileHints;
31
- if (compileHints) log.warning('sections', 'compile hints is V8 only w/ experimental arg! (you used -compile-hints)');
31
+ if (compileHints) log.warning('assemble', 'compile hints is V8 only w/ experimental arg! (you used -compile-hints)');
32
32
 
33
33
  const getType = (params, returns) => {
34
34
  const hash = `${params.join(',')}_${returns.join(',')}`;
35
- if (Prefs.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]}`);
36
36
  if (optLevel >= 1 && typeCache[hash] !== undefined) return typeCache[hash];
37
37
 
38
38
  const type = [ FuncType, ...encodeVector(params), ...encodeVector(returns) ];
@@ -45,7 +45,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
45
45
 
46
46
  let importFuncs = [];
47
47
 
48
- if (optLevel < 1) {
48
+ if (optLevel < 1 || !Prefs.treeshakeWasmImports) {
49
49
  importFuncs = importedFuncs;
50
50
  } else {
51
51
  let imports = new Map();
@@ -80,11 +80,11 @@ export default (funcs, globals, tags, pages, data, flags) => {
80
80
  }
81
81
  globalThis.importFuncs = importFuncs;
82
82
 
83
- if (Prefs.optLog) log('sections', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);
83
+ if (Prefs.optLog) log('assemble', `treeshake: using ${importFuncs.length}/${importedFuncs.length} imports`);
84
84
 
85
85
  const importSection = importFuncs.length === 0 ? [] : createSection(
86
86
  Section.import,
87
- 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)) ]))
88
88
  );
89
89
 
90
90
  const funcSection = createSection(
@@ -100,10 +100,57 @@ export default (funcs, globals, tags, pages, data, flags) => {
100
100
  encodeVector(funcs.map(_ => chHint(0x02, 0x02, 0x02)))
101
101
  );
102
102
 
103
- const globalSection = Object.keys(globals).length === 0 ? [] : createSection(
104
- Section.global,
105
- encodeVector(Object.keys(globals).map(x => [ globals[x].type, 0x01, ...number(globals[x].init ?? 0, globals[x].type).flat(), Opcodes.end ]))
106
- );
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
+ // }
107
154
 
108
155
  const exports = funcs.filter(x => x.export).map((x, i) => [ ...encodeString(x.name === 'main' ? 'm' : x.name), ExportDesc.func, x.index ]);
109
156
 
@@ -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
+ };