porffor 0.2.0-c6c8c81 → 0.2.0-c87ffeb

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 (54) hide show
  1. package/.vscode/launch.json +18 -0
  2. package/LICENSE +20 -20
  3. package/README.md +131 -71
  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 +325 -72
  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 +19 -0
  11. package/compiler/builtins/array.ts +145 -0
  12. package/compiler/builtins/base64.ts +151 -0
  13. package/compiler/builtins/crypto.ts +120 -0
  14. package/compiler/builtins/date.ts +7 -0
  15. package/compiler/builtins/escape.ts +141 -0
  16. package/compiler/builtins/int.ts +147 -0
  17. package/compiler/builtins/number.ts +527 -0
  18. package/compiler/builtins/porffor.d.ts +42 -0
  19. package/compiler/builtins/string.ts +1055 -0
  20. package/compiler/builtins/tostring.ts +45 -0
  21. package/compiler/builtins.js +601 -269
  22. package/compiler/{codeGen.js → codegen.js} +1231 -472
  23. package/compiler/decompile.js +11 -12
  24. package/compiler/embedding.js +22 -22
  25. package/compiler/encoding.js +98 -114
  26. package/compiler/generated_builtins.js +695 -0
  27. package/compiler/index.js +40 -37
  28. package/compiler/log.js +6 -3
  29. package/compiler/opt.js +65 -29
  30. package/compiler/parse.js +42 -11
  31. package/compiler/precompile.js +123 -0
  32. package/compiler/prefs.js +26 -0
  33. package/compiler/prototype.js +177 -37
  34. package/compiler/types.js +37 -0
  35. package/compiler/wasmSpec.js +30 -7
  36. package/compiler/wrap.js +141 -45
  37. package/package.json +9 -5
  38. package/porf +4 -0
  39. package/rhemyn/compile.js +5 -3
  40. package/rhemyn/parse.js +323 -320
  41. package/rhemyn/test/parse.js +58 -58
  42. package/runner/compare.js +34 -34
  43. package/runner/debug.js +122 -0
  44. package/runner/index.js +49 -10
  45. package/runner/profiler.js +102 -0
  46. package/runner/repl.js +42 -9
  47. package/runner/sizes.js +37 -37
  48. package/test262_changes_from_1afe9b87d2_to_04-09.md +270 -0
  49. package/compiler/builtins/base64.js +0 -92
  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/util/enum.js +0 -20
@@ -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) ];
@@ -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;
@@ -150,7 +198,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
150
198
 
151
199
  if (typeCount !== 0) localDecl.push(encodeLocal(typeCount, lastType));
152
200
 
153
- return encodeVector([ ...encodeVector(localDecl), ...x.wasm.flat().filter(x => x <= 0xff), Opcodes.end ]);
201
+ return encodeVector([ ...encodeVector(localDecl), ...x.wasm.flat().filter(x => x != null && x <= 0xff), Opcodes.end ]);
154
202
  }))
155
203
  );
156
204
 
