intrinsics rewritten without ++, --, etc
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// Hidden vars (os, actorsym, init, core_path) come from env
|
||||
// Hidden vars (os, actorsym, init, core_path, analyze, run_ast_fn, json) come from env
|
||||
// In actor spawn mode, also: nota, wota
|
||||
var ACTORDATA = actorsym
|
||||
var SYSYM = '__SYSTEM__'
|
||||
|
||||
@@ -49,23 +50,12 @@ function ends_with(str, suffix) {
|
||||
return search(str, suffix, -length(suffix)) != null
|
||||
}
|
||||
|
||||
var js = use_embed('js')
|
||||
var fd = use_embed('fd')
|
||||
var js = use_embed('js')
|
||||
|
||||
// Get the shop path from HOME environment
|
||||
var home = os.getenv('HOME') || os.getenv('USERPROFILE')
|
||||
if (!home) {
|
||||
os.print('Could not determine home directory\n')
|
||||
os.exit(1)
|
||||
}
|
||||
var shop_path = home + '/.cell'
|
||||
var packages_path = shop_path + '/packages'
|
||||
var core_path = packages_path + '/core'
|
||||
|
||||
if (!fd.is_dir(core_path)) {
|
||||
os.print('Cell shop not found at ' + shop_path + '. Run "cell install" to set up.\n')
|
||||
os.exit(1)
|
||||
}
|
||||
// Derive shop path from core_path (core_path = ~/.cell/packages/core)
|
||||
var packages_path = core_path + '/..'
|
||||
var shop_path = packages_path + '/..'
|
||||
|
||||
var use_cache = {}
|
||||
use_cache['core/os'] = os
|
||||
@@ -74,25 +64,31 @@ use_cache['core/os'] = os
|
||||
function use_core(path) {
|
||||
var cache_key = 'core/' + path
|
||||
if (use_cache[cache_key])
|
||||
return use_cache[cache_key];
|
||||
return use_cache[cache_key]
|
||||
|
||||
var sym = use_embed(replace(path, '/', '_'))
|
||||
|
||||
// Core scripts are in packages/core/
|
||||
var file_path = core_path + '/' + path + MOD_EXT
|
||||
|
||||
if (fd.is_file(file_path)) {
|
||||
var script_blob = fd.slurp(file_path)
|
||||
var script = text(script_blob)
|
||||
var mod = `(function setup_module(use){${script}})`
|
||||
var fn = mach_eval('core:' + path, mod)
|
||||
var result = call(fn,sym, [use_core])
|
||||
use_cache[cache_key] = result;
|
||||
return result;
|
||||
// Check for pre-compiled .mach file first
|
||||
var mach_path = core_path + '/' + path + '.mach'
|
||||
if (fd.is_file(mach_path)) {
|
||||
var result = mach_load(fd.slurp(mach_path), {use: use_core})
|
||||
use_cache[cache_key] = result
|
||||
return result
|
||||
}
|
||||
|
||||
use_cache[cache_key] = sym;
|
||||
return sym;
|
||||
// Fall back to source .cm file — compile at runtime
|
||||
var file_path = core_path + '/' + path + MOD_EXT
|
||||
if (fd.is_file(file_path)) {
|
||||
var script = text(fd.slurp(file_path))
|
||||
var ast = analyze(script, file_path)
|
||||
var result = run_ast_fn('core:' + path, ast, {use: use_core})
|
||||
use_cache[cache_key] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Embedded C module only
|
||||
use_cache[cache_key] = sym
|
||||
return sym
|
||||
}
|
||||
|
||||
var blob = use_core('blob')
|
||||
@@ -191,7 +187,7 @@ function actor_die(err)
|
||||
|
||||
|
||||
|
||||
actor_mod.on_exception(actor_die)
|
||||
//actor_mod.on_exception(actor_die)
|
||||
|
||||
_cell.args = init != null ? init : {}
|
||||
_cell.id = "newguy"
|
||||
@@ -208,10 +204,12 @@ $_.self = create_actor()
|
||||
os.use_cache = use_cache
|
||||
os.global_shop_path = shop_path
|
||||
os.$_ = $_
|
||||
os.analyze = analyze
|
||||
os.run_ast_fn = run_ast_fn
|
||||
os.json = json
|
||||
use_cache['core/json'] = json
|
||||
|
||||
var shop = use_core('internal/shop')
|
||||
|
||||
var json = use_core('json')
|
||||
var time = use_core('time')
|
||||
|
||||
var pronto = use_core('pronto')
|
||||
@@ -801,41 +799,49 @@ var prog = _cell.args.program
|
||||
|
||||
var package = use_core('package')
|
||||
|
||||
var locator = shop.resolve_locator(_cell.args.program + ".ce", null)
|
||||
|
||||
if (!locator) {
|
||||
var pkg = package.find_package_dir(_cell.args.program + ".ce")
|
||||
locator = shop.resolve_locator(_cell.args.program + ".ce", pkg)
|
||||
// Find the .ce file
|
||||
var prog_path = prog + ".ce"
|
||||
if (!fd.is_file(prog_path)) {
|
||||
var pkg_dir = package.find_package_dir(prog_path)
|
||||
if (pkg_dir)
|
||||
prog_path = pkg_dir + '/' + prog + '.ce'
|
||||
}
|
||||
|
||||
if (!locator) {
|
||||
os.print(`Main program ${_cell.args.program} could not be found\n`)
|
||||
if (!fd.is_file(prog_path)) {
|
||||
// Check core packages
|
||||
var core_dir = core_path
|
||||
prog_path = core_dir + '/' + prog + '.ce'
|
||||
}
|
||||
if (!fd.is_file(prog_path)) {
|
||||
os.print(`Main program ${prog} could not be found\n`)
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
$_.clock(_ => {
|
||||
// Get capabilities for the main program
|
||||
var file_info = shop.file_info ? shop.file_info(locator.path) : null
|
||||
var file_info = shop.file_info ? shop.file_info(prog_path) : null
|
||||
var inject = shop.script_inject_for ? shop.script_inject_for(file_info) : []
|
||||
|
||||
// Build env object for injection
|
||||
// Build env with runtime functions + capability injections
|
||||
var env = {}
|
||||
for (var i = 0; i < length(inject); i++) {
|
||||
var key = inject[i]
|
||||
arrfor(array(runtime_env), function(k) { env[k] = runtime_env[k] })
|
||||
var _ki = 0
|
||||
while (_ki < length(inject)) {
|
||||
var inj = inject[_ki]
|
||||
var key = inj
|
||||
if (key && key[0] == '$') key = text(key, 1)
|
||||
if (key == 'fd') env[key] = fd
|
||||
else env[key] = $_[key]
|
||||
if (key == 'fd') env['$fd'] = fd
|
||||
else env['$' + key] = $_[key]
|
||||
_ki = _ki + 1
|
||||
}
|
||||
|
||||
// Create use function bound to the program's package
|
||||
var pkg = file_info ? file_info.package : null
|
||||
var use_fn = function(path) { return shop.use(path, pkg) }
|
||||
env.use = function(path) { return shop.use(path, pkg) }
|
||||
env.args = _cell.args.arg
|
||||
|
||||
// Call with signature: setup_module(args, use, env)
|
||||
// The script wrapper binds $delay, $start, etc. from env
|
||||
var val = call(locator.symbol, null, [_cell.args.arg, use_fn, env])
|
||||
|
||||
if (val)
|
||||
var script = text(fd.slurp(prog_path))
|
||||
var ast = analyze(script, prog_path)
|
||||
var val = run_ast_fn(prog, ast, env)
|
||||
if (val) {
|
||||
log.error('Program must not return anything')
|
||||
disrupt
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user