From 78dfb08f7569904394249758b2d7a563366b60ff Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 20 Jul 2022 09:15:59 +0200 Subject: compiler: require a name in function declarations 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 --- compiler.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/compiler.c b/compiler.c index 26377d8..db8917b 100644 --- a/compiler.c +++ b/compiler.c @@ -36,7 +36,7 @@ static void uc_compiler_compile_constant(uc_compiler_t *compiler); static void uc_compiler_compile_template(uc_compiler_t *compiler); static void uc_compiler_compile_comma(uc_compiler_t *compiler); static void uc_compiler_compile_labelexpr(uc_compiler_t *compiler); -static void uc_compiler_compile_function(uc_compiler_t *compiler); +static void uc_compiler_compile_funcexpr(uc_compiler_t *compiler); static void uc_compiler_compile_and(uc_compiler_t *compiler); static void uc_compiler_compile_or(uc_compiler_t *compiler); static void uc_compiler_compile_nullish(uc_compiler_t *compiler); @@ -76,7 +76,7 @@ uc_compiler_parse_rules[TK_ERROR + 1] = { [TK_TEMPLATE] = { uc_compiler_compile_template, NULL, P_NONE }, [TK_COMMA] = { NULL, uc_compiler_compile_comma, P_COMMA }, [TK_LABEL] = { uc_compiler_compile_labelexpr, NULL, P_NONE }, - [TK_FUNC] = { uc_compiler_compile_function, NULL, P_NONE }, + [TK_FUNC] = { uc_compiler_compile_funcexpr, NULL, P_NONE }, [TK_AND] = { NULL, uc_compiler_compile_and, P_AND }, [TK_OR] = { NULL, uc_compiler_compile_or, P_OR }, [TK_NULLISH] = { NULL, uc_compiler_compile_nullish, P_OR }, @@ -1537,7 +1537,7 @@ uc_compiler_compile_delimitted_block(uc_compiler_t *compiler, uc_tokentype_t end } static void -uc_compiler_compile_function(uc_compiler_t *compiler) +uc_compiler_compile_funcexpr_common(uc_compiler_t *compiler, bool require_name) { uc_compiler_t fncompiler = { 0 }; uc_value_t *name = NULL; @@ -1560,6 +1560,9 @@ uc_compiler_compile_function(uc_compiler_t *compiler) if (slot == -1) uc_compiler_initialize_local(compiler); } + else if (require_name) { + uc_compiler_syntax_error(compiler, compiler->parser->curr.pos, "Expecting function name"); + } uc_compiler_init(&fncompiler, name ? ucv_string_get(name) : NULL, @@ -1646,6 +1649,18 @@ uc_compiler_compile_function(uc_compiler_t *compiler) } } +static void +uc_compiler_compile_funcexpr(uc_compiler_t *compiler) +{ + return uc_compiler_compile_funcexpr_common(compiler, false); +} + +static void +uc_compiler_compile_funcdecl(uc_compiler_t *compiler) +{ + return uc_compiler_compile_funcexpr_common(compiler, true); +} + static void uc_compiler_compile_and(uc_compiler_t *compiler) { @@ -2877,7 +2892,7 @@ uc_compiler_compile_statement(uc_compiler_t *compiler) else if (uc_compiler_parse_match(compiler, TK_TRY)) uc_compiler_compile_try(compiler); else if (uc_compiler_parse_match(compiler, TK_FUNC)) - uc_compiler_compile_function(compiler); + uc_compiler_compile_funcdecl(compiler); else if (uc_compiler_parse_match(compiler, TK_BREAK)) uc_compiler_compile_control(compiler); else if (uc_compiler_parse_match(compiler, TK_CONTINUE)) -- cgit v1.2.3