@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,263 @@
|
|
|
1
|
+
// QJS wrapper for WebSocket on Windows. Delegates to core/websocket_core.zc.
|
|
2
|
+
|
|
3
|
+
raw {
|
|
4
|
+
#if defined(_WIN32) && defined(ZAPP_WORKER_ENGINE_QJS)
|
|
5
|
+
#include "quickjs.h"
|
|
6
|
+
#include "quickjs-libc.h"
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <string.h>
|
|
9
|
+
|
|
10
|
+
extern void* zapp_windows_qjs_get_context(const char* workerId);
|
|
11
|
+
extern void zapp_windows_qjs_dispatch(void (*func)(void*), void* data);
|
|
12
|
+
extern void zapp_windows_qjs_drain_jobs(void* ctx, const char* label);
|
|
13
|
+
extern void zapp_log_worker(int level, int window_id, int worker_id, const char* message);
|
|
14
|
+
#ifndef ZAPP_LOG_ERROR
|
|
15
|
+
#define ZAPP_LOG_ERROR 0
|
|
16
|
+
#define ZAPP_LOG_WARN 1
|
|
17
|
+
#define ZAPP_LOG_INFO 2
|
|
18
|
+
#define ZAPP_LOG_DEBUG 3
|
|
19
|
+
#define ZAPP_LOG_TRACE 4
|
|
20
|
+
#endif
|
|
21
|
+
|
|
22
|
+
static char* zapp_core_ws_connect(const char* url_str, const char** protocols, int protocol_count,
|
|
23
|
+
zapp_ws_event_fn callback, void* user_data);
|
|
24
|
+
static void zapp_core_ws_send_text(const char* ws_id, const char* text);
|
|
25
|
+
static void zapp_core_ws_send_binary(const char* ws_id, const uint8_t* bytes, size_t len);
|
|
26
|
+
static void zapp_core_ws_close(const char* ws_id, int code, const char* reason);
|
|
27
|
+
|
|
28
|
+
// Per-WS callback reference storage
|
|
29
|
+
#define ZAPP_QJS_WS_REF_MAX 64
|
|
30
|
+
typedef struct {
|
|
31
|
+
char ws_id[64];
|
|
32
|
+
char worker_id[128];
|
|
33
|
+
JSContext* ctx;
|
|
34
|
+
JSValue fn;
|
|
35
|
+
int in_use;
|
|
36
|
+
} ZappQjsWsRef;
|
|
37
|
+
static ZappQjsWsRef zapp_qjs_ws_refs[ZAPP_QJS_WS_REF_MAX] = {0};
|
|
38
|
+
|
|
39
|
+
static ZappQjsWsRef* zapp_qjs_ws_ref_find(const char* ws_id) {
|
|
40
|
+
if (!ws_id) return NULL;
|
|
41
|
+
for (int i = 0; i < ZAPP_QJS_WS_REF_MAX; i++) {
|
|
42
|
+
if (zapp_qjs_ws_refs[i].in_use && strcmp(zapp_qjs_ws_refs[i].ws_id, ws_id) == 0)
|
|
43
|
+
return &zapp_qjs_ws_refs[i];
|
|
44
|
+
}
|
|
45
|
+
return NULL;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static ZappQjsWsRef* zapp_qjs_ws_ref_alloc(void) {
|
|
49
|
+
for (int i = 0; i < ZAPP_QJS_WS_REF_MAX; i++) {
|
|
50
|
+
if (!zapp_qjs_ws_refs[i].in_use) return &zapp_qjs_ws_refs[i];
|
|
51
|
+
}
|
|
52
|
+
return NULL;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Dispatch data for delivering WS events on the QJS thread
|
|
56
|
+
typedef struct {
|
|
57
|
+
char ws_id[64];
|
|
58
|
+
char worker_id[128];
|
|
59
|
+
char* json_payload;
|
|
60
|
+
} ZappQjsWsEventData;
|
|
61
|
+
|
|
62
|
+
static void zapp_ws_deliver_on_qjs(void* data) {
|
|
63
|
+
ZappQjsWsEventData* d = (ZappQjsWsEventData*)data;
|
|
64
|
+
|
|
65
|
+
ZappQjsWsRef* ref = zapp_qjs_ws_ref_find(d->ws_id);
|
|
66
|
+
if (!ref) { free(d->json_payload); free(d); return; }
|
|
67
|
+
|
|
68
|
+
void* ctx_ptr = zapp_windows_qjs_get_context(ref->worker_id);
|
|
69
|
+
if (!ctx_ptr || (JSContext*)ctx_ptr != ref->ctx) {
|
|
70
|
+
free(d->json_payload);
|
|
71
|
+
free(d);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
JSContext* ctx = ref->ctx;
|
|
76
|
+
JSValue global = JS_GetGlobalObject(ctx);
|
|
77
|
+
JSValue JSON = JS_GetPropertyStr(ctx, global, "JSON");
|
|
78
|
+
JSValue parse = JS_GetPropertyStr(ctx, JSON, "parse");
|
|
79
|
+
JSValue jsonArg = JS_NewString(ctx, d->json_payload);
|
|
80
|
+
JSValue parsed = JS_Call(ctx, parse, JSON, 1, &jsonArg);
|
|
81
|
+
|
|
82
|
+
JSValue args[1] = { parsed };
|
|
83
|
+
JSValue ret = JS_Call(ctx, ref->fn, JS_UNDEFINED, 1, args);
|
|
84
|
+
if (JS_IsException(ret)) {
|
|
85
|
+
JSValue exc = JS_GetException(ctx);
|
|
86
|
+
const char* s = JS_ToCString(ctx, exc);
|
|
87
|
+
if (s) { zapp_log_worker(ZAPP_LOG_ERROR, -1, -1, s); JS_FreeCString(ctx, s); }
|
|
88
|
+
JS_FreeValue(ctx, exc);
|
|
89
|
+
}
|
|
90
|
+
JS_FreeValue(ctx, ret);
|
|
91
|
+
JS_FreeValue(ctx, parsed);
|
|
92
|
+
JS_FreeValue(ctx, jsonArg);
|
|
93
|
+
JS_FreeValue(ctx, parse);
|
|
94
|
+
JS_FreeValue(ctx, JSON);
|
|
95
|
+
JS_FreeValue(ctx, global);
|
|
96
|
+
zapp_windows_qjs_drain_jobs(ctx, "ws-event");
|
|
97
|
+
|
|
98
|
+
free(d->json_payload);
|
|
99
|
+
free(d);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Core callback: dispatches events to the QJS thread
|
|
103
|
+
static void zapp_qjs_ws_callback(const char* ws_id, const char* event_type, const char* json_payload, void* user_data) {
|
|
104
|
+
(void)event_type;
|
|
105
|
+
if (!ws_id || !json_payload) return;
|
|
106
|
+
|
|
107
|
+
ZappQjsWsEventData* d = (ZappQjsWsEventData*)calloc(1, sizeof(ZappQjsWsEventData));
|
|
108
|
+
strncpy(d->ws_id, ws_id, sizeof(d->ws_id) - 1);
|
|
109
|
+
d->json_payload = _strdup(json_payload);
|
|
110
|
+
|
|
111
|
+
ZappQjsWsRef* ref = zapp_qjs_ws_ref_find(ws_id);
|
|
112
|
+
if (ref) strncpy(d->worker_id, ref->worker_id, sizeof(d->worker_id) - 1);
|
|
113
|
+
|
|
114
|
+
zapp_windows_qjs_dispatch(zapp_ws_deliver_on_qjs, d);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static JSValue qjs_bridge_wsConnect(JSContext* ctx, JSValue this_val, int argc, JSValue* argv, int magic, JSValue* func_data) {
|
|
118
|
+
(void)this_val; (void)magic;
|
|
119
|
+
if (argc < 3) return JS_NewString(ctx, "");
|
|
120
|
+
|
|
121
|
+
const char* urlStr = JS_ToCString(ctx, argv[0]);
|
|
122
|
+
if (!urlStr) return JS_NewString(ctx, "");
|
|
123
|
+
|
|
124
|
+
const char** protos = NULL;
|
|
125
|
+
int protoCount = 0;
|
|
126
|
+
if (!JS_IsUndefined(argv[1]) && !JS_IsNull(argv[1])) {
|
|
127
|
+
JSValue lenVal = JS_GetPropertyStr(ctx, argv[1], "length");
|
|
128
|
+
int32_t arrLen = 0;
|
|
129
|
+
JS_ToInt32(ctx, &arrLen, lenVal);
|
|
130
|
+
JS_FreeValue(ctx, lenVal);
|
|
131
|
+
if (arrLen > 0) {
|
|
132
|
+
protoCount = arrLen;
|
|
133
|
+
protos = (const char**)malloc(sizeof(const char*) * protoCount);
|
|
134
|
+
for (int i = 0; i < protoCount; i++) {
|
|
135
|
+
JSValue el = JS_GetPropertyUint32(ctx, argv[1], (uint32_t)i);
|
|
136
|
+
protos[i] = JS_ToCString(ctx, el);
|
|
137
|
+
JS_FreeValue(ctx, el);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const char* workerId = JS_ToCString(ctx, func_data[0]);
|
|
143
|
+
char workerIdBuf[128] = {0};
|
|
144
|
+
if (workerId) { strncpy(workerIdBuf, workerId, sizeof(workerIdBuf) - 1); JS_FreeCString(ctx, workerId); }
|
|
145
|
+
|
|
146
|
+
JSValue jsFn = JS_DupValue(ctx, argv[2]);
|
|
147
|
+
|
|
148
|
+
char* wsIdC = zapp_core_ws_connect(urlStr, protos, protoCount, zapp_qjs_ws_callback, NULL);
|
|
149
|
+
|
|
150
|
+
for (int i = 0; i < protoCount; i++) {
|
|
151
|
+
if (protos[i]) JS_FreeCString(ctx, protos[i]);
|
|
152
|
+
}
|
|
153
|
+
free(protos);
|
|
154
|
+
JS_FreeCString(ctx, urlStr);
|
|
155
|
+
|
|
156
|
+
if (!wsIdC) {
|
|
157
|
+
JS_FreeValue(ctx, jsFn);
|
|
158
|
+
return JS_NewString(ctx, "");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
ZappQjsWsRef* ref = zapp_qjs_ws_ref_alloc();
|
|
162
|
+
if (ref) {
|
|
163
|
+
memset(ref, 0, sizeof(ZappQjsWsRef));
|
|
164
|
+
strncpy(ref->ws_id, wsIdC, sizeof(ref->ws_id) - 1);
|
|
165
|
+
strncpy(ref->worker_id, workerIdBuf, sizeof(ref->worker_id) - 1);
|
|
166
|
+
ref->ctx = ctx;
|
|
167
|
+
ref->fn = jsFn;
|
|
168
|
+
ref->in_use = 1;
|
|
169
|
+
} else {
|
|
170
|
+
JS_FreeValue(ctx, jsFn);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
JSValue result = JS_NewString(ctx, wsIdC);
|
|
174
|
+
free(wsIdC);
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static JSValue qjs_bridge_wsSend(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
179
|
+
(void)this_val;
|
|
180
|
+
if (argc < 3) return JS_UNDEFINED;
|
|
181
|
+
|
|
182
|
+
const char* wsId = JS_ToCString(ctx, argv[0]);
|
|
183
|
+
if (!wsId) return JS_UNDEFINED;
|
|
184
|
+
|
|
185
|
+
int isBinary = JS_ToBool(ctx, argv[2]);
|
|
186
|
+
|
|
187
|
+
if (isBinary) {
|
|
188
|
+
size_t len = 0;
|
|
189
|
+
uint8_t* bytes = JS_GetArrayBuffer(ctx, &len, argv[1]);
|
|
190
|
+
if (!bytes) {
|
|
191
|
+
JSValue bufProp = JS_GetPropertyStr(ctx, argv[1], "buffer");
|
|
192
|
+
if (!JS_IsUndefined(bufProp)) {
|
|
193
|
+
size_t bufLen = 0;
|
|
194
|
+
uint8_t* bufPtr = JS_GetArrayBuffer(ctx, &bufLen, bufProp);
|
|
195
|
+
JSValue oVal = JS_GetPropertyStr(ctx, argv[1], "byteOffset");
|
|
196
|
+
JSValue lVal = JS_GetPropertyStr(ctx, argv[1], "byteLength");
|
|
197
|
+
int32_t off = 0, blen = 0;
|
|
198
|
+
JS_ToInt32(ctx, &off, oVal);
|
|
199
|
+
JS_ToInt32(ctx, &blen, lVal);
|
|
200
|
+
JS_FreeValue(ctx, oVal);
|
|
201
|
+
JS_FreeValue(ctx, lVal);
|
|
202
|
+
if (bufPtr && off >= 0 && blen > 0 && (size_t)(off + blen) <= bufLen) {
|
|
203
|
+
bytes = bufPtr + off;
|
|
204
|
+
len = (size_t)blen;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
JS_FreeValue(ctx, bufProp);
|
|
208
|
+
}
|
|
209
|
+
if (bytes && len > 0) zapp_core_ws_send_binary(wsId, bytes, len);
|
|
210
|
+
} else {
|
|
211
|
+
const char* text = JS_ToCString(ctx, argv[1]);
|
|
212
|
+
zapp_core_ws_send_text(wsId, text ? text : "");
|
|
213
|
+
if (text) JS_FreeCString(ctx, text);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
JS_FreeCString(ctx, wsId);
|
|
217
|
+
return JS_UNDEFINED;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static JSValue qjs_bridge_wsClose(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
221
|
+
(void)this_val;
|
|
222
|
+
if (argc < 1) return JS_UNDEFINED;
|
|
223
|
+
const char* wsId = JS_ToCString(ctx, argv[0]);
|
|
224
|
+
if (!wsId) return JS_UNDEFINED;
|
|
225
|
+
|
|
226
|
+
int32_t code = 1000;
|
|
227
|
+
if (argc >= 2 && !JS_IsUndefined(argv[1]) && !JS_IsNull(argv[1])) {
|
|
228
|
+
JS_ToInt32(ctx, &code, argv[1]);
|
|
229
|
+
}
|
|
230
|
+
const char* reason = NULL;
|
|
231
|
+
if (argc >= 3 && !JS_IsUndefined(argv[2]) && !JS_IsNull(argv[2])) {
|
|
232
|
+
reason = JS_ToCString(ctx, argv[2]);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
zapp_core_ws_close(wsId, code, reason);
|
|
236
|
+
|
|
237
|
+
// Clean up JS callback reference
|
|
238
|
+
ZappQjsWsRef* ref = zapp_qjs_ws_ref_find(wsId);
|
|
239
|
+
if (ref) {
|
|
240
|
+
JS_FreeValue(ref->ctx, ref->fn);
|
|
241
|
+
ref->in_use = 0;
|
|
242
|
+
memset(ref, 0, sizeof(ZappQjsWsRef));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (reason) JS_FreeCString(ctx, reason);
|
|
246
|
+
JS_FreeCString(ctx, wsId);
|
|
247
|
+
return JS_UNDEFINED;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
void zapp_windows_qjs_websocket_install(JSContext* ctx, JSValue bridge, const char* worker_id) {
|
|
251
|
+
JSValue wIdVal = JS_NewString(ctx, worker_id);
|
|
252
|
+
JSValue funcData[1] = { wIdVal };
|
|
253
|
+
JS_SetPropertyStr(ctx, bridge, "wsConnect",
|
|
254
|
+
JS_NewCFunctionData(ctx, qjs_bridge_wsConnect, 3, 0, 1, funcData));
|
|
255
|
+
JS_FreeValue(ctx, wIdVal);
|
|
256
|
+
|
|
257
|
+
JS_SetPropertyStr(ctx, bridge, "wsSend",
|
|
258
|
+
JS_NewCFunction(ctx, qjs_bridge_wsSend, "wsSend", 3));
|
|
259
|
+
JS_SetPropertyStr(ctx, bridge, "wsClose",
|
|
260
|
+
JS_NewCFunction(ctx, qjs_bridge_wsClose, "wsClose", 3));
|
|
261
|
+
}
|
|
262
|
+
#endif
|
|
263
|
+
}
|