Bug 788172 - Make Proxy a function. r=ejpbruel

This commit is contained in:
Sankha Narayan Guria 2013-07-02 21:57:14 +05:30
Родитель 9b129014be
Коммит 90744864f2
16 изменённых файлов: 47 добавлений и 44 удалений

Просмотреть файл

@ -0,0 +1,14 @@
// Check superficial properties of the Proxy constructor.
var desc = Object.getOwnPropertyDescriptor(this, "Proxy");
assertEq(desc.configurable, true);
assertEq(desc.enumerable, false);
assertEq(desc.writable, true);
assertEq(desc.value, Proxy);
assertEq(typeof Proxy, "function");
assertEq(Object.getPrototypeOf(Proxy), Function.prototype);
assertEq(Proxy.length, 2);
// Proxy is a constructor but has no .prototype property.
assertEq(Proxy.hasOwnProperty("prototype"), false);

Просмотреть файл

@ -2,4 +2,4 @@
var target = function (x, y) {
return x + y;
}
assertEq(new Proxy(target, {})(2, 3), 5);
assertEq(Proxy(target, {})(2, 3), 5);

Просмотреть файл

@ -2,6 +2,6 @@
var target = function (x, y) {
this.foo = x + y;
}
var obj = new (new Proxy(target, {}))(2, 3);
var obj = new (Proxy(target, {}))(2, 3);
assertEq(obj.foo, 5);
assertEq(Object.getPrototypeOf(obj), target.prototype);

Просмотреть файл

@ -1,19 +1,23 @@
load(libdir + "asserts.js");
// Throw a TypeError if Proxy is called as a function
assertThrowsInstanceOf(function () { Proxy(); }, TypeError);
// Throw a TypeError if Proxy is called with less than two arguments
assertThrowsInstanceOf(function () { Proxy(); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy(); }, TypeError);
assertThrowsInstanceOf(function () { Proxy({}); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy({}); }, TypeError);
// Throw a TypeError if the first argument is not a non-null object
assertThrowsInstanceOf(function () { Proxy(0, {}); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy(0, {}); }, TypeError);
assertThrowsInstanceOf(function () { Proxy(null, {}); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy(null, {}); }, TypeError);
// Throw a TypeError if the second argument is not a non-null object
assertThrowsInstanceOf(function () { Proxy({}, 0); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy({}, 0); }, TypeError);
assertThrowsInstanceOf(function () { Proxy({}, null); }, TypeError);
assertThrowsInstanceOf(function () { new Proxy({}, null); }, TypeError);
// Result of the call should be an object
assertEq(typeof Proxy({}, {}), 'object');
assertEq(typeof new Proxy({}, {}), 'object');

Просмотреть файл

