njs_index_t index;
njs_parser_t *parser;
njs_variable_t *arg;
- njs_parser_node_t *node, *body;
+ njs_parser_node_t *node, *body, *last;
parser = lambda->u.parser;
return token;
}
- /*
- * There is no function body or the last function body statement is not
- * "return" statement. If function has body then the body->right node is
- * always present and it is a NJS_TOKEN_STATEMENT link node.
- */
+ last = NULL;
body = parser->node;
- if (body == NULL || body->right->token != NJS_TOKEN_RETURN) {
+ if (body != NULL) {
+ /* Take the last function body statement. */
+ last = body->right;
+
+ if (last == NULL) {
+ /*
+ * The last statement is terminated by semicolon.
+ * Take the last statement itself.
+ */
+ last = body->left;
+ }
+ }
+
+ if (last == NULL || last->token != NJS_TOKEN_RETURN) {
+ /*
+ * There is no function body or the last function body
+ * body statement is not "return" statement.
+ */
node = njs_parser_node_alloc(vm);
if (nxt_slow_path(node == NULL)) {
return NJS_TOKEN_ERROR;
/* Functions. */
+ { nxt_string("function f() { } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { ; } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { ;; } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { return } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { return; } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { return;; } f()"),
+ nxt_string("undefined") },
+
+ { nxt_string("function f() { return 1 } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f() { return 1; } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f() { return 1;; } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f() { return 1\n 2 } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f(a) { if (a) return 'OK' } f(1)+f(0)"),
+ nxt_string("OKundefined") },
+
+ { nxt_string("function f(a) { if (a) return 'OK'; } f(1)+f(0)"),
+ nxt_string("OKundefined") },
+
+ { nxt_string("function f(a) { if (a) return 'OK';; } f(1)+f(0)"),
+ nxt_string("OKundefined") },
+
{ nxt_string("var a = 1; a()"),
nxt_string("TypeError") },
"f.apply(5, [1, 2], 3)"),
nxt_string("8") },
+ { nxt_string("var a = function() { return 1 } + ''; a"),
+ nxt_string("[object Function]") },
+
{ nxt_string("''.concat.call()"),
nxt_string("TypeError") },