Skip to content

Commit 72d4587

Browse files
authored
Support printing unicode characters on Windows
1 parent 0bf36b9 commit 72d4587

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

quickjs-libc.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -3875,15 +3875,42 @@ JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name)
38753875

38763876
/**********************************************************/
38773877

3878+
#ifdef _WIN32
38783879
static JSValue js_print(JSContext *ctx, JSValue this_val,
38793880
int argc, JSValue *argv)
38803881
{
38813882
int i;
38823883
const char *str;
38833884
size_t len;
3884-
3885+
DWORD written;
3886+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
3887+
if (hConsole == INVALID_HANDLE_VALUE)
3888+
return JS_EXCEPTION;
38853889
for(i = 0; i < argc; i++) {
38863890
if (i != 0)
3891+
WriteConsoleW(hConsole, L" ", 1, &written, NULL);
3892+
str = JS_ToCStringLen(ctx, &len, argv[i]);
3893+
if (!str)
3894+
return JS_EXCEPTION;
3895+
DWORD prev = GetConsoleOutputCP();
3896+
SetConsoleOutputCP(CP_UTF8);
3897+
WriteConsoleA(hConsole, str, len, &written, NULL);
3898+
SetConsoleOutputCP(prev);
3899+
JS_FreeCString(ctx, str);
3900+
}
3901+
WriteConsoleW(hConsole, L"\n", 1, &written, NULL);
3902+
FlushFileBuffers(hConsole);
3903+
return JS_UNDEFINED;
3904+
}
3905+
#else
3906+
static JSValue js_print(JSContext *ctx, JSValue this_val,
3907+
int argc, JSValue *argv)
3908+
{
3909+
int i;
3910+
const char *str;
3911+
size_t len;
3912+
for(i = 0; i < argc; i++) {
3913+
if (i != 0)
38873914
putchar(' ');
38883915
str = JS_ToCStringLen(ctx, &len, argv[i]);
38893916
if (!str)
@@ -3895,6 +3922,7 @@ static JSValue js_print(JSContext *ctx, JSValue this_val,
38953922
fflush(stdout);
38963923
return JS_UNDEFINED;
38973924
}
3925+
#endif
38983926

38993927
void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
39003928
{

0 commit comments

Comments
 (0)