Bug 645416, part 29 - Implement Symbol.keyFor(). r=efaust.

--HG--
extra : rebase_source : 2494792929754ba1915f380c0384471c3d5cb8e0
This commit is contained in:
Jason Orendorff 2014-06-23 10:57:04 -05:00
parent a0cbf316e5
commit 7f9e00a477
3 changed files with 47 additions and 0 deletions

View File

@ -50,6 +50,7 @@ const JSFunctionSpec SymbolObject::methods[] = {
const JSFunctionSpec SymbolObject::staticMethods[] = {
JS_FN("for", for_, 1, 0),
JS_FN("keyFor", keyFor, 1, 0),
JS_FS_END
};
@ -148,6 +149,36 @@ SymbolObject::for_(JSContext *cx, unsigned argc, Value *vp)
return true;
}
// ES6 rev 25 (2014 May 22) 19.4.2.7
bool
SymbolObject::keyFor(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
// step 1
HandleValue arg = args.get(0);
if (!arg.isSymbol()) {
js_ReportValueErrorFlags(cx, JSREPORT_ERROR, JSMSG_UNEXPECTED_TYPE, JSDVG_SEARCH_STACK,
arg, js::NullPtr(), "not a symbol", nullptr);
return false;
}
// step 2
if (arg.toSymbol()->code() == JS::SymbolCode::InSymbolRegistry) {
#ifdef DEBUG
RootedString desc(cx, arg.toSymbol()->description());
MOZ_ASSERT(Symbol::for_(cx, desc) == arg.toSymbol());
#endif
args.rval().setString(arg.toSymbol()->description());
return true;
}
// step 3: omitted
// step 4
args.rval().setUndefined();
return true;
}
MOZ_ALWAYS_INLINE bool
IsSymbol(HandleValue v)
{

View File

@ -46,6 +46,7 @@ class SymbolObject : public JSObject
// Static methods.
static bool for_(JSContext *cx, unsigned argc, Value *vp);
static bool keyFor(JSContext *cx, unsigned argc, Value *vp);
// Methods defined on Symbol.prototype.
static bool toString_impl(JSContext *cx, CallArgs args);

View File

@ -0,0 +1,15 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/ */
assertEq(Symbol.keyFor(Symbol.for("moon")), "moon");
assertEq(Symbol.keyFor(Symbol.for("")), "");
assertEq(Symbol.keyFor(Symbol("moon")), undefined);
assertEq(Symbol.keyFor(Symbol.iterator), undefined);
assertThrowsInstanceOf(() => Symbol.keyFor(), TypeError);
assertThrowsInstanceOf(() => Symbol.keyFor(Object(Symbol("moon"))), TypeError);
assertEq(Symbol.keyFor.length, 1);
if (typeof reportCompare === "function")
reportCompare(0, 0);