本文整理汇总了C++中JSLinearString::flatSetAtomized方法的典型用法代码示例。如果您正苦于以下问题:C++ JSLinearString::flatSetAtomized方法的具体用法?C++ JSLinearString::flatSetAtomized怎么用?C++ JSLinearString::flatSetAtomized使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSLinearString
的用法示例。
在下文中一共展示了JSLinearString::flatSetAtomized方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: STRING_TO_ATOM
JSAtom *
js_AtomizeString(JSContext *cx, JSString *strArg, uintN flags)
{
JS_ASSERT(!(flags & ~(ATOM_PINNED|ATOM_INTERNED|ATOM_TMPSTR|ATOM_NOCOPY)));
JS_ASSERT_IF(flags & ATOM_NOCOPY, flags & ATOM_TMPSTR);
if (strArg->isAtomized())
return STRING_TO_ATOM(strArg);
JSLinearString *str = strArg->ensureLinear(cx);
if (!str)
return NULL;
const jschar *chars = str->chars();
size_t length = str->length();
JSString *staticStr = JSString::lookupStaticString(chars, length);
if (staticStr)
return STRING_TO_ATOM(staticStr);
JSAtomState *state = &cx->runtime->atomState;
AtomSet &atoms = state->atoms;
AutoLockAtomsCompartment lock(cx);
AtomSet::AddPtr p = atoms.lookupForAdd(str);
/* Hashing the string should have flattened it if it was a rope. */
JS_ASSERT(str->isFlat() || str->isDependent());
JSLinearString *key;
if (p) {
key = AtomEntryToKey(*p);
} else {
/*
* Ensure that any atomized string lives only in the default
* compartment.
*/
bool needNewString = !!(flags & ATOM_TMPSTR) ||
str->asCell()->compartment() != cx->runtime->atomsCompartment;
/*
* Unless str is already comes from the default compartment and flat,
* we have to relookup the key as the last ditch GC invoked from the
* string allocation or OOM handling may unlock the default
* compartment lock.
*/
if (!needNewString && str->isFlat()) {
str->flatClearExtensible();
key = str;
atoms.add(p, StringToInitialAtomEntry(key));
} else {
if (needNewString) {
SwitchToCompartment sc(cx, cx->runtime->atomsCompartment);
if (flags & ATOM_NOCOPY) {
key = js_NewString(cx, const_cast<jschar *>(str->flatChars()), length);
if (!key)
return NULL;
/* Finish handing off chars to the GC'ed key string. */
JS_ASSERT(flags & ATOM_TMPSTR);
str->u.chars = NULL;
} else {
key = js_NewStringCopyN(cx, chars, length);
if (!key)
return NULL;
}
} else {
JS_ASSERT(str->isDependent());
if (!str->undepend(cx))
return NULL;
key = str;
}
if (!atoms.relookupOrAdd(p, key, StringToInitialAtomEntry(key))) {
JS_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report */
return NULL;
}
}
key->flatSetAtomized();
}
AddAtomEntryFlags(*p, flags & (ATOM_PINNED | ATOM_INTERNED));
JSAtom *atom = STRING_TO_ATOM(key);
return atom;
}