@ -1,6 +1,6 @@
// Forward to the target if the trap is not defined
var target = {};
Object.defineProperty(new Proxy(target, {}), 'foo', {
Object.defineProperty(Proxy(target, {}), 'foo', {
value: 'bar',
writable: true,
enumerable: false,

Просмотреть файл

@ -1,8 +1,8 @@
// Forward to the target if the trap is not defined
assertEq(new Proxy({
assertEq(Proxy({
foo: 'bar'
}, {}).foo, 'bar');
assertEq(new Proxy({
assertEq(Proxy({
foo: 'bar'
}, {})['foo'], 'bar');

Просмотреть файл

@ -6,7 +6,7 @@ Object.defineProperty(target, 'foo', {
enumerable: false,
configurable: true
});
var desc = Object.getOwnPropertyDescriptor(new Proxy(target, {}), 'foo');
var desc = Object.getOwnPropertyDescriptor(Proxy(target, {}), 'foo');
assertEq(desc.value, 'bar');
assertEq(desc.writable, true);
assertEq(desc.enumerable, false);
@ -20,4 +20,4 @@ Object.defineProperty(proto, 'foo', {
configurable: true
});
var target = Object.create(proto);
assertEq(Object.getOwnPropertyDescriptor(new Proxy(target, {}), 'foo'), undefined);
assertEq(Object.getOwnPropertyDescriptor(Proxy(target, {}), 'foo'), undefined);

Просмотреть файл

@ -1,5 +1,5 @@
// Forward to the target if the trap is not defined
var names = Object.getOwnPropertyNames(new Proxy(Object.create(Object.create(null, {
var names = Object.getOwnPropertyNames(Proxy(Object.create(Object.create(null, {
a: {
enumerable: true,
configurable: true

Просмотреть файл

@ -1,5 +1,5 @@
// Forward to the target if the trap is not defined
var proxy = new Proxy(Object.create(Object.create(null, {
var proxy = Proxy(Object.create(Object.create(null, {
'foo': {
configurable: true
}

Просмотреть файл

@ -1,5 +1,5 @@
// Forward to the target if the trap is not defined
var proxy = new Proxy(Object.create(Object.create(null, {
var proxy = Proxy(Object.create(Object.create(null, {
'foo': {
configurable: true
}

Просмотреть файл

@ -1,5 +1,5 @@
// Forward to the target if the trap is not defined
var names = Object.keys(new Proxy(Object.create(Object.create(null, {
var names = Object.keys(Proxy(Object.create(Object.create(null, {
a: {
enumerable: true,
configurable: true

Просмотреть файл

@ -2,5 +2,5 @@
var target = {
foo: 'bar'
};
new Proxy(target, {})['foo'] = 'baz';
Proxy(target, {})['foo'] = 'baz';
assertEq(target.foo, 'baz');

Просмотреть файл

@ -9,7 +9,7 @@ Object.defineProperty(target, 'foo', {
configurable: false
});
assertThrowsInstanceOf(function () {
Object.getOwnPropertyDescriptor(new Proxy(target, {
Object.getOwnPropertyDescriptor(Proxy(target, {
getOwnPropertyDescriptor: function (target, name) {
return {
configurable: true

Просмотреть файл

@ -1784,7 +1784,7 @@ static const JSStdName standard_class_atoms[] = {
#ifdef ENABLE_PARALLEL_JS
{js_InitParallelArrayClass, EAGER_ATOM_AND_OCLASP(ParallelArray)},
#endif
{js_InitProxyClass, EAGER_ATOM_AND_CLASP(Proxy)},
{js_InitProxyClass, EAGER_CLASS_ATOM(Proxy), &js::ObjectProxyClass},
#if ENABLE_INTL_API
{js_InitIntlClass, EAGER_ATOM_AND_CLASP(Intl)},
#endif
@ -1877,6 +1877,7 @@ static const JSStdName object_prototype_names[] = {
#undef TYPED_ARRAY_CLASP
#undef EAGER_ATOM
#undef EAGER_CLASS_ATOM
#undef EAGER_ATOM_CLASP
#undef EAGER_ATOM_AND_CLASP
JS_PUBLIC_API(JSBool)

Просмотреть файл

@ -209,7 +209,6 @@ extern Class IntlClass;
extern Class JSONClass;
extern Class MathClass;
extern Class ObjectClass;
extern Class ProxyClass;
class ArrayBufferObject;
class GlobalObject;

Просмотреть файл

@ -3091,7 +3091,8 @@ proxy_HasInstance(JSContext *cx, HandleObject proxy, MutableHandleValue v, JSBoo
JS_FRIEND_DATA(Class) js::ObjectProxyClass = {
"Proxy",
Class::NON_NATIVE | JSCLASS_IMPLEMENTS_BARRIERS | JSCLASS_HAS_RESERVED_SLOTS(4),
Class::NON_NATIVE | JSCLASS_IMPLEMENTS_BARRIERS | JSCLASS_HAS_RESERVED_SLOTS(4) |
JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy),
JS_PropertyStub, /* addProperty */
JS_DeletePropertyStub, /* delProperty */
JS_PropertyStub, /* getProperty */
@ -3390,23 +3391,6 @@ proxy(JSContext *cx, unsigned argc, jsval *vp)
return true;
}
Class js::ProxyClass = {
"Proxy",
JSCLASS_HAS_CACHED_PROTO(JSProto_Proxy),
JS_PropertyStub, /* addProperty */
JS_DeletePropertyStub, /* delProperty */
JS_PropertyStub, /* getProperty */
JS_StrictPropertyStub, /* setProperty */
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
NULL, /* finalize */
NULL, /* checkAccess */
NULL, /* call */
NULL, /* hasInstance */
proxy /* construct */
};
static JSBool
proxy_create(JSContext *cx, unsigned argc, Value *vp)
{
@ -3485,18 +3469,19 @@ static const JSFunctionSpec static_methods[] = {
JS_FRIEND_API(JSObject *)
js_InitProxyClass(JSContext *cx, HandleObject obj)
{
RootedObject module(cx, NewObjectWithClassProto(cx, &ProxyClass, NULL, obj, SingletonObject));
if (!module)
Rooted<GlobalObject*> global(cx);
RootedFunction ctor(cx);
ctor = global->createConstructor(cx, proxy, cx->names().Proxy, 2);
if (!ctor)
return NULL;
if (!JS_DefineProperty(cx, obj, "Proxy", OBJECT_TO_JSVAL(module),
if (!JS_DefineFunctions(cx, ctor, static_methods))
return NULL;
if (!JS_DefineProperty(cx, obj, "Proxy", OBJECT_TO_JSVAL(ctor),
JS_PropertyStub, JS_StrictPropertyStub, 0)) {
return NULL;
}
if (!JS_DefineFunctions(cx, module, static_methods))
return NULL;
MarkStandardClassInitializedNoProto(obj, &ProxyClass);
return module;
MarkStandardClassInitializedNoProto(obj, &ObjectProxyClass);
return ctor;
}