work on getting quickjs ported to the wii.

i got it working but the port of my duktape code isn't done yet.
This commit is contained in:
Fries 2024-06-26 16:33:02 -07:00
parent 2b527cb917
commit 08c96c9d07
18 changed files with 254 additions and 25 deletions

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "dependencies/GRRLIB/source"] [submodule "dependencies/GRRLIB/source"]
path = dependencies/GRRLIB/source path = dependencies/GRRLIB/source
url = https://git.fries.gay/fries/GRRLIB url = https://git.fries.gay/fries/GRRLIB
[submodule "dependencies/quickjs"]
path = dependencies/quickjs
url = https://git.fries.gay/fries/quickjs

View file

@ -1,2 +1,3 @@
add_subdirectory(duktape) add_subdirectory(duktape)
add_subdirectory(GRRLIB) add_subdirectory(GRRLIB)
add_subdirectory(quickjs)

1
dependencies/quickjs vendored Submodule

@ -0,0 +1 @@
Subproject commit 7b20dcd58fea53cd203df27e2da74bbd2fd013db

View file

@ -1,8 +1,11 @@
add_subdirectory(assets) add_subdirectory(assets)
add_subdirectory(shared)
add_subdirectory(duktape) add_subdirectory(duktape)
add_subdirectory(quickjs)
# add_subdirectory(flacplayer) # add_subdirectory(flacplayer)
add_executable(WiiDuktape main.c) add_executable(WiiDuktape main.c)
target_link_libraries(WiiDuktape GRRLIB) target_link_libraries(WiiDuktape GRRLIB)
target_link_libraries(WiiDuktape duktape_engine) target_link_libraries(WiiDuktape duktape_engine)
target_link_libraries(WiiDuktape quickjs_engine)
ogc_create_dol(WiiDuktape) ogc_create_dol(WiiDuktape)

View file

@ -9,5 +9,6 @@ add_library(duktape_engine ${duktape_engine_sources})
target_link_libraries(duktape_engine duktape) target_link_libraries(duktape_engine duktape)
target_link_libraries(duktape_engine GRRLIB) target_link_libraries(duktape_engine GRRLIB)
target_link_libraries(duktape_engine assets) target_link_libraries(duktape_engine assets)
target_link_libraries(duktape_engine shared)
target_include_directories(duktape_engine PUBLIC ./include) target_include_directories(duktape_engine PUBLIC ./include)
target_include_directories(duktape_engine PRIVATE ./include_private) target_include_directories(duktape_engine PRIVATE ./include_private)

View file

@ -7,8 +7,7 @@
#include <data.h> #include <data.h>
#include <wii_duk.h> #include <wii_duk.h>
#include <game.h> #include <game.h>
#include <microsecond.h>
const float MICROSECOND = 0.000001f;
static float deltatime = 1.0f/60; static float deltatime = 1.0f/60;
static unsigned int green = RGBA(0, 255, 0, 255); static unsigned int green = RGBA(0, 255, 0, 255);
@ -53,7 +52,7 @@ void duktape(GRRLIB_ttfFont * global_font, bool * running) {
duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error); duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error);
// create a wii object on the global object (globalThis) // create a wii object on the global object (globalThis)
define_wii_object(ctx, font, running, &deltatime); duk_define_wii_object(ctx, font, running, &deltatime);
duk_put_global_string(ctx, "wii"); duk_put_global_string(ctx, "wii");
// evaluate functions from game.js // evaluate functions from game.js

View file

@ -2,5 +2,5 @@
#define WII_DUK_H #define WII_DUK_H
#include <duktape.h> #include <duktape.h>
#include <grrlib.h> #include <grrlib.h>
void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime); void duk_define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime);
#endif #endif

View file

