diff --git a/src/common.h b/src/common.h index cd249c9..0af2364 100644 --- a/src/common.h +++ b/src/common.h @@ -9,9 +9,8 @@ extern "C" #include #include -int app_init_quickjs(int argc, char** argv); +int app_run_quickjs(int argc, char** argv); int app_update_quickjs(); -int app_dispose_quickjs(); #ifdef __cplusplus } diff --git a/src/main.c b/src/main.c index b6242fd..70086b8 100644 --- a/src/main.c +++ b/src/main.c @@ -29,45 +29,6 @@ //------------------------------------------------------------------------------------ int main(int argc, char ** argv) { - app_init_quickjs(argc, argv); - // // Initialization - // //-------------------------------------------------------------------------------------- - // const int screenWidth = 800; - // const int screenHeight = 450; - - // InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); - - // SetTargetFPS(60); // Set our game to run at 60 frames-per-second - // //-------------------------------------------------------------------------------------- - - - // // Main game loop - // while (!WindowShouldClose()) // Detect window close button or ESC key - // { - // app_update_quickjs(); - // // Update - // //---------------------------------------------------------------------------------- - // // TODO: Update your variables here - // //---------------------------------------------------------------------------------- - - // // Draw - // //---------------------------------------------------------------------------------- - // BeginDrawing(); - - // ClearBackground(RAYWHITE); - - // DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); - - // EndDrawing(); - // //---------------------------------------------------------------------------------- - // } - - - // // De-Initialization - // //-------------------------------------------------------------------------------------- - // CloseWindow(); // Close window and OpenGL context - // //-------------------------------------------------------------------------------------- - - app_dispose_quickjs(); + app_run_quickjs(argc, argv); return 0; } \ No newline at end of file diff --git a/src/quickjs.c b/src/quickjs.c index 105660d..52d12c3 100644 --- a/src/quickjs.c +++ b/src/quickjs.c @@ -4,7 +4,7 @@ #include #include //#include - +#include #include #include "common.h" @@ -15,6 +15,8 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, static JSRuntime* rt; static JSContext* ctx; +static bool shouldReload = true; +static const char* originalCwd = NULL; static void pstrcpy(char *buf, int buf_size, const char *str) { @@ -96,6 +98,12 @@ int app_update_quickjs(){ break; } } + if(IsKeyPressed(KEY_F5)){ + GLFWwindow* window = glfwGetCurrentContext(); + glfwSetWindowShouldClose(window, GLFW_TRUE); + shouldReload = true; + } + return 0; } @@ -236,7 +244,7 @@ JSModuleDef *js_module_loader(JSContext *ctx, return m; } -int app_init_quickjs(int argc, char** argv){ +static int js_run(int argc, char** argv){ TraceLog(LOG_INFO, "Starting QuickJS"); rt = JS_NewRuntime(); if (!rt) @@ -292,19 +300,23 @@ int app_init_quickjs(int argc, char** argv){ } size_t len = strlen(buf); int res = eval_buf(ctx, buf, len, "main", JS_EVAL_TYPE_MODULE); - if(res){ - return res; - } + + JS_FreeContext(ctx); + JS_FreeRuntime(rt); + return 0; } - - -int app_dispose_quickjs(){ - //js_std_free_handlers(rt); - JS_FreeContext(ctx); - JS_FreeRuntime(rt); - return 0; +int app_run_quickjs(int argc, char** argv){ + const char* original = GetWorkingDirectory(); + char* copy = calloc(strlen(original) + 1, sizeof(char)); + strcpy(copy, original); + originalCwd = copy; + while(shouldReload){ + shouldReload = false; + ChangeDirectory(originalCwd); + js_run(argc, argv); + } } /* also used to initialize the worker context */