51 lines
1.1 KiB
Plaintext
51 lines
1.1 KiB
Plaintext
// Hidden vars (os, args) come from env
|
|
// args[0] = script filename, args[1..] = user args
|
|
var load_internal = os.load_internal
|
|
function use_embed(name) {
|
|
return load_internal("js_" + name + "_use")
|
|
}
|
|
|
|
var fd = use_embed('fd')
|
|
var json = use_embed('json')
|
|
|
|
var use_cache = {}
|
|
use_cache['fd'] = fd
|
|
use_cache['os'] = os
|
|
use_cache['json'] = json
|
|
|
|
function use(path) {
|
|
if (use_cache[path])
|
|
return use_cache[path];
|
|
|
|
var file_path = path + '.cm'
|
|
var script = null
|
|
var result = null
|
|
var exports = null
|
|
|
|
if (fd.is_file(file_path)) {
|
|
script = text(fd.slurp(file_path))
|
|
result = mach_eval(path, script, {use: use})
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
|
|
// Try embedded C module
|
|
result = use_embed(replace(path, '/', '_'))
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
|
|
// Load and run the user's program
|
|
var program = args[0]
|
|
|
|
var user_args = []
|
|
var _i = 1
|
|
while (_i < length(args)) {
|
|
push(user_args, args[_i])
|
|
_i = _i + 1
|
|
}
|
|
var blob = fd.slurp(program)
|
|
stone(blob)
|
|
var script = text(blob)
|
|
mach_eval(program, script, {use: use, args: user_args, json: json})
|