Implement new Reflect methods (#3549)

This patch implements the get, set, has, deleteProperty, ownKeys, and construct methods of the Reflect objects. The Reflect.construct method is missing the new_target support because ecma_op_function_construct doesn't support it yet.

JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu
This commit is contained in:
Daniella Barsony
2020-02-24 11:02:06 +01:00
committed by GitHub
parent 68909fc5de
commit 73daeb19c9
14 changed files with 737 additions and 17 deletions
+80
View File
@@ -0,0 +1,80 @@
// 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.
try {
Reflect.construct ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.construct (Date);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var d = Reflect.construct (Date, [1776, 6, 4]);
assert (d instanceof Date);
assert (d.getFullYear () === 1776);
function func1 (a, b, c) {
this.sum = a + b + c;
}
var args = [1, 2, 3];
var object1 = new func1 (...args);
var object2 = Reflect.construct (func1, args);
assert (object2.sum === 6);
assert (object1.sum === 6);
function CatClass () {
this.name = 'Cat';
}
function DogClass () {
this.name = 'Dog';
}
var obj1 = Reflect.construct (CatClass, args, DogClass);
assert (obj1.name === 'Cat');
// assert (!(ob1 instanceof CatClass)); This is true because new target is not supported yet
// assert (obj1 instanceof DogClass); This is false because new target is not supported yet
try {
Reflect.construct (func1, 5, 5);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.construct (5, 5);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
function func2 () {
throw 5;
}
try {
Reflect.construct (func2, {});
assert (false);
} catch (e) {
assert (e === 5);
}
@@ -0,0 +1,61 @@
// 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.
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var object = {
property: 'Batcat'
};
Reflect.deleteProperty (object, 'property');
assert (object.property === undefined);
assert (2 === Reflect.deleteProperty.length);
try {
Reflect.deleteProperty ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.deleteProperty (42, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.deleteProperty (null, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var target = {bat: 42};
var a = { [Symbol.toPrimitive]: function() { return 'bat' } };
var b = { [Symbol.toPrimitive]: function() { throw 'cat' } };
assert (Reflect.deleteProperty (target, a));
try {
Reflect.deleteProperty (target, b);
assert (false);
} catch (e) {
assert (e === 'cat');
}
+55
View File
@@ -0,0 +1,55 @@
// 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.
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var object1 = {
x: "Bat",
y: "Cat"
};
assert (Reflect.get (object1, 'y') === "Cat");
try {
Reflect.get ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
assert (Reflect.get ({}) === undefined);
var o = {x: 10};
function foo () {
try {
return Reflect.get (o, "x");
} catch (e) {
return 1;
}
}
assert (10 === foo());
var c = {};
function foo2 (a) {
try {
return Reflect.get (c, a);
} catch (e) {
return 1;
}
}
assert (1 === foo2 ({[Symbol.toPrimitive]() { throw new Error(); }}));
+64
View File
@@ -0,0 +1,64 @@
// 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.
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
assert (Reflect.has ({x: 0}, 'x') === true);
assert (Reflect.has ({x: 0}, 'y') === false);
assert (Reflect.has ({x: 0}, 'toString') === true);
var object = {
prop: 'Apple'
};
assert (Reflect.has (object, 'prop') === true);
assert (2 === Reflect.has.length);
try {
Reflect.has ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.has (42, 'batcat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.has (null, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var target = {bat: 42};
var a = { [Symbol.toPrimitive]: function () { return 'bat' } };
var b = { [Symbol.toPrimitive]: function () { throw 'cat' } };
assert (Reflect.has (target, a) === true);
try {
Reflect.has (target, b);
assert (false);
} catch (e) {
assert (e === 'cat');
}
+144
View File
@@ -0,0 +1,144 @@
// 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.
// Copyright 2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
var object1 = {
property1: 42,
property2: 13
};
var array1 = [];
assert (Reflect.ownKeys (object1)[0] === "property1");
assert (Reflect.ownKeys (object1)[1] === "property2");
assert (Reflect.ownKeys (object1).length === 2);
var obj = { a: 1, b: 2};
var keys = Reflect.ownKeys (obj);
assert (2 === keys.length);
assert ("a" === keys[0]);
assert ("b" === keys[1]);
var obj = { a: function(){}, b: function(){} };
var keys = Reflect.ownKeys (obj);
assert (2 === keys.length);
assert ("a" === keys[0]);
assert ("b" === keys[1]);
// Check slow case
var obj = { a: 1, b: 2, c: 3 };
delete obj.b;
var keys = Reflect.ownKeys (obj)
assert (2 === keys.length);
assert ("a" === keys[0]);
assert ("c" === keys[1]);
// Check that non-enumerable properties are being returned.
var keys = Reflect.ownKeys ([1, 2]);
assert (3 === keys.length);
assert ("0" === keys[0]);
assert ("1" === keys[1]);
assert ("string" === typeof keys[0]);
assert ("string" === typeof keys[1]);
assert ("length" === keys[2]);
// Check that no proto properties are returned.
var obj = { foo: "foo" };
var obj2 = { bar: "bar" };
Object.setPrototypeOf (obj, obj2)
keys = Reflect.ownKeys (obj);
assert (1 === keys.length);
assert ("foo" === keys[0]);
// Check that getter properties are returned.
var obj = {};
Object.defineProperty (obj, "getter", function() {})
//obj.__defineGetter__("getter", function() {});
keys = Reflect.ownKeys (obj);
assert (1 === keys.length);
assert ("getter" === keys[0]);
// Check that implementation does not access Array.prototype.
var savedConcat = Array.prototype.concat;
Array.prototype.concat = function() { return []; }
keys = Reflect.ownKeys ({0: 'foo', bar: 'baz'});
assert (2 === keys.length);
assert ('0' === keys[0]);
assert ('bar'=== keys[1]);
assert (Array.prototype === Object.getPrototypeOf (keys))
Array.prototype.concat = savedConcat;
try {
Reflect.ownKeys (4);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.ownKeys("cica");
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.ownKeys(true);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
assert (Reflect.ownKeys (Object (4)) !== [])
assert (Reflect.ownKeys (Object ("foo")) !== ["0", "1", "2", "length"])
assert (Reflect.ownKeys (Object (4)) !== []);
assert (Reflect.ownKeys (Object ("foo")) !== [0, 1, 2, "length"]);
assert (Reflect.ownKeys (Object (true)) !== []);
var id = Symbol("my kitten");
var user = {
name: "Bob",
age: 30,
[id]: "is batcat"
}
assert (Reflect.ownKeys (user)[0] === "name");
assert (Reflect.ownKeys (user)[1] === "age");
assert (Reflect.ownKeys (user)[2] === id);
+68
View File
@@ -0,0 +1,68 @@
// 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.
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var object = {};
Reflect.set (object, 'property', 'batcat');
assert (object.property === 'batcat');
var array = ['cat', 'cat', 'cat'];
Reflect.set (array, 2, 'bat');
assert (array[2] === 'bat');
assert (3 === Reflect.set.length);
try {
Reflect.set ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.set (42, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Reflect.set (null, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var target = {};
var a = { [Symbol.toPrimitive]: function () { return 'bat' } };
var b = { [Symbol.toPrimitive]: function () { throw 'cat' } };
assert (Reflect.set (target, a, 42) === true);
assert (42 === target.bat);
try {
Reflect.set (null, 'bat');
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var y = [];
Object.defineProperty (y, 0, {value: 42, configurable: false});
assert (Reflect.set (y, 'length', 0) === false);
assert (Reflect.set (y, 'length', 2) === true);