WiiDuktape/source/quickjs/wii_qjs.c
Fries 08c96c9d07 work on getting quickjs ported to the wii.
i got it working but the port of my duktape code isn't done yet.
2024-06-26 16:33:02 -07:00

92 lines
2.6 KiB
C

#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;
}