module and program separation
This commit is contained in:
@@ -89,7 +89,7 @@ if get_option('enet')
|
||||
endif
|
||||
|
||||
sources = []
|
||||
src += ['anim.c', 'config.c', 'datastream.c','font.c','gameobject.c','HandmadeMath.c','jsffi.c','model.c','render.c','script.c','simplex.c','spline.c', 'timer.c', 'transform.c','warp.c','yugine.c', 'wildmatch.c', 'sprite.c', 'quadtree.c', 'aabb.c', 'rtree.c']
|
||||
src += ['anim.c', 'config.c', 'datastream.c','font.c','gameobject.c','HandmadeMath.c','jsffi.c','model.c','render.c','script.c','simplex.c','spline.c', 'timer.c', 'transform.c','warp.c','yugine.c', 'wildmatch.c', 'sprite.c', 'quadtree.c', 'aabb.c', 'rtree.c', 'rtree3.c']
|
||||
|
||||
imsrc = ['GraphEditor.cpp','ImCurveEdit.cpp','ImGradient.cpp','imgui_draw.cpp','imgui_tables.cpp','imgui_widgets.cpp','imgui.cpp','ImGuizmo.cpp','imnodes.cpp','implot_items.cpp','implot.cpp', 'imgui_impl_sdlrenderer3.cpp', 'imgui_impl_sdl3.cpp', 'imgui_impl_sdlgpu3.cpp']
|
||||
|
||||
|
||||
@@ -6,24 +6,64 @@ var actor_spawns = {};
|
||||
|
||||
var script_fns = {};
|
||||
|
||||
function use(file)
|
||||
{
|
||||
var par = script_fn(file)
|
||||
if (!par.module_ret)
|
||||
throw new Error(`File ${file} has no valid module definition`)
|
||||
|
||||
return par.module_ret;
|
||||
}
|
||||
|
||||
var script_fn = function script_fn(file) {
|
||||
var script = Resources.replstrs(file);
|
||||
script = `(function use_${file.name()}() { var self = this; var $ = this.__proto__; ${script}; })`;
|
||||
var fn = os.eval(file,script);
|
||||
var content = Resources.replstrs(file);
|
||||
var parsed = parse_file(content)
|
||||
var module_name = file.name()
|
||||
|
||||
if (parsed.module) {
|
||||
var mod_script = `(function setup_${module_name}_module(){ var self = this; ${parsed.module}})`;
|
||||
var module_fn = os.eval(file, mod_script)
|
||||
var module_ret = module_fn.call();
|
||||
if (module_ret === undefined || module_ret === null)
|
||||
throw new Error(`Module ${module_name} must return a value`);
|
||||
|
||||
parsed.module_fn = module_fn;
|
||||
parsed.module_ret = module_ret;
|
||||
}
|
||||
|
||||
if (parsed.program) {
|
||||
var prog_script = `(function use_${module_name}() { var self = this; var $ = this.__proto__; ${parsed.program}})`;
|
||||
parsed.prog_fn = os.eval(file, prog_script);
|
||||
}
|
||||
|
||||
return fn;
|
||||
}
|
||||
|
||||
function parse_file(content) {
|
||||
var parts = content.split(/\n\s*---\s*\n/)
|
||||
if (parts.length === 1)
|
||||
return {
|
||||
program: parts[0]
|
||||
}
|
||||
}
|
||||
|
||||
var module = parts[0].trim();
|
||||
if (!moduleCode.match(/return\s+[^;]+;?\s*$/))
|
||||
throw new Error("Module section must end with a return statement");
|
||||
|
||||
return {
|
||||
module,
|
||||
program: parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.actor_use = function actor_use(script)
|
||||
{
|
||||
var file = Resources.find_script(script);
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
var padawan = {};
|
||||
|
||||
script_fn(file).call(padawan);
|
||||
|
||||
return padawan;
|
||||
return use(file)
|
||||
}
|
||||
|
||||
globalThis.class_use = function class_use(script, config, base, callback) {
|
||||
@@ -35,25 +75,16 @@ globalThis.class_use = function class_use(script, config, base, callback) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!actor_urs[file]) {
|
||||
var newur = Object.create(base);
|
||||
actor_urs[file] = newur;
|
||||
newur._file = file;
|
||||
newur._root = file.dir();
|
||||
actor_spawns[file] = [];
|
||||
newur._script = script_fn(file)
|
||||
}
|
||||
var prog = script_fn(file);
|
||||
var padawan;
|
||||
if (prog.module_ret)
|
||||
padawan = Object.create(prog.module_ret);
|
||||
else
|
||||
padawan = {};
|
||||
|
||||
var padawan = Object.create(actor_urs[file]);
|
||||
actor_spawns[file].push(padawan);
|
||||
|
||||
if (callback) callback(padawan);
|
||||
|
||||
actor_urs[file]._script.call(padawan)
|
||||
|
||||
if (typeof config === "object") Object.merge(padawan, config);
|
||||
|
||||
if (!actor_urs[file].__reggies) actor_urs[file].__reggies = pull_registers(padawan);
|
||||
if (callback) callback(padawan)
|
||||
prog.prog_fn.call(padawan)
|
||||
if (typeof config === 'object') Object.merge(padawan,config);
|
||||
|
||||
return padawan;
|
||||
};
|
||||
|
||||
@@ -295,8 +295,8 @@ console.error = function(e) {
|
||||
if (!e)
|
||||
e = new Error();
|
||||
|
||||
pprint(e, 4)
|
||||
pprint(e.stack, 4)
|
||||
pprint(`${e.name} : ${e.message}
|
||||
${e.stack}`, 4)
|
||||
};
|
||||
|
||||
console.panic = function (e) {
|
||||
@@ -454,6 +454,7 @@ function bare_use(file) {
|
||||
script = `(function ${fnname}() { var self = this; ${script}; })`;
|
||||
Object.assign(globalThis, os.eval(file, script)());
|
||||
} catch(e) {
|
||||
console.log(file)
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
@@ -471,7 +472,7 @@ prosperon.release = function () {
|
||||
debug.enabled = false;
|
||||
};
|
||||
|
||||
bare_use("core/scripts/preconfig.js");
|
||||
//bare_use("core/scripts/preconfig.js");
|
||||
|
||||
if (!profile.enabled) use = stripped_use;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
////////////////////////////////
|
||||
|
||||
#define DATATYPE void *
|
||||
#define DIMS 2
|
||||
#define DIMS 3
|
||||
#define MAXITEMS 64
|
||||
|
||||
////////////////////////////////
|
||||
|
||||
@@ -97,10 +97,13 @@ void script_stop()
|
||||
|
||||
void uncaught_exception(JSContext *js, JSValue v)
|
||||
{
|
||||
JSValue ret;
|
||||
if (!JS_IsUndefined(on_exception) && JS_IsException(v))
|
||||
ret = JS_Call(js, on_exception, JS_UNDEFINED, 1, &v);
|
||||
JS_FreeValue(js,ret);
|
||||
if (!JS_IsUndefined(on_exception) && JS_IsException(v)) {
|
||||
JSValue ex = JS_GetException(js);
|
||||
JSValue ret = JS_Call(js, on_exception, JS_UNDEFINED, 1, &ex);
|
||||
JS_FreeValue(js,ret);
|
||||
JS_FreeValue(js,ex);
|
||||
}
|
||||
|
||||
JS_FreeValue(js,v);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user