From fcc49e6944ab29ab48e8363d2d72e9ca10d3fb76 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sat, 6 Aug 2022 14:23:44 +0200 Subject: 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 --- main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 087efff..02df3b1 100644 --- a/main.c +++ b/main.c @@ -90,7 +90,8 @@ print_usage(const char *app) "-c[flag,flag,...]\n" " Compile the given source file(s) to bytecode instead of executing them.\n" " Supported flags: no-interp (omit interpreter line), interp=... (over-\n" - " ride interpreter line with ...)\n\n" + " ride interpreter line with ...), dynlink=... (force import from ... to\n" + " be treated as shared extensions loaded at runtime).\n\n" "-o path\n" " Output file path when compiling. If omitted, the compiled byte code\n" @@ -204,7 +205,7 @@ parse_template_modeflags(char *opt, uc_parse_config_t *config) } static void -parse_compile_flags(char *opt, char **interp) +parse_compile_flags(char *opt, char **interp, uc_search_path_t *dynlink_list) { char *p, *k, *v; @@ -230,6 +231,12 @@ parse_compile_flags(char *opt, char **interp) else *interp = v; } + else if (!strcmp(k, "dynlink")) { + if (!v) + fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k); + else + uc_vector_push(dynlink_list, v); + } else { fprintf(stderr, "Unrecognized -c flag \"%s\", ignoring\n", k); } @@ -577,7 +584,7 @@ main(int argc, char **argv) case 'c': outfile = "./uc.out"; - parse_compile_flags(optarg, &interp); + parse_compile_flags(optarg, &interp, &config.force_dynlink_list); break; case 's': @@ -640,6 +647,7 @@ main(int argc, char **argv) out: uc_search_path_free(&config.module_search_path); + uc_vector_clear(&config.force_dynlink_list); uc_source_put(source); -- cgit v1.2.3