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:
return num2js(get_timescale());
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)

View file

@ -145,9 +145,6 @@ int script_dofile(const char *file) {
JSValue script_runfile(const char *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);
js_print_exception(obj);
@ -156,6 +153,18 @@ JSValue script_runfile(const char *file)
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;
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);
}
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) {
struct callee c;
c.fn = sym;

View file

@ -19,6 +19,7 @@ void js_stacktrace();
void script_startup();
void script_init();
void script_run(const char *script, const char *file);
JSValue script_compile(const char *file);
void script_evalf(const char *format, ...);
int script_dofile(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);
int script_has_sym(void *sym);
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);

View file

@ -2523,9 +2523,9 @@ limited_editor.inputs['C-q'] = function()
Sound.killall();
Player.players[0].uncontrol(limited_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);
World.kill();
// World.kill();
World.clear_all();
editor.load_json(editor.stash);
Yugine.view_camera(editor_camera);

View file

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

View file

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