Bug 596351 - Proxies should throw TypeErrors for assignments to read-only properties in ES5 strict mode. r=luke

This commit is contained in:
Bobby Holley 2012-03-16 12:47:21 -07:00
parent 7aa891a8c8
commit ab0445c528
4 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1,5 @@
// |jit-test| error: TypeError
"use strict"
var g = newGlobal('new-compartment');
g.eval("foo = {}; Object.defineProperty(foo, 'a', {value: 2, writable: false});");
g.foo.a = 3;

View File

@ -0,0 +1,7 @@
// |jit-test| error: TypeError
"use strict"
var g = newGlobal('new-compartment');
g.eval("bar = {}; Object.freeze(bar);");
g.bar.a = 4;

View File

@ -182,8 +182,18 @@ ProxyHandler::set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, b
return false; return false;
/* The control-flow here differs from ::get() because of the fall-through case below. */ /* The control-flow here differs from ::get() because of the fall-through case below. */
if (desc.obj) { if (desc.obj) {
if (desc.attrs & JSPROP_READONLY) // Check for read-only properties.
if (desc.attrs & JSPROP_READONLY) {
if (strict) {
JSAutoByteString bytes(cx, JSID_TO_STRING(id));
if (!bytes)
return false;
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_CANT_REDEFINE_PROP, bytes.ptr());
return false;
}
return true; return true;
}
if (!desc.setter) { if (!desc.setter) {
// Be wary of the odd explicit undefined setter case possible through // Be wary of the odd explicit undefined setter case possible through
// Object.defineProperty. // Object.defineProperty.
@ -208,8 +218,18 @@ ProxyHandler::set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, b
if (!getPropertyDescriptor(cx, proxy, id, true, &desc)) if (!getPropertyDescriptor(cx, proxy, id, true, &desc))
return false; return false;
if (desc.obj) { if (desc.obj) {
if (desc.attrs & JSPROP_READONLY) // Check for read-only properties.
if (desc.attrs & JSPROP_READONLY) {
if (strict) {
JSAutoByteString bytes(cx, JSID_TO_STRING(id));
if (!bytes)
return false;
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_CANT_REDEFINE_PROP, bytes.ptr());
return false;
}
return true; return true;
}
if (!desc.setter) { if (!desc.setter) {
// Be wary of the odd explicit undefined setter case possible through // Be wary of the odd explicit undefined setter case possible through
// Object.defineProperty. // Object.defineProperty.

View File

@ -230,8 +230,7 @@ bool
Wrapper::set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict, Wrapper::set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id, bool strict,
Value *vp) Value *vp)
{ {
// FIXME (bug 596351): Need deal with strict mode. SET(wrappedObject(wrapper)->setGeneric(cx, id, vp, strict));
SET(wrappedObject(wrapper)->setGeneric(cx, id, vp, false));
} }
bool bool