Skip to content

Commit a838461

Browse files
kkristofgalpeter
authored andcommitted
Implement Object.isExtensible and Object.preventExtensions function
JerryScript-DCO-1.0-Signed-off-by: Kristof Kosztyo [email protected]
1 parent 2a8c928 commit a838461

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,24 @@ ecma_builtin_object_object_freeze (ecma_value_t this_arg, /**< 'this' argument *
192192
* Returned value must be freed with ecma_free_completion_value.
193193
*/
194194
static ecma_completion_value_t
195-
ecma_builtin_object_object_prevent_extensions (ecma_value_t this_arg, /**< 'this' argument */
195+
ecma_builtin_object_object_prevent_extensions (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
196196
ecma_value_t arg) /**< routine's argument */
197197
{
198-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
198+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
199+
200+
if (!ecma_is_value_object (arg))
201+
{
202+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
203+
}
204+
else
205+
{
206+
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
207+
ecma_set_object_extensible (obj_p, false);
208+
209+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (arg, true));
210+
}
211+
212+
return ret_value;
199213
} /* ecma_builtin_object_object_prevent_extensions */
200214

201215
/**
@@ -240,10 +254,27 @@ ecma_builtin_object_object_is_frozen (ecma_value_t this_arg, /**< 'this' argumen
240254
* Returned value must be freed with ecma_free_completion_value.
241255
*/
242256
static ecma_completion_value_t
243-
ecma_builtin_object_object_is_extensible (ecma_value_t this_arg, /**< 'this' argument */
257+
ecma_builtin_object_object_is_extensible (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
244258
ecma_value_t arg) /**< routine's argument */
245259
{
246-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
260+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
261+
262+
if (!ecma_is_value_object (arg))
263+
{
264+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
265+
}
266+
else
267+
{
268+
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
269+
270+
bool extensible = ecma_get_object_extensible (obj_p);
271+
272+
ret_value = ecma_make_simple_completion_value (extensible
273+
? ECMA_SIMPLE_VALUE_TRUE
274+
: ECMA_SIMPLE_VALUE_FALSE);
275+
}
276+
277+
return ret_value;
247278
} /* ecma_builtin_object_object_is_extensible */
248279

249280
/**

tests/jerry/object_is_extensible.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// New objects are extensible.
17+
var empty = {};
18+
assert (Object.isExtensible(empty) === true);
19+
20+
// ...but that can be changed.
21+
Object.preventExtensions(empty);
22+
assert(Object.isExtensible(empty) === false);
23+
24+
// Call on undefined should throw TypeError.
25+
try
26+
{
27+
Object.isExtensible(undefined);
28+
assert (false);
29+
} catch (e) {
30+
assert (e instanceof TypeError);
31+
}
32+
33+
try
34+
{
35+
Object.preventExtensions(undefined);
36+
assert (false);
37+
} catch (e) {
38+
assert (e instanceof TypeError);
39+
}
40+
41+
// The functions below are unimplemented.
42+
// Sealed objects are by definition non-extensible.
43+
// var sealed = Object.seal({});
44+
// assert(Object.isExtensible(sealed) === false);
45+
46+
// Frozen objects are also by definition non-extensible.
47+
// var frozen = Object.freeze({});
48+
// assert(Object.isExtensible(frozen) === false);

0 commit comments

Comments
 (0)