diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-08-05 00:09:50 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-08-05 15:50:13 +0200 |
commit | 54b7fac1af2704ec464a290b099cee8cf96c19f1 (patch) | |
tree | 5cfc294b2a4106d302c8e3fd07562beee4a4f44e /compiler.c | |
parent | d62e3725892a29b1e4407243bafc56916267cc27 (diff) |
compiler: enforce stricter module compilation rules
- Disallow toplevel `return` statements in module functions
- Disallow `export` statements in non-module functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r-- | compiler.c | 8 |
1 files changed, 7 insertions, 1 deletions
@@ -2809,6 +2809,12 @@ uc_compiler_compile_return(uc_compiler_t *compiler) uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); size_t off = chunk->count; + if (compiler->function->module) { + uc_compiler_syntax_error(compiler, 0, "return must be inside function body"); + + return; + } + uc_compiler_compile_expstmt(compiler); /* if we compiled an empty expression statement (`;`), load implicit null */ @@ -3009,7 +3015,7 @@ uc_compiler_compile_export(uc_compiler_t *compiler) uc_value_t *name; ssize_t slot; - if (compiler->program->sources.count == 1 || compiler->scope_depth) { + if (!compiler->function->module || compiler->scope_depth) { uc_compiler_syntax_error(compiler, compiler->parser->prev.pos, "Exports may only appear at top level of a module"); |