2011-04-15 12:31:03 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2011-04-15 12:31:03 +00:00
|
|
|
|
2012-02-20 13:48:24 +00:00
|
|
|
"use strict";
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// This file relies on the fact that the following declaration has been made
|
|
|
|
// in runtime.js:
|
|
|
|
// var $Object = global.Object;
|
2011-05-13 10:58:25 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
var $Proxy = new $Object();
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2011-05-13 10:58:25 +00:00
|
|
|
|
2012-10-25 14:53:26 +00:00
|
|
|
function ProxyCreate(handler, proto) {
|
2011-06-03 10:15:49 +00:00
|
|
|
if (!IS_SPEC_OBJECT(handler))
|
|
|
|
throw MakeTypeError("handler_non_object", ["create"])
|
2011-10-24 16:25:30 +00:00
|
|
|
if (IS_UNDEFINED(proto))
|
|
|
|
proto = null
|
2013-10-17 10:02:45 +00:00
|
|
|
else if (!(IS_SPEC_OBJECT(proto) || IS_NULL(proto)))
|
2011-10-24 16:25:30 +00:00
|
|
|
throw MakeTypeError("proto_non_object", ["create"])
|
2011-05-13 10:58:25 +00:00
|
|
|
return %CreateJSProxy(handler, proto)
|
|
|
|
}
|
2011-05-18 14:00:34 +00:00
|
|
|
|
2012-10-25 14:53:26 +00:00
|
|
|
function ProxyCreateFunction(handler, callTrap, constructTrap) {
|
2011-09-13 11:42:57 +00:00
|
|
|
if (!IS_SPEC_OBJECT(handler))
|
|
|
|
throw MakeTypeError("handler_non_object", ["create"])
|
|
|
|
if (!IS_SPEC_FUNCTION(callTrap))
|
|
|
|
throw MakeTypeError("trap_function_expected", ["createFunction", "call"])
|
|
|
|
if (IS_UNDEFINED(constructTrap)) {
|
2011-10-24 16:25:30 +00:00
|
|
|
constructTrap = DerivedConstructTrap(callTrap)
|
2011-09-16 12:26:29 +00:00
|
|
|
} else if (IS_SPEC_FUNCTION(constructTrap)) {
|
2011-10-24 16:25:30 +00:00
|
|
|
// Make sure the trap receives 'undefined' as this.
|
|
|
|
var construct = constructTrap
|
|
|
|
constructTrap = function() {
|
2013-10-17 10:02:45 +00:00
|
|
|
return %Apply(construct, UNDEFINED, arguments, 0, %_ArgumentsLength());
|
2011-09-16 12:26:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-09-13 11:42:57 +00:00
|
|
|
throw MakeTypeError("trap_function_expected",
|
|
|
|
["createFunction", "construct"])
|
|
|
|
}
|
|
|
|
return %CreateJSFunctionProxy(
|
2011-10-24 16:25:30 +00:00
|
|
|
handler, callTrap, constructTrap, $Function.prototype)
|
2011-09-13 11:42:57 +00:00
|
|
|
}
|
2011-05-18 14:00:34 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
function SetUpProxy() {
|
|
|
|
%CheckIsBootstrapping()
|
|
|
|
|
2014-01-17 11:08:24 +00:00
|
|
|
var global_receiver = %GlobalReceiver(global);
|
|
|
|
global_receiver.Proxy = $Proxy;
|
2013-04-11 12:15:25 +00:00
|
|
|
|
|
|
|
// Set up non-enumerable properties of the Proxy object.
|
|
|
|
InstallFunctions($Proxy, DONT_ENUM, [
|
|
|
|
"create", ProxyCreate,
|
|
|
|
"createFunction", ProxyCreateFunction
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
SetUpProxy();
|
2011-05-18 14:00:34 +00:00
|
|
|
|
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Proxy Builtins
|
2011-05-18 14:00:34 +00:00
|
|
|
|
2011-09-16 12:26:29 +00:00
|
|
|
function DerivedConstructTrap(callTrap) {
|
|
|
|
return function() {
|
|
|
|
var proto = this.prototype
|
|
|
|
if (!IS_SPEC_OBJECT(proto)) proto = $Object.prototype
|
2013-03-04 16:05:12 +00:00
|
|
|
var obj = { __proto__: proto };
|
2011-10-04 12:48:16 +00:00
|
|
|
var result = %Apply(callTrap, obj, arguments, 0, %_ArgumentsLength());
|
2011-09-16 12:26:29 +00:00
|
|
|
return IS_SPEC_OBJECT(result) ? result : obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:42:57 +00:00
|
|
|
function DelegateCallAndConstruct(callTrap, constructTrap) {
|
|
|
|
return function() {
|
|
|
|
return %Apply(%_IsConstructCall() ? constructTrap : callTrap,
|
|
|
|
this, arguments, 0, %_ArgumentsLength())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-18 14:00:34 +00:00
|
|
|
function DerivedGetTrap(receiver, name) {
|
|
|
|
var desc = this.getPropertyDescriptor(name)
|
Implement set trap for proxies, and revamp class hierarchy in preparation:
- Introduce a class JSReceiver, that is a common superclass of JSObject and
JSProxy. Use JSReceiver where appropriate (probably lots of places that we
still have to migrate, but we will find those later with proxy test suite).
- Move appropriate methods to JSReceiver class (SetProperty,
GetPropertyAttribute, Get/SetPrototype, Lookup, and so on).
- Introduce new JSFunctionProxy subclass of JSProxy. Currently only a stub.
- Overhaul enum InstanceType:
* Introduce FIRST/LAST_SPEC_OBJECT_TYPE that ranges over all types that
represent JS objects, and use that consistently to check language types.
* Rename FIRST/LAST_JS_OBJECT_TYPE and FIRST/LAST_FUNCTION_CLASS_TYPE
to FIRST/LAST_[NON]CALLABLE_SPEC_OBJECT_TYPE for clarity.
* Eliminate the overlap over JS_REGEXP_TYPE.
* Also replace FIRST_JS_OBJECT with FIRST_JS_RECEIVER, but only use it where
we exclusively talk about the internal representation type.
* Insert JS_PROXY and JS_FUNCTION_PROXY in the appropriate places.
- Fix all checks concerning classification, especially for functions, to
use the CALLABLE_SPEC_OBJECT range (that includes funciton proxies).
- Handle proxies in SetProperty (that was the easiest part :) ).
- A few simple test cases.
R=kmillikin@chromium.org
Review URL: http://codereview.chromium.org/6992072
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8126 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2011-05-31 16:38:40 +00:00
|
|
|
if (IS_UNDEFINED(desc)) { return desc }
|
2011-05-18 14:00:34 +00:00
|
|
|
if ('value' in desc) {
|
|
|
|
return desc.value
|
|
|
|
} else {
|
Implement set trap for proxies, and revamp class hierarchy in preparation:
- Introduce a class JSReceiver, that is a common superclass of JSObject and
JSProxy. Use JSReceiver where appropriate (probably lots of places that we
still have to migrate, but we will find those later with proxy test suite).
- Move appropriate methods to JSReceiver class (SetProperty,
GetPropertyAttribute, Get/SetPrototype, Lookup, and so on).
- Introduce new JSFunctionProxy subclass of JSProxy. Currently only a stub.
- Overhaul enum InstanceType:
* Introduce FIRST/LAST_SPEC_OBJECT_TYPE that ranges over all types that
represent JS objects, and use that consistently to check language types.
* Rename FIRST/LAST_JS_OBJECT_TYPE and FIRST/LAST_FUNCTION_CLASS_TYPE
to FIRST/LAST_[NON]CALLABLE_SPEC_OBJECT_TYPE for clarity.
* Eliminate the overlap over JS_REGEXP_TYPE.
* Also replace FIRST_JS_OBJECT with FIRST_JS_RECEIVER, but only use it where
we exclusively talk about the internal representation type.
* Insert JS_PROXY and JS_FUNCTION_PROXY in the appropriate places.
- Fix all checks concerning classification, especially for functions, to
use the CALLABLE_SPEC_OBJECT range (that includes funciton proxies).
- Handle proxies in SetProperty (that was the easiest part :) ).
- A few simple test cases.
R=kmillikin@chromium.org
Review URL: http://codereview.chromium.org/6992072
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8126 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2011-05-31 16:38:40 +00:00
|
|
|
if (IS_UNDEFINED(desc.get)) { return desc.get }
|
|
|
|
// The proposal says: desc.get.call(receiver)
|
|
|
|
return %_CallFunction(receiver, desc.get)
|
2011-05-18 14:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
Implement set trap for proxies, and revamp class hierarchy in preparation:
- Introduce a class JSReceiver, that is a common superclass of JSObject and
JSProxy. Use JSReceiver where appropriate (probably lots of places that we
still have to migrate, but we will find those later with proxy test suite).
- Move appropriate methods to JSReceiver class (SetProperty,
GetPropertyAttribute, Get/SetPrototype, Lookup, and so on).
- Introduce new JSFunctionProxy subclass of JSProxy. Currently only a stub.
- Overhaul enum InstanceType:
* Introduce FIRST/LAST_SPEC_OBJECT_TYPE that ranges over all types that
represent JS objects, and use that consistently to check language types.
* Rename FIRST/LAST_JS_OBJECT_TYPE and FIRST/LAST_FUNCTION_CLASS_TYPE
to FIRST/LAST_[NON]CALLABLE_SPEC_OBJECT_TYPE for clarity.
* Eliminate the overlap over JS_REGEXP_TYPE.
* Also replace FIRST_JS_OBJECT with FIRST_JS_RECEIVER, but only use it where
we exclusively talk about the internal representation type.
* Insert JS_PROXY and JS_FUNCTION_PROXY in the appropriate places.
- Fix all checks concerning classification, especially for functions, to
use the CALLABLE_SPEC_OBJECT range (that includes funciton proxies).
- Handle proxies in SetProperty (that was the easiest part :) ).
- A few simple test cases.
R=kmillikin@chromium.org
Review URL: http://codereview.chromium.org/6992072
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8126 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2011-05-31 16:38:40 +00:00
|
|
|
|
|
|
|
function DerivedSetTrap(receiver, name, val) {
|
|
|
|
var desc = this.getOwnPropertyDescriptor(name)
|
|
|
|
if (desc) {
|
|
|
|
if ('writable' in desc) {
|
|
|
|
if (desc.writable) {
|
|
|
|
desc.value = val
|
|
|
|
this.defineProperty(name, desc)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else { // accessor
|
|
|
|
if (desc.set) {
|
|
|
|
// The proposal says: desc.set.call(receiver, val)
|
|
|
|
%_CallFunction(receiver, val, desc.set)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
desc = this.getPropertyDescriptor(name)
|
|
|
|
if (desc) {
|
|
|
|
if ('writable' in desc) {
|
|
|
|
if (desc.writable) {
|
|
|
|
// fall through
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else { // accessor
|
|
|
|
if (desc.set) {
|
|
|
|
// The proposal says: desc.set.call(receiver, val)
|
|
|
|
%_CallFunction(receiver, val, desc.set)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.defineProperty(name, {
|
|
|
|
value: val,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true});
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-03 10:15:49 +00:00
|
|
|
|
|
|
|
function DerivedHasTrap(name) {
|
|
|
|
return !!this.getPropertyDescriptor(name)
|
|
|
|
}
|
2011-07-13 11:01:17 +00:00
|
|
|
|
2011-07-21 11:20:27 +00:00
|
|
|
function DerivedHasOwnTrap(name) {
|
|
|
|
return !!this.getOwnPropertyDescriptor(name)
|
|
|
|
}
|
|
|
|
|
2011-07-13 11:01:17 +00:00
|
|
|
function DerivedKeysTrap() {
|
|
|
|
var names = this.getOwnPropertyNames()
|
|
|
|
var enumerableNames = []
|
|
|
|
for (var i = 0, count = 0; i < names.length; ++i) {
|
|
|
|
var name = names[i]
|
2013-03-06 13:55:21 +00:00
|
|
|
if (IS_SYMBOL(name)) continue
|
2011-10-24 15:56:18 +00:00
|
|
|
var desc = this.getOwnPropertyDescriptor(TO_STRING_INLINE(name))
|
|
|
|
if (!IS_UNDEFINED(desc) && desc.enumerable) {
|
2011-07-13 11:01:17 +00:00
|
|
|
enumerableNames[count++] = names[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enumerableNames
|
|
|
|
}
|
2011-10-24 15:56:18 +00:00
|
|
|
|
|
|
|
function DerivedEnumerateTrap() {
|
|
|
|
var names = this.getPropertyNames()
|
|
|
|
var enumerableNames = []
|
|
|
|
for (var i = 0, count = 0; i < names.length; ++i) {
|
|
|
|
var name = names[i]
|
2013-03-06 13:55:21 +00:00
|
|
|
if (IS_SYMBOL(name)) continue
|
2011-10-24 15:56:18 +00:00
|
|
|
var desc = this.getPropertyDescriptor(TO_STRING_INLINE(name))
|
2013-07-19 14:07:23 +00:00
|
|
|
if (!IS_UNDEFINED(desc)) {
|
|
|
|
if (!desc.configurable) {
|
|
|
|
throw MakeTypeError("proxy_prop_not_configurable",
|
|
|
|
[this, "getPropertyDescriptor", name])
|
|
|
|
}
|
|
|
|
if (desc.enumerable) enumerableNames[count++] = names[i]
|
2011-10-24 15:56:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return enumerableNames
|
|
|
|
}
|
|
|
|
|
|
|
|
function ProxyEnumerate(proxy) {
|
|
|
|
var handler = %GetHandler(proxy)
|
|
|
|
if (IS_UNDEFINED(handler.enumerate)) {
|
|
|
|
return %Apply(DerivedEnumerateTrap, handler, [], 0, 0)
|
|
|
|
} else {
|
2013-03-06 13:55:21 +00:00
|
|
|
return ToNameArray(handler.enumerate(), "enumerate", false)
|
2011-10-24 15:56:18 +00:00
|
|
|
}
|
|
|
|
}
|