porffor 0.2.0-c7b7423 → 0.2.0-c908b46
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.
- package/CONTRIBUTING.md +256 -0
- package/LICENSE +20 -20
- package/README.md +157 -77
- package/asur/README.md +2 -0
- package/asur/index.js +1262 -0
- package/byg/index.js +237 -0
- package/compiler/2c.js +322 -72
- package/compiler/{sections.js → assemble.js} +64 -16
- package/compiler/builtins/annexb_string.js +72 -0
- package/compiler/builtins/annexb_string.ts +18 -0
- package/compiler/builtins/array.ts +145 -0
- package/compiler/builtins/base64.ts +76 -0
- package/compiler/builtins/boolean.ts +21 -0
- package/compiler/builtins/crypto.ts +120 -0
- package/compiler/builtins/date.ts +2070 -0
- package/compiler/builtins/escape.ts +141 -0
- package/compiler/builtins/int.ts +147 -0
- package/compiler/builtins/number.ts +534 -0
- package/compiler/builtins/porffor.d.ts +59 -0
- package/compiler/builtins/string.ts +1070 -0
- package/compiler/builtins/tostring.ts +37 -0
- package/compiler/builtins.js +580 -272
- package/compiler/{codeGen.js → codegen.js} +1390 -553
- package/compiler/decompile.js +3 -4
- package/compiler/embedding.js +22 -22
- package/compiler/encoding.js +98 -114
- package/compiler/generated_builtins.js +1517 -0
- package/compiler/index.js +36 -34
- package/compiler/log.js +6 -3
- package/compiler/opt.js +65 -29
- package/compiler/parse.js +38 -29
- package/compiler/precompile.js +128 -0
- package/compiler/prefs.js +27 -0
- package/compiler/prototype.js +182 -42
- package/compiler/types.js +37 -0
- package/compiler/wasmSpec.js +31 -7
- package/compiler/wrap.js +141 -43
- package/package.json +9 -5
- package/porf +4 -0
- package/rhemyn/compile.js +46 -27
- package/rhemyn/parse.js +322 -320
- package/rhemyn/test/parse.js +58 -58
- package/runner/compare.js +34 -34
- package/runner/debug.js +122 -0
- package/runner/index.js +91 -11
- package/runner/profiler.js +102 -0
- package/runner/repl.js +44 -11
- package/runner/sizes.js +37 -37
- package/compiler/builtins/base64.js +0 -92
- package/runner/info.js +0 -89
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/runner/transform.js +0 -15
- package/tmp.c +0 -69
- 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 =
|
30
|
-
if (compileHints) log.warning('
|
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('
|
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('
|
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
|
99
|
+
// for now just do everything as optimize eager
|
99
100
|
encodeVector(funcs.map(_ => chHint(0x02, 0x02, 0x02)))
|
100
101
|
);
|
101
102
|
|
102
|
-
const
|
103
|
-
|
104
|
-
|
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 (
|
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 (
|
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,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,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
|
+
}; */
|
@@ -0,0 +1,21 @@
|
|
1
|
+
// @porf --funsafe-no-unlikely-proto-checks --valtype=i32
|
2
|
+
|
3
|
+
// 20.3.3.2 Boolean.prototype.toString ()
|
4
|
+
// https://tc39.es/ecma262/#sec-boolean.prototype.tostring
|
5
|
+
export const __Boolean_prototype_toString = (_this: boolean) => {
|
6
|
+
// 1. Let b be ? ThisBooleanValue(this value).
|
7
|
+
// 2. If b is true, return "true"; else return "false".
|
8
|
+
|
9
|
+
let out: bytestring = '';
|
10
|
+
if (_this) out = 'true';
|
11
|
+
else out = 'false';
|
12
|
+
|
13
|
+
return out;
|
14
|
+
};
|
15
|
+
|
16
|
+
// 20.3.3.3 Boolean.prototype.valueOf ()
|
17
|
+
// https://tc39.es/ecma262/#sec-boolean.prototype.valueof
|
18
|
+
export const __Boolean_prototype_valueOf = (_this: boolean) => {
|
19
|
+
// 1. Return ? ThisBooleanValue(this value).
|
20
|
+
return _this;
|
21
|
+
};
|
@@ -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
|
+
};
|