Bug 645416, part 16 - Implement Symbol.prototype.valueOf. r=sfink.

--HG--
extra : rebase_source : 12dfad71e0933ccc3e553854f44bdf0f85eccf64
This commit is contained in:
Jason Orendorff 2014-06-23 10:56:51 -05:00
parent abe45ff112
commit 0ff275fd1e
6 changed files with 53 additions and 0 deletions

View File

@ -44,6 +44,7 @@ const JSPropertySpec SymbolObject::properties[] = {
const JSFunctionSpec SymbolObject::methods[] = {
JS_FN(js_toString_str, toString, 0, 0),
JS_FN(js_valueOf_str, valueOf, 0, 0),
JS_FS_END
};
@ -183,6 +184,27 @@ SymbolObject::toString(JSContext *cx, unsigned argc, Value *vp)
return CallNonGenericMethod<IsSymbol, toString_impl>(cx, args);
}
//ES6 rev 24 (2014 Apr 27) 19.4.3.3
bool
SymbolObject::valueOf_impl(JSContext *cx, CallArgs args)
{
// Step 3, the error case, is handled by CallNonGenericMethod.
HandleValue thisv = args.thisv();
MOZ_ASSERT(IsSymbol(thisv));
if (thisv.isSymbol())
args.rval().set(thisv);
else
args.rval().setSymbol(thisv.toObject().as<SymbolObject>().unbox());
return true;
}
bool
SymbolObject::valueOf(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
return CallNonGenericMethod<IsSymbol, valueOf_impl>(cx, args);
}
JSObject *
js_InitSymbolClass(JSContext *cx, HandleObject obj)
{

View File

@ -48,6 +48,8 @@ class SymbolObject : public JSObject
// Methods defined on Symbol.prototype.
static bool toString_impl(JSContext *cx, CallArgs args);
static bool toString(JSContext *cx, unsigned argc, Value *vp);
static bool valueOf_impl(JSContext *cx, CallArgs args);
static bool valueOf(JSContext *cx, unsigned argc, Value *vp);
static const JSPropertySpec properties[];
static const JSFunctionSpec methods[];

View File

@ -22,6 +22,7 @@ Object.defineProperty(Symbol.prototype, "prop", {
gets++;
assertEq(typeof this, "object");
assertEq(this instanceof Symbol, true);
assertEq(this.valueOf(), sym);
return "got";
},
set: function (v) {
@ -29,6 +30,7 @@ Object.defineProperty(Symbol.prototype, "prop", {
sets++;
assertEq(typeof this, "object");
assertEq(this instanceof Symbol, true);
assertEq(this.valueOf(), sym);
assertEq(v, "newvalue");
}
});
@ -44,6 +46,7 @@ for (var sym of symbols) {
Symbol.prototype.nonStrictMethod = function (arg) {
assertEq(arg, "ok");
assertEq(this instanceof Symbol, true);
assertEq(this.valueOf(), sym);
return 13;
};
assertEq(sym.nonStrictMethod("ok"), 13);

View File

@ -26,5 +26,8 @@ assertEq(hits, 1);
assertEq(Object.getPrototypeOf(Symbol.prototype), Object.prototype);
// Symbol.prototype is not itself a Symbol object.
assertThrowsInstanceOf(() => Symbol.prototype.valueOf(), TypeError);
if (typeof reportCompare === "function")
reportCompare(0, 0);

View File

@ -23,6 +23,7 @@ assertEq(desc.writable, true);
assertEq(Symbol.for.length, 1);
assertEq(Symbol.prototype.toString.length, 0);
assertEq(Symbol.prototype.valueOf.length, 0);
if (typeof reportCompare === "function")
reportCompare(0, 0);

View File

@ -0,0 +1,22 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/ */
var symbols = [
Symbol(),
Symbol("ok"),
Symbol.for("dummies"),
Symbol.iterator
];
for (var sym of symbols) {
assertEq(sym.valueOf(), sym);
assertEq(Object(sym).valueOf(), sym);
}
// Any other value throws.
var nonsymbols = [undefined, null, NaN, {}, Symbol.prototype];
for (var nonsym of nonsymbols)
assertThrowsInstanceOf(() => Symbol.prototype.valueOf.call(nonsym), TypeError);
if (typeof reportCompare === "function")
reportCompare(0, 0);