Bug 537863 - Make various global properties (NaN, Infinity, undefined) readonly per ES5. r=jorendorff

This commit is contained in:
Jeff Walden 2010-01-04 23:10:36 -06:00
Родитель 6498a14ba5
Коммит d6c8370ece
6 изменённых файлов: 71 добавлений и 5 удалений

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

@ -1343,7 +1343,8 @@ JS_InitStandardClasses(JSContext *cx, JSObject *obj)
/* Define a top-level property 'undefined' with the undefined value. */
atom = cx->runtime->atomState.typeAtoms[JSTYPE_VOID];
if (!obj->defineProperty(cx, ATOM_TO_JSID(atom), JSVAL_VOID,
JS_PropertyStub, JS_PropertyStub, JSPROP_PERMANENT)) {
JS_PropertyStub, JS_PropertyStub,
JSPROP_PERMANENT | JSPROP_READONLY)) {
return JS_FALSE;
}
@ -1550,7 +1551,7 @@ JS_ResolveStandardClass(JSContext *cx, JSObject *obj, jsval id,
*resolved = JS_TRUE;
return obj->defineProperty(cx, ATOM_TO_JSID(atom), JSVAL_VOID,
JS_PropertyStub, JS_PropertyStub,
JSPROP_PERMANENT);
JSPROP_PERMANENT | JSPROP_READONLY);
}
/* Try for class constructors/prototypes named by well-known atoms. */

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

@ -790,14 +790,15 @@ js_InitNumberClass(JSContext *cx, JSObject *obj)
/* ECMA 15.1.1.1 */
rt = cx->runtime;
if (!JS_DefineProperty(cx, obj, js_NaN_str, rt->NaNValue,
NULL, NULL, JSPROP_PERMANENT)) {
if (!JS_DefineProperty(cx, obj, js_NaN_str, rt->NaNValue, JS_PropertyStub, JS_PropertyStub,
JSPROP_PERMANENT | JSPROP_READONLY)) {
return NULL;
}
/* ECMA 15.1.1.2 */
if (!JS_DefineProperty(cx, obj, js_Infinity_str, rt->positiveInfinityValue,
NULL, NULL, JSPROP_PERMANENT)) {
JS_PropertyStub, JS_PropertyStub,
JSPROP_PERMANENT | JSPROP_READONLY)) {
return NULL;
}
return proto;

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

@ -0,0 +1 @@

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

@ -0,0 +1,60 @@
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var gTestfile = 'global-numeric-properties.js';
//-----------------------------------------------------------------------------
var BUGNUMBER = 537863;
var summary =
'undefined, Infinity, and NaN global properties should not be writable';
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
var desc, old, error;
var global = this;
var names = ["NaN", "Infinity", "undefined"];
for (var i = 0; i < names.length; i++)
{
var name = names[i];
desc = Object.getOwnPropertyDescriptor(global, name);
assertEq(desc !== undefined, true, name + " should be present");
assertEq(desc.enumerable, false, name + " should not be enumerable");
assertEq(desc.configurable, false, name + " should not be configurable");
assertEq(desc.writable, false, name + " should not be writable");
old = global[name];
global[name] = 17;
assertEq(global[name], old, name + " changed on setting?");
error = "before";
try
{
throw new TypeError("SpiderMonkey doesn't currently implement " +
"strict-mode throwing when setting a readonly " +
"property, not running this bit of test for now; " +
"see bug 537873");
(function() { "use strict"; global[name] = 42; error = "didn't throw"; })();
}
catch (e)
{
if (e instanceof TypeError)
error = "typeerror";
else
error = "bad exception: " + e;
}
assertEq(error, "typeerror", "wrong strict mode error setting " + name);
}
/******************************************************************************/
reportCompare(true, true);
print("All tests passed!");

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

@ -0,0 +1,2 @@
url-prefix ../../jsreftest.html?test=ecma_5/misc/
script global-numeric-properties.js

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

@ -0,0 +1 @@
gTestsubsuite = 'misc';