@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.
Files changed (209) hide show
  1. package/README.md +55 -0
  2. package/dist/zapp-cli.js +9471 -0
  3. package/native/src/app/app.zc +490 -0
  4. package/native/src/event/event.zc +24 -0
  5. package/native/src/event/events.zc +70 -0
  6. package/native/src/platform/darwin/backend.zc +923 -0
  7. package/native/src/platform/darwin/backend_bootstrap.zc +9 -0
  8. package/native/src/platform/darwin/bootstrap.zc +9 -0
  9. package/native/src/platform/darwin/engine_jsc.zc +86 -0
  10. package/native/src/platform/darwin/engine_qjs.zc +92 -0
  11. package/native/src/platform/darwin/platform.zc +156 -0
  12. package/native/src/platform/darwin/webview.zc +550 -0
  13. package/native/src/platform/darwin/webview_bootstrap.zc +9 -0
  14. package/native/src/platform/darwin/window.zc +1223 -0
  15. package/native/src/platform/darwin/worker/common.zc +223 -0
  16. package/native/src/platform/darwin/worker/core/base64_core.zc +29 -0
  17. package/native/src/platform/darwin/worker/core/crypto_core.zc +19 -0
  18. package/native/src/platform/darwin/worker/core/encoding_core.zc +32 -0
  19. package/native/src/platform/darwin/worker/core/fetch_core.zc +145 -0
  20. package/native/src/platform/darwin/worker/core/url_core.zc +69 -0
  21. package/native/src/platform/darwin/worker/core/websocket_core.zc +179 -0
  22. package/native/src/platform/darwin/worker/dispatch.zc +55 -0
  23. package/native/src/platform/darwin/worker/jsc/base64_jsc.zc +39 -0
  24. package/native/src/platform/darwin/worker/jsc/crypto_jsc.zc +49 -0
  25. package/native/src/platform/darwin/worker/jsc/encoding_jsc.zc +86 -0
  26. package/native/src/platform/darwin/worker/jsc/fetch_jsc.zc +149 -0
  27. package/native/src/platform/darwin/worker/jsc/url_jsc.zc +54 -0
  28. package/native/src/platform/darwin/worker/jsc/websocket_jsc.zc +127 -0
  29. package/native/src/platform/darwin/worker/jsc.zc +670 -0
  30. package/native/src/platform/darwin/worker/mod.zc +30 -0
  31. package/native/src/platform/darwin/worker/qjs/fetch_qjs.zc +233 -0
  32. package/native/src/platform/darwin/worker/qjs/qjs_macros.zc +23 -0
  33. package/native/src/platform/darwin/worker/qjs/websocket_qjs.zc +223 -0
  34. package/native/src/platform/darwin/worker/qjs.zc +1053 -0
  35. package/native/src/platform/darwin/worker/timers.zc +149 -0
  36. package/native/src/platform/darwin/worker/timers_qjs.zc +209 -0
  37. package/native/src/platform/platform.zc +64 -0
  38. package/native/src/platform/shared/log.zc +156 -0
  39. package/native/src/platform/shared/worker/qjs/base64_qjs.zc +38 -0
  40. package/native/src/platform/shared/worker/qjs/crypto_qjs.zc +44 -0
  41. package/native/src/platform/shared/worker/qjs/encoding_qjs.zc +95 -0
  42. package/native/src/platform/shared/worker/qjs/url_qjs.zc +65 -0
  43. package/native/src/platform/shared/worker_registry.zc +206 -0
  44. package/native/src/platform/window.zc +446 -0
  45. package/native/src/platform/windows/backend.zc +452 -0
  46. package/native/src/platform/windows/backend_bootstrap.zc +9 -0
  47. package/native/src/platform/windows/bootstrap.zc +9 -0
  48. package/native/src/platform/windows/engine_qjs.zc +60 -0
  49. package/native/src/platform/windows/platform.zc +387 -0
  50. package/native/src/platform/windows/webview.zc +1175 -0
  51. package/native/src/platform/windows/webview_bootstrap.zc +9 -0
  52. package/native/src/platform/windows/window.zc +1271 -0
  53. package/native/src/platform/windows/worker/common.zc +409 -0
  54. package/native/src/platform/windows/worker/core/base64_core.zc +52 -0
  55. package/native/src/platform/windows/worker/core/crypto_core.zc +34 -0
  56. package/native/src/platform/windows/worker/core/encoding_core.zc +60 -0
  57. package/native/src/platform/windows/worker/core/fetch_core.zc +274 -0
  58. package/native/src/platform/windows/worker/core/url_core.zc +216 -0
  59. package/native/src/platform/windows/worker/core/websocket_core.zc +343 -0
  60. package/native/src/platform/windows/worker/dispatch.zc +34 -0
  61. package/native/src/platform/windows/worker/mod.zc +46 -0
  62. package/native/src/platform/windows/worker/qjs/fetch_qjs.zc +255 -0
  63. package/native/src/platform/windows/worker/qjs/websocket_qjs.zc +263 -0
  64. package/native/src/platform/windows/worker/qjs.zc +1049 -0
  65. package/native/src/platform/windows/worker/timers_qjs.zc +288 -0
  66. package/native/src/platform/worker.zc +8 -0
  67. package/native/src/service/service.zc +228 -0
  68. package/native/vendor/quickjs-ng/.gitattributes +4 -0
  69. package/native/vendor/quickjs-ng/.github/dependabot.yml +7 -0
  70. package/native/vendor/quickjs-ng/.github/workflows/ci.yml +812 -0
  71. package/native/vendor/quickjs-ng/.github/workflows/docs.yml +49 -0
  72. package/native/vendor/quickjs-ng/.github/workflows/release.yml +162 -0
  73. package/native/vendor/quickjs-ng/.github/workflows/test-docs.yml +23 -0
  74. package/native/vendor/quickjs-ng/.github/workflows/tsan.yml +32 -0
  75. package/native/vendor/quickjs-ng/.github/workflows/valgrind.yml +33 -0
  76. package/native/vendor/quickjs-ng/.gitmodules +5 -0
  77. package/native/vendor/quickjs-ng/CMakeLists.txt +553 -0
  78. package/native/vendor/quickjs-ng/LICENSE +24 -0
  79. package/native/vendor/quickjs-ng/Makefile +149 -0
  80. package/native/vendor/quickjs-ng/amalgam.js +53 -0
  81. package/native/vendor/quickjs-ng/api-test.c +927 -0
  82. package/native/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
  83. package/native/vendor/quickjs-ng/builtin-array-fromasync.js +36 -0
  84. package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
  85. package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.js +194 -0
  86. package/native/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
  87. package/native/vendor/quickjs-ng/builtin-iterator-zip.js +210 -0
  88. package/native/vendor/quickjs-ng/ctest.c +17 -0
  89. package/native/vendor/quickjs-ng/cutils.h +2013 -0
  90. package/native/vendor/quickjs-ng/cxxtest.cc +2 -0
  91. package/native/vendor/quickjs-ng/dtoa.c +1619 -0
  92. package/native/vendor/quickjs-ng/dtoa.h +87 -0
  93. package/native/vendor/quickjs-ng/examples/fib.c +67 -0
  94. package/native/vendor/quickjs-ng/examples/fib_module.js +10 -0
  95. package/native/vendor/quickjs-ng/examples/hello.js +1 -0
  96. package/native/vendor/quickjs-ng/examples/hello_module.js +6 -0
  97. package/native/vendor/quickjs-ng/examples/meson.build +17 -0
  98. package/native/vendor/quickjs-ng/examples/pi_bigint.js +118 -0
  99. package/native/vendor/quickjs-ng/examples/point.c +154 -0
  100. package/native/vendor/quickjs-ng/examples/test_fib.js +8 -0
  101. package/native/vendor/quickjs-ng/examples/test_point.js +43 -0
  102. package/native/vendor/quickjs-ng/fuzz.c +51 -0
  103. package/native/vendor/quickjs-ng/gen/function_source.c +81 -0
  104. package/native/vendor/quickjs-ng/gen/hello.c +53 -0
  105. package/native/vendor/quickjs-ng/gen/hello_module.c +106 -0
  106. package/native/vendor/quickjs-ng/gen/repl.c +3053 -0
  107. package/native/vendor/quickjs-ng/gen/standalone.c +324 -0
  108. package/native/vendor/quickjs-ng/gen/test_fib.c +81 -0
  109. package/native/vendor/quickjs-ng/libregexp-opcode.h +58 -0
  110. package/native/vendor/quickjs-ng/libregexp.c +2687 -0
  111. package/native/vendor/quickjs-ng/libregexp.h +98 -0
  112. package/native/vendor/quickjs-ng/libunicode-table.h +4707 -0
  113. package/native/vendor/quickjs-ng/libunicode.c +1746 -0
  114. package/native/vendor/quickjs-ng/libunicode.h +126 -0
  115. package/native/vendor/quickjs-ng/list.h +107 -0
  116. package/native/vendor/quickjs-ng/lre-test.c +73 -0
  117. package/native/vendor/quickjs-ng/meson.build +684 -0
  118. package/native/vendor/quickjs-ng/meson_options.txt +6 -0
  119. package/native/vendor/quickjs-ng/qjs-wasi-reactor.c +208 -0
  120. package/native/vendor/quickjs-ng/qjs.c +748 -0
  121. package/native/vendor/quickjs-ng/qjsc.c +673 -0
  122. package/native/vendor/quickjs-ng/quickjs-atom.h +267 -0
  123. package/native/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
  124. package/native/vendor/quickjs-ng/quickjs-libc.c +4986 -0
  125. package/native/vendor/quickjs-ng/quickjs-libc.h +79 -0
  126. package/native/vendor/quickjs-ng/quickjs-opcode.h +369 -0
  127. package/native/vendor/quickjs-ng/quickjs.c +60259 -0
  128. package/native/vendor/quickjs-ng/quickjs.h +1419 -0
  129. package/native/vendor/quickjs-ng/repl.js +1927 -0
  130. package/native/vendor/quickjs-ng/run-test262.c +2417 -0
  131. package/native/vendor/quickjs-ng/standalone.js +129 -0
  132. package/native/vendor/quickjs-ng/tests/assert.js +49 -0
  133. package/native/vendor/quickjs-ng/tests/bug1221.js +16 -0
  134. package/native/vendor/quickjs-ng/tests/bug1296.js +12 -0
  135. package/native/vendor/quickjs-ng/tests/bug1297.js +22 -0
  136. package/native/vendor/quickjs-ng/tests/bug1301.js +21 -0
  137. package/native/vendor/quickjs-ng/tests/bug1302.js +24 -0
  138. package/native/vendor/quickjs-ng/tests/bug1305.js +26 -0
  139. package/native/vendor/quickjs-ng/tests/bug1318.js +54 -0
  140. package/native/vendor/quickjs-ng/tests/bug1352.js +8 -0
  141. package/native/vendor/quickjs-ng/tests/bug1354.js +6 -0
  142. package/native/vendor/quickjs-ng/tests/bug1355.js +58 -0
  143. package/native/vendor/quickjs-ng/tests/bug1368.js +9 -0
  144. package/native/vendor/quickjs-ng/tests/bug39/1.js +6 -0
  145. package/native/vendor/quickjs-ng/tests/bug39/2.js +6 -0
  146. package/native/vendor/quickjs-ng/tests/bug39/3.js +7 -0
  147. package/native/vendor/quickjs-ng/tests/bug488-upstream.js +7 -0
  148. package/native/vendor/quickjs-ng/tests/bug633/0.js +7 -0
  149. package/native/vendor/quickjs-ng/tests/bug633/1.js +4 -0
  150. package/native/vendor/quickjs-ng/tests/bug633/2.js +4 -0
  151. package/native/vendor/quickjs-ng/tests/bug633/3.js +4 -0
  152. package/native/vendor/quickjs-ng/tests/bug645/0.js +4 -0
  153. package/native/vendor/quickjs-ng/tests/bug645/1.js +9 -0
  154. package/native/vendor/quickjs-ng/tests/bug645/2.js +7 -0
  155. package/native/vendor/quickjs-ng/tests/bug648.js +13 -0
  156. package/native/vendor/quickjs-ng/tests/bug652.js +4 -0
  157. package/native/vendor/quickjs-ng/tests/bug741.js +19 -0
  158. package/native/vendor/quickjs-ng/tests/bug775.js +7 -0
  159. package/native/vendor/quickjs-ng/tests/bug776.js +7 -0
  160. package/native/vendor/quickjs-ng/tests/bug832.js +2 -0
  161. package/native/vendor/quickjs-ng/tests/bug858.js +26 -0
  162. package/native/vendor/quickjs-ng/tests/bug904.js +6 -0
  163. package/native/vendor/quickjs-ng/tests/bug988.js +7 -0
  164. package/native/vendor/quickjs-ng/tests/bug999.js +3 -0
  165. package/native/vendor/quickjs-ng/tests/destructured-export.js +8 -0
  166. package/native/vendor/quickjs-ng/tests/detect_module/0.js +1 -0
  167. package/native/vendor/quickjs-ng/tests/detect_module/1.js +2 -0
  168. package/native/vendor/quickjs-ng/tests/detect_module/2.js +1 -0
  169. package/native/vendor/quickjs-ng/tests/detect_module/3.js +8 -0
  170. package/native/vendor/quickjs-ng/tests/detect_module/4.js +3 -0
  171. package/native/vendor/quickjs-ng/tests/empty.js +0 -0
  172. package/native/vendor/quickjs-ng/tests/fixture_cyclic_import.js +2 -0
  173. package/native/vendor/quickjs-ng/tests/fixture_string_exports.js +12 -0
  174. package/native/vendor/quickjs-ng/tests/function_source.js +14 -0
  175. package/native/vendor/quickjs-ng/tests/microbench.js +1267 -0
  176. package/native/vendor/quickjs-ng/tests/null_or_undefined.js +38 -0
  177. package/native/vendor/quickjs-ng/tests/str-pad-leak.js +5 -0
  178. package/native/vendor/quickjs-ng/tests/test_bigint.js +107 -0
  179. package/native/vendor/quickjs-ng/tests/test_bjson.js +366 -0
  180. package/native/vendor/quickjs-ng/tests/test_builtin.js +1314 -0
  181. package/native/vendor/quickjs-ng/tests/test_closure.js +220 -0
  182. package/native/vendor/quickjs-ng/tests/test_cyclic_import.js +12 -0
  183. package/native/vendor/quickjs-ng/tests/test_domexception.js +35 -0
  184. package/native/vendor/quickjs-ng/tests/test_language.js +755 -0
  185. package/native/vendor/quickjs-ng/tests/test_loop.js +367 -0
  186. package/native/vendor/quickjs-ng/tests/test_queue_microtask.js +39 -0
  187. package/native/vendor/quickjs-ng/tests/test_std.js +340 -0
  188. package/native/vendor/quickjs-ng/tests/test_string_exports.js +25 -0
  189. package/native/vendor/quickjs-ng/tests/test_worker.js +43 -0
  190. package/native/vendor/quickjs-ng/tests/test_worker_module.js +30 -0
  191. package/native/vendor/quickjs-ng/tests.conf +14 -0
  192. package/native/vendor/quickjs-ng/unicode_download.sh +19 -0
  193. package/native/vendor/quickjs-ng/unicode_gen.c +3108 -0
  194. package/native/vendor/quickjs-ng/unicode_gen_def.h +310 -0
  195. package/native/vendor/quickjs-ng/update-version.sh +32 -0
  196. package/native/vendor/webview2/include/WebView2.h +60636 -0
  197. package/native/vendor/webview2/include/WebView2EnvironmentOptions.h +406 -0
  198. package/package.json +33 -0
  199. package/src/backend.ts +139 -0
  200. package/src/build-config.ts +87 -0
  201. package/src/build.ts +276 -0
  202. package/src/common.ts +195 -0
  203. package/src/config.ts +89 -0
  204. package/src/dev.ts +164 -0
  205. package/src/generate.ts +200 -0
  206. package/src/icons.ts +116 -0
  207. package/src/init.ts +190 -0
  208. package/src/package.ts +150 -0
  209. package/src/zapp-cli.ts +263 -0
