From 72d4587163066dbb05e9ad32b3ded5b16c33fa5e Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 30 Sep 2024 19:30:08 +0800 Subject: [PATCH] Support printing unicode characters on Windows --- quickjs-libc.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index b60bec9..b8102b7 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -3875,15 +3875,42 @@ JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name) /**********************************************************/ +#ifdef _WIN32 static JSValue js_print(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { int i; const char *str; size_t len; - + DWORD written; + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + if (hConsole == INVALID_HANDLE_VALUE) + return JS_EXCEPTION; for(i = 0; i < argc; i++) { if (i != 0) + WriteConsoleW(hConsole, L" ", 1, &written, NULL); + str = JS_ToCStringLen(ctx, &len, argv[i]); + if (!str) + return JS_EXCEPTION; + DWORD prev = GetConsoleOutputCP(); + SetConsoleOutputCP(CP_UTF8); + WriteConsoleA(hConsole, str, len, &written, NULL); + SetConsoleOutputCP(prev); + JS_FreeCString(ctx, str); + } + WriteConsoleW(hConsole, L"\n", 1, &written, NULL); + FlushFileBuffers(hConsole); + return JS_UNDEFINED; +} +#else +static JSValue js_print(JSContext *ctx, JSValue this_val, + int argc, JSValue *argv) +{ + int i; + const char *str; + size_t len; + for(i = 0; i < argc; i++) { + if (i != 0) putchar(' '); str = JS_ToCStringLen(ctx, &len, argv[i]); if (!str) @@ -3895,6 +3922,7 @@ static JSValue js_print(JSContext *ctx, JSValue this_val, fflush(stdout); return JS_UNDEFINED; } +#endif void js_std_add_helpers(JSContext *ctx, int argc, char **argv) {