diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-08-05 00:15:30 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-08-05 16:37:41 +0200 |
commit | 304995b88d4e068db43a5edb677c2d525f7b49d3 (patch) | |
tree | d3259491ea9e4b525a09ec171de5faee8bfce3e2 /tests | |
parent | 506cc372436f6b03ac79762e66e307bb4c5a28ea (diff) |
compiler: rework export index allocation
The current implementation of the module export offset tracking was
inadequate and failed to properly handle larger module dependency
graphs. In order to properly support nested module imports/exports,
the following changes have been introduced:
- Gather export slots during module compilation and emit corresponding
export opcodes as one contiguous block at the end of the module
function body, right before the final return. This ensures that
interleaved imports of other modules do not place foreign exports
between our module exports.
- Track the number of program wide allocated export slots in order
to derive per-module-source offsets for the global VM export list.
- Derive import opcode source index from the module source export
offset and the index of the requested name within the module source
export name list.
- Improve error reporting for circular module imports.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/custom/04_modules/14_circular_imports | 43 | ||||
-rw-r--r-- | tests/custom/04_modules/15_complex_imports | 151 |
2 files changed, 194 insertions, 0 deletions
diff --git a/tests/custom/04_modules/14_circular_imports b/tests/custom/04_modules/14_circular_imports new file mode 100644 index 0000000..0b6070a --- /dev/null +++ b/tests/custom/04_modules/14_circular_imports @@ -0,0 +1,43 @@ +Circular imports are not possible and will lead to a compilation error. + +-- Testcase -- +import a_val from "./files/a.uc"; +-- End -- + +-- File a.uc -- +import b_val from "./b.uc"; +export default "a"; +-- End -- + +-- File b.uc -- +import a_val from "./a.uc"; +export default "b"; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stderr -- +Syntax error: Unable to compile module './files/a.uc': + + | Syntax error: Unable to compile module './files/b.uc': + | + | | Syntax error: Circular dependency + | | In ./files/b.uc, line 1, byte 19: + | | + | | `import a_val from "./a.uc";` + | | Near here --------^ + | + | In ./files/a.uc, line 1, byte 27: + | + | `import b_val from "./b.uc";` + | Near here ----------------^ + +In [stdin], line 1, byte 33: + + `import a_val from "./files/a.uc";` + Near here ----------------------^ + + +-- End -- diff --git a/tests/custom/04_modules/15_complex_imports b/tests/custom/04_modules/15_complex_imports new file mode 100644 index 0000000..f4dd588 --- /dev/null +++ b/tests/custom/04_modules/15_complex_imports @@ -0,0 +1,151 @@ +This testcase implements a somewhat complex dependency chain to stress +test the compiler module resolving. + +The dependency tree is: + +root + + mod1 + + mod4 + + mod8 + + mod2 + + mod4 + + mod6 + + mod8 + + mod9 + + mod3 + + mod4 + + mod6 + + mod4 + + mod5 + + mod1 + + mod4 + + mod8 + + mod2 + + mod4 + + mod6 + + mod8 + + mod9 + + mod4 + + mod6 + + mod8 + + mod9 + + mod4 + + mod6 + + mod6 + + mod7 + + mod5 + + mod1 + + mod4 + + mod8 + + mod2 + + mod4 + + mod6 + + mod8 + + mod9 + + mod4 + + mod6 + + mod8 + + mod9 + + mod4 + + mod6 + + mod6 + + mod8 + +-- Testcase -- +import mod1 from 'mod1'; +import mod2 from 'mod2'; +import mod3 from 'mod3'; +import mod4 from 'mod4'; +import mod5 from 'mod5'; +import mod6 from 'mod6'; +import mod7 from 'mod7'; +import mod8 from 'mod8'; + +print("root: ", [ mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8 ], "\n"); +-- End -- + +-- File mod1.uc -- +import mod4 from 'mod4'; +import mod8 from 'mod8'; + +print("mod1: ", [ mod4, mod8 ], "\n"); + +export default 'mod1'; +-- End -- + +-- File mod2.uc -- +import mod9 from 'mod9'; +import mod4 from 'mod4'; +import mod8 from 'mod8'; +import mod6 from 'mod6'; + +print("mod2: ", [ mod4, mod6, mod8, mod9 ], "\n"); + +export default 'mod2'; +-- End -- + +-- File mod3.uc -- +import mod4 from 'mod4'; +import mod6 from 'mod6'; + +print("mod3: ", [ mod4, mod6 ], "\n"); + +export default 'mod3'; +-- End -- + +-- File mod4.uc -- +export default 'mod4'; +-- End -- + +-- File mod5.uc -- +import mod1 from 'mod1'; +import mod4 from 'mod4'; +import mod2 from 'mod2'; +import mod9 from 'mod9'; +import mod8 from 'mod8'; +import mod6 from 'mod6'; + +print("mod5: ", [ mod1, mod2, mod4, mod6, mod8, mod9 ], "\n"); + +export default 'mod5'; +-- End -- + +-- File mod6.uc -- +export default 'mod6'; +-- End -- + +-- File mod7.uc -- +import mod6 from 'mod6'; +import mod5 from 'mod5'; + +print("mod7: ", [ mod5, mod6 ], "\n"); + +export default 'mod7'; +-- End -- + +-- File mod8.uc -- +export default 'mod8'; +-- End -- + +-- File mod9.uc -- +import mod4 from 'mod4'; +import mod6 from 'mod6'; + +print("mod9: ", [ mod4, mod6 ], "\n"); + +export default 'mod9'; +-- End -- + +-- Args -- +-R -L files/ +-- End -- + +-- Expect stdout -- +mod1: [ "mod4", "mod8" ] +mod9: [ "mod4", "mod6" ] +mod2: [ "mod4", "mod6", "mod8", "mod9" ] +mod3: [ "mod4", "mod6" ] +mod5: [ "mod1", "mod2", "mod4", "mod6", "mod8", "mod9" ] +mod7: [ "mod5", "mod6" ] +root: [ "mod1", "mod2", "mod3", "mod4", "mod5", "mod6", "mod7", "mod8" ] +-- End -- |