Proxy: getPrototypeOf

This commit is contained in:
Leonardo Balter 2015-06-02 17:34:51 -04:00
parent c3e71dcb0b
commit d012f5c680
13 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,31 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Trap is called with handler as context and target as the first parameter.
info: >
[[GetPrototypeOf]] ( )
...
8. Let handlerProto be Call(trap, handler, «target»).
...
---*/
var _handler, _target;
var target = {};
var handler = {
getPrototypeOf: function(t) {
_handler = this;
_target = t;
return {};
}
};
var p = new Proxy(target, handler);
Object.getPrototypeOf(p);
assert.sameValue(_handler, handler);
assert.sameValue(_target, target);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Return trap result if it's an Object and target is extensible.
info: >
[[GetPrototypeOf]] ( )
...
1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
...
4. Let target be the value of the [[ProxyTarget]] internal slot of O.
5. Let trap be GetMethod(handler, "getPrototypeOf").
...
8. Let handlerProto be Call(trap, handler, «target»).
...
11. Let extensibleTarget be IsExtensible(target).
12. ReturnIfAbrupt(extensibleTarget).
13. If extensibleTarget is true, return handlerProto.
...
---*/
var prot = { foo: 1 };
var p = new Proxy({}, {
getPrototypeOf: function() {
return prot;
}
});
assert.sameValue(Object.getPrototypeOf(p), prot);

View File

@ -0,0 +1,40 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throws a TypeError if the target is not extensible and the trap result is
not the same as the target.[[GetPrototypeOf]] result.
info: >
[[GetPrototypeOf]] ( )
...
1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
...
4. Let target be the value of the [[ProxyTarget]] internal slot of O.
5. Let trap be GetMethod(handler, "getPrototypeOf").
...
8. Let handlerProto be Call(trap, handler, «target»).
...
11. Let extensibleTarget be IsExtensible(target).
...
14. Let targetProto be target.[[GetPrototypeOf]]().
15. ReturnIfAbrupt(targetProto).
16. If SameValue(handlerProto, targetProto) is false, throw a TypeError
exception.
...
---*/
var target = Object.create({ foo: 1 });
var p = new Proxy(target, {
getPrototypeOf: function() {
return {};
}
});
Object.preventExtensions(target);
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Return trap result is target is not extensible, but trap result has the same
value as target.[[GetPrototypeOf]] result.
info: >
[[GetPrototypeOf]] ( )
...
1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
...
4. Let target be the value of the [[ProxyTarget]] internal slot of O.
5. Let trap be GetMethod(handler, "getPrototypeOf").
...
8. Let handlerProto be Call(trap, handler, «target»).
...
11. Let extensibleTarget be IsExtensible(target).
...
14. Let targetProto be target.[[GetPrototypeOf]]().
...
17. Return handlerProto.
---*/
var target = Object.create(Array.prototype);
var p = new Proxy(target, {
getPrototypeOf: function() {
return Array.prototype;
}
});
Object.preventExtensions(target);
assert.sameValue(Object.getPrototypeOf(p), Array.prototype);

View File

@ -0,0 +1,15 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throws a TypeError exception if handler is null.
---*/
var p = Proxy.revocable({}, {});
p.revoke();
assert.throws(TypeError, function() {
Object.getPrototypeOf(p.proxy);
});

View File

@ -0,0 +1,21 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Trap returns abrupt.
info: >
8. Let handlerProto be Call(trap, handler, «target»).
9. ReturnIfAbrupt(handlerProto).
includes: [Test262Error.js]
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,15 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throws if trap is not callable.
---*/
var p = new Proxy({}, {
getPrototypeOf: {}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,12 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Return target.[[GetPrototypeOf]]() if trap is undefined.
---*/
var target = Object.create(Array.prototype);
var p = new Proxy(target, {});
assert.sameValue(Object.getPrototypeOf(p), Array.prototype);

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throw a TypeError exception if trap result is false.
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
return false;
}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throw a TypeError exception if trap result is a Number.
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
return 0;
}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
throw a TypeError exception if trap result is a String.
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
return "";
}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,18 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throw a TypeError exception if trap result is a Symbol.
features: [Symbol]
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
return Symbol();
}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.1
description: >
Throw a TypeError exception if trap result is undefined.
---*/
var p = new Proxy({}, {
getPrototypeOf: function() {
return undefined;
}
});
assert.throws(TypeError, function() {
Object.getPrototypeOf(p);
});