Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: src/proxy.js

Issue 7628021: Make function proxies work as constructors. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/ia32/builtins-ia32.cc ('k') | src/runtime.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/proxy.js
diff --git a/src/proxy.js b/src/proxy.js
index 123e5671ac90727328b6bd5bd71d4fdbac068006..8162a3903db00e625b688b1ffb16e862634eb9b5 100644
--- a/src/proxy.js
+++ b/src/proxy.js
@@ -42,13 +42,18 @@ $Proxy.createFunction = function(handler, callTrap, constructTrap) {
if (!IS_FUNCTION(callTrap))
throw MakeTypeError("trap_function_expected", ["createFunction", "call"])
if (IS_UNDEFINED(constructTrap)) {
- constructTrap = callTrap
- } else if (!IS_FUNCTION(constructTrap)) {
+ var construct = DerivedConstructTrap(callTrap)
+ } else if (IS_FUNCTION(constructTrap)) {
+ construct = function() {
+ // Make sure the trap gets undefined as this.
+ return $Function.prototype.apply.call(constructTrap, void 0, arguments)
+ }
+ } else {
throw MakeTypeError("trap_function_expected",
["createFunction", "construct"])
}
return %CreateJSFunctionProxy(
- handler, callTrap, constructTrap, $Function.prototype)
+ handler, callTrap, construct, $Function.prototype)
}
@@ -57,13 +62,22 @@ $Proxy.createFunction = function(handler, callTrap, constructTrap) {
// Builtins
////////////////////////////////////////////////////////////////////////////////
+function DerivedConstructTrap(callTrap) {
+ return function() {
+ var proto = this.prototype
+ if (!IS_SPEC_OBJECT(proto)) proto = $Object.prototype
+ var obj = new $Object()
+ obj.__proto__ = proto
+ var result = $Function.prototype.apply.call(callTrap, obj, arguments)
+ return IS_SPEC_OBJECT(result) ? result : obj
+ }
+}
+
function DelegateCallAndConstruct(callTrap, constructTrap) {
return function() {
if (%_IsConstructCall()) {
- // return constructTrap.apply(this, arguments)
return $Function.prototype.apply.call(constructTrap, this, arguments)
} else {
- // return callTrap.apply(this, arguments)
return $Function.prototype.apply.call(callTrap, this, arguments)
}
}
« no previous file with comments | « src/ia32/builtins-ia32.cc ('k') | src/runtime.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698