Skip to content

Commit 9027a4f

Browse files
feat: upgrade quickjs and add asan target
1 parent ca9287f commit 9027a4f

File tree

12 files changed

+315
-193
lines changed

12 files changed

+315
-193
lines changed

dependencies/quickjs-ng.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package("quickjs-ng")
66
add_urls("https://github.com/quickjs-ng/quickjs/archive/refs/tags/$(version).tar.gz",
77
"https://github.com/quickjs-ng/quickjs.git", {submodules = false})
88

9-
add_versions("v0.8.0", "7e60e1e0dcd07d25664331308a2f4aee2a88d60d85896e828d25df7c3d40204e")
9+
add_versions("v0.11.0", "v0.11.0")
1010

1111
add_configs("libc", {description = "Build standard library modules as part of the library", default = false, type = "boolean"})
1212

@@ -15,7 +15,6 @@ package("quickjs-ng")
1515
end
1616

1717
add_deps("cmake")
18-
1918
if on_check then
2019
on_check("windows", function (package)
2120
local vs_toolset = package:toolchain("msvc"):config("vs_toolset")
@@ -26,8 +25,8 @@ package("quickjs-ng")
2625
end
2726
end)
2827
on_check("wasm", "cross", function (package)
29-
if package:version():eq("0.8.0") then
30-
raise("package(quickjs-ng v0.8.0) unsupported platform")
28+
if package:version():eq("0.11.0") then
29+
raise("package(quickjs-ng v0.11.0) unsupported platform")
3130
end
3231
end)
3332
end
@@ -37,9 +36,9 @@ package("quickjs-ng")
3736
io.replace("CMakeLists.txt", "if(NOT WIN32 AND NOT EMSCRIPTEN)", "if(0)", {plain = true})
3837

3938
local configs = {}
40-
table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
39+
table.insert(configs, "-DCMAKE_BUILD_TYPE=Release")
4140
if package:is_plat("windows") then
42-
if false then
41+
if package:is_debug() then
4342
-- add /debug to link flags
4443
table.insert(configs, "-DCMAKE_EXE_LINKER_FLAGS_RELEASE=\"/DEBUG\"")
4544
table.insert(configs, "-DCMAKE_SHARED_LINKER_FLAGS_RELEASE=\"/DEBUG\"")
@@ -51,10 +50,11 @@ package("quickjs-ng")
5150
end
5251

5352
table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
54-
table.insert(configs, "-DCONFIG_ASAN=" .. (package:config("asan") and "ON" or "OFF"))
55-
table.insert(configs, "-DCONFIG_MSAN=" .. (package:config("msan") and "ON" or "OFF"))
56-
table.insert(configs, "-DCONFIG_UBSAN=" .. (package:config("ubsan") and "ON" or "OFF"))
57-
table.insert(configs, "-DBUILD_QJS_LIBC=" .. (package:config("libc") and "ON" or "OFF"))
53+
table.insert(configs, "-DQJS_CONFIG_ASAN=" .. (package:config("asan") and "ON" or "OFF"))
54+
table.insert(configs, "-DQJS_CONFIG_MSAN=" .. (package:config("msan") and "ON" or "OFF"))
55+
table.insert(configs, "-DQJS_CONFIG_UBSAN=" .. (package:config("ubsan") and "ON" or "OFF"))
56+
table.insert(configs, "-DQJS_BUILD_LIBC=" .. (package:config("libc") and "ON" or "OFF"))
57+
io.replace("CMakeLists.txt", "add_executable(test_conv\n tests/test_conv.c\n)", "", {plain = true})
5858
if package:config("shared") and package:is_plat("windows") then
5959
table.insert(configs, "-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON")
6060
end

