Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import statement support for dynamic extensions #98

Merged
merged 5 commits into from
Aug 6, 2022
Merged

Conversation

jow-
Copy link
Owner

@jow- jow- commented Aug 6, 2022

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 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.

@jow- jow- force-pushed the dynlink-support branch 2 times, most recently from 05b76f1 to 851a376 Compare August 6, 2022 19:04
jow- added 5 commits August 6, 2022 23:25
If a compile error is raised at offset 0, try to resolve line and
character position anyway.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Try to resolve the source offset to line and character position and only
fall back to report the location as instruction offset if we weren't able
to determine the line number.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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>
@jow- jow- force-pushed the dynlink-support branch from 851a376 to fcc49e6 Compare August 6, 2022 21:25
@jow- jow- merged commit d0ae910 into master Aug 6, 2022
@jow- jow- deleted the dynlink-support branch August 6, 2022 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant