module and program separation
This commit is contained in:
@@ -89,7 +89,7 @@ if get_option('enet')
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
sources = []
|
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']
|
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 = {};
|
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_fn = function script_fn(file) {
|
||||||
var script = Resources.replstrs(file);
|
var content = Resources.replstrs(file);
|
||||||
script = `(function use_${file.name()}() { var self = this; var $ = this.__proto__; ${script}; })`;
|
var parsed = parse_file(content)
|
||||||
var fn = os.eval(file,script);
|
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;
|
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)
|
globalThis.actor_use = function actor_use(script)
|
||||||
{
|
{
|
||||||
var file = Resources.find_script(script);
|
var file = Resources.find_script(script);
|
||||||
if (!file)
|
if (!file)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var padawan = {};
|
return use(file)
|
||||||
|
|
||||||
script_fn(file).call(padawan);
|
|
||||||
|
|
||||||
return padawan;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
globalThis.class_use = function class_use(script, config, base, callback) {
|
globalThis.class_use = function class_use(script, config, base, callback) {
|
||||||
@@ -34,26 +74,17 @@ globalThis.class_use = function class_use(script, config, base, callback) {
|
|||||||
if (callback) callback(ret);
|
if (callback) callback(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!actor_urs[file]) {
|
var prog = script_fn(file);
|
||||||
var newur = Object.create(base);
|
var padawan;
|
||||||
actor_urs[file] = newur;
|
if (prog.module_ret)
|
||||||
newur._file = file;
|
padawan = Object.create(prog.module_ret);
|
||||||
newur._root = file.dir();
|
else
|
||||||
actor_spawns[file] = [];
|
padawan = {};
|
||||||
newur._script = script_fn(file)
|
|
||||||
}
|
if (callback) callback(padawan)
|
||||||
|
prog.prog_fn.call(padawan)
|
||||||
var padawan = Object.create(actor_urs[file]);
|
if (typeof config === 'object') Object.merge(padawan,config);
|
||||||
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);
|
|
||||||
|
|
||||||
return padawan;
|
return padawan;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -295,8 +295,8 @@ console.error = function(e) {
|
|||||||
if (!e)
|
if (!e)
|
||||||
e = new Error();
|
e = new Error();
|
||||||
|
|
||||||
pprint(e, 4)
|
pprint(`${e.name} : ${e.message}
|
||||||
pprint(e.stack, 4)
|
${e.stack}`, 4)
|
||||||
};
|
};
|
||||||
|
|
||||||
console.panic = function (e) {
|
console.panic = function (e) {
|
||||||
@@ -454,6 +454,7 @@ function bare_use(file) {
|
|||||||
script = `(function ${fnname}() { var self = this; ${script}; })`;
|
script = `(function ${fnname}() { var self = this; ${script}; })`;
|
||||||
Object.assign(globalThis, os.eval(file, script)());
|
Object.assign(globalThis, os.eval(file, script)());
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.log(file)
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -471,7 +472,7 @@ prosperon.release = function () {
|
|||||||
debug.enabled = false;
|
debug.enabled = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
bare_use("core/scripts/preconfig.js");
|
//bare_use("core/scripts/preconfig.js");
|
||||||
|
|
||||||
if (!profile.enabled) use = stripped_use;
|
if (!profile.enabled) use = stripped_use;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|
||||||
#define DATATYPE void *
|
#define DATATYPE void *
|
||||||
#define DIMS 2
|
#define DIMS 3
|
||||||
#define MAXITEMS 64
|
#define MAXITEMS 64
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|||||||
@@ -97,10 +97,13 @@ void script_stop()
|
|||||||
|
|
||||||
void uncaught_exception(JSContext *js, JSValue v)
|
void uncaught_exception(JSContext *js, JSValue v)
|
||||||
{
|
{
|
||||||
JSValue ret;
|
if (!JS_IsUndefined(on_exception) && JS_IsException(v)) {
|
||||||
if (!JS_IsUndefined(on_exception) && JS_IsException(v))
|
JSValue ex = JS_GetException(js);
|
||||||
ret = JS_Call(js, on_exception, JS_UNDEFINED, 1, &v);
|
JSValue ret = JS_Call(js, on_exception, JS_UNDEFINED, 1, &ex);
|
||||||
JS_FreeValue(js,ret);
|
JS_FreeValue(js,ret);
|
||||||
|
JS_FreeValue(js,ex);
|
||||||
|
}
|
||||||
|
|
||||||
JS_FreeValue(js,v);
|
JS_FreeValue(js,v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user