Example #1
0
bool
JSCompartment::getTemplateLiteralObject(JSContext* cx, HandleArrayObject rawStrings,
                                        MutableHandleObject templateObj)
{
    if (TemplateRegistry::AddPtr p = templateLiteralMap_.lookupForAdd(rawStrings)) {
        templateObj.set(p->value());

        // The template object must have been frozen when it was added to the
        // registry.
        MOZ_ASSERT(!templateObj->nonProxyIsExtensible());
    } else {
        MOZ_ASSERT(templateObj->nonProxyIsExtensible());
        RootedValue rawValue(cx, ObjectValue(*rawStrings));
        if (!DefineDataProperty(cx, templateObj, cx->names().raw, rawValue, 0))
            return false;
        if (!FreezeObject(cx, rawStrings))
            return false;
        if (!FreezeObject(cx, templateObj))
            return false;

        if (!templateLiteralMap_.relookupOrAdd(p, rawStrings, templateObj))
            return false;
    }

    return true;
}