Add import statement support for dynamic extensions #98
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Major changes:
lib: internally expose new uc_require_library() helper
Break out the core logic of the uc_require() stl function into a new
uc_require_library() helper function and make it available for usage
outside of lib.c. Also add a new boolean parameter to the helper function
which allows restricting runtime require operations of modules to dynamic
libraries only.
vm: introduce new I_DYNLOAD opcode
The I_DYNLOAD opcode is basically a bytecode level instruction for
uc_require() with semantics similar to I_IMPORT. It allows loading
a dynamic extension library at runtime and treating values from the
resulting module context object like exports from a compile time source
module.
For example the statement
import { readfile, writefile } from "fs"
would import the readfile() and writefile() functions of fs.so as
readonly live bindings into the current file scope.
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 isassumed 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 flagdynlink=...
which allows forcing a particular module name expressionto be treated as dynamic extension. This is useful to e.g. force resolving
import { x } from "foo"
to a dynamic extensionfoo.so
loaded at runtimeeven if a plain
foo.uc
exists in the search path during compilation or ifno such module is available at build time.