Proxy: construct

This commit is contained in:
Leonardo Balter 2015-06-02 19:16:46 -04:00
parent 450b830026
commit 2c4077c17a
12 changed files with 279 additions and 0 deletions

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.14
description: >
trap is called with handler object as its context, and parameters are:
target, an array list with the called arguments and the new target, and the
constructor new.target.
info: >
[[Construct]] ( argumentsList, newTarget)
9. Let newObj be Call(trap, handler, «target, argArray, newTarget »).
---*/
var _target, _handler, _args, _P;
function Target() {}
var handler = {
construct: function(t, args, newTarget) {
_handler = this;
_target = t;
_args = args;
_P = newTarget;
return new t(args[0], args[1]);
}
};
var P = new Proxy(Target, handler);
new P(1, 2);
assert.sameValue(_handler, handler, "trap context is the handler object");
assert.sameValue(_target, Target, "first parameter is the target object");
assert.sameValue(_args.length, 2, "arguments list contains all call arguments");
assert.sameValue(_args[0], 1, "arguments list has first call argument");
assert.sameValue(_args[1], 2, "arguments list has second call argument");
assert.sameValue(_P, P, "constructor is sent as the third parameter");

View File

@ -0,0 +1,23 @@
// 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.14
description: >
Return the result from the trap method.
info: >
[[Construct]] ( argumentsList, newTarget)
12. Return newObj
---*/
function Target(a, b) {
this.sum = a + b;
};
var handler = {
construct: function(t, c, args) {
return { sum: 42 };
}
};
var P = new Proxy(Target, handler);
assert.sameValue((new P(1, 2)).sum, 42);

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.14
description: >
[[Construct]] ( argumentsList, newTarget)
2. If handler is null, throw a TypeError exception.
---*/
var p = Proxy.revocable(function() {}, {});
p.revoke();
assert.throws(TypeError, function() {
new p.proxy();
});

View File

@ -0,0 +1,24 @@
// 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.14
description: >
Return abrupt from constructor call.
info: >
[[Construct]] ( argumentsList, newTarget)
9. Let newObj be Call(trap, handler, «target, argArray, newTarget »).
10. ReturnIfAbrupt(newObj).
includes: [Test262Error.js]
---*/
function Target() {}
var P = new Proxy(Target, {
construct: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
new P();
});

View File

@ -0,0 +1,24 @@
// 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.14
description: >
Throws a TypeError if trap result is not an Object: Boolean
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return true;
}
});
assert.throws(TypeError, function() {
new P();
});

View File

@ -0,0 +1,24 @@
// 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.14
description: >
Throws a TypeError if trap result is not an Object: Number
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return 0;
}
});
assert.throws(TypeError, function() {
new P();
});

View File

@ -0,0 +1,24 @@
// 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.14
description: >
Throws a TypeError if trap result is not an Object: String
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return "";
}
});
assert.throws(TypeError, function() {
new P();
});

View File

@ -0,0 +1,25 @@
// 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.14
description: >
Throws a TypeError if trap result is not an Object: Symbol
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
features: [Symbol]
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return Symbol();
}
});
assert.throws(TypeError, function() {
new P();
});

View File

@ -0,0 +1,24 @@
// 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.14
description: >
Throws a TypeError if trap result is not an Object: undefined
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return undefined;
}
});
assert.throws(TypeError, function() {
new P();
});

View File

@ -0,0 +1,16 @@
// 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.14
description: >
Throws if trap is not callable.
---*/
function Target() {}
var p = new Proxy(Target, {
construct: {}
});
assert.throws(TypeError, function() {
new p();
});

View File

@ -0,0 +1,19 @@
// 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.14
description: >
If trap is undefined, propagate the construct to the target object.
info: >
[[Construct]] ( argumentsList, newTarget)
7. If trap is undefined, then
b. Return Construct(target, argumentsList, newTarget).
---*/
function Target(arg) {
this.attr = arg;
}
var P = new Proxy(Target, {});
assert.sameValue((new P("foo")).attr, "foo");

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.14
description: >
If trap is undefined, propagate the construct to the target object.
info: >
[[Construct]] ( argumentsList, newTarget)
7. If trap is undefined, then
b. Return Construct(target, argumentsList, newTarget).
---*/
function Target(arg) {
this.attr = arg;
}
var P = new Proxy(Target, {
construct: undefined
});
assert.sameValue((new P("foo")).attr, "foo");