Skip to content

Commit

Permalink
Support printing unicode characters on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns authored Sep 30, 2024
1 parent 0bf36b9 commit 72d4587
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down

0 comments on commit 72d4587

Please sign in to comment.