@@ -0,0 +1,490 @@
1
+ import "../platform/platform.zc";
2
+ import "../platform/window.zc";
3
+ import "../event/event.zc";
4
+ import "../event/events.zc";
5
+ import "../service/service.zc";
6
+ import "../platform/shared/log.zc";
7
+ import "std/json.zc";
8
+ include <string.h>
9
+
10
+ struct AppConfig {
11
+ name: string;
12
+ applicationShouldTerminateAfterLastWindowClosed: bool;
13
+ webContentInspectable: bool;
14
+ maxWorkers: int;
15
+ qjsStackSize: int; // QuickJS stack size in bytes (0 = default 4MB)
16
+ }
17
+
18
+ opaque struct _AppConfig {
19
+ name: string;
20
+ applicationShouldTerminateAfterLastWindowClosed: bool;
21
+ webContentInspectable: bool;
22
+ maxWorkers: int;
23
+ qjsStackSize: int;
24
+ }
25
+
26
+ struct App {
27
+ config: _AppConfig;
28
+ window: WindowManager;
29
+ event: EventManager;
30
+ service: ServiceManager;
31
+ has_backend: bool;
32
+ }
33
+
34
+ impl App {
35
+ fn new(config: AppConfig) -> Self {
36
+ app_set_bootstrap_config(config);
37
+ let windowManager = WindowManager::new();
38
+ windowManager.webContentInspectable = config.webContentInspectable;
39
+ return App{
40
+ config: _AppConfig{
41
+ name: config.name,
42
+ applicationShouldTerminateAfterLastWindowClosed: config.applicationShouldTerminateAfterLastWindowClosed,
43
+ webContentInspectable: config.webContentInspectable,
44
+ maxWorkers: config.maxWorkers,
45
+ qjsStackSize: config.qjsStackSize,
46
+ },
47
+ window: windowManager,
48
+ event: EventManager::new(),
49
+ service: ServiceManager::new(),
50
+ has_backend: false,
51
+ };
52
+ }
53
+
54
+ fn config(self) -> AppConfig {
55
+ return AppConfig{
56
+ name: self.config.name,
57
+ applicationShouldTerminateAfterLastWindowClosed: self.config.applicationShouldTerminateAfterLastWindowClosed,
58
+ webContentInspectable: self.config.webContentInspectable,
59
+ maxWorkers: self.config.maxWorkers,
60
+ qjsStackSize: self.config.qjsStackSize,
61
+ };
62
+ }
63
+
64
+ fn run(self) -> int {
65
+ // Initialize logging with build config log level
66
+ raw {
67
+ extern const char* zapp_build_log_level(void);
68
+ const char* level_str = zapp_build_log_level();
69
+ int level = ZAPP_LOG_INFO; // default
70
+ if (level_str) {
71
+ if (strcmp(level_str, "error") == 0) level = ZAPP_LOG_ERROR;
72
+ else if (strcmp(level_str, "warn") == 0) level = ZAPP_LOG_WARN;
73
+ else if (strcmp(level_str, "info") == 0) level = ZAPP_LOG_INFO;
74
+ else if (strcmp(level_str, "debug") == 0) level = ZAPP_LOG_DEBUG;
75
+ else if (strcmp(level_str, "trace") == 0) level = ZAPP_LOG_TRACE;
76
+ }
77
+ zapp_log_init(level);
78
+ }
79
+
80
+ println "Running app: {self.config.name}";
81
+ self.window.app = self;
82
+ self.event.app = self;
83
+ self.service.app = self;
84
+ if !service_start_all(self) {
85
+ return 1;
86
+ }
87
+ app_set_active(self);
88
+ platform_bind_runtime(self);
89
+ app_start_backend(self);
90
+ let result = platform_run(self);
91
+ app_stop_backend(self);
92
+ service_shutdown_all(self);
93
+ app_set_active(NULL);
94
+ return result;
95
+ }
96
+
97
+ fn on_shutdown(self, cb: fn*() -> void) -> void {
98
+ let _self = self;
99
+ let _cb = cb;
100
+ }
101
+
102
+ fn should_quit(self, cb: fn*() -> bool) -> void {
103
+ let _self = self;
104
+ let _cb = cb;
105
+ }
106
+ }
107
+
108
+ // Top-level wrappers keep cross-module callsites simple and stable.
109
+ fn app_get_config(app: App*) -> AppConfig {
110
+ return app.config();
111
+ }
112
+
113
+ static let zapp_active_app_ptr: void* = NULL;
114
+ static let zapp_has_bootstrap_config: bool = false;
115
+ static let zapp_bootstrap_config: AppConfig;
116
+
117
+ fn app_get_active() -> void* {
118
+ return zapp_active_app_ptr;
119
+ }
120
+
121
+ fn app_set_active(app: App*) -> void {
122
+ zapp_active_app_ptr = (void*)app;
123
+ }
124
+
125
+ fn app_set_bootstrap_config(config: AppConfig) -> void {
126
+ zapp_bootstrap_config = config;
127
+ zapp_has_bootstrap_config = true;
128
+ }
129
+
130
+ fn app_get_bootstrap_config() -> AppConfig {
131
+ if zapp_has_bootstrap_config {
132
+ return zapp_bootstrap_config;
133
+ }
134
+ return AppConfig{
135
+ name: "Zapp App",
136
+ applicationShouldTerminateAfterLastWindowClosed: false,
137
+ webContentInspectable: true,
138
+ maxWorkers: 0,
139
+ qjsStackSize: 0,
140
+ };
141
+ }
142
+
143
+ fn app_get_bootstrap_name() -> string {
144
+ return app_get_bootstrap_config().name;
145
+ }
146
+
147
+ fn app_get_bootstrap_application_should_terminate_after_last_window_closed() -> bool {
148
+ return app_get_bootstrap_config().applicationShouldTerminateAfterLastWindowClosed;
149
+ }
150
+
151
+ fn app_get_bootstrap_web_content_inspectable() -> bool {
152
+ return app_get_bootstrap_config().webContentInspectable;
153
+ }
154
+
155
+ fn app_get_bootstrap_max_workers() -> int {
156
+ return app_get_bootstrap_config().maxWorkers;
157
+ }
158
+
159
+ fn app_get_bootstrap_qjs_stack_size() -> int {
160
+ let size = app_get_bootstrap_config().qjsStackSize;
161
+ if size <= 0 {
162
+ return 4 * 1024 * 1024; // Default 4 MB
163
+ }
164
+ return size;
165
+ }
166
+
167
+ @cfg(apple)
168
+ fn zapp_backend_context_create(app: void*, script: string) -> int;
169
+ @cfg(apple)
170
+ fn zapp_backend_context_destroy() -> void;
171
+
172
+ @cfg(windows)
173
+ fn zapp_backend_context_create(app: void*, script: string) -> int;
174
+ @cfg(windows)
175
+ fn zapp_backend_context_destroy() -> void;
176
+
177
+ @cfg(ZAPP_LINUX)
178
+ fn zapp_backend_context_create(app: void*, script: string) -> int {
179
+ raw { (void)app; (void)script; }
180
+ return -1;
181
+ }
182
+ @cfg(ZAPP_LINUX)
183
+ fn zapp_backend_context_destroy() -> void {}
184
+
185
+ raw {
186
+ extern const char* zapp_build_backend_script_path(void);
187
+ }
188
+
189
+ raw {
190
+ #include <stdio.h>
191
+ #include <stdlib.h>
192
+
193
+ static char* zapp_read_file_utf8(const char* path) {
194
+ if (!path || path[0] == '\0') return NULL;
195
+ FILE* f = fopen(path, "rb");
196
+ if (!f) return NULL;
197
+ fseek(f, 0, SEEK_END);
198
+ long len = ftell(f);
199
+ fseek(f, 0, SEEK_SET);
200
+ if (len <= 0) { fclose(f); return NULL; }
201
+ char* buf = (char*)malloc((size_t)len + 1);
202
+ if (!buf) { fclose(f); return NULL; }
203
+ size_t read = fread(buf, 1, (size_t)len, f);
204
+ fclose(f);
205
+ buf[read] = '\0';
206
+ return buf;
207
+ }
208
+ }
209
+
210
+ fn app_start_backend(app: App*) -> void {
211
+ raw {
212
+ const char* backendPath = zapp_build_backend_script_path();
213
+ if (backendPath == NULL || backendPath[0] == '\0') return;
214
+
215
+ char* source = zapp_read_file_utf8(backendPath);
216
+ if (source == NULL) {
217
+ zapp_log_backend(ZAPP_LOG_ERROR, "failed to read backend script");
218
+ return;
219
+ }
220
+
221
+ int result = zapp_backend_context_create((void*)app, source);
222
+ free(source);
223
+ if (result == 0) {
224
+ app->has_backend = true;
225
+ zapp_log_backend(ZAPP_LOG_INFO, "backend context started");
226
+ } else {
227
+ zapp_log_backend(ZAPP_LOG_ERROR, "backend context failed to start");
228
+ }
229
+ }
230
+ }
231
+
232
+ fn app_stop_backend(app: App*) -> void {
233
+ if app.has_backend {
234
+ zapp_backend_context_destroy();
235
+ app.has_backend = false;
236
+ }
237
+ }
238
+
239
+ fn parse_wire(msg: string, out_type: string*, out_key: string*, out_payload: string*) -> void {
240
+ let p: string = msg;
241
+ *out_type = p;
242
+ *out_key = p;
243
+ *out_payload = p;
244
+ while *p != '\0' {
245
+ if *p == '\n' {
246
+ *p = '\0';
247
+ p++;
248
+ if (void*)*out_key == (void*)msg {
249
+ *out_key = p;
250
+ } else {
251
+ *out_payload = p;
252
+ break;
253
+ }
254
+ } else {
255
+ p++;
256
+ }
257
+ }
258
+ }
259
+
260
+ static let zapp_app_configured: bool = false;
261
+
262
+ fn app_apply_configure(app: App*, payload: string) -> void {
263
+ if app == NULL || payload == NULL {
264
+ return;
265
+ }
266
+ raw {
267
+ if (zapp_app_configured) {
268
+ zapp_log_backend(ZAPP_LOG_WARN, "App.configure() ignored: already configured");
269
+ return;
270
+ }
271
+ zapp_app_configured = true;
272
+ }
273
+ let result = JsonValue::parse(payload);
274
+ if result.is_err() {
275
+ return;
276
+ }
277
+ let json = result.unwrap();
278
+ let name_opt = json.get_string("name");
279
+ if name_opt.is_some() {
280
+ raw {
281
+ if (app->config.name) free((void*)app->config.name);
282
+ app->config.name = strdup(name_opt.val);
283
+ }
284
+ }
285
+ let quit_opt = json.get_bool("applicationShouldTerminateAfterLastWindowClosed");
286
+ if quit_opt.is_some() {
287
+ app.config.applicationShouldTerminateAfterLastWindowClosed = quit_opt.unwrap();
288
+ }
289
+ let insp_opt = json.get_bool("webContentInspectable");
290
+ if insp_opt.is_some() {
291
+ app.config.webContentInspectable = insp_opt.unwrap();
292
+ app.window.webContentInspectable = app.config.webContentInspectable;
293
+ }
294
+ let max_opt = json.get_int("maxWorkers");
295
+ if max_opt.is_some() {
296
+ app.config.maxWorkers = max_opt.unwrap();
297
+ }
298
+ raw { zapp_log_backend(ZAPP_LOG_INFO, "app configured from backend"); }
299
+ }
300
+
301
+ fn app_should_quit() -> bool {
302
+ return true;
303
+ }
304
+
305
+ @cfg(apple)
306
+ fn zapp_dispatch_invoke_result(request_id: string, payload_json: string) -> void;
307
+ @cfg(apple)
308
+ fn platform_sync_handle_bridge(action: string, payload_json: string) -> void;
309
+ @cfg(apple)
310
+ fn app_handle_window_message(app: App*, action: string, payload: string) -> void;
311
+ @cfg(apple)
312
+ fn app_handle_app_action(app: App*, action: string) -> void;
313
+ @cfg(apple)
314
+ fn app_open_external(app: App*, payload: string) -> void;
315
+ @cfg(apple)
316
+ fn app_handle_dialog(app: App*, action: string, payload: string) -> void;
317
+
318
+ @cfg(windows)
319
+ fn zapp_dispatch_invoke_result(request_id: string, payload_json: string) -> void;
320
+
321
+ @cfg(windows)
322
+ fn platform_sync_handle_bridge(action: string, payload_json: string) -> void;
323
+
324
+ @cfg(windows)
325
+ fn app_handle_window_message(app: App*, action: string, payload: string) -> void;
326
+
327
+ @cfg(windows)
328
+ fn app_open_external(app: App*, payload: string) -> void;
329
+ @cfg(windows)
330
+ fn app_handle_dialog(app: App*, action: string, payload: string) -> void;
331
+ @cfg(windows)
332
+ fn app_handle_app_action(app: App*, action: string) -> void;
333
+
334
+ @cfg(ZAPP_LINUX)
335
+ fn zapp_dispatch_invoke_result(request_id: string, payload_json: string) -> void {
336
+ raw { (void)request_id; (void)payload_json; }
337
+ }
338
+ @cfg(ZAPP_LINUX)
339
+ fn platform_sync_handle_bridge(action: string, payload_json: string) -> void {
340
+ raw { (void)action; (void)payload_json; }
341
+ }
342
+ @cfg(ZAPP_LINUX)
343
+ fn app_handle_window_message(app: App*, action: string, payload: string) -> void {
344
+ raw { (void)app; (void)action; (void)payload; }
345
+ }
346
+ @cfg(ZAPP_LINUX)
347
+ fn app_handle_app_action(app: App*, action: string) -> void {
348
+ raw { (void)app; (void)action; }
349
+ }
350
+ @cfg(ZAPP_LINUX)
351
+ fn app_open_external(app: App*, payload: string) -> void {
352
+ raw { (void)app; (void)payload; }
353
+ }
354
+ @cfg(ZAPP_LINUX)
355
+ fn app_handle_dialog(app: App*, action: string, payload: string) -> void {
356
+ raw { (void)app; (void)action; (void)payload; }
357
+ }
358
+
359
+ fn app_invoke_rpc(app: App*, method: string, request_payload: string) -> void {
360
+ if method == "" || request_payload == "" {
361
+ return;
362
+ }
363
+ let parse_result = JsonValue::parse(request_payload);
364
+ if parse_result.is_err() {
365
+ return;
366
+ }
367
+ let json = parse_result.unwrap();
368
+ let id_opt = json.get_string("id");
369
+ if id_opt.is_none() {
370
+ return;
371
+ }
372
+ let request_id = id_opt.unwrap();
373
+ let cap_opt = json.get_string("capability");
374
+ let capability: string = "";
375
+ if cap_opt.is_some() {
376
+ capability = cap_opt.unwrap();
377
+ }
378
+
379
+ // Extract args as a sub-object and re-serialize
380
+ let args_opt = json.get("args");
381
+ let args_str: string = "{{}}";
382
+
383
+ raw {
384
+ // Re-extract args substring from original JSON for service invocation
385
+ // (serializing back would lose fidelity; pass the raw substring)
386
+ const char* argsKey = "\"args\":";
387
+ const char* argsStart = strstr((const char*)request_payload, argsKey);
388
+ if (argsStart) {
389
+ argsStart += strlen(argsKey);
390
+ while (*argsStart == ' ' || *argsStart == '\n' || *argsStart == '\r' || *argsStart == '\t') argsStart++;
391
+ const char* argsEnd = strstr(argsStart, ",\"meta\"");
392
+ if (!argsEnd) argsEnd = strrchr(argsStart, '}');
393
+ if (argsEnd && argsEnd > argsStart) {
394
+ static char _args_buf[65536];
395
+ size_t len = (size_t)(argsEnd - argsStart);
396
+ if (len >= sizeof(_args_buf)) len = sizeof(_args_buf) - 1;
397
+ memcpy(_args_buf, argsStart, len);
398
+ _args_buf[len] = '\0';
399
+ args_str = _args_buf;
400
+ }
401
+ }
402
+ }
403
+
404
+ let invoke_result = service_invoke_with_policy(app, method, args_str, capability);
405
+
406
+ if invoke_result.is_err() {
407
+ let error_code = invoke_result.err;
408
+ let error_message = service_error_message(error_code);
409
+ raw {
410
+ char response[70000];
411
+ snprintf(response, sizeof(response),
412
+ "{\"v\":1,\"id\":\"%s\",\"ok\":false,\"error\":{\"code\":\"%s\",\"message\":\"%s\"}}",
413
+ (const char*)request_id, (const char*)error_code, (const char*)error_message);
414
+ zapp_dispatch_invoke_result(request_id, response);
415
+ }
416
+ } else {
417
+ let result_val = invoke_result.unwrap();
418
+ raw {
419
+ char response[70000];
420
+ const char* r = (result_val && result_val[0] != '\0') ? (const char*)result_val : "{}";
421
+ snprintf(response, sizeof(response),
422
+ "{\"v\":1,\"id\":\"%s\",\"ok\":true,\"result\":%s}",
423
+ (const char*)request_id, r);
424
+ zapp_dispatch_invoke_result(request_id, response);
425
+ }
426
+ }
427
+ }
428
+
429
+ fn app_handle_frontend_message(app: App*, msg: string) -> void {
430
+ let msg_type: string = "";
431
+ let key: string = "";
432
+ let payload: string = "";
433
+ parse_wire(msg, &msg_type, &key, &payload);
434
+
435
+ if msg_type == "invoke" {
436
+ let _ = service_invoke(app, key, payload);
437
+ return;
438
+ }
439
+
440
+ if msg_type == "invoke_rpc" {
441
+ app_invoke_rpc(app, key, payload);
442
+ return;
443
+ }
444
+
445
+ if msg_type == "emit" {
446
+ app.event.emit(key, payload);
447
+ return;
448
+ }
449
+
450
+ if msg_type == "worker" {
451
+ platform_handle_worker_bridge(app, key, payload);
452
+ return;
453
+ }
454
+
455
+ if msg_type == "sync" {
456
+ platform_sync_handle_bridge(key, payload);
457
+ return;
458
+ }
459
+
460
+ if msg_type == "window" {
461
+ app_handle_window_message(app, key, payload);
462
+ return;
463
+ }
464
+
465
+ if msg_type == "app" {
466
+ if key == "configure" {
467
+ app_apply_configure(app, payload);
468
+ return;
469
+ }
470
+ if key == "openExternal" {
471
+ app_open_external(app, payload);
472
+ return;
473
+ }
474
+ app_handle_app_action(app, key);
475
+ return;
476
+ }
477
+
478
+ if msg_type == "dialog" {
479
+ app_handle_dialog(app, key, payload);
480
+ return;
481
+ }
482
+ }
483
+
484
+ // Native entry for ObjC script message handlers.
485
+ fn zapp_handle_message(app_ptr: void*, msg_c: string) -> void {
486
+ if app_ptr == NULL || msg_c == NULL {
487
+ return;
488
+ }
489
+ app_handle_frontend_message((App*)app_ptr, msg_c);
490
+ }
@@ -0,0 +1,24 @@
1
+ struct App;
2
+
3
+ struct EventManager {
4
+ app: App*;
5
+ send_event_to_js: fn*(App*, string, string);
6
+ }
7
+
8
+ impl EventManager {
9
+ fn new() -> Self {
10
+ return EventManager{ app: NULL, send_event_to_js: NULL };
11
+ }
12
+
13
+ fn emit(self, name: string, payload: string) -> void {
14
+ let _n = name;
15
+ let _p = payload;
16
+ if self.app != NULL && self.send_event_to_js != NULL {
17
+ raw {
18
+ typedef void (*SendFn)(void*, char*, char*);
19
+ SendFn cb = (SendFn)self->send_event_to_js;
20
+ cb((void*)self->app, (char*)name, (char*)payload);
21
+ }
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,70 @@
1
+ struct WindowEvents {
2
+ READY: int;
3
+ FOCUS: int;
4
+ BLUR: int;
5
+ RESIZE: int;
6
+ MOVE: int;
7
+ CLOSE: int;
8
+ MINIMIZE: int;
9
+ MAXIMIZE: int;
10
+ RESTORE: int;
11
+ FULLSCREEN: int;
12
+ UNFULLSCREEN: int;
13
+ }
14
+
15
+ def WindowEvent = WindowEvents{
16
+ READY: 0,
17
+ FOCUS: 1,
18
+ BLUR: 2,
19
+ RESIZE: 3,
20
+ MOVE: 4,
21
+ CLOSE: 5,
22
+ MINIMIZE: 6,
23
+ MAXIMIZE: 7,
24
+ RESTORE: 8,
25
+ FULLSCREEN: 9,
26
+ UNFULLSCREEN: 10,
27
+ };
28
+
29
+ struct AppEvents {
30
+ STARTED: int;
31
+ SHUTDOWN: int;
32
+ }
33
+
34
+ def AppEvent = AppEvents{
35
+ STARTED: 100,
36
+ SHUTDOWN: 101,
37
+ };
38
+
39
+ struct EventResults {
40
+ ALLOW: int;
41
+ CANCEL: int;
42
+ }
43
+
44
+ def EventResult = EventResults{
45
+ ALLOW: 0,
46
+ CANCEL: 1,
47
+ };
48
+
49
+ raw {
50
+ // C-accessible macros for use in raw blocks and Obj-C delegates
51
+ #define ZAPP_EVENT_WINDOW_READY 0
52
+ #define ZAPP_EVENT_WINDOW_FOCUS 1
53
+ #define ZAPP_EVENT_WINDOW_BLUR 2
54
+ #define ZAPP_EVENT_WINDOW_RESIZE 3
55
+ #define ZAPP_EVENT_WINDOW_MOVE 4
56
+ #define ZAPP_EVENT_WINDOW_CLOSE 5
57
+ #define ZAPP_EVENT_WINDOW_MINIMIZE 6
58
+ #define ZAPP_EVENT_WINDOW_MAXIMIZE 7
59
+ #define ZAPP_EVENT_WINDOW_RESTORE 8
60
+ #define ZAPP_EVENT_WINDOW_FULLSCREEN 9
61
+ #define ZAPP_EVENT_WINDOW_UNFULLSCREEN 10
62
+
63
+ #define ZAPP_EVENT_APP_STARTED 100
64
+ #define ZAPP_EVENT_APP_SHUTDOWN 101
65
+
66
+ #define ZAPP_EVENT_RESULT_ALLOW 0
67
+ #define ZAPP_EVENT_RESULT_CANCEL 1
68
+
69
+ #define ZAPP_MAX_WINDOW_EVENT_TYPES 11
70
+ }