run dylibs
This commit is contained in:
@@ -446,6 +446,7 @@ static JSValue js_os_dylib_close(JSContext *js, JSValue self, int argc, JSValue
|
||||
/* Load a native .cm module from a dylib handle.
|
||||
Uses cell_rt_native_module_load from qbe_helpers.c */
|
||||
extern JSValue cell_rt_native_module_load(JSContext *ctx, void *dl_handle);
|
||||
extern JSValue cell_rt_native_module_load_named(JSContext *ctx, void *dl_handle, const char *sym_name);
|
||||
|
||||
static JSValue js_os_native_module_load(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
{
|
||||
@@ -459,6 +460,24 @@ static JSValue js_os_native_module_load(JSContext *js, JSValue self, int argc, J
|
||||
return cell_rt_native_module_load(js, handle);
|
||||
}
|
||||
|
||||
static JSValue js_os_native_module_load_named(JSContext *js, JSValue self, int argc, JSValue *argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
return JS_ThrowTypeError(js, "native_module_load_named requires (dylib, sym_name)");
|
||||
|
||||
void *handle = JS_GetOpaque(argv[0], js_dylib_class_id);
|
||||
if (!handle)
|
||||
return JS_ThrowTypeError(js, "First argument must be a dylib object");
|
||||
|
||||
const char *sym_name = JS_ToCString(js, argv[1]);
|
||||
if (!sym_name)
|
||||
return JS_EXCEPTION;
|
||||
|
||||
JSValue result = cell_rt_native_module_load_named(js, handle, sym_name);
|
||||
JS_FreeCString(js, sym_name);
|
||||
return result;
|
||||
}
|
||||
|
||||
JSC_CCALL(os_print,
|
||||
size_t len;
|
||||
const char *str = JS_ToCStringLen(js, &len, argv[0]);
|
||||
@@ -635,6 +654,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||
MIST_FUNC_DEF(os, dylib_symbol, 2),
|
||||
MIST_FUNC_DEF(os, dylib_has_symbol, 2),
|
||||
MIST_FUNC_DEF(os, native_module_load, 1),
|
||||
MIST_FUNC_DEF(os, native_module_load_named, 2),
|
||||
MIST_FUNC_DEF(os, embedded_module, 1),
|
||||
MIST_FUNC_DEF(os, load_internal, 1),
|
||||
MIST_FUNC_DEF(os, internal_exists, 1),
|
||||
|
||||
@@ -481,7 +481,8 @@ function try_native_mod_dylib(pkg, stem) {
|
||||
if (!fd.is_file(dylib_path)) return null
|
||||
var handle = os.dylib_open(dylib_path)
|
||||
if (!handle) return null
|
||||
return os.native_module_load(handle)
|
||||
var sym = Shop.c_symbol_for_file(pkg, stem)
|
||||
return os.native_module_load_named(handle, sym)
|
||||
}
|
||||
|
||||
// Default capabilities injected into scripts
|
||||
@@ -1677,4 +1678,28 @@ Shop.parse_package = function(locator) {
|
||||
}
|
||||
}
|
||||
|
||||
Shop.use_native = function(path, package_context) {
|
||||
var src_path = path
|
||||
if (!starts_with(path, '/'))
|
||||
src_path = fd.realpath(path)
|
||||
if (!fd.is_file(src_path)) { print('File not found: ' + path); disrupt }
|
||||
|
||||
var file_info = Shop.file_info(src_path)
|
||||
var pkg = file_info.package || package_context
|
||||
|
||||
var sym_name = null
|
||||
if (pkg)
|
||||
sym_name = Shop.c_symbol_for_file(pkg, fd.basename(src_path))
|
||||
|
||||
var build = Shop.use('build', 'core')
|
||||
var dylib_path = build.compile_native(src_path, null, null, pkg)
|
||||
|
||||
var handle = os.dylib_open(dylib_path)
|
||||
if (!handle) { print('Failed to open native dylib: ' + dylib_path); disrupt }
|
||||
|
||||
if (sym_name)
|
||||
return os.native_module_load_named(handle, sym_name)
|
||||
return os.native_module_load(handle)
|
||||
}
|
||||
|
||||
return Shop
|
||||
25
run_aot.ce
Normal file
25
run_aot.ce
Normal file
@@ -0,0 +1,25 @@
|
||||
// run_aot.ce — compile a .ce program to native dylib and run it
|
||||
//
|
||||
// Usage:
|
||||
// cell run_aot <program.ce>
|
||||
|
||||
var shop = use('internal/shop')
|
||||
var fd = use('fd')
|
||||
|
||||
if (length(args) < 1) {
|
||||
print('usage: cell run_aot <program.ce>')
|
||||
return
|
||||
}
|
||||
|
||||
var file = args[0]
|
||||
if (!fd.is_file(file)) {
|
||||
if (!ends_with(file, '.ce') && fd.is_file(file + '.ce'))
|
||||
file = file + '.ce'
|
||||
else {
|
||||
print('file not found: ' + file)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var abs = fd.realpath(file)
|
||||
shop.use_native(abs)
|
||||
Reference in New Issue
Block a user