@zappdev/cli 0.1.0
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/README.md +55 -0
- package/dist/zapp-cli.js +9471 -0
- package/native/src/app/app.zc +490 -0
- package/native/src/event/event.zc +24 -0
- package/native/src/event/events.zc +70 -0
- package/native/src/platform/darwin/backend.zc +923 -0
- package/native/src/platform/darwin/backend_bootstrap.zc +9 -0
- package/native/src/platform/darwin/bootstrap.zc +9 -0
- package/native/src/platform/darwin/engine_jsc.zc +86 -0
- package/native/src/platform/darwin/engine_qjs.zc +92 -0
- package/native/src/platform/darwin/platform.zc +156 -0
- package/native/src/platform/darwin/webview.zc +550 -0
- package/native/src/platform/darwin/webview_bootstrap.zc +9 -0
- package/native/src/platform/darwin/window.zc +1223 -0
- package/native/src/platform/darwin/worker/common.zc +223 -0
- package/native/src/platform/darwin/worker/core/base64_core.zc +29 -0
- package/native/src/platform/darwin/worker/core/crypto_core.zc +19 -0
- package/native/src/platform/darwin/worker/core/encoding_core.zc +32 -0
- package/native/src/platform/darwin/worker/core/fetch_core.zc +145 -0
- package/native/src/platform/darwin/worker/core/url_core.zc +69 -0
- package/native/src/platform/darwin/worker/core/websocket_core.zc +179 -0
- package/native/src/platform/darwin/worker/dispatch.zc +55 -0
- package/native/src/platform/darwin/worker/jsc/base64_jsc.zc +39 -0
- package/native/src/platform/darwin/worker/jsc/crypto_jsc.zc +49 -0
- package/native/src/platform/darwin/worker/jsc/encoding_jsc.zc +86 -0
- package/native/src/platform/darwin/worker/jsc/fetch_jsc.zc +149 -0
- package/native/src/platform/darwin/worker/jsc/url_jsc.zc +54 -0
- package/native/src/platform/darwin/worker/jsc/websocket_jsc.zc +127 -0
- package/native/src/platform/darwin/worker/jsc.zc +670 -0
- package/native/src/platform/darwin/worker/mod.zc +30 -0
- package/native/src/platform/darwin/worker/qjs/fetch_qjs.zc +233 -0
- package/native/src/platform/darwin/worker/qjs/qjs_macros.zc +23 -0
- package/native/src/platform/darwin/worker/qjs/websocket_qjs.zc +223 -0
- package/native/src/platform/darwin/worker/qjs.zc +1053 -0
- package/native/src/platform/darwin/worker/timers.zc +149 -0
- package/native/src/platform/darwin/worker/timers_qjs.zc +209 -0
- package/native/src/platform/platform.zc +64 -0
- package/native/src/platform/shared/log.zc +156 -0
- package/native/src/platform/shared/worker/qjs/base64_qjs.zc +38 -0
- package/native/src/platform/shared/worker/qjs/crypto_qjs.zc +44 -0
- package/native/src/platform/shared/worker/qjs/encoding_qjs.zc +95 -0
- package/native/src/platform/shared/worker/qjs/url_qjs.zc +65 -0
- package/native/src/platform/shared/worker_registry.zc +206 -0
- package/native/src/platform/window.zc +446 -0
- package/native/src/platform/windows/backend.zc +452 -0
- package/native/src/platform/windows/backend_bootstrap.zc +9 -0
- package/native/src/platform/windows/bootstrap.zc +9 -0
- package/native/src/platform/windows/engine_qjs.zc +60 -0
- package/native/src/platform/windows/platform.zc +387 -0
- package/native/src/platform/windows/webview.zc +1175 -0
- package/native/src/platform/windows/webview_bootstrap.zc +9 -0
- package/native/src/platform/windows/window.zc +1271 -0
- package/native/src/platform/windows/worker/common.zc +409 -0
- package/native/src/platform/windows/worker/core/base64_core.zc +52 -0
- package/native/src/platform/windows/worker/core/crypto_core.zc +34 -0
- package/native/src/platform/windows/worker/core/encoding_core.zc +60 -0
- package/native/src/platform/windows/worker/core/fetch_core.zc +274 -0
- package/native/src/platform/windows/worker/core/url_core.zc +216 -0
- package/native/src/platform/windows/worker/core/websocket_core.zc +343 -0
- package/native/src/platform/windows/worker/dispatch.zc +34 -0
- package/native/src/platform/windows/worker/mod.zc +46 -0
- package/native/src/platform/windows/worker/qjs/fetch_qjs.zc +255 -0
- package/native/src/platform/windows/worker/qjs/websocket_qjs.zc +263 -0
- package/native/src/platform/windows/worker/qjs.zc +1049 -0
- package/native/src/platform/windows/worker/timers_qjs.zc +288 -0
- package/native/src/platform/worker.zc +8 -0
- package/native/src/service/service.zc +228 -0
- package/native/vendor/quickjs-ng/.gitattributes +4 -0
- package/native/vendor/quickjs-ng/.github/dependabot.yml +7 -0
- package/native/vendor/quickjs-ng/.github/workflows/ci.yml +812 -0
- package/native/vendor/quickjs-ng/.github/workflows/docs.yml +49 -0
- package/native/vendor/quickjs-ng/.github/workflows/release.yml +162 -0
- package/native/vendor/quickjs-ng/.github/workflows/test-docs.yml +23 -0
- package/native/vendor/quickjs-ng/.github/workflows/tsan.yml +32 -0
- package/native/vendor/quickjs-ng/.github/workflows/valgrind.yml +33 -0
- package/native/vendor/quickjs-ng/.gitmodules +5 -0
- package/native/vendor/quickjs-ng/CMakeLists.txt +553 -0
- package/native/vendor/quickjs-ng/LICENSE +24 -0
- package/native/vendor/quickjs-ng/Makefile +149 -0
- package/native/vendor/quickjs-ng/amalgam.js +53 -0
- package/native/vendor/quickjs-ng/api-test.c +927 -0
- package/native/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
- package/native/vendor/quickjs-ng/builtin-array-fromasync.js +36 -0
- package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
- package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.js +194 -0
- package/native/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
- package/native/vendor/quickjs-ng/builtin-iterator-zip.js +210 -0
- package/native/vendor/quickjs-ng/ctest.c +17 -0
- package/native/vendor/quickjs-ng/cutils.h +2013 -0
- package/native/vendor/quickjs-ng/cxxtest.cc +2 -0
- package/native/vendor/quickjs-ng/dtoa.c +1619 -0
- package/native/vendor/quickjs-ng/dtoa.h +87 -0
- package/native/vendor/quickjs-ng/examples/fib.c +67 -0
- package/native/vendor/quickjs-ng/examples/fib_module.js +10 -0
- package/native/vendor/quickjs-ng/examples/hello.js +1 -0
- package/native/vendor/quickjs-ng/examples/hello_module.js +6 -0
- package/native/vendor/quickjs-ng/examples/meson.build +17 -0
- package/native/vendor/quickjs-ng/examples/pi_bigint.js +118 -0
- package/native/vendor/quickjs-ng/examples/point.c +154 -0
- package/native/vendor/quickjs-ng/examples/test_fib.js +8 -0
- package/native/vendor/quickjs-ng/examples/test_point.js +43 -0
- package/native/vendor/quickjs-ng/fuzz.c +51 -0
- package/native/vendor/quickjs-ng/gen/function_source.c +81 -0
- package/native/vendor/quickjs-ng/gen/hello.c +53 -0
- package/native/vendor/quickjs-ng/gen/hello_module.c +106 -0
- package/native/vendor/quickjs-ng/gen/repl.c +3053 -0
- package/native/vendor/quickjs-ng/gen/standalone.c +324 -0
- package/native/vendor/quickjs-ng/gen/test_fib.c +81 -0
- package/native/vendor/quickjs-ng/libregexp-opcode.h +58 -0
- package/native/vendor/quickjs-ng/libregexp.c +2687 -0
- package/native/vendor/quickjs-ng/libregexp.h +98 -0
- package/native/vendor/quickjs-ng/libunicode-table.h +4707 -0
- package/native/vendor/quickjs-ng/libunicode.c +1746 -0
- package/native/vendor/quickjs-ng/libunicode.h +126 -0
- package/native/vendor/quickjs-ng/list.h +107 -0
- package/native/vendor/quickjs-ng/lre-test.c +73 -0
- package/native/vendor/quickjs-ng/meson.build +684 -0
- package/native/vendor/quickjs-ng/meson_options.txt +6 -0
- package/native/vendor/quickjs-ng/qjs-wasi-reactor.c +208 -0
- package/native/vendor/quickjs-ng/qjs.c +748 -0
- package/native/vendor/quickjs-ng/qjsc.c +673 -0
- package/native/vendor/quickjs-ng/quickjs-atom.h +267 -0
- package/native/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
- package/native/vendor/quickjs-ng/quickjs-libc.c +4986 -0
- package/native/vendor/quickjs-ng/quickjs-libc.h +79 -0
- package/native/vendor/quickjs-ng/quickjs-opcode.h +369 -0
- package/native/vendor/quickjs-ng/quickjs.c +60259 -0
- package/native/vendor/quickjs-ng/quickjs.h +1419 -0
- package/native/vendor/quickjs-ng/repl.js +1927 -0
- package/native/vendor/quickjs-ng/run-test262.c +2417 -0
- package/native/vendor/quickjs-ng/standalone.js +129 -0
- package/native/vendor/quickjs-ng/tests/assert.js +49 -0
- package/native/vendor/quickjs-ng/tests/bug1221.js +16 -0
- package/native/vendor/quickjs-ng/tests/bug1296.js +12 -0
- package/native/vendor/quickjs-ng/tests/bug1297.js +22 -0
- package/native/vendor/quickjs-ng/tests/bug1301.js +21 -0
- package/native/vendor/quickjs-ng/tests/bug1302.js +24 -0
- package/native/vendor/quickjs-ng/tests/bug1305.js +26 -0
- package/native/vendor/quickjs-ng/tests/bug1318.js +54 -0
- package/native/vendor/quickjs-ng/tests/bug1352.js +8 -0
- package/native/vendor/quickjs-ng/tests/bug1354.js +6 -0
- package/native/vendor/quickjs-ng/tests/bug1355.js +58 -0
- package/native/vendor/quickjs-ng/tests/bug1368.js +9 -0
- package/native/vendor/quickjs-ng/tests/bug39/1.js +6 -0
- package/native/vendor/quickjs-ng/tests/bug39/2.js +6 -0
- package/native/vendor/quickjs-ng/tests/bug39/3.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug488-upstream.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug633/0.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug633/1.js +4 -0
- package/native/vendor/quickjs-ng/tests/bug633/2.js +4 -0
- package/native/vendor/quickjs-ng/tests/bug633/3.js +4 -0
- package/native/vendor/quickjs-ng/tests/bug645/0.js +4 -0
- package/native/vendor/quickjs-ng/tests/bug645/1.js +9 -0
- package/native/vendor/quickjs-ng/tests/bug645/2.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug648.js +13 -0
- package/native/vendor/quickjs-ng/tests/bug652.js +4 -0
- package/native/vendor/quickjs-ng/tests/bug741.js +19 -0
- package/native/vendor/quickjs-ng/tests/bug775.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug776.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug832.js +2 -0
- package/native/vendor/quickjs-ng/tests/bug858.js +26 -0
- package/native/vendor/quickjs-ng/tests/bug904.js +6 -0
- package/native/vendor/quickjs-ng/tests/bug988.js +7 -0
- package/native/vendor/quickjs-ng/tests/bug999.js +3 -0
- package/native/vendor/quickjs-ng/tests/destructured-export.js +8 -0
- package/native/vendor/quickjs-ng/tests/detect_module/0.js +1 -0
- package/native/vendor/quickjs-ng/tests/detect_module/1.js +2 -0
- package/native/vendor/quickjs-ng/tests/detect_module/2.js +1 -0
- package/native/vendor/quickjs-ng/tests/detect_module/3.js +8 -0
- package/native/vendor/quickjs-ng/tests/detect_module/4.js +3 -0
- package/native/vendor/quickjs-ng/tests/empty.js +0 -0
- package/native/vendor/quickjs-ng/tests/fixture_cyclic_import.js +2 -0
- package/native/vendor/quickjs-ng/tests/fixture_string_exports.js +12 -0
- package/native/vendor/quickjs-ng/tests/function_source.js +14 -0
- package/native/vendor/quickjs-ng/tests/microbench.js +1267 -0
- package/native/vendor/quickjs-ng/tests/null_or_undefined.js +38 -0
- package/native/vendor/quickjs-ng/tests/str-pad-leak.js +5 -0
- package/native/vendor/quickjs-ng/tests/test_bigint.js +107 -0
- package/native/vendor/quickjs-ng/tests/test_bjson.js +366 -0
- package/native/vendor/quickjs-ng/tests/test_builtin.js +1314 -0
- package/native/vendor/quickjs-ng/tests/test_closure.js +220 -0
- package/native/vendor/quickjs-ng/tests/test_cyclic_import.js +12 -0
- package/native/vendor/quickjs-ng/tests/test_domexception.js +35 -0
- package/native/vendor/quickjs-ng/tests/test_language.js +755 -0
- package/native/vendor/quickjs-ng/tests/test_loop.js +367 -0
- package/native/vendor/quickjs-ng/tests/test_queue_microtask.js +39 -0
- package/native/vendor/quickjs-ng/tests/test_std.js +340 -0
- package/native/vendor/quickjs-ng/tests/test_string_exports.js +25 -0
- package/native/vendor/quickjs-ng/tests/test_worker.js +43 -0
- package/native/vendor/quickjs-ng/tests/test_worker_module.js +30 -0
- package/native/vendor/quickjs-ng/tests.conf +14 -0
- package/native/vendor/quickjs-ng/unicode_download.sh +19 -0
- package/native/vendor/quickjs-ng/unicode_gen.c +3108 -0
- package/native/vendor/quickjs-ng/unicode_gen_def.h +310 -0
- package/native/vendor/quickjs-ng/update-version.sh +32 -0
- package/native/vendor/webview2/include/WebView2.h +60636 -0
- package/native/vendor/webview2/include/WebView2EnvironmentOptions.h +406 -0
- package/package.json +33 -0
- package/src/backend.ts +139 -0
- package/src/build-config.ts +87 -0
- package/src/build.ts +276 -0
- package/src/common.ts +195 -0
- package/src/config.ts +89 -0
- package/src/dev.ts +164 -0
- package/src/generate.ts +200 -0
- package/src/icons.ts +116 -0
- package/src/init.ts +190 -0
- package/src/package.ts +150 -0
- package/src/zapp-cli.ts +263 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {assert} from "./assert.js"
|
|
2
|
+
|
|
3
|
+
let ex
|
|
4
|
+
try {
|
|
5
|
+
null.x
|
|
6
|
+
} catch (e) {
|
|
7
|
+
ex = e
|
|
8
|
+
}
|
|
9
|
+
assert(ex instanceof TypeError)
|
|
10
|
+
assert(ex.message, "cannot read property 'x' of null")
|
|
11
|
+
ex = undefined
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
null["x"]
|
|
15
|
+
} catch (e) {
|
|
16
|
+
ex = e
|
|
17
|
+
}
|
|
18
|
+
assert(ex instanceof TypeError)
|
|
19
|
+
assert(ex.message, "cannot read property 'x' of null")
|
|
20
|
+
ex = undefined
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
undefined.x
|
|
24
|
+
} catch (e) {
|
|
25
|
+
ex = e
|
|
26
|
+
}
|
|
27
|
+
assert(ex instanceof TypeError)
|
|
28
|
+
assert(ex.message, "cannot read property 'x' of undefined")
|
|
29
|
+
ex = undefined
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
undefined["x"]
|
|
33
|
+
} catch (e) {
|
|
34
|
+
ex = e
|
|
35
|
+
}
|
|
36
|
+
assert(ex instanceof TypeError)
|
|
37
|
+
assert(ex.message, "cannot read property 'x' of undefined")
|
|
38
|
+
ex = undefined
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { assert, assertThrows } from "./assert.js";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function bigint_pow(a, n)
|
|
5
|
+
{
|
|
6
|
+
var r, i;
|
|
7
|
+
r = 1n;
|
|
8
|
+
for(i = 0n; i < n; i++)
|
|
9
|
+
r *= a;
|
|
10
|
+
return r;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* a must be < b */
|
|
14
|
+
function test_less(a, b)
|
|
15
|
+
{
|
|
16
|
+
assert(a < b);
|
|
17
|
+
assert(!(b < a));
|
|
18
|
+
assert(a <= b);
|
|
19
|
+
assert(!(b <= a));
|
|
20
|
+
assert(b > a);
|
|
21
|
+
assert(!(a > b));
|
|
22
|
+
assert(b >= a);
|
|
23
|
+
assert(!(a >= b));
|
|
24
|
+
assert(a != b);
|
|
25
|
+
assert(!(a == b));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* a must be numerically equal to b */
|
|
29
|
+
function test_eq(a, b)
|
|
30
|
+
{
|
|
31
|
+
assert(a == b);
|
|
32
|
+
assert(b == a);
|
|
33
|
+
assert(!(a != b));
|
|
34
|
+
assert(!(b != a));
|
|
35
|
+
assert(a <= b);
|
|
36
|
+
assert(b <= a);
|
|
37
|
+
assert(!(a < b));
|
|
38
|
+
assert(a >= b);
|
|
39
|
+
assert(b >= a);
|
|
40
|
+
assert(!(a > b));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function test_bigint1()
|
|
44
|
+
{
|
|
45
|
+
var a, r;
|
|
46
|
+
|
|
47
|
+
test_less(2n, 3n);
|
|
48
|
+
test_eq(3n, 3n);
|
|
49
|
+
|
|
50
|
+
test_less(2, 3n);
|
|
51
|
+
test_eq(3, 3n);
|
|
52
|
+
|
|
53
|
+
test_less(2.1, 3n);
|
|
54
|
+
test_eq(Math.sqrt(4), 2n);
|
|
55
|
+
|
|
56
|
+
a = bigint_pow(3n, 100n);
|
|
57
|
+
assert((a - 1n) != a);
|
|
58
|
+
assert(a, 515377520732011331036461129765621272702107522001n);
|
|
59
|
+
assert(a, 0x5a4653ca673768565b41f775d6947d55cf3813d1n);
|
|
60
|
+
|
|
61
|
+
r = 1n << 31n;
|
|
62
|
+
assert(r, 2147483648n, "1 << 31n === 2147483648n");
|
|
63
|
+
|
|
64
|
+
r = 1n << 32n;
|
|
65
|
+
assert(r, 4294967296n, "1 << 32n === 4294967296n");
|
|
66
|
+
|
|
67
|
+
assert(String(-9223372036854775808n), "-9223372036854775808");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function test_bigint2()
|
|
71
|
+
{
|
|
72
|
+
assert(BigInt(""), 0n);
|
|
73
|
+
assert(BigInt(" 123"), 123n);
|
|
74
|
+
assert(BigInt(" 123 "), 123n);
|
|
75
|
+
assertThrows(SyntaxError, () => { BigInt("+") } );
|
|
76
|
+
assertThrows(SyntaxError, () => { BigInt("-") } );
|
|
77
|
+
assertThrows(SyntaxError, () => { BigInt("\x00a") } );
|
|
78
|
+
assertThrows(SyntaxError, () => { BigInt(" 123 r") } );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function test_bigint_map()
|
|
82
|
+
{
|
|
83
|
+
var m = new Map();
|
|
84
|
+
assert(m.size, 0);
|
|
85
|
+
|
|
86
|
+
for (let i = 0n; i < 1337n; i++) {
|
|
87
|
+
const r = m.set(i, i.toString());
|
|
88
|
+
assert(r, m);
|
|
89
|
+
}
|
|
90
|
+
assert(m.size, 1337);
|
|
91
|
+
|
|
92
|
+
for (let i = 0n; i < 1337n; i++) {
|
|
93
|
+
const r = m.get(i);
|
|
94
|
+
assert(r, i.toString());
|
|
95
|
+
}
|
|
96
|
+
assert(m.get(1337n), undefined);
|
|
97
|
+
assert(m.size, 1337);
|
|
98
|
+
|
|
99
|
+
for (let i = 0n; i < 1337n; i++)
|
|
100
|
+
assert(m.delete(i));
|
|
101
|
+
assert(!m.delete(1337n));
|
|
102
|
+
assert(m.size, 0);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
test_bigint1();
|
|
106
|
+
test_bigint2();
|
|
107
|
+
test_bigint_map();
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import * as std from "qjs:std";
|
|
2
|
+
import * as bjson from "qjs:bjson";
|
|
3
|
+
import { assert } from "./assert.js";
|
|
4
|
+
|
|
5
|
+
function base64decode(s) {
|
|
6
|
+
var A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
7
|
+
var n = s.indexOf("=");
|
|
8
|
+
if (n < 0) n = s.length;
|
|
9
|
+
if (n & 3 === 1) throw Error("bad base64"); // too much padding
|
|
10
|
+
var r = new Uint8Array(3 * (n>>2) + (n>>1 & 1) + (n & 1));
|
|
11
|
+
var a, b, c, d, i, j;
|
|
12
|
+
a = b = c = d = i = j = 0;
|
|
13
|
+
while (i+3 < n) {
|
|
14
|
+
a = A.indexOf(s[i++]);
|
|
15
|
+
b = A.indexOf(s[i++]);
|
|
16
|
+
c = A.indexOf(s[i++]);
|
|
17
|
+
d = A.indexOf(s[i++]);
|
|
18
|
+
if (~63 & (a|b|c|d)) throw Error("bad base64");
|
|
19
|
+
r[j++] = a<<2 | b>>4;
|
|
20
|
+
r[j++] = 255 & b<<4 | c>>2;
|
|
21
|
+
r[j++] = 255 & c<<6 | d;
|
|
22
|
+
}
|
|
23
|
+
switch (n & 3) {
|
|
24
|
+
case 2:
|
|
25
|
+
a = A.indexOf(s[i++]);
|
|
26
|
+
b = A.indexOf(s[i++]);
|
|
27
|
+
if (~63 & (a|b)) throw Error("bad base64");
|
|
28
|
+
if (b & 15) throw Error("bad base64");
|
|
29
|
+
r[j++] = a<<2 | b>>4;
|
|
30
|
+
break;
|
|
31
|
+
case 3:
|
|
32
|
+
a = A.indexOf(s[i++]);
|
|
33
|
+
b = A.indexOf(s[i++]);
|
|
34
|
+
c = A.indexOf(s[i++]);
|
|
35
|
+
if (~63 & (a|b|c)) throw Error("bad base64");
|
|
36
|
+
if (c & 3) throw Error("bad base64");
|
|
37
|
+
r[j++] = a<<2 | b>>4;
|
|
38
|
+
r[j++] = 255 & b<<4 | c>>2;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
return r.buffer;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function toHex(a)
|
|
45
|
+
{
|
|
46
|
+
var i, s = "", tab, v;
|
|
47
|
+
tab = new Uint8Array(a);
|
|
48
|
+
for(i = 0; i < tab.length; i++) {
|
|
49
|
+
v = tab[i].toString(16);
|
|
50
|
+
if (v.length < 2)
|
|
51
|
+
v = "0" + v;
|
|
52
|
+
if (i !== 0)
|
|
53
|
+
s += " ";
|
|
54
|
+
s += v;
|
|
55
|
+
}
|
|
56
|
+
return s;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isArrayLike(a)
|
|
60
|
+
{
|
|
61
|
+
return Array.isArray(a) ||
|
|
62
|
+
(a instanceof Uint8ClampedArray) ||
|
|
63
|
+
(a instanceof Uint8Array) ||
|
|
64
|
+
(a instanceof Uint16Array) ||
|
|
65
|
+
(a instanceof Uint32Array) ||
|
|
66
|
+
(a instanceof Int8Array) ||
|
|
67
|
+
(a instanceof Int16Array) ||
|
|
68
|
+
(a instanceof Int32Array) ||
|
|
69
|
+
(a instanceof Float16Array) ||
|
|
70
|
+
(a instanceof Float32Array) ||
|
|
71
|
+
(a instanceof Float64Array);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function toStr(a)
|
|
75
|
+
{
|
|
76
|
+
var s, i, props, prop;
|
|
77
|
+
|
|
78
|
+
switch(typeof(a)) {
|
|
79
|
+
case "object":
|
|
80
|
+
if (a === null)
|
|
81
|
+
return "null";
|
|
82
|
+
if (a instanceof Date) {
|
|
83
|
+
s = "Date(" + toStr(a.valueOf()) + ")";
|
|
84
|
+
} else if (a instanceof Number) {
|
|
85
|
+
s = "Number(" + toStr(a.valueOf()) + ")";
|
|
86
|
+
} else if (a instanceof String) {
|
|
87
|
+
s = "String(" + toStr(a.valueOf()) + ")";
|
|
88
|
+
} else if (a instanceof Boolean) {
|
|
89
|
+
s = "Boolean(" + toStr(a.valueOf()) + ")";
|
|
90
|
+
} else if (isArrayLike(a)) {
|
|
91
|
+
s = "[";
|
|
92
|
+
for(i = 0; i < a.length; i++) {
|
|
93
|
+
if (i != 0)
|
|
94
|
+
s += ",";
|
|
95
|
+
s += toStr(a[i]);
|
|
96
|
+
}
|
|
97
|
+
s += "]";
|
|
98
|
+
} else {
|
|
99
|
+
props = Object.keys(a);
|
|
100
|
+
s = "{";
|
|
101
|
+
for(i = 0; i < props.length; i++) {
|
|
102
|
+
if (i != 0)
|
|
103
|
+
s += ",";
|
|
104
|
+
prop = props[i];
|
|
105
|
+
s += prop + ":" + toStr(a[prop]);
|
|
106
|
+
}
|
|
107
|
+
s += "}";
|
|
108
|
+
}
|
|
109
|
+
return s;
|
|
110
|
+
case "undefined":
|
|
111
|
+
return "undefined";
|
|
112
|
+
case "string":
|
|
113
|
+
return JSON.stringify(a);
|
|
114
|
+
case "number":
|
|
115
|
+
if (a == 0 && 1 / a < 0)
|
|
116
|
+
return "-0";
|
|
117
|
+
else
|
|
118
|
+
return a.toString();
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
return a.toString();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function bjson_test(a)
|
|
126
|
+
{
|
|
127
|
+
var buf, r, a_str, r_str;
|
|
128
|
+
a_str = toStr(a);
|
|
129
|
+
buf = bjson.write(a);
|
|
130
|
+
if (0) {
|
|
131
|
+
print(a_str, "->", toHex(buf));
|
|
132
|
+
}
|
|
133
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
134
|
+
r_str = toStr(r);
|
|
135
|
+
if (a_str != r_str) {
|
|
136
|
+
print(a_str);
|
|
137
|
+
print(r_str);
|
|
138
|
+
assert(false);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function bjson_test_arraybuffer()
|
|
143
|
+
{
|
|
144
|
+
var buf, array_buffer;
|
|
145
|
+
|
|
146
|
+
array_buffer = new ArrayBuffer(4);
|
|
147
|
+
assert(array_buffer.byteLength, 4);
|
|
148
|
+
assert(array_buffer.maxByteLength, 4);
|
|
149
|
+
assert(array_buffer.resizable, false);
|
|
150
|
+
buf = bjson.write(array_buffer);
|
|
151
|
+
array_buffer = bjson.read(buf, 0, buf.byteLength);
|
|
152
|
+
assert(array_buffer.byteLength, 4);
|
|
153
|
+
assert(array_buffer.maxByteLength, 4);
|
|
154
|
+
assert(array_buffer.resizable, false);
|
|
155
|
+
|
|
156
|
+
array_buffer = new ArrayBuffer(4, {maxByteLength: 4});
|
|
157
|
+
assert(array_buffer.byteLength, 4);
|
|
158
|
+
assert(array_buffer.maxByteLength, 4);
|
|
159
|
+
assert(array_buffer.resizable, true);
|
|
160
|
+
buf = bjson.write(array_buffer);
|
|
161
|
+
array_buffer = bjson.read(buf, 0, buf.byteLength);
|
|
162
|
+
assert(array_buffer.byteLength, 4);
|
|
163
|
+
assert(array_buffer.maxByteLength, 4);
|
|
164
|
+
assert(array_buffer.resizable, true);
|
|
165
|
+
|
|
166
|
+
array_buffer = new ArrayBuffer(4, {maxByteLength: 8});
|
|
167
|
+
assert(array_buffer.byteLength, 4);
|
|
168
|
+
assert(array_buffer.maxByteLength, 8);
|
|
169
|
+
assert(array_buffer.resizable, true);
|
|
170
|
+
buf = bjson.write(array_buffer);
|
|
171
|
+
array_buffer = bjson.read(buf, 0, buf.byteLength);
|
|
172
|
+
assert(array_buffer.byteLength, 4);
|
|
173
|
+
assert(array_buffer.maxByteLength, 8);
|
|
174
|
+
assert(array_buffer.resizable, true);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/* test multiple references to an object including circular
|
|
178
|
+
references */
|
|
179
|
+
function bjson_test_reference()
|
|
180
|
+
{
|
|
181
|
+
var array, buf, i, n, array_buffer;
|
|
182
|
+
n = 16;
|
|
183
|
+
array = [];
|
|
184
|
+
for(i = 0; i < n; i++)
|
|
185
|
+
array[i] = {};
|
|
186
|
+
array_buffer = new ArrayBuffer(n);
|
|
187
|
+
for(i = 0; i < n; i++) {
|
|
188
|
+
array[i].next = array[(i + 1) % n];
|
|
189
|
+
array[i].idx = i;
|
|
190
|
+
array[i].typed_array = new Uint8Array(array_buffer, i, 1);
|
|
191
|
+
}
|
|
192
|
+
buf = bjson.write(array, bjson.WRITE_OBJ_REFERENCE);
|
|
193
|
+
|
|
194
|
+
array = bjson.read(buf, 0, buf.byteLength, bjson.READ_OBJ_REFERENCE);
|
|
195
|
+
|
|
196
|
+
/* check the result */
|
|
197
|
+
for(i = 0; i < n; i++) {
|
|
198
|
+
assert(array[i].next, array[(i + 1) % n]);
|
|
199
|
+
assert(array[i].idx, i);
|
|
200
|
+
assert(array[i].typed_array.buffer, array_buffer);
|
|
201
|
+
assert(array[i].typed_array.length, 1);
|
|
202
|
+
assert(array[i].typed_array.byteOffset, i);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function bjson_test_regexp()
|
|
207
|
+
{
|
|
208
|
+
var buf, r;
|
|
209
|
+
|
|
210
|
+
bjson_test(/xyzzy/);
|
|
211
|
+
bjson_test(/xyzzy/digu);
|
|
212
|
+
|
|
213
|
+
buf = bjson.write(/(?<ππΈπ°>dog)/);
|
|
214
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
215
|
+
assert("sup dog".match(r).groups["ππΈπ°"], "dog");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function bjson_test_map()
|
|
219
|
+
{
|
|
220
|
+
var buf, r, xs;
|
|
221
|
+
|
|
222
|
+
xs = [["key", "value"]];
|
|
223
|
+
buf = bjson.write(new Map(xs));
|
|
224
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
225
|
+
assert(r instanceof Map);
|
|
226
|
+
assert([...r].toString(), xs.toString());
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function bjson_test_set()
|
|
230
|
+
{
|
|
231
|
+
var buf, r, xs;
|
|
232
|
+
|
|
233
|
+
xs = ["one", "two", "three"];
|
|
234
|
+
buf = bjson.write(new Set(xs));
|
|
235
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
236
|
+
assert(r instanceof Set);
|
|
237
|
+
assert([...r].toString(), xs.toString());
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function bjson_test_symbol()
|
|
241
|
+
{
|
|
242
|
+
var buf, r, o;
|
|
243
|
+
|
|
244
|
+
o = {[Symbol.toStringTag]: "42"};
|
|
245
|
+
buf = bjson.write(o);
|
|
246
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
247
|
+
assert(o.toString(), r.toString());
|
|
248
|
+
|
|
249
|
+
o = Symbol('foo');
|
|
250
|
+
buf = bjson.write(o);
|
|
251
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
252
|
+
assert(o.toString(), r.toString());
|
|
253
|
+
assert(o !== r);
|
|
254
|
+
|
|
255
|
+
o = Symbol.for('foo');
|
|
256
|
+
buf = bjson.write(o);
|
|
257
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
258
|
+
assert(o, r);
|
|
259
|
+
|
|
260
|
+
o = Symbol.toStringTag;
|
|
261
|
+
buf = bjson.write(o);
|
|
262
|
+
r = bjson.read(buf, 0, buf.byteLength);
|
|
263
|
+
assert(o, r);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function bjson_test_bytecode()
|
|
267
|
+
{
|
|
268
|
+
var buf, o, r, e, i;
|
|
269
|
+
|
|
270
|
+
o = std.evalScript(";(function f(o){ return o.i })", {compile_only: true});
|
|
271
|
+
buf = bjson.write(o, bjson.WRITE_OBJ_BYTECODE);
|
|
272
|
+
try {
|
|
273
|
+
bjson.read(buf, 0, buf.byteLength);
|
|
274
|
+
} catch (_e) {
|
|
275
|
+
e = _e;
|
|
276
|
+
}
|
|
277
|
+
assert(String(e), "SyntaxError: no bytecode allowed");
|
|
278
|
+
|
|
279
|
+
o = bjson.read(buf, 0, buf.byteLength, bjson.READ_OBJ_BYTECODE);
|
|
280
|
+
assert(String(o), "[function bytecode]");
|
|
281
|
+
o = std.evalScript(o, {eval_function: true});
|
|
282
|
+
for (i = 0; i < 42; i++) o({i}); // exercise o.i IC
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function bjson_test_fuzz()
|
|
286
|
+
{
|
|
287
|
+
var corpus = [
|
|
288
|
+
["GP////8QAAAAAARg"],
|
|
289
|
+
["GP/////m5uaCLQ=="],
|
|
290
|
+
["GP////8AEQATBgYGBgYGBgYGBgb/////EAARAC8R/78vEf+/"],
|
|
291
|
+
["GP////8ACH8ACv////9//////////////////////////////9//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAA+fn5+fn5+fn5+fn5AAAAAAAGAKs="],
|
|
292
|
+
["GP////8ADgAAABQA=", bjson.READ_OBJ_REFERENCE],
|
|
293
|
+
];
|
|
294
|
+
for (var [input, flags] of corpus) {
|
|
295
|
+
var buf = base64decode(input);
|
|
296
|
+
try {
|
|
297
|
+
bjson.read(buf, 0, buf.byteLength, flags);
|
|
298
|
+
} catch (e) {
|
|
299
|
+
if (/invalid version/.test(e.message)) throw e; // corpus needs update
|
|
300
|
+
if (/checksum error/.test(e.message)) throw e;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function bjson_test_csum()
|
|
306
|
+
{
|
|
307
|
+
var buf = bjson.write("fortytwo");
|
|
308
|
+
var tab = new Uint8Array(buf);
|
|
309
|
+
for (var i = 5; i < tab.length; i++) {
|
|
310
|
+
for (var k = 0; k < 256; k++) {
|
|
311
|
+
var t = tab[i];
|
|
312
|
+
if (t == k)
|
|
313
|
+
continue;
|
|
314
|
+
tab[i] = k;
|
|
315
|
+
var caught = false;
|
|
316
|
+
try {
|
|
317
|
+
bjson.read(buf, 0, buf.byteLength);
|
|
318
|
+
} catch (e) {
|
|
319
|
+
caught = /checksum error/.test(e.message);
|
|
320
|
+
}
|
|
321
|
+
assert(caught);
|
|
322
|
+
tab[i] = t;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function bjson_test_all()
|
|
328
|
+
{
|
|
329
|
+
var obj;
|
|
330
|
+
|
|
331
|
+
bjson_test({x:1, y:2, if:3});
|
|
332
|
+
bjson_test([1, 2, 3]);
|
|
333
|
+
bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
|
|
334
|
+
if (typeof BigInt !== "undefined") {
|
|
335
|
+
bjson_test([BigInt("1"), -BigInt("0x123456789"),
|
|
336
|
+
BigInt("0x123456789abcdef123456789abcdef")]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
bjson_test([new Date(1234), new String("abc"), new Number(-12.1), new Boolean(true)]);
|
|
340
|
+
|
|
341
|
+
bjson_test(new Int32Array([123123, 222111, -32222]));
|
|
342
|
+
bjson_test(new Float16Array([1024, 1024.5]));
|
|
343
|
+
bjson_test(new Float64Array([123123, 222111.5]));
|
|
344
|
+
|
|
345
|
+
/* tested with a circular reference */
|
|
346
|
+
obj = {};
|
|
347
|
+
obj.x = obj;
|
|
348
|
+
try {
|
|
349
|
+
bjson.write(obj);
|
|
350
|
+
assert(false);
|
|
351
|
+
} catch(e) {
|
|
352
|
+
assert(e instanceof TypeError);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
bjson_test_arraybuffer();
|
|
356
|
+
bjson_test_reference();
|
|
357
|
+
bjson_test_regexp();
|
|
358
|
+
bjson_test_map();
|
|
359
|
+
bjson_test_set();
|
|
360
|
+
bjson_test_symbol();
|
|
361
|
+
bjson_test_bytecode();
|
|
362
|
+
bjson_test_fuzz();
|
|
363
|
+
bjson_test_csum();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
bjson_test_all();
|