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

Module import export support #96

Merged
merged 21 commits into from
Jul 30, 2022
Merged

Module import export support #96

merged 21 commits into from
Jul 30, 2022

Conversation

jow-
Copy link
Owner

@jow- jow- commented Jul 28, 2022

This PR introdcues support for ES6 like import and export statements ("JavaScript modules") to allow resolving and compiling module code at compile time without having to resort to dynamic loading through require() at runtime.

jow- added 3 commits July 28, 2022 13:18
 - Use nested switches instead of lookup tables to detect tokens
 - Simplify input buffer logic
 - Reduce amount of intermediate states

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Add support for the `import`, `export`, `from` and `as` keywords used in
module import and export statements.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
The upcoming module import support requires constant object values to
implement module wildcard import.

Reuse the existing u64 bit in ucv heads to mark array or object values
as constant and add corresponding `ucv_is_constant()` and
`ucv_set_constant()` helpers.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
@jow- jow- force-pushed the module-import-export-support branch 6 times, most recently from cbc741c to 324357b Compare July 29, 2022 10:29
jow- added 16 commits July 30, 2022 00:41
When stringifying upvalue references, resolve their target value and
convert it to a string. Only yield the abstract string representation
if the target value cannot be resolved.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Reject modifications on array values with a type exception when
the constant flag is set on the array operated upon.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Extend abstract source objects to maintain a list of exported symbols and
add functions to append and lookup exported names.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
The sizeof(size_t) might differ from the sizeof(uint32_t) used to serialize
compiled bytecode, so extra care is needed to properly encode and decode
upvalue slot values which are defined as (size_t)-1 / 2 + n.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
We must always report the chunk source position relative to the function
start offset, even if it is zero.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
The upcoming module support requires maintaining multiple source objects
within the same program, so add the necessary infrastructure for it.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Add a helper function to query the global index of a named export within
a specific source which is a prerequisite for compiling import statements.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Upcoming module support will rely on upresolved upvalues which are patched
at runtime to realize module imports, make sure the VM trace code does not
choke on such unresolved upvalues.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Resolve upvalue references to their actual values when pushing such
references onto the stack (or when attempting to call them as method).

This allows constructing objects of pointers, as needed for wildcard
module imports.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Reject modifications on object and array values with a type exception when
the constant flag is set on the value operated upon.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Introduce new opcodes to realize module imports and exports. The export
operation will capture a local variable as upvalue and store it in VM
wide module export registry while the import operation will connect an
upvalue from the module export registry with a preallocated upvalue in
the running function scope.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
The upcoming compile-time module support will require the configured
extension search path in the compiler as well, so move it to the
already shared uc_parse_config_t structure and add the appropriate
utility functions to initialize, append and free the search path
vector.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Replace all occurrences for the test file directory path with "." in stderr
and stdout results to ensure stable test outputs.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Report the proper source location when raising an error due to an
increment/decrement operation on a constant value.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
So far we allowed anonymous toplevel function expressions which makes
little sense since those can't be used for anything.

Require toplevel function declarations to be named and turn a missing
name into a compile time syntax error.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Do not require a parent function compiler reference to lookup an already
declared (potentially unresolved) upvalue in the current scope. Instead,
search the named upvalues in the current function scope in case there is
no parent compiler reference.

This is required for the upcoming module support which will use unresolved
upvalues to realize import/export functionality.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
@jow- jow- force-pushed the module-import-export-support branch 3 times, most recently from 1c3e897 to 2b94960 Compare July 30, 2022 11:36
jow- added 2 commits July 30, 2022 13:46
This commit introduces syntax level support for ES6 style module import
and export statements. Imports are resolved at compile time and the
corresponding module code is compiled into the main program.

Also add testcases to cover import and export statement semantics.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Trim down the libucode.so size somewhat by marking purely internal,
non-public API functions hidden.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
@jow- jow- force-pushed the module-import-export-support branch from 2b94960 to 156d584 Compare July 30, 2022 11:46
@jow- jow- merged commit e55965a into master Jul 30, 2022
@jow- jow- deleted the module-import-export-support branch July 30, 2022 12:02
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