From c8ce152ade99e520fa0c5f7b92f199b19c480774 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 3 Dec 2025 16:40:37 -0600 Subject: [PATCH] use build if built --- scripts/engine.cm | 10 ++--- scripts/init.ce | 23 ------------ scripts/js.c | 4 ++ scripts/shop.cm | 96 ++++++++++++++++++++++++----------------------- 4 files changed, 56 insertions(+), 77 deletions(-) delete mode 100644 scripts/init.ce diff --git a/scripts/engine.cm b/scripts/engine.cm index 0143bf0d..99fcd613 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -705,17 +705,13 @@ actor_mod.setname(cell.args.program) var prog = cell.args.program // Resolve the main program path -var resolved_prog = shop.resolve_locator(cell.args.program, ACTOR_EXT, null) -if (!resolved_prog) { +var locator = shop.resolve_locator(cell.args.program, ACTOR_EXT, null) +if (!locator) { throw new Error(`Main program ${cell.args.program} could not be found`) } -prog = resolved_prog.path -var prog_script = `(function start($_, arg) { var args = arg; ${resolved_prog.script} ; })` -var fn = js.eval(cell.args.program, prog_script) - $_.clock(_ => { - var val = fn($_, cell.args.arg); + var val = locator.symbol.call(null, $_, cell.args.arg); if (val) throw new Error('Program must not return anything'); diff --git a/scripts/init.ce b/scripts/init.ce deleted file mode 100644 index 6668eeb9..00000000 --- a/scripts/init.ce +++ /dev/null @@ -1,23 +0,0 @@ -// cell init - Initialize a new .cell program shop -var shop = use('shop') - -// Initialize the .cell directory structure -log.console("Initializing .cell program shop...") - -var success = shop.init() - -if (success) { - log.console("Created .cell directory structure:") - log.console(" .cell/") - log.console(" ├── cell.toml (manifest)") - log.console(" ├── lock.toml (checksums)") - log.console(" ├── modules/ (vendored source)") - log.console(" ├── build/ (compiled modules)") - log.console(" └── patches/ (patches)") - log.console("") - log.console("Edit .cell/cell.toml to configure your project.") -} else { - log.error("Failed to initialize .cell directory") -} - -$_.stop() \ No newline at end of file diff --git a/scripts/js.c b/scripts/js.c index fa4fcdc6..f69c1bac 100644 --- a/scripts/js.c +++ b/scripts/js.c @@ -40,11 +40,15 @@ JSC_CCALL(os_calc_mem, // Evaluate a string of JavaScript code in the current QuickJS context. JSC_SSCALL(os_eval, + if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script."); + if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script."); ret = JS_Eval(js,str2,strlen(str2),str, 0); ) // Compile a string of JavaScript code into a function object. JSC_SSCALL(js_compile, + if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script."); + if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script."); ret = JS_Eval(js, str2, strlen(str2), str, JS_EVAL_FLAG_COMPILE_ONLY); ) diff --git a/scripts/shop.cm b/scripts/shop.cm index b37d7899..e55131f6 100644 --- a/scripts/shop.cm +++ b/scripts/shop.cm @@ -177,31 +177,6 @@ Shop.save_lock = function(lock) { fd.slurpwrite('.cell/lock.toml', utf8.encode(toml.encode(lock))); } -// Initialize .cell directory structure -Shop.init = function() { - if (!fd.is_dir('.cell')) { - fd.mkdir('.cell') - } - - if (!fd.is_dir('.cell/modules')) { - fd.mkdir('.cell/modules') - } - - if (!fd.is_dir('.cell/build')) { - fd.mkdir('.cell/build') - } - - if (!fd.is_dir('.cell/cache')) { - fd.mkdir('.cell/cache') - } - - if (!fd.is_file('.cell/lock.toml')) { - fd.slurpwrite('.cell/lock.toml', '# Lock file for module integrity\n'); - } - - return true -} - // Parse module locator (e.g., "https://git.world/jj/mod@v0.6.3") Shop.parse_locator = function(locator) { var protocol = null @@ -451,6 +426,38 @@ Shop.verify = function(locator) { var open_dls = {} +var script_forms = [] + +script_forms['.cm'] = function(path, script) { + return `(function setup_module($_){${script}})` +} + +script_forms['.ce'] = function(path, script) { + return `(function start($_, arg) { var args = arg; ${script} ; })` +} + +function resolve_mod_fn(path) +{ + if (!fd.is_file(path)) throw new Error(`path ${path} is not a file`) + + var cache_path = `.cell/build/${path}.o` + if (fd.is_file(cache_path) && fd.stat(path).mtime <= fd.stat(cache_path).mtime) { + var obj = fd.slurp(cache_path) + var fn = js.compile_unblob(obj) + return js.eval_compile(fn) + } + + var ext = path.substring(path.lastIndexOf('.')) + var script_form = script_forms[ext] + if (!script_form) throw new Error(`No script form for extension ${ext}`) + log.console(`building ${path}`) + var script = script_form(path, text(fd.slurp(path))) + var fn = js.compile("script", script) + ensure_dir(cache_path.substring(0, cache_path.lastIndexOf('/'))) + fd.slurpwrite(cache_path, js.compile_blob(fn)) + return js.eval_compile(fn) +} + function resolve_locator(path, ext, ctx) { var local_path @@ -459,17 +466,25 @@ function resolve_locator(path, ext, ctx) else local_path = path + ext - if (fd.is_file(local_path)) - return {path: local_path, scope: SCOPE_LOCAL, script: text(fd.slurp(local_path))}; + if (fd.is_file(local_path)) { + var fn = resolve_mod_fn(local_path) + return {path: local_path, scope: SCOPE_LOCAL, symbol:fn} + } var mod_path = `.cell/modules/${get_package_from_path(path, ctx)}${ext}` - - if (fd.is_file(mod_path)) - return {path: mod_path, scope: SCOPE_PACKAGE, script: text(fd.slurp(mod_path))}; + if (fd.is_file(mod_path)) { + var fn = resolve_mod_fn(mod_path) + return {path: mod_path, scope: SCOPE_PACKAGE, symbol:fn} + } var core = core_qop.read(path + ext) - if (core) - return {path, scope: SCOPE_CORE, script: text(core)}; + if (core) { + var form = script_forms[ext] + if (!form) throw new Error(`No script form for extension ${ext}`) + var script = form(null,text(core)) + var fn = js.compile(path, script) + return {path, scope: SCOPE_CORE, symbol:js.eval_compile(fn)}; + } return null; } @@ -522,19 +537,6 @@ function resolve_c_symbol(path, package_ctx) return null } -function mod_scriptor(name, script) -{ - // TODO: need a safe name here - return `(function setup_module($_){${script}})` -} - -function eval_mod(path, script, c_sym) -{ - var content = mod_scriptor(path, script) - var fn = js.eval(path, content) - return fn.call(c_sym) -} - // first looks in local // then in dependencies // then in core @@ -557,9 +559,9 @@ Shop.use = function(path, package_context) { if (c_resolve.scope < mod_resolve.scope) use_cache[cache_key] = c_resolve.symbol else if (mod_resolve.scope < c_resolve.scope) - use_cache[cache_key] = eval_mod(mod_resolve.path, mod_resolve.script) + use_cache[cache_key] = mod_resolve.symbol.call() else - use_cache[cache_key] = eval_mod(mod_resolve.path, mod_resolve.script, c_resolve.symbol) + use_cache[cache_key] = mod_resolve.symbol.call(c_resolve.symbol) return use_cache[cache_key] }