Skip to content

Commit eb97860

Browse files
galpeterdbatyai
authored andcommitted
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 [email protected]
1 parent 0f754ff commit eb97860

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

jerry-core/vm/opcodes-ecma-relational-equality.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ opfunc_instanceof (ecma_value_t left_value, /**< left value */
116116
/**
117117
* 'in' opcode handler.
118118
*
119-
* See also: ECMA-262 v5, 11.8.7
119+
* See also:
120+
* * ECMA-262 v5, 11.8.7
121+
* * ECAM-262 v6, 12.9.3
120122
*
121123
* @return ecma value
122124
* returned value must be freed with ecma_free_value.
@@ -130,28 +132,17 @@ opfunc_in (ecma_value_t left_value, /**< left value */
130132
return ecma_raise_type_error (ECMA_ERR_MSG ("Expected an object in 'in' check."));
131133
}
132134

133-
bool to_string = !ecma_is_value_string (left_value);
135+
ecma_string_t *property_name_p = ecma_op_to_prop_name (left_value);
134136

135-
if (to_string)
137+
if (JERRY_UNLIKELY (property_name_p == NULL))
136138
{
137-
left_value = ecma_op_to_string (left_value);
138-
139-
if (ECMA_IS_VALUE_ERROR (left_value))
140-
{
141-
return left_value;
142-
}
139+
return ECMA_VALUE_ERROR;
143140
}
144141

145-
ecma_string_t *left_value_prop_name_p = ecma_get_string_from_value (left_value);
146142
ecma_object_t *right_value_obj_p = ecma_get_object_from_value (right_value);
147-
148143
ecma_value_t result = ecma_make_boolean_value (ecma_op_object_has_property (right_value_obj_p,
149-
left_value_prop_name_p));
150-
151-
if (to_string)
152-
{
153-
ecma_free_value (left_value);
154-
}
144+
property_name_p));
145+
ecma_deref_ecma_string (property_name_p);
155146
return result;
156147
} /* opfunc_in */
157148

tests/jerry/es2015/symbol-in.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var test_symbol = Symbol ('Test');
17+
18+
var obj = {};
19+
20+
assert ((test_symbol in obj) === false);
21+
22+
obj[test_symbol] = 'value';
23+
24+
assert ((test_symbol in obj) === true);
25+
assert (obj[test_symbol] === 'value');
26+
27+
28+
var array = [];
29+
30+
assert ((test_symbol in array) === false);
31+
32+
array[test_symbol] = 'value';
33+
34+
assert ((test_symbol in array) === true);
35+
assert (array[test_symbol] === 'value');
36+
37+
38+
assert ((test_symbol in Symbol) === false);
39+
40+
try {
41+
test_symbol in test_symbol;
42+
assert (false);
43+
} catch (ex) {
44+
assert (ex instanceof TypeError);
45+
}
46+
47+
try {
48+
test_symbol in 3;
49+
assert (false);
50+
} catch (ex) {
51+
assert (ex instanceof TypeError);
52+
}
53+
54+
try {
55+
test_symbol in 'Test';
56+
assert (false);
57+
} catch (ex) {
58+
assert (ex instanceof TypeError);
59+
}

0 commit comments

Comments
 (0)