Add quickjs to tree

This commit is contained in:
2023-04-22 19:07:37 +00:00
parent 653e80cae8
commit 1e8d76961d
68 changed files with 97874 additions and 9 deletions

View File

@@ -55,13 +55,13 @@ void engine_init()
}
resources_init();
YughInfo("Starting physics ...");
phys2d_init();
YughInfo("Starting sound ...");
sound_init();
YughInfo("Starting scripts ...");
script_init();
YughInfo("Starting physics ...");
phys2d_init();
YughInfo("Starting sound ...");
sound_init();
}

View File

@@ -27,11 +27,14 @@ static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
return 0;
}
void script_init() {
void script_startup()
{
rt = JS_NewRuntime();
js = JS_NewContext(rt);
ffi_load();
}
void script_init() {
/* Load all prefabs into memory */
script_dofile("scripts/engine.js");
script_dofile("config.js");

View File

@@ -12,6 +12,7 @@ struct callee {
JSValue obj;
};
void script_startup();
void script_init();
void script_run(const char *script);
int script_dofile(const char *file);

View File

@@ -7,6 +7,7 @@
#include "input.h"
#include "openglrender.h"
#include "gameobject.h"
#include "font.h"
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
@@ -14,6 +15,8 @@
#include "timer.h"
#include "quickjs/quickjs.h"
#include "script.h"
#include "ffi.h"
@@ -85,10 +88,25 @@ void seghandle(int sig) {
*/
}
void compile_script(const char *file)
{
const char *script = slurp_text(file);
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAG_COMPILE_ONLY|JS_EVAL_TYPE_GLOBAL);
size_t out_len;
uint8_t *out;
out = JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE);
FILE *f = fopen("out.jsc", "w");
fwrite(out, sizeof out[0], out_len, f);
fclose(f);
}
int main(int argc, char **args) {
int logout = 1;
ed = 1;
script_startup();
for (int i = 1; i < argc; i++) {
if (args[i][0] == '-') {
switch(args[i][1]) {
@@ -113,6 +131,10 @@ int main(int argc, char **args) {
exit(1);
break;
case 's':
compile_script(args[2]);
exit(0);
case 'h':
printf("-l Set log file\n");
printf("-p Launch engine in play mode instead of editor mode\n");