From 7b20dcd58fea53cd203df27e2da74bbd2fd013db Mon Sep 17 00:00:00 2001 From: Fries Date: Wed, 26 Jun 2024 16:22:49 -0700 Subject: [PATCH] patch quickjs to compile on the wii --- CMakeLists.txt | 13 +++++++++---- cutils.c | 20 ++++++++++++++++++++ quickjs-libc.c | 49 ++++++++++++++++++++++++++----------------------- quickjs.c | 2 +- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f95b7..649e0d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ macro(xcheck_add_c_compiler_flag FLAG) endmacro() xcheck_add_c_compiler_flag(-Wall) -if(NOT MSVC AND NOT IOS) +if(NOT MSVC AND NOT IOS AND NOT NINTENDO_WII) xcheck_add_c_compiler_flag(-Werror) xcheck_add_c_compiler_flag(-Wextra) endif() @@ -155,7 +155,12 @@ list(APPEND qjs_defines _GNU_SOURCE) if(WIN32) list(APPEND qjs_defines WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0602) endif() -list(APPEND qjs_libs qjs ${CMAKE_DL_LIBS}) +if (NINTENDO_WII) + set (BUILD_QJS_LIBC ON) + list(APPEND qjs_libs qjs ogc) +elseif() + list(APPEND qjs_libs qjs ${CMAKE_DL_LIBS}) +endif() find_package(Threads) if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") list(APPEND qjs_libs ${CMAKE_THREAD_LIBS_INIT}) @@ -234,7 +239,7 @@ endif() # # run-test262 uses pthreads. -if(NOT WIN32 AND NOT EMSCRIPTEN) +if(NOT WIN32 AND NOT EMSCRIPTEN AND NOT NINTENDO_WII) add_executable(run-test262 quickjs-libc.c run-test262.c @@ -330,7 +335,7 @@ add_executable(test_conv # Install target # -if(NOT IOS) +if(NOT IOS OR NOT NINTENDO_WII) file(STRINGS quickjs.h quickjs_h REGEX QJS_VERSION) string(REGEX MATCHALL "([0-9])" QJS_VERSION "${quickjs_h}") list(GET QJS_VERSION 0 QJS_VERSION_MAJOR) diff --git a/cutils.c b/cutils.c index 56982df..eb8b724 100644 --- a/cutils.c +++ b/cutils.c @@ -28,6 +28,22 @@ #include #include #include +#if defined(__wii__) + +#if defined(HW_RVL) + #define TB_BUS_CLOCK 243000000u + #define TB_CORE_CLOCK 729000000u +#elif defined(HW_DOL) + #define TB_BUS_CLOCK 162000000u + #define TB_CORE_CLOCK 486000000u +#endif + +#define TB_TIMER_CLOCK (TB_BUS_CLOCK/4000) //4th of the bus frequency +#define ticks_to_nanosecs(ticks) ((((unsigned long)(ticks)*8000)/(unsigned long)(TB_TIMER_CLOCK/125))) + +extern unsigned long gettime(void); + +#endif #if !defined(_MSC_VER) #include #endif @@ -1169,6 +1185,10 @@ uint64_t js__hrtime_ns(void) { result = (double) counter.QuadPart / scaled_freq; return (uint64_t) result; } +#elif defined(__wii__) +uint64_t js__hrtime_ns(void) { + return ticks_to_nanosecs(gettime()) * NANOSEC; +} #else uint64_t js__hrtime_ns(void) { struct timespec t; diff --git a/quickjs-libc.c b/quickjs-libc.c index a594d74..a24a3b7 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -59,8 +59,8 @@ #define getcwd _getcwd #define chdir _chdir #else -#include -#if !defined(__wasi__) +// #include +#if !defined(__wasi__) && !defined(__wii__) #include #include #include @@ -80,7 +80,7 @@ extern char **environ; #endif /* _WIN32 */ -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) /* enable the os.Worker API. IT relies on POSIX threads */ #define USE_WORKER #endif @@ -480,7 +480,7 @@ typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx, const char *module_name); -#if defined(_WIN32) || defined(__wasi__) +#if defined(_WIN32) || defined(__wasi__) || defined(__wii__) static JSModuleDef *js_module_loader_so(JSContext *ctx, const char *module_name) { @@ -557,7 +557,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValue func_val, return -1; if (!strchr(module_name, ':')) { strcpy(buf, "file://"); -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) /* realpath() cannot be used with modules compiled with qjsc because the corresponding module source code is not necessarily present */ @@ -830,7 +830,7 @@ static void js_std_file_finalizer(JSRuntime *rt, JSValue val) JSSTDFile *s = JS_GetOpaque(val, js_std_file_class_id); if (s) { if (s->f && s->close_in_finalizer) { -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) if (s->is_popen) pclose(s->f); else @@ -921,7 +921,7 @@ static JSValue js_std_open(JSContext *ctx, JSValue this_val, return JS_EXCEPTION; } -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) static JSValue js_std_popen(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -957,7 +957,7 @@ static JSValue js_std_popen(JSContext *ctx, JSValue this_val, JS_FreeCString(ctx, mode); return JS_EXCEPTION; } -#endif // !defined(__wasi__) +#endif // !defined(__wasi__) && !defined(__wii__) static JSValue js_std_fdopen(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) @@ -992,7 +992,7 @@ static JSValue js_std_fdopen(JSContext *ctx, JSValue this_val, return JS_EXCEPTION; } -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) static JSValue js_std_tmpfile(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -1065,7 +1065,7 @@ static JSValue js_std_file_close(JSContext *ctx, JSValue this_val, return JS_EXCEPTION; if (!s->f) return JS_ThrowTypeError(ctx, "invalid file handle"); -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) if (s->is_popen) err = js_get_errno(pclose(s->f)); else @@ -1299,7 +1299,7 @@ static JSValue js_std_file_putByte(JSContext *ctx, JSValue this_val, } /* urlGet */ -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) #define URL_GET_PROGRAM "curl -s -i --" #define URL_GET_BUF_SIZE 4096 @@ -1493,7 +1493,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val, JS_FreeValue(ctx, response); return JS_EXCEPTION; } -#endif // !defined(__wasi__) +#endif // !defined(__wasi__) && !defined(__wii__) static JSClassDef js_std_file_class = { "FILE", @@ -1526,7 +1526,7 @@ static const JSCFunctionListEntry js_std_funcs[] = { JS_CFUNC_DEF("setenv", 1, js_std_setenv ), JS_CFUNC_DEF("unsetenv", 1, js_std_unsetenv ), JS_CFUNC_DEF("getenviron", 1, js_std_getenviron ), -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ), #endif JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ), @@ -1534,7 +1534,7 @@ static const JSCFunctionListEntry js_std_funcs[] = { /* FILE I/O */ JS_CFUNC_DEF("open", 2, js_std_open ), -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) JS_CFUNC_DEF("popen", 2, js_std_popen ), JS_CFUNC_DEF("tmpfile", 0, js_std_tmpfile ), #endif @@ -1751,7 +1751,7 @@ static JSValue js_os_ttySetRaw(JSContext *ctx, JSValue this_val, } return JS_UNDEFINED; } -#elif !defined(__wasi__) +#elif !defined(__wasi__) && !defined(__wii__) static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -2004,7 +2004,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValue this_val, return JS_UNDEFINED; } -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) static JSValue js_os_cputime(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -2395,7 +2395,10 @@ static int js_os_poll(JSContext *ctx) } } - ret = select(fd_max + 1, &rfds, &wfds, NULL, tvp); + // ret = select(fd_max + 1, &rfds, &wfds, NULL, tvp); + + // jank stub + ret = 0; if (ret > 0) { list_for_each(el, &ts->os_rw_handlers) { rh = list_entry(el, JSOSRWHandler, link); @@ -2734,7 +2737,7 @@ static char *realpath(const char *path, char *buf) } #endif -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) /* return [path, errorcode] */ static JSValue js_os_realpath(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) @@ -2758,7 +2761,7 @@ static JSValue js_os_realpath(JSContext *ctx, JSValue this_val, } #endif -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) static JSValue js_os_symlink(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { @@ -3709,7 +3712,7 @@ static const JSCFunctionListEntry js_os_funcs[] = { JS_CFUNC_MAGIC_DEF("read", 4, js_os_read_write, 0 ), JS_CFUNC_MAGIC_DEF("write", 4, js_os_read_write, 1 ), JS_CFUNC_DEF("isatty", 1, js_os_isatty ), -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) JS_CFUNC_DEF("ttyGetWinSize", 1, js_os_ttyGetWinSize ), JS_CFUNC_DEF("ttySetRaw", 1, js_os_ttySetRaw ), #endif @@ -3724,7 +3727,7 @@ static const JSCFunctionListEntry js_os_funcs[] = { OS_FLAG(SIGILL), OS_FLAG(SIGSEGV), OS_FLAG(SIGTERM), -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) OS_FLAG(SIGQUIT), OS_FLAG(SIGPIPE), OS_FLAG(SIGALRM), @@ -3766,10 +3769,10 @@ static const JSCFunctionListEntry js_os_funcs[] = { JS_CFUNC_MAGIC_DEF("stat", 1, js_os_stat, 0 ), JS_CFUNC_DEF("utimes", 3, js_os_utimes ), JS_CFUNC_DEF("sleep", 1, js_os_sleep ), -#if !defined(__wasi__) +#if !defined(__wasi__) && !defined(__wii__) JS_CFUNC_DEF("realpath", 1, js_os_realpath ), #endif -#if !defined(_WIN32) && !defined(__wasi__) +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__wii__) JS_CFUNC_MAGIC_DEF("lstat", 1, js_os_stat, 1 ), JS_CFUNC_DEF("symlink", 2, js_os_symlink ), JS_CFUNC_DEF("readlink", 1, js_os_readlink ), diff --git a/quickjs.c b/quickjs.c index b75c8cd..ed30b90 100644 --- a/quickjs.c +++ b/quickjs.c @@ -62,7 +62,7 @@ #define NO_TM_GMTOFF #endif -#if !defined(EMSCRIPTEN) && !defined(__wasi__) +#if !defined(EMSCRIPTEN) && !defined(__wasi__) && !defined(__wii__) #include "quickjs-c-atomics.h" #define CONFIG_ATOMICS #endif