Support printing unicode characters on Windows

This commit is contained in:
Andrew Johnson 2024-09-30 19:30:08 +08:00 committed by GitHub
parent 0bf36b98b9
commit 72d4587163
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3875,13 +3875,40 @@ 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(' ');
@ -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)
{