/** * Run the code, starting from specified instruction position */ ecma_completion_value_t vm_run_from_pos (const vm_instr_t *instrs_p, /**< byte-code array */ vm_instr_counter_t start_pos, /**< position of starting instruction */ ecma_value_t this_binding_value, /**< value of 'ThisBinding' */ ecma_object_t *lex_env_p, /**< lexical environment to use */ bool is_strict, /**< is the code is strict mode code (ECMA-262 v5, 10.1.1) */ bool is_eval_code) /**< is the code is eval code (ECMA-262 v5, 10.1) */ { ecma_completion_value_t completion; const vm_instr_t *curr = &instrs_p[start_pos]; JERRY_ASSERT (curr->op_idx == VM_OP_REG_VAR_DECL); const idx_t min_reg_num = curr->data.reg_var_decl.min; const idx_t max_reg_num = curr->data.reg_var_decl.max; JERRY_ASSERT (max_reg_num >= min_reg_num); const int32_t regs_num = max_reg_num - min_reg_num + 1; MEM_DEFINE_LOCAL_ARRAY (regs, regs_num, ecma_value_t); vm_frame_ctx_t frame_ctx; frame_ctx.instrs_p = instrs_p; frame_ctx.pos = (vm_instr_counter_t) (start_pos + 1); frame_ctx.this_binding = this_binding_value; frame_ctx.lex_env_p = lex_env_p; frame_ctx.is_strict = is_strict; frame_ctx.is_eval_code = is_eval_code; frame_ctx.is_call_in_direct_eval_form = false; frame_ctx.min_reg_num = min_reg_num; frame_ctx.max_reg_num = max_reg_num; frame_ctx.tmp_num_p = ecma_alloc_number (); vm_stack_add_frame (&frame_ctx.stack_frame, regs, regs_num); vm_frame_ctx_t *prev_context_p = vm_top_context_p; vm_top_context_p = &frame_ctx; #ifdef MEM_STATS interp_mem_stats_context_enter (&frame_ctx, start_pos); #endif /* MEM_STATS */ completion = vm_loop (&frame_ctx, NULL); JERRY_ASSERT (ecma_is_completion_value_throw (completion) || ecma_is_completion_value_return (completion)); vm_top_context_p = prev_context_p; vm_stack_free_frame (&frame_ctx.stack_frame); ecma_dealloc_number (frame_ctx.tmp_num_p); #ifdef MEM_STATS interp_mem_stats_context_exit (&frame_ctx, start_pos); #endif /* MEM_STATS */ MEM_FINALIZE_LOCAL_ARRAY (regs); return completion; } /* vm_run_from_pos */
/** * Run the code, starting from specified opcode */ ecma_completion_value_t vm_run_from_pos (opcode_counter_t start_pos, /**< identifier of starting opcode */ ecma_value_t this_binding_value, /**< value of 'ThisBinding' */ ecma_object_t *lex_env_p, /**< lexical environment to use */ bool is_strict, /**< is the code is strict mode code (ECMA-262 v5, 10.1.1) */ bool is_eval_code) /**< is the code is eval code (ECMA-262 v5, 10.1) */ { ecma_completion_value_t completion; const opcode_t *curr = &__program[start_pos]; JERRY_ASSERT (curr->op_idx == __op__idx_reg_var_decl); const idx_t min_reg_num = curr->data.reg_var_decl.min; const idx_t max_reg_num = curr->data.reg_var_decl.max; JERRY_ASSERT (max_reg_num >= min_reg_num); const int32_t regs_num = max_reg_num - min_reg_num + 1; MEM_DEFINE_LOCAL_ARRAY (regs, regs_num, ecma_value_t); int_data_t int_data; int_data.pos = (opcode_counter_t) (start_pos + 1); int_data.this_binding = this_binding_value; int_data.lex_env_p = lex_env_p; int_data.is_strict = is_strict; int_data.is_eval_code = is_eval_code; int_data.min_reg_num = min_reg_num; int_data.max_reg_num = max_reg_num; int_data.tmp_num_p = ecma_alloc_number (); ecma_stack_add_frame (&int_data.stack_frame, regs, regs_num); int_data_t *prev_context_p = vm_top_context_p; vm_top_context_p = &int_data; #ifdef MEM_STATS interp_mem_stats_context_enter (&int_data, start_pos); #endif /* MEM_STATS */ completion = vm_loop (&int_data, NULL); JERRY_ASSERT (ecma_is_completion_value_normal (completion) || ecma_is_completion_value_throw (completion) || ecma_is_completion_value_return (completion) || ecma_is_completion_value_exit (completion)); vm_top_context_p = prev_context_p; ecma_stack_free_frame (&int_data.stack_frame); ecma_dealloc_number (int_data.tmp_num_p); #ifdef MEM_STATS interp_mem_stats_context_exit (&int_data, start_pos); #endif /* MEM_STATS */ MEM_FINALIZE_LOCAL_ARRAY (regs); return completion; } /* vm_run_from_pos */
/** * Run global code */ jerry_completion_code_t vm_run_global (void) { JERRY_ASSERT (__program != NULL); JERRY_ASSERT (vm_top_context_p == NULL); #ifdef MEM_STATS interp_mem_stats_print_legend (); #endif /* MEM_STATS */ bool is_strict = false; vm_instr_counter_t start_pos = 0; opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (__program, start_pos++); if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) { is_strict = true; } ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL); ecma_object_t *lex_env_p = ecma_get_global_environment (); ecma_completion_value_t completion = vm_run_from_pos (__program, start_pos, ecma_make_object_value (glob_obj_p), lex_env_p, is_strict, false); jerry_completion_code_t ret_code; if (ecma_is_completion_value_return (completion)) { JERRY_ASSERT (ecma_is_value_undefined (ecma_get_completion_value_value (completion))); ret_code = JERRY_COMPLETION_CODE_OK; } else { JERRY_ASSERT (ecma_is_completion_value_throw (completion)); ret_code = JERRY_COMPLETION_CODE_UNHANDLED_EXCEPTION; } ecma_free_completion_value (completion); ecma_deref_object (glob_obj_p); ecma_deref_object (lex_env_p); JERRY_ASSERT (vm_top_context_p == NULL); return ret_code; } /* vm_run_global */
/** * Run specified eval-mode bytecode * * @return completion value */ ecma_completion_value_t vm_run_eval (const bytecode_data_header_t *bytecode_data_p, /**< byte-code data header */ bool is_direct) /**< is eval called in direct mode? */ { vm_instr_counter_t first_instr_index = 0u; opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (bytecode_data_p, first_instr_index++); bool is_strict = ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) != 0); ecma_value_t this_binding; ecma_object_t *lex_env_p; /* ECMA-262 v5, 10.4.2 */ if (is_direct) { this_binding = vm_get_this_binding (); lex_env_p = vm_get_lex_env (); } else { this_binding = ecma_make_object_value (ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL)); lex_env_p = ecma_get_global_environment (); } if (is_strict) { ecma_object_t *strict_lex_env_p = ecma_create_decl_lex_env (lex_env_p); ecma_deref_object (lex_env_p); lex_env_p = strict_lex_env_p; } ecma_completion_value_t completion = vm_run_from_pos (bytecode_data_p, first_instr_index, this_binding, lex_env_p, is_strict, true, NULL); if (ecma_is_completion_value_return (completion)) { completion = ecma_make_normal_completion_value (ecma_get_completion_value_value (completion)); } else { JERRY_ASSERT (ecma_is_completion_value_throw (completion)); } ecma_deref_object (lex_env_p); ecma_free_value (this_binding, true); return completion; } /* vm_run_eval */
/** * 'for-in' opcode handler * * See also: * ECMA-262 v5, 12.6.4 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t opfunc_for_in (vm_instr_t instr, /**< instruction */ vm_frame_ctx_t *int_data_p) /**< interpreter context */ { const idx_t expr_idx = instr.data.for_in.expr; const idx_t block_end_oc_idx_1 = instr.data.for_in.oc_idx_1; const idx_t block_end_oc_idx_2 = instr.data.for_in.oc_idx_2; const vm_instr_counter_t for_in_end_oc = (vm_instr_counter_t) ( vm_calc_instr_counter_from_idx_idx (block_end_oc_idx_1, block_end_oc_idx_2) + int_data_p->pos); ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 1., 2. */ ECMA_TRY_CATCH (expr_value, get_variable_value (int_data_p, expr_idx, false), ret_value); int_data_p->pos++; vm_instr_t meta_instr = vm_get_instr (int_data_p->instrs_p, for_in_end_oc); JERRY_ASSERT (meta_instr.op_idx == VM_OP_META); JERRY_ASSERT (meta_instr.data.meta.type == OPCODE_META_TYPE_END_FOR_IN); /* 3. */ if (!ecma_is_value_undefined (expr_value) && !ecma_is_value_null (expr_value)) { /* 4. */ ECMA_TRY_CATCH (obj_expr_value, ecma_op_to_object (expr_value), ret_value); ecma_object_t *obj_p = ecma_get_object_from_value (obj_expr_value); ecma_collection_iterator_t names_iterator; ecma_collection_header_t *names_p = vm_helper_for_in_enumerate_properties_names (obj_p); if (names_p != NULL) { ecma_collection_iterator_init (&names_iterator, names_p); const vm_instr_counter_t for_in_body_begin_oc = int_data_p->pos; const vm_instr_counter_t for_in_body_end_oc = for_in_end_oc; while (ecma_collection_iterator_next (&names_iterator)) { ecma_value_t name_value = *names_iterator.current_value_p; ecma_string_t *name_p = ecma_get_string_from_value (name_value); if (ecma_op_object_get_property (obj_p, name_p) != NULL) { ecma_completion_value_t completion = set_variable_value (int_data_p, int_data_p->pos, OPCODE_REG_SPECIAL_FOR_IN_PROPERTY_NAME, name_value); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); vm_run_scope_t run_scope_for_in = { for_in_body_begin_oc, for_in_body_end_oc }; ecma_completion_value_t for_in_body_completion = vm_loop (int_data_p, &run_scope_for_in); if (ecma_is_completion_value_empty (for_in_body_completion)) { JERRY_ASSERT (int_data_p->pos == for_in_body_end_oc); int_data_p->pos = for_in_body_begin_oc; } else { JERRY_ASSERT (ecma_is_completion_value_throw (for_in_body_completion) || ecma_is_completion_value_return (for_in_body_completion) || ecma_is_completion_value_jump (for_in_body_completion)); JERRY_ASSERT (int_data_p->pos <= for_in_body_end_oc); ret_value = for_in_body_completion; break; } } } ecma_free_values_collection (names_p, true); } ECMA_FINALIZE (obj_expr_value); } int_data_p->pos = (vm_instr_counter_t) (for_in_end_oc + 1u); ECMA_FINALIZE (expr_value); return ret_value; } /* opfunc_for_in */
/** * [[Call]] implementation for Function objects, * created through 13.2 (ECMA_OBJECT_TYPE_FUNCTION) * or 15.3.4.5 (ECMA_OBJECT_TYPE_BOUND_FUNCTION), * and for built-in Function objects * from section 15 (ECMA_OBJECT_TYPE_BUILT_IN_FUNCTION). * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */ ecma_value_t this_arg_value, /**< 'this' argument's value */ const ecma_value_t* arguments_list_p, /**< arguments list */ ecma_length_t arguments_list_len) /**< length of arguments list */ { JERRY_ASSERT (func_obj_p != NULL && !ecma_is_lexical_environment (func_obj_p)); JERRY_ASSERT (ecma_op_is_callable (ecma_make_object_value (func_obj_p))); JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION) { if (unlikely (ecma_get_object_is_builtin (func_obj_p))) { ret_value = ecma_builtin_dispatch_call (func_obj_p, this_arg_value, arguments_list_p, arguments_list_len); } else { /* Entering Function Code (ECMA-262 v5, 10.4.3) */ ecma_property_t *scope_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_SCOPE); ecma_property_t *bytecode_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_CODE_BYTECODE); ecma_property_t *code_prop_p = ecma_get_internal_property (func_obj_p, ECMA_INTERNAL_PROPERTY_CODE_FLAGS_AND_OFFSET); ecma_object_t *scope_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, scope_prop_p->u.internal_property.value); uint32_t code_prop_value = code_prop_p->u.internal_property.value; // 8. bool is_strict; bool do_instantiate_args_obj; const vm_instr_t *instrs_p = MEM_CP_GET_POINTER (const vm_instr_t, bytecode_prop_p->u.internal_property.value); vm_instr_counter_t code_first_instr_pos = ecma_unpack_code_internal_property_value (code_prop_value, &is_strict, &do_instantiate_args_obj); ecma_value_t this_binding; // 1. if (is_strict) { this_binding = ecma_copy_value (this_arg_value, true); } else if (ecma_is_value_undefined (this_arg_value) || ecma_is_value_null (this_arg_value)) { // 2. this_binding = ecma_make_object_value (ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL)); } else { // 3., 4. ecma_completion_value_t completion = ecma_op_to_object (this_arg_value); JERRY_ASSERT (ecma_is_completion_value_normal (completion)); this_binding = ecma_get_completion_value_value (completion); } // 5. ecma_object_t *local_env_p = ecma_create_decl_lex_env (scope_p); // 9. ECMA_TRY_CATCH (args_var_declaration_ret, ecma_function_call_setup_args_variables (func_obj_p, local_env_p, arguments_list_p, arguments_list_len, is_strict, do_instantiate_args_obj), ret_value); ecma_completion_value_t completion = vm_run_from_pos (instrs_p, code_first_instr_pos, this_binding, local_env_p, is_strict, false); if (ecma_is_completion_value_return (completion)) { ret_value = ecma_make_normal_completion_value (ecma_get_completion_value_value (completion)); } else { ret_value = completion; } ECMA_FINALIZE (args_var_declaration_ret); ecma_deref_object (local_env_p); ecma_free_value (this_binding, true); } } else if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BUILT_IN_FUNCTION)
/** * Perform 'eval' with code stored in continuous character buffer * * See also: * ecma_op_eval * ECMA-262 v5, 15.1.2.1 (steps 2 to 8) * * @return completion value */ ecma_completion_value_t ecma_op_eval_chars_buffer (const jerry_api_char_t *code_p, /**< code characters buffer */ size_t code_buffer_size, /**< size of the buffer */ bool is_direct, /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */ bool is_called_from_strict_mode_code) /**< is eval is called from strict mode code */ { JERRY_ASSERT (code_p != NULL); ecma_completion_value_t completion; const vm_instr_t *instrs_p; jsp_status_t parse_status; bool is_strict_call = (is_direct && is_called_from_strict_mode_code); parse_status = parser_parse_eval (code_p, code_buffer_size, is_strict_call, &instrs_p); if (parse_status == JSP_STATUS_SYNTAX_ERROR) { completion = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_SYNTAX)); } else if (parse_status == JSP_STATUS_REFERENCE_ERROR) { completion = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_REFERENCE)); } else { JERRY_ASSERT (parse_status == JSP_STATUS_OK); vm_instr_counter_t first_instr_index = 0u; bool is_strict_prologue = false; opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (instrs_p, first_instr_index++); if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) { is_strict_prologue = true; } bool is_strict = (is_strict_call || is_strict_prologue); ecma_value_t this_binding; ecma_object_t *lex_env_p; /* ECMA-262 v5, 10.4.2 */ if (is_direct) { this_binding = vm_get_this_binding (); lex_env_p = vm_get_lex_env (); } else { this_binding = ecma_make_object_value (ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL)); lex_env_p = ecma_get_global_environment (); } if (is_strict) { ecma_object_t *strict_lex_env_p = ecma_create_decl_lex_env (lex_env_p); ecma_deref_object (lex_env_p); lex_env_p = strict_lex_env_p; } completion = vm_run_from_pos (instrs_p, first_instr_index, this_binding, lex_env_p, is_strict, true); if (ecma_is_completion_value_return (completion)) { completion = ecma_make_normal_completion_value (ecma_get_completion_value_value (completion)); } else { JERRY_ASSERT (ecma_is_completion_value_throw (completion)); } ecma_deref_object (lex_env_p); ecma_free_value (this_binding, true); } return completion; } /* ecma_op_eval_chars_buffer */
/** * Run the code, starting from specified instruction position */ ecma_completion_value_t vm_run_from_pos (const bytecode_data_header_t *header_p, /**< byte-code data header */ vm_instr_counter_t start_pos, /**< position of starting instruction */ ecma_value_t this_binding_value, /**< value of 'ThisBinding' */ ecma_object_t *lex_env_p, /**< lexical environment to use */ bool is_strict, /**< is the code is strict mode code (ECMA-262 v5, 10.1.1) */ bool is_eval_code, /**< is the code is eval code (ECMA-262 v5, 10.1) */ ecma_collection_header_t *arg_collection_p) /**< * - collection of function call arguments, * if arguments for the called function * are placed on registers; * - NULL - otherwise. */ { ecma_completion_value_t completion; const vm_instr_t *instrs_p = header_p->instrs_p; const vm_instr_t *curr = &instrs_p[start_pos]; JERRY_ASSERT (curr->op_idx == VM_OP_REG_VAR_DECL); const uint32_t tmp_regs_num = curr->data.reg_var_decl.tmp_regs_num; const uint32_t local_var_regs_num = curr->data.reg_var_decl.local_var_regs_num; const uint32_t arg_regs_num = curr->data.reg_var_decl.arg_regs_num; uint32_t regs_num = VM_SPECIAL_REGS_NUMBER + tmp_regs_num + local_var_regs_num + arg_regs_num; MEM_DEFINE_LOCAL_ARRAY (regs, regs_num, ecma_value_t); vm_frame_ctx_t frame_ctx; frame_ctx.bytecode_header_p = header_p; frame_ctx.pos = (vm_instr_counter_t) (start_pos + 1); frame_ctx.lex_env_p = lex_env_p; frame_ctx.is_strict = is_strict; frame_ctx.is_eval_code = is_eval_code; frame_ctx.is_call_in_direct_eval_form = false; frame_ctx.tmp_num_p = ecma_alloc_number (); vm_stack_add_frame (&frame_ctx.stack_frame, regs, regs_num, local_var_regs_num, arg_regs_num, arg_collection_p); vm_stack_frame_set_reg_value (&frame_ctx.stack_frame, VM_REG_SPECIAL_THIS_BINDING, ecma_copy_value (this_binding_value, false)); vm_frame_ctx_t *prev_context_p = vm_top_context_p; vm_top_context_p = &frame_ctx; #ifdef MEM_STATS interp_mem_stats_context_enter (&frame_ctx, start_pos); #endif /* MEM_STATS */ completion = vm_loop (&frame_ctx, NULL); JERRY_ASSERT (ecma_is_completion_value_throw (completion) || ecma_is_completion_value_return (completion)); vm_top_context_p = prev_context_p; vm_stack_free_frame (&frame_ctx.stack_frame); ecma_dealloc_number (frame_ctx.tmp_num_p); #ifdef MEM_STATS interp_mem_stats_context_exit (&frame_ctx, start_pos); #endif /* MEM_STATS */ MEM_FINALIZE_LOCAL_ARRAY (regs); return completion; } /* vm_run_from_pos */