Implement Proxy object [[Set]] internal method (#3605)

The algorithm is based on ECMA-262 v6, 9.5.9

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2020-03-14 20:06:27 +01:00
committed by GitHub
parent 47d85a12e2
commit ccca998f43
2 changed files with 218 additions and 9 deletions
+115 -7
View File
@@ -12,20 +12,128 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: Update these tests when the internal routine has been implemented
// Copyright 2015 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 target = {};
var handler = { set (target) {
throw 42;
}};
// test basic funcionality
function Monster() {
this.eyeCount = 4;
}
var handler = {
set(obj, prop, value) {
if (prop == 'eyeCount') {
obj[prop] = value;
} else {
obj[prop] = "foo";
}
}
};
var monster = new Monster();
var proxy = new Proxy(monster, handler);
proxy.eyeCount = 1;
proxy.foo = "bar";
assert(monster.eyeCount === 1);
assert(monster.foo === "foo");
var target = { foo: "foo"};
var handler = {
set: function(obj, prop, value) {
obj[prop] = "";
}
};
var proxy = new Proxy(target, handler);
proxy.foo = 12;
assert(target.foo === "");
var properties = ["bla", "0", 1, Symbol(), {[Symbol.toPrimitive]() {return "a"}}];
var target = {};
var handler = {};
var proxy = new Proxy(target, handler);
// test when property does not exist on target
for (var p of properties) {
proxy.p = 42;
assert(target.p === 42);
}
// test when property exists as writable data on target
for (var p of properties) {
Object.defineProperty(target, p, {
writable: true,
value: 24
});
proxy.p = 42;
assert(target.p === 42);
}
// test when target is a proxy
var target = {};
var handler = {
set(obj, prop, value) {
obj[prop] = value;
}
};
var proxy = new Proxy(target, handler);
var proxy2 = new Proxy(proxy, handler);
proxy2.prop = "foo";
assert(target.prop === "foo");
// test when handler is null
var target = {};
var handler = {
set(obj, prop, value) {
obj[prop] = value;
}
};
var revocable = Proxy.revocable (target, {});
var proxy = revocable.proxy;
revocable.revoke();
try {
// vm_op_set_value
proxy.a = 5
proxy.prop = 42;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when invariants gets violated
var target = {};
var handler = { set() {return 42} };
var proxy = new Proxy(target, handler);
Object.defineProperty(target, "key", {
configurable: false,
writable: false,
value: 0
});
try {
proxy.key = 600;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
Object.defineProperty(target, "key2", {
configurable: false,
set: undefined
});
try {
proxy.key2 = 500;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}