Skip to content

Ref to function self should not create new object #2632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions jerry-core/ecma/operations/ecma-function-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
}

ecma_value_t ret_value = vm_run (bytecode_data_p,
ecma_value_t ret_value = vm_run (ext_func_p,
bytecode_data_p,
this_binding,
local_env_p,
ECMA_PARSE_NO_OPTS,
Expand Down Expand Up @@ -779,7 +780,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
JERRY_ASSERT (!(bytecode_data_p->status_flags & CBC_CODE_FLAGS_ARGUMENTS_NEEDED));
}

ecma_value_t ret_value = vm_run (bytecode_data_p,
ecma_value_t ret_value = vm_run (NULL,
bytecode_data_p,
arrow_func_p->this_binding,
local_env_p,
ECMA_PARSE_NO_OPTS,
Expand Down
1 change: 1 addition & 0 deletions jerry-core/vm/vm-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef const uint8_t *vm_instr_counter_t;
*/
typedef struct vm_frame_ctx_t
{
const ecma_extended_object_t *func_obj_p; /**< currently executed fucntion */
const ecma_compiled_code_t *bytecode_header_p; /**< currently executed byte-code data */
uint8_t *byte_code_p; /**< current byte code pointer */
uint8_t *byte_code_start_p; /**< byte code start pointer */
Expand Down
23 changes: 17 additions & 6 deletions jerry-core/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ vm_run_global (const ecma_compiled_code_t *bytecode_p) /**< pointer to bytecode
{
ecma_object_t *glob_obj_p = ecma_builtin_get_global ();

return vm_run (bytecode_p,
return vm_run (NULL,
bytecode_p,
ecma_make_object_value (glob_obj_p),
ecma_get_global_environment (),
false,
Expand Down Expand Up @@ -288,7 +289,8 @@ vm_run_eval (ecma_compiled_code_t *bytecode_data_p, /**< byte-code data */
lex_env_p = strict_lex_env_p;
}

ecma_value_t completion_value = vm_run (bytecode_data_p,
ecma_value_t completion_value = vm_run (NULL,
bytecode_data_p,
this_binding,
lex_env_p,
parse_opts,
Expand Down Expand Up @@ -733,8 +735,16 @@ vm_init_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
else
{
is_immutable_binding = (self_reference == literal_start_p[value_index]);
lit_value = vm_construct_literal_object (frame_ctx_p,
literal_start_p[value_index]);
if (is_immutable_binding)
{
lit_value = ecma_make_object_value ((ecma_object_t *) frame_ctx_p->func_obj_p);
ecma_ref_object ((ecma_object_t *) frame_ctx_p->func_obj_p);
}
else
{
lit_value = vm_construct_literal_object (frame_ctx_p,
literal_start_p[value_index]);
}
}

if (literal_index < register_end)
Expand Down Expand Up @@ -3471,7 +3481,8 @@ vm_execute (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
* @return ecma value
*/
ecma_value_t
vm_run (const ecma_compiled_code_t *bytecode_header_p, /**< byte-code data header */
vm_run (const ecma_extended_object_t *func_obj_p, /**< function object */
const ecma_compiled_code_t *bytecode_header_p, /**< byte-code data header */
ecma_value_t this_binding_value, /**< value of 'ThisBinding' */
ecma_object_t *lex_env_p, /**< lexical environment to use */
uint32_t parse_opts, /**< ecma_parse_opts_t option bits */
Expand Down Expand Up @@ -3502,7 +3513,7 @@ vm_run (const ecma_compiled_code_t *bytecode_header_p, /**< byte-code data heade
frame_ctx.literal_start_p = literal_p;
literal_p += args_p->literal_end;
}

frame_ctx.func_obj_p = func_obj_p;
frame_ctx.bytecode_header_p = bytecode_header_p;
frame_ctx.byte_code_p = (uint8_t *) literal_p;
frame_ctx.byte_code_start_p = (uint8_t *) literal_p;
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ typedef enum
ecma_value_t vm_run_global (const ecma_compiled_code_t *bytecode_p);
ecma_value_t vm_run_eval (ecma_compiled_code_t *bytecode_data_p, uint32_t parse_opts);

ecma_value_t vm_run (const ecma_compiled_code_t *bytecode_header_p, ecma_value_t this_binding_value,
ecma_value_t vm_run (const ecma_extended_object_t *func_obj_p,
const ecma_compiled_code_t *bytecode_header_p, ecma_value_t this_binding_value,
ecma_object_t *lex_env_p, uint32_t parse_opts, const ecma_value_t *arg_list_p,
ecma_length_t arg_list_len);

Expand Down
28 changes: 28 additions & 0 deletions tests/jerry/function-self-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 foo = function bar() {
assert(foo === bar);
};
foo();

var globalFunc;
function nest(func) {
globalFunc = func;
func();
}

nest(function bar() {
assert(globalFunc === bar);
});