@elurjs/universal 0.1.10 → 0.1.12
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/cli/commands.mjs +1 -1
- package/cli/create-app.mjs +11 -6
- package/cli/dev.mjs +4 -3
- package/cli/elur.mjs +60 -4
- package/docs/INSTALACION-Y-DEV.md +7 -4
- package/package.json +5 -1
- package/targets/linux/CMakeLists.txt +196 -0
- package/targets/linux/README.md +422 -0
- package/targets/linux/ci/tsan.supp +15 -0
- package/targets/linux/generated/elur_module_registrar.cpp +18 -0
- package/targets/linux/generated/module_sources.cmake +7 -0
- package/targets/linux/src/elur_applicator.cpp +5073 -0
- package/targets/linux/src/elur_applicator.h +387 -0
- package/targets/linux/src/elur_clip.cpp +76 -0
- package/targets/linux/src/elur_clip.h +23 -0
- package/targets/linux/src/elur_crypto_module.cpp +121 -0
- package/targets/linux/src/elur_events.h +57 -0
- package/targets/linux/src/elur_fonts.cpp +106 -0
- package/targets/linux/src/elur_fonts.h +31 -0
- package/targets/linux/src/elur_host.cpp +1041 -0
- package/targets/linux/src/elur_host.h +66 -0
- package/targets/linux/src/elur_http_module.cpp +437 -0
- package/targets/linux/src/elur_js.h +40 -0
- package/targets/linux/src/elur_modules.cpp +453 -0
- package/targets/linux/src/elur_modules.h +159 -0
- package/targets/linux/src/elur_renderer.h +46 -0
- package/targets/linux/src/elur_shot.cpp +64 -0
- package/targets/linux/src/elur_shot.h +17 -0
- package/targets/linux/src/elur_skia_a11y.cpp +987 -0
- package/targets/linux/src/elur_skia_a11y.h +146 -0
- package/targets/linux/src/elur_skia_app.cpp +1770 -0
- package/targets/linux/src/elur_skia_app.h +18 -0
- package/targets/linux/src/elur_state_module.cpp +99 -0
- package/targets/linux/src/elur_tck_module.cpp +186 -0
- package/targets/linux/src/elur_ui_module.cpp +84 -0
- package/targets/linux/src/elur_yoga.cpp +452 -0
- package/targets/linux/src/elur_yoga.h +95 -0
- package/targets/linux/src/main.cpp +294 -0
- package/targets/windows/Package.appxmanifest +28 -0
- package/targets/windows/README.md +44 -0
- package/targets/windows/elur-windows.vcxproj +133 -0
- package/targets/windows/packages.config +10 -0
- package/targets/windows/src/elur_crypto_module_win.cpp +106 -0
- package/targets/windows/src/elur_events.h +58 -0
- package/targets/windows/src/elur_host_win.cpp +645 -0
- package/targets/windows/src/elur_host_win.h +75 -0
- package/targets/windows/src/elur_http_module_win.cpp +331 -0
- package/targets/windows/src/elur_js.h +40 -0
- package/targets/windows/src/elur_modules.cpp +460 -0
- package/targets/windows/src/elur_modules.h +159 -0
- package/targets/windows/src/elur_state_module.cpp +99 -0
- package/targets/windows/src/elur_ui_module_win.cpp +75 -0
- package/targets/windows/src/elur_uia_host.h +129 -0
- package/targets/windows/src/elur_win_applicator.cpp +3118 -0
- package/targets/windows/src/elur_win_applicator.h +186 -0
- package/targets/windows/src/elur_yoga.cpp +452 -0
- package/targets/windows/src/elur_yoga.h +95 -0
- package/targets/windows/src/main.cpp +321 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// elur_events.h — envelope de evento host→JS compartido por los
|
|
2
|
+
// aplicadores del target Linux (Platform Views y el carril Skia
|
|
3
|
+
// opt-in, ADR-0123). Un solo constructor del envelope garantiza el
|
|
4
|
+
// contrato de src/events.ts en ambos renderers.
|
|
5
|
+
//
|
|
6
|
+
// merge: el payload JSON del attach ({"key":…} de filas template) se
|
|
7
|
+
// fusiona en data — mismo algoritmo que callEventHandler de Kotlin.
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <cinttypes>
|
|
11
|
+
#include <cstdio>
|
|
12
|
+
#include <functional>
|
|
13
|
+
#include <cstdint>
|
|
14
|
+
#include <windows.h>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <unordered_map>
|
|
17
|
+
|
|
18
|
+
namespace elur {
|
|
19
|
+
|
|
20
|
+
// src/events.ts EVENT_CONTRACT_VERSION (ADR-0179).
|
|
21
|
+
constexpr int EVENT_CONTRACT_V = 5;
|
|
22
|
+
|
|
23
|
+
inline int64_t hostNowMs() { return (int64_t)GetTickCount64(); }
|
|
24
|
+
|
|
25
|
+
struct EventBind {
|
|
26
|
+
std::string handler;
|
|
27
|
+
bool continuous = false;
|
|
28
|
+
std::string payload; // JSON objeto — se mergea en data del envelope
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Callbacks que el host registra (call into JS).
|
|
32
|
+
struct HostCallbacks {
|
|
33
|
+
std::function<void(const char *name)> callHandler;
|
|
34
|
+
std::function<void(const char *name, const char *value)> callHandlerStr;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Emite el envelope {v,event,origin,ts,data} al handler del bind.
|
|
38
|
+
inline void emitHostEvent(const HostCallbacks &cb, const EventBind &bind,
|
|
39
|
+
const char *event, const char *origin,
|
|
40
|
+
const std::string &dataJson) {
|
|
41
|
+
std::string data = dataJson;
|
|
42
|
+
if (bind.payload.size() > 2 && bind.payload.front() == '{' &&
|
|
43
|
+
!data.empty() && data.back() == '}') {
|
|
44
|
+
std::string inner = bind.payload.substr(1, bind.payload.size() - 2);
|
|
45
|
+
data.pop_back();
|
|
46
|
+
if (data.back() != '{')
|
|
47
|
+
data += ',';
|
|
48
|
+
data += inner + "}";
|
|
49
|
+
}
|
|
50
|
+
char ts[32];
|
|
51
|
+
snprintf(ts, sizeof(ts), "%" PRId64, hostNowMs());
|
|
52
|
+
std::string env = "{\"v\":" + std::to_string(EVENT_CONTRACT_V) +
|
|
53
|
+
",\"event\":\"" + event + "\",\"origin\":\"" + origin +
|
|
54
|
+
"\",\"ts\":" + ts + ",\"data\":" + data + "}";
|
|
55
|
+
cb.callHandlerStr(bind.handler.c_str(), env.c_str());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
} // namespace elur
|
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
// elur_host_win.cpp — ver header. Port de elur_host.cpp (Linux) al
|
|
2
|
+
// hilo UI de WinUI: tick callback → CompositionTarget::Rendering;
|
|
3
|
+
// g_timeout → DispatcherQueueTimer; uri launcher → ShellExecuteW;
|
|
4
|
+
// GtkSettings → UISettings(UIColorType::Background); glib entropy →
|
|
5
|
+
// BCryptGenRandom. Anti-reentrancia (invariante 4, ADR-0187 §S): un
|
|
6
|
+
// callback host→JS durante applyFrame se encola y drena por
|
|
7
|
+
// DispatcherQueue, nunca anida JS_Call sobre la pila del apply.
|
|
8
|
+
|
|
9
|
+
#include "elur_host_win.h"
|
|
10
|
+
|
|
11
|
+
#include "elur_js.h"
|
|
12
|
+
#include "elur_modules.h"
|
|
13
|
+
#include "elur_win_applicator.h"
|
|
14
|
+
|
|
15
|
+
#include <windows.h>
|
|
16
|
+
#include <bcrypt.h>
|
|
17
|
+
#include <cstdio>
|
|
18
|
+
#include <fstream>
|
|
19
|
+
#include <mutex>
|
|
20
|
+
#include <queue>
|
|
21
|
+
#include <shellapi.h>
|
|
22
|
+
#include <unordered_map>
|
|
23
|
+
#include <winrt/Microsoft.UI.Dispatching.h>
|
|
24
|
+
#include <winrt/Microsoft.UI.Xaml.Media.Imaging.h>
|
|
25
|
+
#include <winrt/Microsoft.UI.Xaml.Media.h>
|
|
26
|
+
#include <winrt/Windows.Graphics.Imaging.h>
|
|
27
|
+
#include <winrt/Windows.Storage.Streams.h>
|
|
28
|
+
#include <winrt/Windows.Foundation.h>
|
|
29
|
+
#include <winrt/Windows.UI.ViewManagement.h>
|
|
30
|
+
#include <winrt/Windows.UI.h>
|
|
31
|
+
|
|
32
|
+
#pragma comment(lib, "bcrypt.lib")
|
|
33
|
+
|
|
34
|
+
// Registradores de módulos builtin — símbolos globales (no en
|
|
35
|
+
// namespace elur), igual que en el host Linux.
|
|
36
|
+
void elurRegisterStateModule();
|
|
37
|
+
void elurRegisterHttpModule();
|
|
38
|
+
void elurRegisterCryptoModule();
|
|
39
|
+
void elurRegisterUiModule();
|
|
40
|
+
void dlog(const char *fmt, ...);
|
|
41
|
+
|
|
42
|
+
namespace mud = winrt::Microsoft::UI::Dispatching;
|
|
43
|
+
namespace wf = winrt::Windows::Foundation;
|
|
44
|
+
namespace wuv = winrt::Windows::UI::ViewManagement;
|
|
45
|
+
|
|
46
|
+
namespace elur {
|
|
47
|
+
namespace {
|
|
48
|
+
|
|
49
|
+
JSRuntime *g_rt = nullptr;
|
|
50
|
+
JSContext *g_ctx = nullptr;
|
|
51
|
+
HostOptions g_opts;
|
|
52
|
+
mud::DispatcherQueue g_dq{nullptr};
|
|
53
|
+
bool g_frameArmed = false;
|
|
54
|
+
winrt::event_token g_frameTok{};
|
|
55
|
+
std::unordered_map<int, mud::DispatcherQueueTimer> g_timers;
|
|
56
|
+
|
|
57
|
+
int g_inApply = 0;
|
|
58
|
+
struct PendingCall {
|
|
59
|
+
std::string name;
|
|
60
|
+
bool hasValue;
|
|
61
|
+
std::string value;
|
|
62
|
+
};
|
|
63
|
+
std::vector<PendingCall> g_pendingCalls;
|
|
64
|
+
bool g_drainScheduled = false;
|
|
65
|
+
|
|
66
|
+
void callHandler0(const char *name);
|
|
67
|
+
void callHandlerStr(const char *name, const char *value);
|
|
68
|
+
|
|
69
|
+
void drainPendingCalls() {
|
|
70
|
+
g_drainScheduled = false;
|
|
71
|
+
std::vector<PendingCall> batch;
|
|
72
|
+
batch.swap(g_pendingCalls);
|
|
73
|
+
for (auto &p : batch) {
|
|
74
|
+
if (p.hasValue)
|
|
75
|
+
callHandlerStr(p.name.c_str(), p.value.c_str());
|
|
76
|
+
else
|
|
77
|
+
callHandler0(p.name.c_str());
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
void scheduleDrain() {
|
|
82
|
+
if (g_drainScheduled)
|
|
83
|
+
return;
|
|
84
|
+
g_drainScheduled = true;
|
|
85
|
+
g_dq.TryEnqueue([] { drainPendingCalls(); });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void deferCall0(const char *name) {
|
|
89
|
+
g_pendingCalls.push_back({name, false, {}});
|
|
90
|
+
scheduleDrain();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void deferCallStr(const char *name, const char *value) {
|
|
94
|
+
g_pendingCalls.push_back({name, true, value ? value : ""});
|
|
95
|
+
scheduleDrain();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void logJsException(JSContext *ctx, const char *label) {
|
|
99
|
+
JSValue exc = JS_GetException(ctx);
|
|
100
|
+
const char *s = JS_ToCString(ctx, exc);
|
|
101
|
+
fprintf(stderr, "[elur] %s: %s\n", label, s ? s : "unknown");
|
|
102
|
+
dlog("[elur] %s: %s\n", label, s ? s : "unknown");
|
|
103
|
+
if (s)
|
|
104
|
+
JS_FreeCString(ctx, s);
|
|
105
|
+
JS_FreeValue(ctx, exc);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
void drainJobs() {
|
|
109
|
+
JSContext *pctx = nullptr;
|
|
110
|
+
while (JS_IsJobPending(g_rt)) {
|
|
111
|
+
int r = JS_ExecutePendingJob(g_rt, &pctx);
|
|
112
|
+
if (r < 0) {
|
|
113
|
+
logJsException(pctx ? pctx : g_ctx, "job");
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
void callHandler0(const char *name) {
|
|
120
|
+
if (!g_ctx)
|
|
121
|
+
return;
|
|
122
|
+
if (g_inApply > 0) {
|
|
123
|
+
deferCall0(name);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
JSValue global = JS_GetGlobalObject(g_ctx);
|
|
127
|
+
JSValue fn = JS_GetPropertyStr(g_ctx, global, "__elurCallHandler");
|
|
128
|
+
if (JS_IsFunction(g_ctx, fn)) {
|
|
129
|
+
JSValue arg = JS_NewString(g_ctx, name);
|
|
130
|
+
JSValue r = JS_Call(g_ctx, fn, global, 1, &arg);
|
|
131
|
+
if (JS_IsException(r))
|
|
132
|
+
logJsException(g_ctx, name);
|
|
133
|
+
JS_FreeValue(g_ctx, r);
|
|
134
|
+
JS_FreeValue(g_ctx, arg);
|
|
135
|
+
drainJobs();
|
|
136
|
+
}
|
|
137
|
+
JS_FreeValue(g_ctx, fn);
|
|
138
|
+
JS_FreeValue(g_ctx, global);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
void callHandlerStr(const char *name, const char *value) {
|
|
142
|
+
if (!g_ctx)
|
|
143
|
+
return;
|
|
144
|
+
if (g_inApply > 0) {
|
|
145
|
+
deferCallStr(name, value);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
JSValue global = JS_GetGlobalObject(g_ctx);
|
|
149
|
+
JSValue fn =
|
|
150
|
+
JS_GetPropertyStr(g_ctx, global, "__elurCallHandlerWithString");
|
|
151
|
+
if (JS_IsFunction(g_ctx, fn)) {
|
|
152
|
+
JSValue args[2] = {JS_NewString(g_ctx, name),
|
|
153
|
+
JS_NewString(g_ctx, value ? value : "")};
|
|
154
|
+
JSValue r = JS_Call(g_ctx, fn, global, 2, args);
|
|
155
|
+
if (JS_IsException(r))
|
|
156
|
+
logJsException(g_ctx, name);
|
|
157
|
+
JS_FreeValue(g_ctx, r);
|
|
158
|
+
JS_FreeValue(g_ctx, args[0]);
|
|
159
|
+
JS_FreeValue(g_ctx, args[1]);
|
|
160
|
+
drainJobs();
|
|
161
|
+
}
|
|
162
|
+
JS_FreeValue(g_ctx, fn);
|
|
163
|
+
JS_FreeValue(g_ctx, global);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ── Bridge JS → host ───────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
JSValue jsFlush(JSContext *ctx, JSValueConst, int argc,
|
|
169
|
+
JSValueConst *argv) {
|
|
170
|
+
if (argc < 1)
|
|
171
|
+
return JS_UNDEFINED;
|
|
172
|
+
const char *json = JS_ToCString(ctx, argv[0]);
|
|
173
|
+
if (!json)
|
|
174
|
+
return JS_UNDEFINED;
|
|
175
|
+
g_inApply++;
|
|
176
|
+
JSValue v = JS_ParseJSON(ctx, json, strlen(json), "<flush>");
|
|
177
|
+
g_winapp.applyFrame(ctx, v);
|
|
178
|
+
JS_FreeValue(ctx, v);
|
|
179
|
+
g_inApply--;
|
|
180
|
+
JS_FreeCString(ctx, json);
|
|
181
|
+
return JS_UNDEFINED;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
JSValue jsFlushOps(JSContext *ctx, JSValueConst, int argc,
|
|
185
|
+
JSValueConst *argv) {
|
|
186
|
+
if (argc < 2)
|
|
187
|
+
return JS_UNDEFINED;
|
|
188
|
+
g_inApply++;
|
|
189
|
+
JSValue wrap = JS_NewObject(ctx);
|
|
190
|
+
JS_SetPropertyStr(ctx, wrap, "frame", JS_DupValue(ctx, argv[0]));
|
|
191
|
+
JS_SetPropertyStr(ctx, wrap, "ops", JS_DupValue(ctx, argv[1]));
|
|
192
|
+
g_winapp.applyFrame(ctx, wrap);
|
|
193
|
+
JS_FreeValue(ctx, wrap);
|
|
194
|
+
g_inApply--;
|
|
195
|
+
return JS_UNDEFINED;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
void onRenderingFrame(winrt::Windows::Foundation::IInspectable const &,
|
|
199
|
+
winrt::Windows::Foundation::IInspectable const &) {
|
|
200
|
+
g_frameArmed = false;
|
|
201
|
+
mux::Media::CompositionTarget::Rendering(g_frameTok);
|
|
202
|
+
callHandler0("__elur_frame");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
JSValue jsRequestFrame(JSContext *ctx, JSValueConst, int,
|
|
206
|
+
JSValueConst *) {
|
|
207
|
+
if (!g_frameArmed) {
|
|
208
|
+
g_frameArmed = true;
|
|
209
|
+
g_frameTok =
|
|
210
|
+
mux::Media::CompositionTarget::Rendering(onRenderingFrame);
|
|
211
|
+
}
|
|
212
|
+
return JS_UNDEFINED;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
JSValue jsSetTimer(JSContext *ctx, JSValueConst, int argc,
|
|
216
|
+
JSValueConst *argv) {
|
|
217
|
+
int32_t id = 0;
|
|
218
|
+
int64_t ms = 0;
|
|
219
|
+
if (argc < 2 || JS_ToInt32(ctx, &id, argv[0]) ||
|
|
220
|
+
JS_ToInt64(ctx, &ms, argv[1]) < 0)
|
|
221
|
+
return JS_ThrowTypeError(ctx, "setTimer(id, ms) requiere números");
|
|
222
|
+
auto t = g_dq.CreateTimer();
|
|
223
|
+
t.Interval(winrt::Windows::Foundation::TimeSpan{
|
|
224
|
+
std::max<int64_t>(1, ms) * 10000}); // 100ns units
|
|
225
|
+
t.IsRepeating(false);
|
|
226
|
+
int tid = id;
|
|
227
|
+
t.Tick([tid](auto const &, auto const &) {
|
|
228
|
+
auto it = g_timers.find(tid);
|
|
229
|
+
if (it != g_timers.end())
|
|
230
|
+
g_timers.erase(it);
|
|
231
|
+
callHandlerStr("__elur_timer", std::to_string(tid).c_str());
|
|
232
|
+
});
|
|
233
|
+
auto old = g_timers.find(id);
|
|
234
|
+
if (old != g_timers.end())
|
|
235
|
+
old->second.Stop();
|
|
236
|
+
g_timers.insert_or_assign(id, t);
|
|
237
|
+
t.Start();
|
|
238
|
+
return JS_UNDEFINED;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
JSValue jsClearTimer(JSContext *ctx, JSValueConst, int argc,
|
|
242
|
+
JSValueConst *argv) {
|
|
243
|
+
int32_t id = 0;
|
|
244
|
+
if (argc < 1 || JS_ToInt32(ctx, &id, argv[0]))
|
|
245
|
+
return JS_ThrowTypeError(ctx, "clearTimer(id) requiere un entero");
|
|
246
|
+
auto it = g_timers.find(id);
|
|
247
|
+
if (it != g_timers.end()) {
|
|
248
|
+
it->second.Stop();
|
|
249
|
+
g_timers.erase(it);
|
|
250
|
+
}
|
|
251
|
+
return JS_UNDEFINED;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
JSValue jsOpenUrl(JSContext *ctx, JSValueConst, int argc,
|
|
255
|
+
JSValueConst *argv) {
|
|
256
|
+
if (argc < 1)
|
|
257
|
+
return JS_UNDEFINED;
|
|
258
|
+
const char *url = JS_ToCString(ctx, argv[0]);
|
|
259
|
+
if (url) {
|
|
260
|
+
int wlen = MultiByteToWideChar(CP_UTF8, 0, url, -1, nullptr, 0);
|
|
261
|
+
std::wstring w((size_t)wlen, L'\0');
|
|
262
|
+
MultiByteToWideChar(CP_UTF8, 0, url, -1, w.data(), wlen);
|
|
263
|
+
ShellExecuteW(nullptr, L"open", w.c_str(), nullptr, nullptr,
|
|
264
|
+
SW_SHOWNORMAL);
|
|
265
|
+
JS_FreeCString(ctx, url);
|
|
266
|
+
}
|
|
267
|
+
return JS_UNDEFINED;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
JSValue jsColorScheme(JSContext *ctx, JSValueConst, int,
|
|
271
|
+
JSValueConst *) {
|
|
272
|
+
// UISettings: UIColorType::Background del tema de apps — negro en
|
|
273
|
+
// dark, blanco en light (documentado, no infiere del theme actual).
|
|
274
|
+
try {
|
|
275
|
+
wuv::UISettings s;
|
|
276
|
+
auto c = s.GetColorValue(wuv::UIColorType::Background);
|
|
277
|
+
return JS_NewString(ctx,
|
|
278
|
+
(c.R + c.G + c.B) < 384 ? "dark" : "light");
|
|
279
|
+
} catch (...) {
|
|
280
|
+
return JS_NewString(ctx, "light");
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
JSValue jsDebugNow(JSContext *ctx, JSValueConst, int, JSValueConst *) {
|
|
285
|
+
return JS_NewFloat64(ctx, g_opts.debugNow);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
JSValue jsCallCommand(JSContext *ctx, JSValueConst, int argc,
|
|
289
|
+
JSValueConst *argv) {
|
|
290
|
+
int32_t callId = 0;
|
|
291
|
+
if (argc < 4 || JS_ToInt32(ctx, &callId, argv[0]))
|
|
292
|
+
return JS_ThrowTypeError(ctx, "callCommand(callId,mod,cmd,args)");
|
|
293
|
+
std::string mod = jsStr(ctx, argv[1]);
|
|
294
|
+
std::string cmd = jsStr(ctx, argv[2]);
|
|
295
|
+
std::string args = jsStr(ctx, argv[3]);
|
|
296
|
+
modules::registry().dispatch(callId, mod, cmd, args);
|
|
297
|
+
return JS_UNDEFINED;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
JSValue jsCancelCommand(JSContext *ctx, JSValueConst, int argc,
|
|
301
|
+
JSValueConst *argv) {
|
|
302
|
+
int32_t callId = 0;
|
|
303
|
+
if (argc >= 1 && JS_ToInt32(ctx, &callId, argv[0]) == 0)
|
|
304
|
+
modules::registry().cancel(callId);
|
|
305
|
+
return JS_UNDEFINED;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
JSValue jsReportError(JSContext *ctx, JSValueConst, int argc,
|
|
309
|
+
JSValueConst *argv) {
|
|
310
|
+
std::string kind = argc > 0 ? jsStr(ctx, argv[0]) : "?";
|
|
311
|
+
std::string json = argc > 1 ? jsStr(ctx, argv[1]) : "{}";
|
|
312
|
+
fprintf(stderr, "[elur-error] %s %s\n", kind.c_str(), json.c_str());
|
|
313
|
+
return JS_UNDEFINED;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
JSValue jsConsole(JSContext *ctx, JSValueConst thisv, int argc,
|
|
317
|
+
JSValueConst *argv) {
|
|
318
|
+
const char *magic = JS_ToCString(ctx, thisv); // unused
|
|
319
|
+
(void)magic;
|
|
320
|
+
std::string line;
|
|
321
|
+
for (int i = 0; i < argc; i++) {
|
|
322
|
+
const char *s = JS_ToCString(ctx, argv[i]);
|
|
323
|
+
if (!line.empty())
|
|
324
|
+
line += ' ';
|
|
325
|
+
line += s ? s : "";
|
|
326
|
+
if (s)
|
|
327
|
+
JS_FreeCString(ctx, s);
|
|
328
|
+
}
|
|
329
|
+
fprintf(stderr, "[js] %s\n", line.c_str());
|
|
330
|
+
return JS_UNDEFINED;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
JSValue jsRandomBytes(JSContext *ctx, JSValueConst, int argc,
|
|
334
|
+
JSValueConst *argv) {
|
|
335
|
+
int32_t len = 0;
|
|
336
|
+
if (argc < 1 || JS_ToInt32(ctx, &len, argv[0]) < 0)
|
|
337
|
+
return JS_ThrowTypeError(ctx, "__elurRandom(len) requiere entero");
|
|
338
|
+
if (len < 0 || len > 65536)
|
|
339
|
+
return JS_ThrowRangeError(ctx, "__elurRandom: len fuera de 0..65536");
|
|
340
|
+
std::vector<uint8_t> buf((size_t)len);
|
|
341
|
+
if (len > 0 && BCryptGenRandom(nullptr, buf.data(), (ULONG)len,
|
|
342
|
+
BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0)
|
|
343
|
+
return JS_ThrowInternalError(ctx, "__elurRandom: sin entropía");
|
|
344
|
+
return JS_NewArrayBufferCopy(ctx, buf.data(), buf.size());
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
void installGlobals() {
|
|
348
|
+
JSValue global = JS_GetGlobalObject(g_ctx);
|
|
349
|
+
|
|
350
|
+
JSValue console = JS_NewObject(g_ctx);
|
|
351
|
+
for (const char *m : {"log", "info", "warn", "error", "debug"})
|
|
352
|
+
JS_SetPropertyStr(g_ctx, console, m,
|
|
353
|
+
JS_NewCFunction(g_ctx, jsConsole, m, 1));
|
|
354
|
+
JS_SetPropertyStr(g_ctx, global, "console", console);
|
|
355
|
+
JS_SetPropertyStr(g_ctx, global, "__elurRandom",
|
|
356
|
+
JS_NewCFunction(g_ctx, jsRandomBytes, "__elurRandom", 1));
|
|
357
|
+
|
|
358
|
+
JSValue bridge = JS_NewObject(g_ctx);
|
|
359
|
+
JS_SetPropertyStr(g_ctx, bridge, "flush",
|
|
360
|
+
JS_NewCFunction(g_ctx, jsFlush, "flush", 1));
|
|
361
|
+
JS_SetPropertyStr(g_ctx, bridge, "flushOps",
|
|
362
|
+
JS_NewCFunction(g_ctx, jsFlushOps, "flushOps", 2));
|
|
363
|
+
JS_SetPropertyStr(g_ctx, bridge, "requestFrame",
|
|
364
|
+
JS_NewCFunction(g_ctx, jsRequestFrame, "requestFrame", 0));
|
|
365
|
+
JS_SetPropertyStr(g_ctx, bridge, "setTimer",
|
|
366
|
+
JS_NewCFunction(g_ctx, jsSetTimer, "setTimer", 2));
|
|
367
|
+
JS_SetPropertyStr(g_ctx, bridge, "clearTimer",
|
|
368
|
+
JS_NewCFunction(g_ctx, jsClearTimer, "clearTimer", 1));
|
|
369
|
+
JS_SetPropertyStr(g_ctx, bridge, "openUrl",
|
|
370
|
+
JS_NewCFunction(g_ctx, jsOpenUrl, "openUrl", 1));
|
|
371
|
+
JS_SetPropertyStr(g_ctx, bridge, "colorScheme",
|
|
372
|
+
JS_NewCFunction(g_ctx, jsColorScheme, "colorScheme", 0));
|
|
373
|
+
JS_SetPropertyStr(g_ctx, bridge, "debugNow",
|
|
374
|
+
JS_NewCFunction(g_ctx, jsDebugNow, "debugNow", 0));
|
|
375
|
+
JS_SetPropertyStr(g_ctx, bridge, "callCommand",
|
|
376
|
+
JS_NewCFunction(g_ctx, jsCallCommand, "callCommand", 4));
|
|
377
|
+
JS_SetPropertyStr(
|
|
378
|
+
g_ctx, bridge, "cancelCommand",
|
|
379
|
+
JS_NewCFunction(g_ctx, jsCancelCommand, "cancelCommand", 1));
|
|
380
|
+
JS_SetPropertyStr(g_ctx, bridge, "reportError",
|
|
381
|
+
JS_NewCFunction(g_ctx, jsReportError, "reportError", 2));
|
|
382
|
+
// Declarados por contrato (ADR-0044/0158/0172/0187) — el runtime
|
|
383
|
+
// nunca los infiere.
|
|
384
|
+
JS_SetPropertyStr(g_ctx, bridge, "engine", JS_NewString(g_ctx, "quickjs-ng"));
|
|
385
|
+
JS_SetPropertyStr(g_ctx, bridge, "platform", JS_NewString(g_ctx, "windows"));
|
|
386
|
+
JS_SetPropertyStr(g_ctx, bridge, "threadModel", JS_NewString(g_ctx, "S"));
|
|
387
|
+
JS_SetPropertyStr(g_ctx, bridge, "bridgeAbi", JS_NewInt32(g_ctx, 1));
|
|
388
|
+
JS_SetPropertyStr(g_ctx, global, "ElurBridge", bridge);
|
|
389
|
+
|
|
390
|
+
JS_FreeValue(g_ctx, global);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
bool evalFile(const std::string &path, const char *label) {
|
|
394
|
+
std::ifstream f(path, std::ios::binary);
|
|
395
|
+
if (!f) {
|
|
396
|
+
fprintf(stderr, "[elur] no se pudo leer %s\n", path.c_str());
|
|
397
|
+
dlog("[elur] no se pudo leer %s\n", path.c_str());
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
std::string buf((std::istreambuf_iterator<char>(f)),
|
|
401
|
+
std::istreambuf_iterator<char>());
|
|
402
|
+
bool ok = true;
|
|
403
|
+
if (path.size() >= 5 && path.substr(path.size() - 5) == ".qjsc") {
|
|
404
|
+
JSValue fn = JS_ReadObject(g_ctx, (const uint8_t *)buf.data(),
|
|
405
|
+
buf.size(), JS_READ_OBJ_BYTECODE);
|
|
406
|
+
if (JS_IsException(fn)) {
|
|
407
|
+
logJsException(g_ctx, label);
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
JSValue r = JS_EvalFunction(g_ctx, fn);
|
|
411
|
+
if (JS_IsException(r)) {
|
|
412
|
+
logJsException(g_ctx, label);
|
|
413
|
+
ok = false;
|
|
414
|
+
}
|
|
415
|
+
JS_FreeValue(g_ctx, r);
|
|
416
|
+
} else {
|
|
417
|
+
JSValue r = JS_Eval(g_ctx, buf.data(), buf.size(), path.c_str(),
|
|
418
|
+
JS_EVAL_TYPE_GLOBAL);
|
|
419
|
+
if (JS_IsException(r)) {
|
|
420
|
+
logJsException(g_ctx, label);
|
|
421
|
+
ok = false;
|
|
422
|
+
}
|
|
423
|
+
JS_FreeValue(g_ctx, r);
|
|
424
|
+
}
|
|
425
|
+
drainJobs();
|
|
426
|
+
return ok;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
} // namespace
|
|
430
|
+
|
|
431
|
+
void hostPostUi(std::function<void()> fn) {
|
|
432
|
+
g_dq.TryEnqueue([f = std::move(fn)] { f(); });
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
JSContext *hostCtx() { return g_ctx; }
|
|
436
|
+
void hostCallHandler(const char *name) { callHandler0(name); }
|
|
437
|
+
void hostCallHandlerStr(const char *name, const char *value) {
|
|
438
|
+
callHandlerStr(name, value);
|
|
439
|
+
}
|
|
440
|
+
void hostDrainJobs() { drainJobs(); }
|
|
441
|
+
|
|
442
|
+
void scheduleShot(HWND hwnd, const std::string &path);
|
|
443
|
+
|
|
444
|
+
bool hostInit(const muxc::Canvas &root, const HostOptions &opts) {
|
|
445
|
+
g_opts = opts;
|
|
446
|
+
dlog("[elur] dq...\n");
|
|
447
|
+
g_dq = mud::DispatcherQueue::GetForCurrentThread();
|
|
448
|
+
dlog("[elur] dq ok\n");
|
|
449
|
+
|
|
450
|
+
g_rt = JS_NewRuntime();
|
|
451
|
+
if (!g_rt)
|
|
452
|
+
return false;
|
|
453
|
+
g_ctx = JS_NewContext(g_rt);
|
|
454
|
+
if (!g_ctx) {
|
|
455
|
+
JS_FreeRuntime(g_rt);
|
|
456
|
+
g_rt = nullptr;
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
dlog("[elur] qjs ok\n");
|
|
460
|
+
|
|
461
|
+
installGlobals();
|
|
462
|
+
dlog("[elur] globals ok\n");
|
|
463
|
+
modules::initRegistry([](const char *name, const char *payload) {
|
|
464
|
+
callHandlerStr(name, payload);
|
|
465
|
+
});
|
|
466
|
+
std::string appId = opts.app;
|
|
467
|
+
auto slash = appId.find_last_of("/\\");
|
|
468
|
+
if (slash != std::string::npos)
|
|
469
|
+
appId = appId.substr(slash + 1);
|
|
470
|
+
auto dot = appId.find_last_of('.');
|
|
471
|
+
if (dot != std::string::npos)
|
|
472
|
+
appId = appId.substr(0, dot);
|
|
473
|
+
modules::registry().setAppId(appId);
|
|
474
|
+
elurRegisterStateModule();
|
|
475
|
+
elurRegisterHttpModule();
|
|
476
|
+
elurRegisterCryptoModule();
|
|
477
|
+
elurRegisterUiModule();
|
|
478
|
+
modules::registerGeneratedModules();
|
|
479
|
+
dlog("[elur] modules ok\n");
|
|
480
|
+
|
|
481
|
+
HostCallbacks cb;
|
|
482
|
+
cb.callHandler = [](const char *n) { callHandler0(n); };
|
|
483
|
+
cb.callHandlerStr = [](const char *n, const char *v) {
|
|
484
|
+
callHandlerStr(n, v);
|
|
485
|
+
};
|
|
486
|
+
g_winapp.init(root, opts, cb);
|
|
487
|
+
dlog("[elur] applicator init ok\n");
|
|
488
|
+
g_winapp.setViewport(opts.width, opts.height);
|
|
489
|
+
if (opts.rtl)
|
|
490
|
+
g_winapp.setRtl(true);
|
|
491
|
+
g_winapp.trace = opts.trace;
|
|
492
|
+
|
|
493
|
+
std::string dir = opts.bundleDir;
|
|
494
|
+
if (!dir.empty() && dir.back() != '/' && dir.back() != '\\')
|
|
495
|
+
dir += '/';
|
|
496
|
+
// Preferir bytecode qjsc vendored si el bundle lo trae (match
|
|
497
|
+
// BC exacto — igual que el host Linux).
|
|
498
|
+
std::string sigQjsc = dir + "elurSignals.qjsc";
|
|
499
|
+
std::string appQjsc = dir + opts.app;
|
|
500
|
+
auto dotPos = appQjsc.find_last_of('.');
|
|
501
|
+
if (dotPos != std::string::npos)
|
|
502
|
+
appQjsc = appQjsc.substr(0, dotPos) + ".qjsc";
|
|
503
|
+
auto exists = [](const std::string &p) {
|
|
504
|
+
std::ifstream t(p, std::ios::binary);
|
|
505
|
+
return t.good();
|
|
506
|
+
};
|
|
507
|
+
const std::string sig =
|
|
508
|
+
exists(sigQjsc) ? sigQjsc : dir + "elurSignals.js";
|
|
509
|
+
const std::string app =
|
|
510
|
+
exists(appQjsc) ? appQjsc : dir + opts.app;
|
|
511
|
+
|
|
512
|
+
bool ok = evalFile(sig, "signals");
|
|
513
|
+
dlog("[elur] eval signals %d\n", (int)ok);
|
|
514
|
+
if (ok)
|
|
515
|
+
ok = evalFile(app, "app");
|
|
516
|
+
dlog("[elur] eval app %d\n", (int)ok);
|
|
517
|
+
|
|
518
|
+
JSValue global = JS_GetGlobalObject(g_ctx);
|
|
519
|
+
JSValue fn = JS_GetPropertyStr(g_ctx, global, "__elurFlush");
|
|
520
|
+
if (JS_IsFunction(g_ctx, fn)) {
|
|
521
|
+
JSValue r = JS_Call(g_ctx, fn, global, 0, nullptr);
|
|
522
|
+
if (JS_IsException(r))
|
|
523
|
+
logJsException(g_ctx, "__elurFlush");
|
|
524
|
+
JS_FreeValue(g_ctx, r);
|
|
525
|
+
drainJobs();
|
|
526
|
+
}
|
|
527
|
+
JS_FreeValue(g_ctx, fn);
|
|
528
|
+
JS_FreeValue(g_ctx, global);
|
|
529
|
+
|
|
530
|
+
char vp[64];
|
|
531
|
+
snprintf(vp, sizeof(vp), "{\"width\":%d,\"height\":%d}", opts.width,
|
|
532
|
+
opts.height);
|
|
533
|
+
callHandlerStr("__elur_viewport", vp);
|
|
534
|
+
dlog("[elur] viewport sent\n");
|
|
535
|
+
JSValue scheme = jsColorScheme(g_ctx, JS_UNDEFINED, 0, nullptr);
|
|
536
|
+
const char *sc = JS_ToCString(g_ctx, scheme);
|
|
537
|
+
callHandlerStr("__elur_scheme", sc ? sc : "light");
|
|
538
|
+
if (sc)
|
|
539
|
+
JS_FreeCString(g_ctx, sc);
|
|
540
|
+
JS_FreeValue(g_ctx, scheme);
|
|
541
|
+
if (!opts.deeplink.empty())
|
|
542
|
+
callHandlerStr("__elur_deeplink", opts.deeplink.c_str());
|
|
543
|
+
drainJobs();
|
|
544
|
+
if (!opts.shot.empty())
|
|
545
|
+
scheduleShot((HWND)opts.hwnd, opts.shot);
|
|
546
|
+
return ok;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// --shot: renderiza el visual tree a PNG y cierra. Verificación
|
|
550
|
+
// headless equivalente al screencap del host Android.
|
|
551
|
+
// Los Completed de IAsyncXxx corren en threadpool: todo acceso a
|
|
552
|
+
// objetos Xaml (rtb, PixelWidth) rebota por g_dq al hilo UI.
|
|
553
|
+
void quitApp(int code) {
|
|
554
|
+
g_dq.TryEnqueue([code] { PostQuitMessage(code); });
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
static mud::DispatcherQueueTimer g_shotTimer{nullptr};
|
|
558
|
+
|
|
559
|
+
void scheduleShot(HWND hwnd, const std::string &path) {
|
|
560
|
+
// Captura síncrona vía PrintWindow (PW_RENDERFULLCONTENT cubre el
|
|
561
|
+
// contenido DComposition de WinUI 3). RenderTargetBitmap no aplica:
|
|
562
|
+
// en unpackaged devuelve un IBuffer vacío (Length=Capacity=0).
|
|
563
|
+
auto timer = mud::DispatcherQueue::GetForCurrentThread().CreateTimer();
|
|
564
|
+
g_shotTimer = timer;
|
|
565
|
+
timer.Interval(std::chrono::milliseconds(800));
|
|
566
|
+
timer.IsRepeating(false);
|
|
567
|
+
std::wstring wpath(path.begin(), path.end());
|
|
568
|
+
timer.Tick([hwnd, wpath](auto const &, auto const &) {
|
|
569
|
+
RECT rc{};
|
|
570
|
+
if (!hwnd || !GetClientRect(hwnd, &rc)) {
|
|
571
|
+
dlog("[elur] shot: hwnd inválido\n");
|
|
572
|
+
quitApp(1);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
int w = rc.right - rc.left, h = rc.bottom - rc.top;
|
|
576
|
+
dlog("[elur] shot client %dx%d\n", w, h);
|
|
577
|
+
HDC hdcWin = GetDC(hwnd);
|
|
578
|
+
HDC hdcMem = CreateCompatibleDC(hdcWin);
|
|
579
|
+
HBITMAP hbmp = CreateCompatibleBitmap(hdcWin, w, h);
|
|
580
|
+
HGDIOBJ old = SelectObject(hdcMem, hbmp);
|
|
581
|
+
BOOL ok = PrintWindow(hwnd, hdcMem, PW_RENDERFULLCONTENT);
|
|
582
|
+
SelectObject(hdcMem, old);
|
|
583
|
+
if (!ok) {
|
|
584
|
+
dlog("[elur] shot: PrintWindow falló err=%lu\n", GetLastError());
|
|
585
|
+
DeleteObject(hbmp);
|
|
586
|
+
DeleteDC(hdcMem);
|
|
587
|
+
ReleaseDC(hwnd, hdcWin);
|
|
588
|
+
quitApp(1);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
BITMAPINFO bmi{};
|
|
592
|
+
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
593
|
+
bmi.bmiHeader.biWidth = w;
|
|
594
|
+
bmi.bmiHeader.biHeight = -h; // top-down
|
|
595
|
+
bmi.bmiHeader.biPlanes = 1;
|
|
596
|
+
bmi.bmiHeader.biBitCount = 32;
|
|
597
|
+
bmi.bmiHeader.biCompression = BI_RGB;
|
|
598
|
+
std::vector<uint8_t> px((size_t)w * h * 4);
|
|
599
|
+
int rows = GetDIBits(hdcMem, hbmp, 0, (UINT)h, px.data(), &bmi,
|
|
600
|
+
DIB_RGB_COLORS);
|
|
601
|
+
DeleteObject(hbmp);
|
|
602
|
+
DeleteDC(hdcMem);
|
|
603
|
+
ReleaseDC(hwnd, hdcWin);
|
|
604
|
+
if (rows != h) {
|
|
605
|
+
dlog("[elur] shot: GetDIBits rows=%d\n", rows);
|
|
606
|
+
quitApp(1);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
BITMAPFILEHEADER fh{};
|
|
610
|
+
fh.bfType = 0x4D42;
|
|
611
|
+
fh.bfOffBits = sizeof(fh) + sizeof(BITMAPINFOHEADER);
|
|
612
|
+
fh.bfSize = fh.bfOffBits + (DWORD)px.size();
|
|
613
|
+
BITMAPINFOHEADER ih = bmi.bmiHeader;
|
|
614
|
+
ih.biSizeImage = (DWORD)px.size();
|
|
615
|
+
if (FILE *f = _wfopen(wpath.c_str(), L"wb")) {
|
|
616
|
+
fwrite(&fh, sizeof fh, 1, f);
|
|
617
|
+
fwrite(&ih, sizeof ih, 1, f);
|
|
618
|
+
fwrite(px.data(), 1, px.size(), f);
|
|
619
|
+
fclose(f);
|
|
620
|
+
dlog("[elur] shot saved (%zu bytes)\n", px.size());
|
|
621
|
+
quitApp(0);
|
|
622
|
+
} else {
|
|
623
|
+
dlog("[elur] shot: no pudo abrir %ls\n", wpath.c_str());
|
|
624
|
+
quitApp(1);
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
timer.Start();
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
void hostShutdown() {
|
|
631
|
+
g_timers.clear();
|
|
632
|
+
if (g_frameArmed)
|
|
633
|
+
mux::Media::CompositionTarget::Rendering(g_frameTok);
|
|
634
|
+
g_winapp.shutdown();
|
|
635
|
+
if (g_ctx) {
|
|
636
|
+
JS_FreeContext(g_ctx);
|
|
637
|
+
g_ctx = nullptr;
|
|
638
|
+
}
|
|
639
|
+
if (g_rt) {
|
|
640
|
+
JS_FreeRuntime(g_rt);
|
|
641
|
+
g_rt = nullptr;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
} // namespace elur
|