@@ -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
+ };
@@ -0,0 +1,151 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+
3
+ // while (len >= 8) {
4
+ // Porffor.wasm`
5
+ // local tmp i64
6
+ // local.get ${i}
7
+ // i64.load 0 4
8
+ // local.set tmp
9
+
10
+ // local k i64
11
+ // i64.const 0
12
+ // local.set k
13
+
14
+ // loop 64
15
+ // local.get ${j}
16
+
17
+ // local.get ${keyStrPtr}
18
+
19
+ // local.get tmp
20
+
21
+ // ;; k * 6
22
+ // i64.const 58
23
+
24
+ // local.get k
25
+ // i64.const 6
26
+ // i64.mul
27
+
28
+ // i64.sub
29
+
30
+ // ;; tmp >> (58 - (k * 6))
31
+ // i64.shr_u
32
+
33
+ // ;; (tmp >> (58 - (k * 6))) & 0x3f
34
+ // i64.const 63
35
+ // i64.and
36
+
37
+ // i32.wrap_i64
38
+
39
+ // ;; keyStrPtr + ...
40
+ // i32.add
41
+
42
+ // ;; load character from keyStr
43
+ // i32.load8_u 0 4
44
+
45
+ // ;; store in output at j
46
+ // i32.store8 0 4
47
+
48
+ // local.get ${j}
49
+ // i32.const 1
50
+ // i32.add
51
+ // local.set ${j}
52
+
53
+ // local.get k
54
+ // i64.const 1
55
+ // i64.add
56
+ // local.tee k
57
+
58
+ // i64.const 8
59
+ // i64.lt_s
60
+ // br_if 0
61
+ // end
62
+
63
+ // `;
64
+
65
+ // // len -= 6;
66
+ // i += 6;
67
+ // }
68
+
69
+ // // while (k < 8) {
70
+ // // Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + Porffor.wasm.i32.wrap_i64(Porffor.wasm.i64.and(
71
+ // // Porffor.wasm.i64.shr_u(tmp, Porffor.wasm.i64.extend_i32_u(58 - k * 6)),
72
+ // // Porffor.wasm.i64.const(0x3f)
73
+ // // )), 0, 4), 0, 4);
74
+ // // k += 1;
75
+ // // }
76
+
77
+ // i += 6;
78
+ // len -= 6;
79
+ // }
80
+
81
+ export const btoa = (input: bytestring): bytestring => {
82
+ const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
83
+ const keyStrPtr: i32 = Porffor.wasm`local.get ${keyStr}`;
84
+
85
+ let len: i32 = input.length;
86
+ let output: bytestring = '';
87
+ output.length = 4 * (len / 3 + !!(len % 3));
88
+
89
+ let i: i32 = Porffor.wasm`local.get ${input}`,
90
+ j: i32 = Porffor.wasm`local.get ${output}`;
91
+
92
+ const endPtr = i + len;
93
+ while (i < endPtr) {
94
+ const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
95
+ const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
96
+ const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
97
+
98
+ const enc1: i32 = chr1 >> 2;
99
+ const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
100
+ let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
101
+ let enc4: i32 = chr3 & 63;
102
+
103
+ if (chr2 == -1) {
104
+ enc3 = 64;
105
+ enc4 = 64;
106
+ } else if (chr3 == -1) {
107
+ enc4 = 64;
108
+ }
109
+
110
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
111
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
112
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
113
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
114
+ }
115
+
116
+ return output;
117
+ };
118
+
119
+ /* var atob = function (input) {
120
+ const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
121
+
122
+ let output = "";
123
+ let chr1, chr2, chr3;
124
+ let enc1, enc2, enc3, enc4;
125
+ let i = 0;
126
+
127
+ while (i < input.length) {
128
+ enc1 = keyStr.indexOf(input.charAt(i++));
129
+ enc2 = keyStr.indexOf(input.charAt(i++));
130
+ enc3 = keyStr.indexOf(input.charAt(i++));
131
+ enc4 = keyStr.indexOf(input.charAt(i++));
132
+
133
+ chr1 = (enc1 << 2) | (enc2 >> 4);
134
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
135
+ chr3 = ((enc3 & 3) << 6) | enc4;
136
+
137
+ // output += String.fromCharCode(chr1);
138
+ Porffor.bytestring.appendCharCode(output, chr1);
139
+
140
+ if (enc3 != 64) {
141
+ // output += String.fromCharCode(chr2);
142
+ Porffor.bytestring.appendCharCode(output, chr2);
143
+ }
144
+ if (enc4 != 64) {
145
+ // output += String.fromCharCode(chr3);
146
+ Porffor.bytestring.appendCharCode(output, chr3);
147
+ }
148
+ }
149
+
150
+ return output;
151
+ }; */
@@ -0,0 +1,120 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+
3
+ export const __crypto_randomUUID = (): bytestring => {
4
+ let bytes: bytestring = '................';
5
+
6
+ const bytesPtr: i32 = Porffor.wasm`local.get ${bytes}`;
7
+
8
+ let a: i32 = bytesPtr;
9
+ let aEndPtr: i32 = a + 16;
10
+ while (a < aEndPtr) {
11
+ Porffor.wasm.i32.store8(a++, Porffor.randomByte(), 0, 4);
12
+ }
13
+
14
+ // bytes[6] = (bytes[6] & 0b00001111) | 0b01000000
15
+ Porffor.wasm.i32.store8(
16
+ bytesPtr,
17
+ (Porffor.wasm.i32.load8_u(bytesPtr, 0, 10) & 0b00001111) | 0b01000000,
18
+ 0,
19
+ 10 // 4 + 6
20
+ );
21
+
22
+ // bytes[8] = (bytes[8] & 0b00111111) | 0b10000000
23
+ Porffor.wasm.i32.store8(
24
+ bytesPtr,
25
+ (Porffor.wasm.i32.load8_u(bytesPtr, 0, 12) & 0b00111111) | 0b10000000,
26
+ 0,
27
+ 12 // 4 + 8
28
+ );
29
+
30
+ let output: bytestring = '------------------------------------';
31
+
32
+ let i: i32 = Porffor.wasm`local.get ${output}`;
33
+ let j: i32 = bytesPtr;
34
+
35
+ // bytes[0..4]-bytes[4..6]-bytes[6..8]-bytes[8..10]-bytes[10..15]
36
+ // 00112233-4455-6677-8899-aabbccddeeff
37
+
38
+ // bytes[0..4]-
39
+ let endPtr: i32 = i + 8;
40
+ while (i < endPtr) {
41
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
42
+
43
+ let lower: i32 = (byte & 0x0f) + 48;
44
+ if (lower > 57) lower += 39;
45
+
46
+ let upper: i32 = (byte >> 4) + 48;
47
+ if (upper > 57) upper += 39;
48
+
49
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
50
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
51
+ }
52
+ i++;
53
+
54
+ // bytes[4..6]-
55
+ endPtr = i + 4;
56
+ while (i < endPtr) {
57
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
58
+
59
+ let lower: i32 = (byte & 0x0f) + 48;
60
+ if (lower > 57) lower += 39;
61
+
62
+ let upper: i32 = (byte >> 4) + 48;
63
+ if (upper > 57) upper += 39;
64
+
65
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
66
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
67
+ }
68
+ i++;
69
+
70
+ // bytes[6..8]-
71
+ endPtr = i + 4;
72
+ while (i < endPtr) {
73
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
74
+
75
+ let lower: i32 = (byte & 0x0f) + 48;
76
+ if (lower > 57) lower += 39;
77
+
78
+ let upper: i32 = (byte >> 4) + 48;
79
+ if (upper > 57) upper += 39;
80
+
81
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
82
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
83
+ }
84
+ i++;
85
+
86
+ // bytes[8..10]-
87
+ endPtr = i + 4;
88
+ while (i < endPtr) {
89
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
90
+
91
+ let lower: i32 = (byte & 0x0f) + 48;
92
+ if (lower > 57) lower += 39;
93
+
94
+ let upper: i32 = (byte >> 4) + 48;
95
+ if (upper > 57) upper += 39;
96
+
97
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
98
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
99
+ }
100
+ i++;
101
+
102
+ // bytes[10..15]
103
+ endPtr = i + 12;
104
+ while (i < endPtr) {
105
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
106
+
107
+ let lower: i32 = (byte & 0x0f) + 48;
108
+ if (lower > 57) lower += 39;
109
+
110
+ let upper: i32 = (byte >> 4) + 48;
111
+ if (upper > 57) upper += 39;
112
+
113
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
114
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
115
+ }
116
+ i++;
117
+
118
+
119
+ return output;
120
+ };
@@ -0,0 +1,7 @@
1
+ export const Date = (): bytestring => {
2
+
3
+ };
4
+
5
+ export const Date$constructor = (): Date => {
6
+
7
+ };