Add support for Symbols when using the 'in' operation (#3202)
The change adds support for the 'in' operation to handle Symbols. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
@@ -116,7 +116,9 @@ opfunc_instanceof (ecma_value_t left_value, /**< left value */
|
|||||||
/**
|
/**
|
||||||
* 'in' opcode handler.
|
* 'in' opcode handler.
|
||||||
*
|
*
|
||||||
* See also: ECMA-262 v5, 11.8.7
|
* See also:
|
||||||
|
* * ECMA-262 v5, 11.8.7
|
||||||
|
* * ECAM-262 v6, 12.9.3
|
||||||
*
|
*
|
||||||
* @return ecma value
|
* @return ecma value
|
||||||
* returned value must be freed with ecma_free_value.
|
* returned value must be freed with ecma_free_value.
|
||||||
@@ -130,28 +132,17 @@ opfunc_in (ecma_value_t left_value, /**< left value */
|
|||||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Expected an object in 'in' check."));
|
return ecma_raise_type_error (ECMA_ERR_MSG ("Expected an object in 'in' check."));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool to_string = !ecma_is_value_string (left_value);
|
ecma_string_t *property_name_p = ecma_op_to_prop_name (left_value);
|
||||||
|
|
||||||
if (to_string)
|
if (JERRY_UNLIKELY (property_name_p == NULL))
|
||||||
{
|
{
|
||||||
left_value = ecma_op_to_string (left_value);
|
return ECMA_VALUE_ERROR;
|
||||||
|
|
||||||
if (ECMA_IS_VALUE_ERROR (left_value))
|
|
||||||
{
|
|
||||||
return left_value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ecma_string_t *left_value_prop_name_p = ecma_get_string_from_value (left_value);
|
|
||||||
ecma_object_t *right_value_obj_p = ecma_get_object_from_value (right_value);
|
ecma_object_t *right_value_obj_p = ecma_get_object_from_value (right_value);
|
||||||
|
|
||||||
ecma_value_t result = ecma_make_boolean_value (ecma_op_object_has_property (right_value_obj_p,
|
ecma_value_t result = ecma_make_boolean_value (ecma_op_object_has_property (right_value_obj_p,
|
||||||
left_value_prop_name_p));
|
property_name_p));
|
||||||
|
ecma_deref_ecma_string (property_name_p);
|
||||||
if (to_string)
|
|
||||||
{
|
|
||||||
ecma_free_value (left_value);
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
} /* opfunc_in */
|
} /* opfunc_in */
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/* 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 test_symbol = Symbol ('Test');
|
||||||
|
|
||||||
|
var obj = {};
|
||||||
|
|
||||||
|
assert ((test_symbol in obj) === false);
|
||||||
|
|
||||||
|
obj[test_symbol] = 'value';
|
||||||
|
|
||||||
|
assert ((test_symbol in obj) === true);
|
||||||
|
assert (obj[test_symbol] === 'value');
|
||||||
|
|
||||||
|
|
||||||
|
var array = [];
|
||||||
|
|
||||||
|
assert ((test_symbol in array) === false);
|
||||||
|
|
||||||
|
array[test_symbol] = 'value';
|
||||||
|
|
||||||
|
assert ((test_symbol in array) === true);
|
||||||
|
assert (array[test_symbol] === 'value');
|
||||||
|
|
||||||
|
|
||||||
|
assert ((test_symbol in Symbol) === false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
test_symbol in test_symbol;
|
||||||
|
assert (false);
|
||||||
|
} catch (ex) {
|
||||||
|
assert (ex instanceof TypeError);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
test_symbol in 3;
|
||||||
|
assert (false);
|
||||||
|
} catch (ex) {
|
||||||
|
assert (ex instanceof TypeError);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
test_symbol in 'Test';
|
||||||
|
assert (false);
|
||||||
|
} catch (ex) {
|
||||||
|
assert (ex instanceof TypeError);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user