From 419ccff61136215bdcc39090b78b0a63bb0365b3 Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Wed, 13 Nov 2019 12:35:28 +0100 Subject: [PATCH] Fix local variable declaration issues after function argument initialization. (#3304) Fixes #3298 Fixes #3299 Fixes #3300 Fixes #3302 JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- jerry-core/parser/js/byte-code.h | 8 +++--- jerry-core/vm/vm.c | 8 ++++++ jerry-core/vm/vm.h | 13 +++++---- tests/jerry/es2015/function-param-init2.js | 16 +++++++++++ .../es2015/regression-test-issue-3298.js | 22 +++++++++++++++ .../es2015/regresssion-test-issue-3302.js | 26 ++++++++++++++++++ .../jerry/fail/regresssion-test-issue-3299.js | 27 +++++++++++++++++++ .../jerry/fail/regresssion-test-issue-3300.js | 23 ++++++++++++++++ 8 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 tests/jerry/es2015/regression-test-issue-3298.js create mode 100644 tests/jerry/es2015/regresssion-test-issue-3302.js create mode 100644 tests/jerry/fail/regresssion-test-issue-3299.js create mode 100644 tests/jerry/fail/regresssion-test-issue-3300.js diff --git a/jerry-core/parser/js/byte-code.h b/jerry-core/parser/js/byte-code.h index 95ebb2c05..e38294662 100644 --- a/jerry-core/parser/js/byte-code.h +++ b/jerry-core/parser/js/byte-code.h @@ -303,13 +303,13 @@ CBC_OPCODE (CBC_EVAL, CBC_NO_FLAG, 0, \ VM_OC_EVAL) \ CBC_OPCODE (CBC_CREATE_LOCAL, CBC_HAS_LITERAL_ARG, 0, \ - VM_OC_NONE) \ + VM_OC_INIT_LOCALS) \ CBC_OPCODE (CBC_CREATE_LET, CBC_HAS_LITERAL_ARG, 0, \ - VM_OC_NONE) \ + VM_OC_INIT_LOCALS) \ CBC_OPCODE (CBC_CREATE_CONST, CBC_HAS_LITERAL_ARG, 0, \ - VM_OC_NONE) \ + VM_OC_INIT_LOCALS) \ CBC_OPCODE (CBC_INIT_LOCAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \ - VM_OC_NONE) \ + VM_OC_INIT_LOCALS) \ CBC_OPCODE (CBC_CREATE_VAR_EVAL, CBC_HAS_LITERAL_ARG, 0, \ VM_OC_NONE) \ CBC_OPCODE (CBC_CREATE_VAR_FUNC_EVAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \ diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index c82249248..95ac3ea60 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -1312,6 +1312,13 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ continue; } #if ENABLED (JERRY_ES2015) + case VM_OC_INIT_LOCALS: + { + frame_ctx_p->byte_code_p = byte_code_start_p; + vm_init_loop (frame_ctx_p); + byte_code_p = frame_ctx_p->byte_code_p; + continue; + } case VM_OC_ASSIGN_LET_CONST: { uint32_t literal_index; @@ -3424,6 +3431,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ continue; } #endif /* ENABLED (JERRY_LINE_INFO) */ + case VM_OC_NONE: default: { JERRY_ASSERT (VM_OC_GROUP_GET_INDEX (opcode_data) == VM_OC_NONE); diff --git a/jerry-core/vm/vm.h b/jerry-core/vm/vm.h index a9ee58c31..c0cef3231 100644 --- a/jerry-core/vm/vm.h +++ b/jerry-core/vm/vm.h @@ -202,9 +202,7 @@ typedef enum VM_OC_RIGHT_SHIFT, /**< right shift */ VM_OC_UNS_RIGHT_SHIFT, /**< unsigned right shift */ -#if ENABLED (JERRY_ES2015) VM_OC_BLOCK_CREATE_CONTEXT, /**< create lexical environment for blocks enclosed in braces */ -#endif /* ENABLED (JERRY_ES2015) */ VM_OC_WITH, /**< with */ VM_OC_FOR_IN_CREATE_CONTEXT, /**< for in create context */ VM_OC_FOR_IN_GET_NEXT, /**< get next */ @@ -220,11 +218,14 @@ typedef enum VM_OC_BREAKPOINT_ENABLED, /**< enabled breakpoint for debugger */ VM_OC_BREAKPOINT_DISABLED, /**< disabled breakpoint for debugger */ #endif /* ENABLED (JERRY_DEBUGGER) */ -#if ENABLED (JERRY_LINE_INFO) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) VM_OC_RESOURCE_NAME, /**< resource name of the current function */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ +#if ENABLED (JERRY_LINE_INFO) VM_OC_LINE, /**< line number of the next statement */ #endif /* ENABLED (JERRY_LINE_INFO) */ #if ENABLED (JERRY_ES2015) + VM_OC_INIT_LOCALS, /**< call vm_init_loop() */ VM_OC_ASSIGN_LET_CONST, /**< assign values to let/const declarations */ VM_OC_CLONE_CONTEXT, /**< clone lexical environment with let/const declarations */ VM_OC_SET_COMPUTED_PROPERTY, /**< set computed property */ @@ -260,15 +261,17 @@ typedef enum VM_OC_BREAKPOINT_ENABLED = VM_OC_NONE, /**< enabled breakpoint for debugger is unused */ VM_OC_BREAKPOINT_DISABLED = VM_OC_NONE, /**< disabled breakpoint for debugger is unused */ #endif /* !ENABLED (JERRY_DEBUGGER) */ -#if !ENABLED (JERRY_LINE_INFO) +#if !ENABLED (JERRY_LINE_INFO) && !ENABLED (JERRY_ES2015_MODULE_SYSTEM) VM_OC_RESOURCE_NAME = VM_OC_NONE, /**< resource name of the current function is unused */ +#endif /* !ENABLED (JERRY_LINE_INFO) && !ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ +#if !ENABLED (JERRY_LINE_INFO) VM_OC_LINE = VM_OC_NONE, /**< line number of the next statement is unused */ #endif /* !ENABLED (JERRY_LINE_INFO) */ #if !ENABLED (JERRY_ES2015) + VM_OC_INIT_LOCALS = VM_OC_NONE, /**< call vm_init_loop() */ VM_OC_ASSIGN_LET_CONST = VM_OC_NONE, /**< assign values to let/const declarations */ VM_OC_CLONE_CONTEXT = VM_OC_NONE, /**< clone lexical environment with let/const declarations */ VM_OC_SET_COMPUTED_PROPERTY = VM_OC_NONE, /**< set computed property is unused */ - VM_OC_BLOCK_CREATE_CONTEXT = VM_OC_NONE, /**< create context for blocks enclosed in braces */ VM_OC_FOR_OF_CREATE_CONTEXT = VM_OC_NONE, /**< for of create context */ VM_OC_FOR_OF_GET_NEXT = VM_OC_NONE, /**< get next */ diff --git a/tests/jerry/es2015/function-param-init2.js b/tests/jerry/es2015/function-param-init2.js index 237a5b704..f44df5d0d 100644 --- a/tests/jerry/es2015/function-param-init2.js +++ b/tests/jerry/es2015/function-param-init2.js @@ -63,3 +63,19 @@ function j(a = arguments[1]) assert(a === 2); } j(undefined,2); + +function k(a = 2) +{ + let d = 5; + assert(d === 5); + eval("assert(a === 2)"); +} +k(); + +function i(a = 3) +{ + const d = 6; + assert(d === 6); + eval("assert(a === 3)"); +} +i(); diff --git a/tests/jerry/es2015/regression-test-issue-3298.js b/tests/jerry/es2015/regression-test-issue-3298.js new file mode 100644 index 000000000..57e1227ba --- /dev/null +++ b/tests/jerry/es2015/regression-test-issue-3298.js @@ -0,0 +1,22 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var $ = { $: function ( $) { } }; + +function g ( b = (local = $) ) { + var o ; + function f ( ) { return this === o } +} + +g () diff --git a/tests/jerry/es2015/regresssion-test-issue-3302.js b/tests/jerry/es2015/regresssion-test-issue-3302.js new file mode 100644 index 000000000..46e42d9a3 --- /dev/null +++ b/tests/jerry/es2015/regresssion-test-issue-3302.js @@ -0,0 +1,26 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +function g ( a, b = a ) { + function a ( ) { } + function $ ( ) { } + eval ( "" ) +} +g ( ); + +$ = function ( ) { + function x ( ) { } +} + +function j ( ) { } diff --git a/tests/jerry/fail/regresssion-test-issue-3299.js b/tests/jerry/fail/regresssion-test-issue-3299.js new file mode 100644 index 000000000..98fa21b8e --- /dev/null +++ b/tests/jerry/fail/regresssion-test-issue-3299.js @@ -0,0 +1,27 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +function f (a, b = $+ function ( ) { } ( ), $) { } +var $ = { $: function ($) { }} +f( ) + +function duplicatedArg ( a , b = d , c ) { + try { + var eval + var id_1 + if ( id_2 === eval ( ) ) { } + } finally { } +} + +duplicatedArg(1, 2) diff --git a/tests/jerry/fail/regresssion-test-issue-3300.js b/tests/jerry/fail/regresssion-test-issue-3300.js new file mode 100644 index 000000000..0ecfbfdfe --- /dev/null +++ b/tests/jerry/fail/regresssion-test-issue-3300.js @@ -0,0 +1,23 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +y = 6 +function i ( a , b = ( y ) + 2 , c = typeof id_0 ) { + function x ( ) { } + eval ( "//Single Line Comments\u2029 var =;" ) + c === "undefined" + print ( y === 10 ) + ( id_2 === 11 ) +} +i ( )