summaryrefslogtreecommitdiffhomepage
path: root/compiler.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-08-06 14:23:44 +0200
committerJo-Philipp Wich <jo@mein.io>2022-08-06 23:25:11 +0200
commitfcc49e6944ab29ab48e8363d2d72e9ca10d3fb76 (patch)
tree1ccab2a62017db277b5de73e0ce6a4ed93aa751a /compiler.c
parentc9442f12ee056fd50f314408052917cc5f359bb4 (diff)
compiler: add import statement support for dynamic extensions
Utilize the new I_DYNLINK vm opcode to support import statements referring to dynamic extension modules. During compilation, the compiler will try to infer the type of the imported module from the resolved file path; if it ends with `.so`, the module is assumed to by a dynamic extension and loading/binding of the module is deferred to runtime using I_DYNLINK opcodes. Additionally, the `-c` cli option gained support for a new compiler flag `dynlink=...` which allows forcing a particular module name expression to be treated as dynamic extension. This is useful to e.g. force resolving `import { x } from "foo"` to a dynamic extension `foo.so` loaded at runtime even if a plain `foo.uc` exists in the search path during compilation or if no such module is available at build time. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r--compiler.c70
1 files changed, 68 insertions, 2 deletions
diff --git a/compiler.c b/compiler.c
index 7fd4da2..1199d82 100644
--- a/compiler.c
+++ b/compiler.c
@@ -3330,6 +3330,68 @@ uc_compiler_acquire_source(uc_compiler_t *compiler, const char *path)
}
static bool
+uc_compiler_compile_dynload(uc_compiler_t *compiler, const char *name, uc_value_t *imports)
+{
+ uc_value_t *modname = ucv_string_new(name);
+ size_t i, n_imports;
+ uc_value_t *import;
+
+ for (i = 0, n_imports = 0; i < ucv_array_length(imports); i++) {
+ import = ucv_array_get(imports, i);
+
+ if (ucv_boolean_get(import)) {
+ uc_compiler_emit_insn(compiler, 0, I_DYNLOAD);
+ uc_compiler_emit_u32(compiler, 0, 0);
+ uc_compiler_emit_constant_index(compiler, 0, modname);
+ }
+ else {
+ n_imports++;
+ }
+ }
+
+ if (n_imports > 0) {
+ uc_compiler_emit_insn(compiler, 0, I_DYNLOAD);
+ uc_compiler_emit_u32(compiler, 0, n_imports | ((compiler->upvals.count - n_imports) << 16));
+ uc_compiler_emit_constant_index(compiler, 0, modname);
+
+ for (i = 0; i < ucv_array_length(imports); i++) {
+ import = ucv_get(ucv_array_get(imports, i));
+
+ if (!import)
+ import = ucv_string_new("default");
+
+ if (!ucv_boolean_get(import))
+ uc_compiler_emit_constant_index(compiler, 0, import);
+
+ ucv_put(import);
+ }
+ }
+
+ ucv_put(modname);
+
+ return true;
+}
+
+static bool
+uc_compiler_is_dynlink_module(uc_compiler_t *compiler, const char *name, const char *path)
+{
+ uc_search_path_t *dynlink_list = &compiler->parser->config->force_dynlink_list;
+ size_t i;
+ char *p;
+
+ for (i = 0; i < dynlink_list->count; i++)
+ if (!strcmp(dynlink_list->entries[i], name))
+ return true;
+
+ if (!path)
+ return false;
+
+ p = strrchr(path, '.');
+
+ return (p && !strcmp(p, ".so"));
+}
+
+static bool
uc_compiler_compile_module(uc_compiler_t *compiler, const char *name, uc_value_t *imports)
{
uc_source_t *source;
@@ -3341,7 +3403,10 @@ uc_compiler_compile_module(uc_compiler_t *compiler, const char *name, uc_value_t
path = uc_compiler_resolve_module_path(compiler, name);
- if (path) {
+ if (uc_compiler_is_dynlink_module(compiler, name, path)) {
+ res = uc_compiler_compile_dynload(compiler, name, imports);
+ }
+ else if (path) {
source = uc_compiler_acquire_source(compiler, path);
if (source) {
@@ -3361,6 +3426,8 @@ uc_compiler_compile_module(uc_compiler_t *compiler, const char *name, uc_value_t
res = false;
}
+
+ uc_source_put(source);
}
else {
uc_compiler_syntax_error(compiler, compiler->parser->curr.pos,
@@ -3369,7 +3436,6 @@ uc_compiler_compile_module(uc_compiler_t *compiler, const char *name, uc_value_t
return false;
}
- uc_source_put(source);
free(path);
return res;