Add script compile; level load eval environments

This commit is contained in:
John Alanbrook 2023-08-29 14:41:40 +00:00
parent 9dc04f6ce7
commit a3c6292eb2
6 changed files with 77 additions and 37 deletions

View file

@ -1053,6 +1053,14 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 121: case 121:
return num2js(get_timescale()); return num2js(get_timescale());
break; break;
case 122:
str = JS_ToCString(js, argv[1]);
return script_compile(str);
case 123:
str = JS_ToCString(js, argv[1]);
file_eval_env(str, argv[2]);
break;
} }
if (str) if (str)

View file

@ -145,9 +145,6 @@ int script_dofile(const char *file) {
JSValue script_runfile(const char *file) JSValue script_runfile(const char *file)
{ {
const char *script = slurp_text(file); const char *script = slurp_text(file);
int bufsize = strlen(script)+50;
char scriptbuffer[bufsize];
snprintf(scriptbuffer,bufsize, "(function(){%s})()", script);
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS); JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS);
js_print_exception(obj); js_print_exception(obj);
@ -156,6 +153,18 @@ JSValue script_runfile(const char *file)
return obj; return obj;
} }
JSValue script_compile(const char *file)
{
const char *script = slurp_text(file);
char strbuf[strlen(script)+50];
sprintf(strbuf, "(function(){%s})", script);
JSValue fn = JS_Eval(js, strbuf, strlen(script), file, JS_EVAL_FLAGS);
free(script);
return fn;
}
/* env is an object in the scripting environment; /* env is an object in the scripting environment;
s is the function to call on that object s is the function to call on that object
*/ */
@ -165,6 +174,15 @@ void script_eval_w_env(const char *s, JSValue env) {
JS_FreeValue(js, v); JS_FreeValue(js, v);
} }
void file_eval_env(const char *file, JSValue env)
{
char *script = slurp_text(file);
JSValue v = JS_EvalThis(js, env, script, strlen(script), file, JS_EVAL_FLAGS);
free(script);
js_print_exception(v);
JS_FreeValue(js,v);
}
void script_call_sym(JSValue sym) { void script_call_sym(JSValue sym) {
struct callee c; struct callee c;
c.fn = sym; c.fn = sym;

View file

@ -19,6 +19,7 @@ void js_stacktrace();
void script_startup(); void script_startup();
void script_init(); void script_init();
void script_run(const char *script, const char *file); void script_run(const char *script, const char *file);
JSValue script_compile(const char *file);
void script_evalf(const char *format, ...); void script_evalf(const char *format, ...);
int script_dofile(const char *file); int script_dofile(const char *file);
JSValue script_runfile(const char *file); JSValue script_runfile(const char *file);
@ -35,6 +36,7 @@ void call_callee(struct callee *c);
void script_callee(struct callee c, int argc, JSValue *argv); void script_callee(struct callee c, int argc, JSValue *argv);
int script_has_sym(void *sym); int script_has_sym(void *sym);
void script_eval_w_env(const char *s, JSValue env); void script_eval_w_env(const char *s, JSValue env);
void file_eval_env(const char *file, JSValue env);
time_t file_mod_secs(const char *file); time_t file_mod_secs(const char *file);

View file

@ -2523,9 +2523,9 @@ limited_editor.inputs['C-q'] = function()
Sound.killall(); Sound.killall();
Player.players[0].uncontrol(limited_editor); Player.players[0].uncontrol(limited_editor);
Player.players[0].control(editor); Player.players[0].control(editor);
register_gui(editor.ed_gui, editor); Register.gui.register(editor.ed_gui, editor);
Debug.register_call(editor.ed_debug, editor); Debug.register_call(editor.ed_debug, editor);
World.kill(); // World.kill();
World.clear_all(); World.clear_all();
editor.load_json(editor.stash); editor.load_json(editor.stash);
Yugine.view_camera(editor_camera); Yugine.view_camera(editor_camera);

View file

@ -9,6 +9,10 @@ function load(file) {
files[file] = modtime; files[file] = modtime;
} }
function compile(file) {
return cmd(122, file);
}
var Cmdline = {}; var Cmdline = {};
Cmdline.cmds = []; Cmdline.cmds = [];
@ -63,9 +67,6 @@ Cmdline.register_cmd("e", function(pawn) {
function run(file) function run(file)
{ {
// var text = IO.slurp(file);
// eval?.(`"use strict";${text}`);
// return;
var modtime = cmd(119, file); var modtime = cmd(119, file);
if (modtime === 0) { if (modtime === 0) {
Log.stack(); Log.stack();
@ -99,7 +100,7 @@ var Log = {
var lmatch = nnn.match(/\:\d*\)/); var lmatch = nnn.match(/\:\d*\)/);
var line = lmatch ? lmatch[0].shift(1).shift(-1) : "0"; var line = lmatch ? lmatch[0].shift(1).shift(-1) : "0";
yughlog(lvl, lg, file, line); yughlog(lvl, msg, file, line);
}, },
info(msg) { info(msg) {
@ -918,12 +919,10 @@ var Player = {
} }
}, },
control(pawn) {
this.pawns.push_unique(pawn);
},
uncontrol(pawn) { uncontrol(pawn) {
this.pawns = this.pawns.filter(x => x !== pawn); this.players.forEach(function(p) {
p.pawns = p.pawns.filter(x => x !== pawn);
});
}, },
obj_controlled(obj) { obj_controlled(obj) {
@ -934,13 +933,20 @@ var Player = {
return false; return false;
}, },
create() {
var n = Object.create(this);
n.pawns = [];
n.gamepads = [];
n.control = function(pawn) { n.pawns.push_unique(pawn); };
n.uncontrol = function(pawn) { n.pawns = n.pawns.filter(x => x !== pawn); };
this.players.push(n);
return n;
},
}; };
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
var player1 = Object.create(Player); Player.create();
player1.pawns = [];
player1.gamepads = [];
Player.players.push(player1);
} }
var Register = { var Register = {
@ -997,7 +1003,7 @@ var Register = {
Register.registries.forEach(function(x) { Register.registries.forEach(function(x) {
x.clear(); x.clear();
}); });
Player.players.forEach(x => x.uncontrol(obj)); Player.uncontrol(obj);
}, },
endofloop(fn) { endofloop(fn) {
@ -1326,20 +1332,20 @@ var Level = {
} }
},this); },this);
//eval_filename(this.script, this.scriptfile); cmd(123, this.scriptfile, self);
eval(this.script);
if (typeof extern === 'object') if (typeof this.update === 'function')
Object.assign(this, extern); Register.update.register(this.update, this);
if (typeof update === 'function') if (typeof this.gui === 'function')
Register.update.register(update, this); Register.gui.register(this.gui, this);
if (typeof gui === 'function') if (typeof this.nk_gui === 'function')
register_gui(gui, this); register_nk_gui(this.nk_gui, this);
if (typeof nk_gui === 'function') if (typeof this.inputs === 'object') {
register_nk_gui(nk_gui, this); Player.players[0].control(this);
}
}, },
revert() { revert() {
@ -1515,9 +1521,10 @@ var Level = {
newlevel._pos = [0,0]; newlevel._pos = [0,0];
newlevel._angle = 0; newlevel._angle = 0;
newlevel.color = Color.green; newlevel.color = Color.green;
newlevel.toString = function() { /* newlevel.toString = function() {
return (newlevel.unique ? "#" : "") + newlevel.file; return (newlevel.unique ? "#" : "") + newlevel.file;
}; };
*/
newlevel.filejson = newlevel.save(); newlevel.filejson = newlevel.save();
return newlevel; return newlevel;
}, },
@ -1550,8 +1557,6 @@ var Level = {
if (!file.endsWith(".lvl")) file = file + ".lvl"; if (!file.endsWith(".lvl")) file = file + ".lvl";
var newlevel = Level.create(); var newlevel = Level.create();
Log.warn(`MAKING LEVEL ${file}`);
if (IO.exists(file)) { if (IO.exists(file)) {
newlevel.filejson = IO.slurp(file); newlevel.filejson = IO.slurp(file);
@ -1574,7 +1579,7 @@ var Level = {
} }
newlevel.from = scriptfile.replace('.js',''); newlevel.from = scriptfile.replace('.js','');
newlevel.file = newlevel.from;
newlevel.run(); newlevel.run();
return newlevel; return newlevel;
@ -1791,6 +1796,12 @@ var Level = {
var World = Level.create(); var World = Level.create();
World.name = "World"; World.name = "World";
World.fullpath = function() { return World.name; }; World.fullpath = function() { return World.name; };
World.load = function(lvl) {
if (World.loaded)
World.loaded.kill();
World.loaded = World.spawn(lvl);
};
var gameobjects = {}; var gameobjects = {};
var Prefabs = gameobjects; var Prefabs = gameobjects;
@ -2044,7 +2055,7 @@ var gameobject = {
if (this.level) if (this.level)
this.level.unregister(this); this.level.unregister(this);
this.uncontrol(); Player.uncontrol(this);
this.instances.remove(this); this.instances.remove(this);
Register.unregister_obj(this); Register.unregister_obj(this);
// Signal.clear_obj(this); // Signal.clear_obj(this);
@ -2218,6 +2229,7 @@ locks.forEach(x => gameobject.obscure(x));
function load_configs(file) { function load_configs(file) {
var configs = JSON.parse(IO.slurp(file)); var configs = JSON.parse(IO.slurp(file));
for (var key in configs) { for (var key in configs) {
if (typeof globalThis[key] !== "object") continue;
Object.assign(globalThis[key], configs[key]); Object.assign(globalThis[key], configs[key]);
} }

View file

@ -10,5 +10,5 @@ function nogamegui()
}).draw(Window.dimensions.scale(0.5)); }).draw(Window.dimensions.scale(0.5));
} }
register_gui(nogamegui); Register.gui.register(nogamegui);