scripts/rebuild.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ foreach ($pidx in $pids) {
55

66

77
xmake b --yes inject
8-
(xmake b --yes shell) -and (xmake r inject new)
8+
xmake b --yes shell
9+
if ($LASTEXITCODE -ne 0) {
10+
exit $LASTEXITCODE
11+
}
12+
xmake r inject new

scripts/rebuild_asan.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$pids = (Get-WmiObject Win32_Process -Filter "name = 'rundll32.exe'" | where { $_.CommandLine -like '-breeze-asan' }).ProcessId
2+
foreach ($pidx in $pids) {
3+
Stop-Process -Id $pidx -Force
4+
}
5+
6+
xmake f --toolchain=clang-cl -m releasedbg -y --asan=y
7+
if ($LASTEXITCODE -ne 0) {
8+
exit $LASTEXITCODE
9+
}
10+
xmake r --yes -v asan_test

scripts/rebuild_taskbar.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ foreach ($pidx in $pids) {
33
Stop-Process -Id $pidx -Force
44
}
55

6-
xmake r --yes shell taskbar
6+
xmake r --yes shell taskbar -- -taskbar

src/asan/asan_main.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <chrono>
2+
#include <thread>
3+
extern "C" __declspec(dllimport) void func();
4+
5+
int main() {
6+
func();
7+
while (1) {
8+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
9+
}
10+
}

src/shell/entry.cc

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <iostream>
3535
#include <objbase.h>
3636
#include <optional>
37+
#include <print>
3738
#include <string>
3839
#include <string_view>
3940
#include <thread>
@@ -53,7 +54,6 @@ namespace mb_shell {
5354
window_proc_hook entry::main_window_loop_hook{};
5455
void main() {
5556
set_thread_locale_utf8();
56-
5757
AllocConsole();
5858
freopen("CONOUT$", "w", stdout);
5959
freopen("CONOUT$", "w", stderr);
@@ -63,16 +63,15 @@ void main() {
6363
install_error_handlers();
6464
config::run_config_loader();
6565

66+
static script_context script_ctx;
6667
std::thread([]() {
67-
script_context ctx;
68-
6968
auto data_dir = config::data_directory();
7069
auto script_dir = data_dir / "scripts";
7170

7271
if (!std::filesystem::exists(script_dir))
7372
std::filesystem::create_directories(script_dir);
7473

75-
ctx.watch_folder(script_dir, [&]() {
74+
script_ctx.watch_folder(script_dir, [&]() {
7675
return !context_menu_hooks::block_js_reload.load();
7776
});
7877
}).detach();
@@ -132,43 +131,59 @@ void main() {
132131
}
133132

134133
if (filename == "rundll32.exe") {
135-
SetProcessDPIAware();
136-
CoInitialize(nullptr);
137-
std::thread([]() {
138-
CPPTRACE_TRY {
139-
SetThreadDpiAwarenessContext(
140-
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
141-
taskbar_render taskbar;
142-
auto monitor =
143-
MonitorFromPoint({0, 0}, MONITOR_DEFAULTTOPRIMARY);
144-
if (!monitor) {
145-
MessageBoxW(NULL, L"Failed to get primary monitor",
146-
L"Error", MB_ICONERROR);
147-
return;
134+
auto command_line = std::wstring(GetCommandLineW());
135+
136+
if (command_line.contains(L"-taskbar")) {
137+
SetProcessDPIAware();
138+
CoInitialize(nullptr);
139+
std::thread([]() {
140+
CPPTRACE_TRY {
141+
SetThreadDpiAwarenessContext(
142+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
143+
taskbar_render taskbar;
144+
auto monitor =
145+
MonitorFromPoint({0, 0}, MONITOR_DEFAULTTOPRIMARY);
146+
if (!monitor) {
147+
MessageBoxW(NULL, L"Failed to get primary monitor",
148+
L"Error", MB_ICONERROR);
149+
return;
150+
}
151+
taskbar.monitor.cbSize = sizeof(MONITORINFO);
152+
if (GetMonitorInfoW(monitor, &taskbar.monitor) == 0) {
153+
MessageBoxW(NULL,
154+
(L"Failed to get monitor info: " +
155+
std::to_wstring(GetLastError()))
156+
.c_str(),
157+
L"Error", MB_ICONERROR);
158+
return;
159+
}
160+
taskbar.position = taskbar_render::menu_position::bottom;
161+
if (auto res = taskbar.init(); !res) {
162+
MessageBoxW(NULL, L"Failed to initialize taskbar",
163+
L"Error", MB_ICONERROR);
164+
return;
165+
}
166+
167+
taskbar.rt.start_loop();
148168
}
149-
taskbar.monitor.cbSize = sizeof(MONITORINFO);
150-
if (GetMonitorInfoW(monitor, &taskbar.monitor) == 0) {
151-
MessageBoxW(NULL,
152-
(L"Failed to get monitor info: " +
153-
std::to_wstring(GetLastError()))
154-
.c_str(),
155-
L"Error", MB_ICONERROR);
156-
return;
157-
}
158-
taskbar.position = taskbar_render::menu_position::bottom;
159-
if (auto res = taskbar.init(); !res) {
160-
MessageBoxW(NULL, L"Failed to initialize taskbar", L"Error",
161-
MB_ICONERROR);
162-
return;
169+
CPPTRACE_CATCH(const std::exception &e) {
170+
std::cerr << "Error in taskbar thread: " << e.what()
171+
<< std::endl;
172+
cpptrace::from_current_exception().print();
163173
}
174+
}).detach();
175+
}
176+
}
164177

165-
taskbar.rt.start_loop();
166-
}
167-
CPPTRACE_CATCH(const std::exception &e) {
168-
std::cerr << "Error in taskbar thread: " << e.what()
169-
<< std::endl;
170-
cpptrace::from_current_exception().print();
171-
}
178+
if (filename == "asan_test.exe") {
179+
// ASAN environment
180+
init_render_global();
181+
ShowWindow(GetConsoleWindow(), SW_SHOW);
182+
std::thread([]() {
183+
script_ctx.js_ready_future.wait();
184+
script_ctx.js->enqueueJob([]() {
185+
script_ctx.js->eval("globalThis.showConfigPage()", "asan.js");
186+
});
172187
}).detach();
173188
}
174189
}
@@ -177,10 +192,6 @@ void main() {
177192
int APIENTRY DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved) {
178193
switch (fdwReason) {
179194
case DLL_PROCESS_ATTACH: {
180-
auto cmdline = std::string(GetCommandLineA());
181-
182-
std::ranges::transform(cmdline, cmdline.begin(), tolower);
183-
184195
mb_shell::main();
185196
break;
186197
}

src/shell/script/quickjspp.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ template <typename... Ts> struct js_traits<std::variant<Ts...>> {
329329
}
330330

331331
if constexpr (is_vector<U>::value) {
332-
if (JS_IsArray(ctx, v) == 1) {
332+
if (JS_IsArray(v) == 1) {
333333
auto firstElement = JS_GetPropertyUint32(ctx, v, 0);
334334
bool ok = isCompatible<std::decay_t<typename U::value_type>>(
335335
ctx, firstElement);
@@ -341,7 +341,7 @@ template <typename... Ts> struct js_traits<std::variant<Ts...>> {
341341
}
342342

343343
if constexpr (is_pair<U>::value) {
344-
if (JS_IsArray(ctx, v) == 1) {
344+
if (JS_IsArray(v) == 1) {
345345
// todo: check length?
346346
auto firstElement = JS_GetPropertyUint32(ctx, v, 0);
347347
auto secondElement = JS_GetPropertyUint32(ctx, v, 1);
@@ -392,7 +392,7 @@ template <typename... Ts> struct js_traits<std::variant<Ts...>> {
392392
case JS_TAG_FUNCTION_BYTECODE:
393393
return is_callable<T>::value;
394394
case JS_TAG_OBJECT:
395-
if (JS_IsArray(ctx, v) == 1)
395+
if (JS_IsArray(v) == 1)
396396
return is_vector<T>::value || is_pair<T>::value;
397397
if constexpr (is_shared_ptr<T>::value) {
398398
if (JS_GetClassID(v) == js_traits<T>::QJSClassId)
@@ -1346,7 +1346,7 @@ class Value {
13461346
JS_FreeValue(ctx, v);
13471347
}
13481348

1349-
bool isError() const { return JS_IsError(ctx, v); }
1349+
bool isError() const { return JS_IsError(v); }
13501350

13511351
/** Conversion helper function: value.as<T>()
13521352
* @tparam T type to convert to
@@ -1504,7 +1504,7 @@ class Runtime {
15041504
static void promise_unhandled_rejection_tracker(JSContext *ctx,
15051505
JSValue promise,
15061506
JSValue reason,
1507-
JS_BOOL is_handled,
1507+
bool is_handled,
15081508
void *opaque);
15091509

15101510
static JSModuleDef *module_loader(JSContext *ctx, const char *module_name,
@@ -1937,6 +1937,7 @@ template <> struct js_traits<Value> {
19371937
}
19381938

19391939
static JSValue wrap(JSContext *ctx, Value v) noexcept {
1940+
assert(ctx);
19401941
assert(JS_GetRuntime(ctx) == JS_GetRuntime(v.ctx));
19411942
return v.release();
19421943
}
@@ -2081,7 +2082,7 @@ template <class T> struct js_traits<std::vector<T>> {
20812082
}
20822083

20832084
static std::vector<T> unwrap(JSContext *ctx, JSValueConst jsarr) {
2084-
int e = JS_IsArray(ctx, jsarr);
2085+
int e = JS_IsArray(jsarr);
20852086
if (e == 0)
20862087
JS_ThrowTypeError(
20872088
ctx, "js_traits<std::vector<T>>::unwrap expects array");
@@ -2116,7 +2117,7 @@ template <typename U, typename V> struct js_traits<std::pair<U, V>> {
21162117
}
21172118

21182119
static std::pair<U, V> unwrap(JSContext *ctx, JSValueConst jsarr) {
2119-
int e = JS_IsArray(ctx, jsarr);
2120+
int e = JS_IsArray(jsarr);
21202121
if (e == 0)
21212122
JS_ThrowTypeError(ctx, "js_traits<%s>::unwrap expects array",
21222123
QJSPP_TYPENAME(std::pair<U, V>));
@@ -2160,7 +2161,7 @@ template <typename... T> struct js_traits<std::tuple<T...>> {
21602161
}
21612162
}
21622163
static std::tuple<T...> unwrap(JSContext *ctx, JSValueConst jsarr) {
2163-
if (!JS_IsArray(ctx, jsarr)) {
2164+
if (!JS_IsArray(jsarr)) {
21642165
JS_ThrowTypeError(ctx, "Expected an array");
21652166
throw exception{ctx};
21662167
}
@@ -2271,7 +2272,7 @@ inline Value exception::get() { return context().getException(); }
22712272
inline void Runtime::promise_unhandled_rejection_tracker(JSContext *ctx,
22722273
JSValue promise,
22732274
JSValue reason,
2274-
JS_BOOL is_handled,
2275+
bool is_handled,
22752276
void *opaque) {
22762277
auto &context = Context::get(ctx);
22772278
if (context.onUnhandledPromiseRejection) {

0 commit comments

Comments
 (0)