/* 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. */ #include "ecma-alloc.h" #include "ecma-builtins.h" #include "ecma-conversion.h" #include "ecma-exceptions.h" #include "ecma-function-object.h" #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" #include "ecma-lex-env.h" #include "ecma-objects.h" #include "ecma-try-catch-macro.h" #include "opcodes.h" #include "vm-defines.h" /** \addtogroup vm Virtual machine * @{ * * \addtogroup vm_opcodes Opcodes * @{ */ /** * 'Variable declaration' opcode handler. * * See also: ECMA-262 v5, 10.5 - Declaration binding instantiation (block 8). * * @return ecma value * Returned value is simple and so need not be freed. * However, ecma_free_value may be called for it, but it is a no-op. */ ecma_value_t vm_var_decl (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */ ecma_string_t *var_name_str_p) /**< variable name */ { if (!ecma_op_has_binding (frame_ctx_p->lex_env_p, var_name_str_p)) { const bool is_configurable_bindings = frame_ctx_p->is_eval_code; ecma_value_t completion_value = ecma_op_create_mutable_binding (frame_ctx_p->lex_env_p, var_name_str_p, is_configurable_bindings); JERRY_ASSERT (ecma_is_value_empty (completion_value)); /* Skipping SetMutableBinding as we have already checked that there were not * any binding with specified name in current lexical environment * and CreateMutableBinding sets the created binding's value to undefined */ JERRY_ASSERT (ecma_is_value_undefined (ecma_op_get_binding_value (frame_ctx_p->lex_env_p, var_name_str_p, vm_is_strict_mode ()))); } return ECMA_VALUE_EMPTY; } /* vm_var_decl */ /** * 'typeof' opcode handler. * * See also: ECMA-262 v5, 11.4.3 * * @return ecma value * Returned value must be freed with ecma_free_value */ ecma_value_t opfunc_typeof (ecma_value_t left_value) /**< left value */ { return ecma_make_magic_string_value (ecma_get_typeof_lit_id (left_value)); } /* opfunc_typeof */ /** * Update getter or setter for object literals. */ void opfunc_set_accessor (bool is_getter, /**< is getter accessor */ ecma_value_t object, /**< object value */ ecma_string_t *accessor_name_p, /**< accessor name */ ecma_value_t accessor) /**< accessor value */ { ecma_object_t *object_p = ecma_get_object_from_value (object); ecma_property_t *property_p = ecma_find_named_property (object_p, accessor_name_p); if (property_p != NULL && ECMA_PROPERTY_GET_TYPE (*property_p) != ECMA_PROPERTY_TYPE_NAMEDACCESSOR) { ecma_delete_property (object_p, ECMA_PROPERTY_VALUE_PTR (property_p)); property_p = NULL; } if (property_p == NULL) { ecma_object_t *getter_func_p = NULL; ecma_object_t *setter_func_p = NULL; if (is_getter) { getter_func_p = ecma_get_object_from_value (accessor); } else { setter_func_p = ecma_get_object_from_value (accessor); } ecma_create_named_accessor_property (object_p, accessor_name_p, getter_func_p, setter_func_p, ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE, NULL); } else if (is_getter) { ecma_object_t *getter_func_p = ecma_get_object_from_value (accessor); ecma_set_named_accessor_property_getter (object_p, ECMA_PROPERTY_VALUE_PTR (property_p), getter_func_p); } else { ecma_object_t *setter_func_p = ecma_get_object_from_value (accessor); ecma_set_named_accessor_property_setter (object_p, ECMA_PROPERTY_VALUE_PTR (property_p), setter_func_p); } } /* opfunc_set_accessor */ /** * Deletes an object property. * * @return ecma value * Returned value must be freed with ecma_free_value */ ecma_value_t vm_op_delete_prop (ecma_value_t object, /**< base object */ ecma_value_t property, /**< property name */ bool is_strict) /**< strict mode */ { if (ecma_is_value_undefined (object)) { return ECMA_VALUE_TRUE; } ecma_value_t check_coercible = ecma_op_check_object_coercible (object); if (ECMA_IS_VALUE_ERROR (check_coercible)) { return check_coercible; } JERRY_ASSERT (check_coercible == ECMA_VALUE_EMPTY); ecma_string_t *name_string_p = ecma_op_to_prop_name (property); if (JERRY_UNLIKELY (name_string_p == NULL)) { return ECMA_VALUE_ERROR; } ecma_value_t obj_value = ecma_op_to_object (object); /* The ecma_op_check_object_coercible call already checked the op_to_object error cases. */ JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (obj_value)); JERRY_ASSERT (ecma_is_value_object (obj_value)); ecma_object_t *obj_p = ecma_get_object_from_value (obj_value); JERRY_ASSERT (!ecma_is_lexical_environment (obj_p)); ecma_value_t delete_op_ret = ecma_op_object_delete (obj_p, name_string_p, is_strict); JERRY_ASSERT (ecma_is_value_boolean (delete_op_ret) || (is_strict == true && ECMA_IS_VALUE_ERROR (delete_op_ret))); ecma_deref_object (obj_p); ecma_deref_ecma_string (name_string_p); return delete_op_ret; } /* vm_op_delete_prop */ /** * Deletes a variable. * * @return ecma value * Returned value must be freed with ecma_free_value */ ecma_value_t vm_op_delete_var (ecma_value_t name_literal, /**< name literal */ ecma_object_t *lex_env_p) /**< lexical environment */ { ecma_value_t completion_value = ECMA_VALUE_EMPTY; ecma_string_t *var_name_str_p = ecma_get_string_from_value (name_literal); ecma_object_t *ref_base_lex_env_p = ecma_op_resolve_reference_base (lex_env_p, var_name_str_p); if (ref_base_lex_env_p == NULL) { completion_value = ECMA_VALUE_TRUE; } else { JERRY_ASSERT (ecma_is_lexical_environment (ref_base_lex_env_p)); completion_value = ecma_op_delete_binding (ref_base_lex_env_p, var_name_str_p); } return completion_value; } /* vm_op_delete_var */ /** * 'for-in' opcode handler * * See also: * ECMA-262 v5, 12.6.4 * * @return chain list of property names */ ecma_collection_chunk_t * opfunc_for_in (ecma_value_t left_value, /**< left value */ ecma_value_t *result_obj_p) /**< expression object */ { ecma_collection_chunk_t *prop_names_p = NULL; /* 3. */ if (ecma_is_value_undefined (left_value) || ecma_is_value_null (left_value)) { return prop_names_p; } /* 4. */ ecma_value_t obj_expr_value = ecma_op_to_object (left_value); /* ecma_op_to_object will only raise error on null/undefined values but those are handled above. */ JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (obj_expr_value)); ecma_object_t *obj_p = ecma_get_object_from_value (obj_expr_value); ecma_collection_header_t *prop_names_coll_p; prop_names_coll_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_ENUMERABLE_PROTOTYPE); if (prop_names_coll_p->item_count != 0) { prop_names_p = ECMA_GET_POINTER (ecma_collection_chunk_t, prop_names_coll_p->first_chunk_cp); ecma_ref_object (obj_p); *result_obj_p = ecma_make_object_value (obj_p); } /* Note: We release the collection header here, and return the chunk list of the collection, which will * be handled and released by the vm loop. */ jmem_pools_free (prop_names_coll_p, sizeof (ecma_collection_header_t)); ecma_free_value (obj_expr_value); return prop_names_p; } /* opfunc_for_in */ /** * @} * @} */