@ -2,6 +2,7 @@
#include "data.h" #include "data.h"
#include "grrlib_duk.h" #include "grrlib_duk.h"
#include "rgba.h" #include "rgba.h"
#include <wii_obj.h>
static GRRLIB_ttfFont * font; static GRRLIB_ttfFont * font;
static bool * running; static bool * running;
@ -9,37 +10,34 @@ static unsigned int green = RGBA(0, 255, 0, 255);
static float * deltatime; static float * deltatime;
static duk_ret_t native_print(duk_context * ctx) { static duk_ret_t native_print(duk_context * ctx) {
GRRLIB_PrintfTTF(0, 0, font, duk_to_string(ctx, 0), 32, green); shared_native_print(duk_to_string(ctx, 0), font);
return 0; return 0;
} }
static duk_ret_t native_exit(duk_context * ctx) { static duk_ret_t native_exit(duk_context * ctx) {
*running = false; shared_native_exit(running);
return 0; return 0;
} }
static duk_ret_t pad_buttons_down(duk_context * ctx) { static duk_ret_t pad_buttons_down(duk_context * ctx) {
unsigned int pressed = PAD_ButtonsDown(0); duk_push_number(ctx, shared_pad_buttons_down());
duk_push_number(ctx, pressed);
return 1; return 1;
} }
static duk_ret_t get_file(duk_context * ctx) { static duk_ret_t get_file(duk_context * ctx) {
const char* filename = duk_to_string(ctx, 0); const char* filename = duk_to_string(ctx, 0);
shared_file_obj file = shared_get_file(filename);
void* ptr; if (shared_is_valid_file(file)) {
size_t size;
if (get_file_pointer(filename, &ptr, &size)) {
duk_idx_t file_obj = duk_push_object(ctx); duk_idx_t file_obj = duk_push_object(ctx);
duk_push_number(ctx, size); duk_push_number(ctx, file.size);
duk_put_prop_string(ctx, file_obj, "size"); duk_put_prop_string(ctx, file_obj, "size");
duk_push_pointer(ctx, ptr); duk_push_pointer(ctx, file.file_ptr);
duk_put_prop_string(ctx, file_obj, "file_ptr"); duk_put_prop_string(ctx, file_obj, "file_ptr");
duk_push_string(ctx, filename); duk_push_string(ctx, file.filename);
duk_put_prop_string(ctx, file_obj, "filename"); duk_put_prop_string(ctx, file_obj, "filename");
return 1; return 1;
@ -60,7 +58,7 @@ static void define_pad_object(duk_context *ctx) {
duk_put_prop_string(ctx, pad_obj, "buttons_down"); duk_put_prop_string(ctx, pad_obj, "buttons_down");
} }
void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime) { void duk_define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime) {
font = grrlib_font; font = grrlib_font;
running = global_running; running = global_running;
deltatime = global_deltatime; deltatime = global_deltatime;

View file

@ -7,6 +7,7 @@
#include <data.h> #include <data.h>
#include <duktape_engine.h> #include <duktape_engine.h>
#include <quickjs_engine.h>
#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) #define LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))
@ -45,15 +46,16 @@ static void start_duktape() {
static void start_quickjs() { static void start_quickjs() {
running = true; running = true;
while (running) { quickjs(font, &running);
PAD_ScanPads(); // while (running) {
unsigned int pressed = PAD_ButtonsDown(0); // PAD_ScanPads();
GRRLIB_PrintfTTF(0, 0, font, "QuickJS is not implemented, yet..", 48, RGBA(255, 255, 255, 255)); // unsigned int pressed = PAD_ButtonsDown(0);
GRRLIB_Render(); // GRRLIB_PrintfTTF(0, 0, font, "QuickJS is not implemented, yet..", 48, RGBA(255, 255, 255, 255));
if (pressed & PAD_BUTTON_START) { // GRRLIB_Render();
running = false; // if (pressed & PAD_BUTTON_START) {
} // running = false;
} // }
// }
} }
static void update_menu(unsigned int * pressed) { static void update_menu(unsigned int * pressed) {

View file

@ -0,0 +1,12 @@
set(quickjs_engine_sources
quickjs_engine.c
wii_qjs.c
)
add_library(quickjs_engine ${quickjs_engine_sources})
target_link_libraries(quickjs_engine PRIVATE qjs)
target_link_libraries(quickjs_engine PRIVATE GRRLIB)
target_link_libraries(quickjs_engine PRIVATE assets)
target_link_libraries(quickjs_engine PRIVATE shared)
target_include_directories(quickjs_engine PUBLIC ./include)
target_include_directories(quickjs_engine PRIVATE ./include_private)

View file

@ -0,0 +1,5 @@
#ifndef QUICKJS_ENGINE_H
#define QUICKJS_ENGINE_H
#include <grrlib.h>
void quickjs(GRRLIB_ttfFont * global_font, bool * running);
#endif

View file

@ -0,0 +1,6 @@
#ifndef WII_QJS_H
#define WII_QJS_H
#include <quickjs.h>
#include <grrlib.h>
JSValue qjs_define_wii_object(JSContext * ctx, GRRLIB_ttfFont * global_font, bool * global_running, float * global_deltatime);
#endif

View file

@ -0,0 +1,34 @@
#include <quickjs.h>
#include <ogc/lwp_watchdog.h>
#include <quickjs_engine.h>
#include <microsecond.h>
#include <wii_qjs.h>
static float deltatime = 1.0f/60;
static unsigned int green = RGBA(0, 255, 0, 255);
static GRRLIB_ttfFont * font;
void quickjs(GRRLIB_ttfFont * global_font, bool * running) {
font = global_font;
JSRuntime * rt = JS_NewRuntime();
JSContext * ctx = JS_NewContext(rt);
JSValue wii_obj = qjs_define_wii_object(ctx, font, running, &deltatime);
JSValue global_obj = JS_GetGlobalObject(ctx);
JS_SetPropertyStr(ctx, global_obj, "wii", wii_obj);
while (*running) {
const unsigned int beginningOfFrame = ticks_to_microsecs(gettime());
PAD_ScanPads();
// fun
JS_Eval(ctx, "wii.print('hi')", strlen("wii.print('hi')"), "file", 0);
GRRLIB_Render();
const unsigned int endOfFrame = ticks_to_microsecs(gettime());
deltatime = (endOfFrame - beginningOfFrame) * MICROSECOND;
}
}

92
source/quickjs/wii_qjs.c Normal file
View file

@ -0,0 +1,92 @@
#include <wii_qjs.h>
#include <grrlib.h>
#include <data.h>
#include <wii_obj.h>
#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))
static GRRLIB_ttfFont * font;
static bool * running;
static unsigned int green = RGBA(0, 255, 0, 255);
static float * deltatime;
static JSClassID file_class_id;
static void file_class_finalizer(JSRuntime * rt, JSValue val) {
}
static JSClassDef file_class = {
"FILE",
.finalizer = file_class_finalizer
};
static JSValue native_print(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) {
shared_native_print(JS_ToCString(ctx, argv[0]), font);
return 0;
}
static JSValue native_exit(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) {
shared_native_exit(running);
return 0;
}
static JSValue pad_buttons_down(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) {
return JS_NewUint32(ctx, shared_pad_buttons_down());
}
static JSValue get_file(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) {
const char * filename = JS_ToCString(ctx, argv[0]);
shared_file_obj file = shared_get_file(filename);
if (shared_is_valid_file(file)) {
JSValue wii_obj = JS_NewObject(ctx);
JSValue file_ptr = JS_NewObjectClass(ctx, file_class_id);
JS_SetOpaque(file_ptr, file.file_ptr);
JS_SetPropertyStr(ctx, wii_obj, "size", JS_NewNumber(ctx, file.size));
JS_SetPropertyStr(ctx, wii_obj, "file_ptr", file_ptr);
JS_SetPropertyStr(ctx, wii_obj, "filename", JS_NewString(ctx, file.filename));
return wii_obj;
}
return JS_EXCEPTION;
}
static JSValue get_deltatime(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) {
return JS_NewNumber(ctx, *deltatime);
}
static JSValue define_pad_object(JSContext * ctx) {
JSValue pad_obj = JS_NewObject(ctx);
const JSCFunctionListEntry pad_obj_funcs[] = {
JS_CFUNC_DEF("buttons_down", 0, pad_buttons_down)
};
JS_SetPropertyFunctionList(ctx, pad_obj, pad_obj_funcs, LENGTH(pad_obj_funcs));
return pad_obj;
}
static const JSCFunctionListEntry wii_obj_funcs[] = {
JS_CFUNC_DEF("print", 1, native_print),
JS_CFUNC_DEF("exit", 0, native_exit),
JS_CFUNC_DEF("get_file", 1, get_file),
JS_CFUNC_DEF("get_deltatime", 0, get_deltatime)
};
JSValue qjs_define_wii_object(JSContext * ctx, GRRLIB_ttfFont * global_font, bool * global_running, float * global_deltatime) {
font = global_font;
running = global_running;
deltatime = global_deltatime;
JSRuntime * rt = JS_GetRuntime(ctx);
JS_NewClassID(rt, &file_class_id);
JS_NewClass(rt, file_class_id, &file_class);
JSValue wii_obj = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, wii_obj, wii_obj_funcs, LENGTH(wii_obj_funcs));
return wii_obj;
}

View file

@ -0,0 +1,8 @@
set(shared_sources
wii_obj.c
)
add_library(shared ${shared_sources})
target_link_libraries(shared PRIVATE GRRLIB)
target_link_libraries(shared PRIVATE assets)
target_include_directories(shared PUBLIC ./include)

View file

@ -0,0 +1,4 @@
#ifndef MICROSECOND_H
#define MICROSECOND_H
#define MICROSECOND 0.000001f
#endif

View file

@ -0,0 +1,17 @@
#ifndef WII_OBJ_H
#define WII_OBJ_H
#include <grrlib.h>
typedef struct {
size_t size;
void * file_ptr;
const char * filename;
} shared_file_obj;
void shared_native_print(const char * message, GRRLIB_ttfFont * font);
void shared_native_exit(bool * running);
unsigned int shared_pad_buttons_down();
shared_file_obj shared_get_file(const char * filename);
bool shared_is_valid_file(shared_file_obj file);
#endif

43
source/shared/wii_obj.c Normal file
View file

@ -0,0 +1,43 @@
#include <wii_obj.h>
#include <data.h>
static unsigned int green = RGBA(0, 255, 0, 255);
void shared_native_print(const char * message, GRRLIB_ttfFont * font) {
GRRLIB_PrintfTTF(0, 0, font, message, 32, green);
}
void shared_native_exit(bool * running) {
*running = false;
}
unsigned int shared_pad_buttons_down() {
return PAD_ButtonsDown(0);
}
shared_file_obj shared_get_file(const char * filename) {
void* ptr;
size_t size;
if (get_file_pointer(filename, &ptr, &size)) {
shared_file_obj file = {
.size = size,
.file_ptr = ptr,
.filename = filename
};
return file;
}
shared_file_obj file = {
.size = 0,
.file_ptr = NULL,
.filename = NULL
};
return file;
}
bool shared_is_valid_file(shared_file_obj file) {
return file.size > 0 && file.file_ptr != NULL && file.filename != NULL;
}