[njs] A return statement has been disabled outside of function.

Igor Sysoev igor at sysoev.ru
Tue Aug 16 16:14:34 UTC 2016


details:   http://hg.nginx.org/njs/rev/e6e192a55774
branches:  
changeset: 155:e6e192a55774
user:      Igor Sysoev <igor at sysoev.ru>
date:      Mon Aug 15 11:33:37 2016 +0300
description:
A return statement has been disabled outside of function.

diffstat:

 njs/njs_parser.c         |  7 +++++++
 njs/test/njs_unit_test.c |  3 +++
 2 files changed, 10 insertions(+), 0 deletions(-)

diffs (30 lines):

diff -r 0c9a771b7664 -r e6e192a55774 njs/njs_parser.c
--- a/njs/njs_parser.c	Thu Aug 11 13:57:36 2016 +0300
+++ b/njs/njs_parser.c	Mon Aug 15 11:33:37 2016 +0300
@@ -584,6 +584,13 @@ njs_parser_return_statement(njs_vm_t *vm
     njs_token_t        token;
     njs_parser_node_t  *node;
 
+    if (parser->scope == NJS_SCOPE_GLOBAL) {
+        nxt_alert(&vm->trace, NXT_LEVEL_ERROR,
+                  "SyntaxError: Illegal return statement");
+
+        return NXT_ERROR;
+    }
+
     node = njs_parser_node_alloc(vm);
     if (nxt_slow_path(node == NULL)) {
         return NJS_TOKEN_ERROR;
diff -r 0c9a771b7664 -r e6e192a55774 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Aug 11 13:57:36 2016 +0300
+++ b/njs/test/njs_unit_test.c	Mon Aug 15 11:33:37 2016 +0300
@@ -3290,6 +3290,9 @@ static njs_unit_test_t  njs_test[] =
 
     /* Functions. */
 
+    { nxt_string("return"),
+      nxt_string("SyntaxError: Illegal return statement in 1") },
+
     { nxt_string("function () { } f()"),
       nxt_string("SyntaxError: Unexpected token \"(\" in 1") },
 



More information about the nginx-devel mailing list