本文整理汇总了C++中JSString类的典型用法代码示例。如果您正苦于以下问题:C++ JSString类的具体用法?C++ JSString怎么用?C++ JSString使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Atomize
/* |callee| requires a usage string provided by JS_DefineFunctionsWithHelp. */
void
js::ReportUsageError(JSContext* cx, HandleObject callee, const char* msg)
{
const char* usageStr = "usage";
PropertyName* usageAtom = Atomize(cx, usageStr, strlen(usageStr))->asPropertyName();
RootedId id(cx, NameToId(usageAtom));
DebugOnly<Shape*> shape = static_cast<Shape*>(callee->as<JSFunction>().lookup(cx, id));
MOZ_ASSERT(!shape->configurable());
MOZ_ASSERT(!shape->writable());
MOZ_ASSERT(shape->hasDefaultGetter());
RootedValue usage(cx);
if (!JS_GetProperty(cx, callee, "usage", &usage))
return;
if (!usage.isString()) {
JS_ReportError(cx, "%s", msg);
} else {
JSString* str = usage.toString();
if (!str->ensureFlat(cx))
return;
AutoStableStringChars chars(cx);
if (!chars.initTwoByte(cx, str))
return;
JS_ReportError(cx, "%s. Usage: %hs", msg, chars.twoByteRange().start().get());
}
}
示例2: getReservedSlot
JSAtom *
SavedFrame::getSource()
{
const Value &v = getReservedSlot(JSSLOT_SOURCE);
JSString *s = v.toString();
return &s->asAtom();
}
示例3: MOZ_ASSERT
bool
JSCompartment::wrap(JSContext* cx, MutableHandleString strp)
{
MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(this));
MOZ_ASSERT(cx->compartment() == this);
/* If the string is already in this compartment, we are done. */
JSString* str = strp;
if (str->zoneFromAnyThread() == zone())
return true;
/* If the string is an atom, we don't have to copy. */
if (str->isAtom()) {
MOZ_ASSERT(str->isPermanentAtom() || str->zone()->isAtomsZone());
return true;
}
/* Check the cache. */
RootedValue key(cx, StringValue(str));
if (WrapperMap::Ptr p = crossCompartmentWrappers.lookup(CrossCompartmentKey(key))) {
strp.set(p->value().get().toString());
return true;
}
/* No dice. Make a copy, and cache it. */
JSString* copy = CopyStringPure(cx, str);
if (!copy)
return false;
if (!putWrapper(cx, CrossCompartmentKey(key), StringValue(copy)))
return false;
strp.set(copy);
return true;
}
示例4: while
// Overview: These functions convert a JSString from holding a string in rope form
// down to a simple String representation. It does so by building up the string
// backwards, since we want to avoid recursion, we expect that the tree structure
// representing the rope is likely imbalanced with more nodes down the left side
// (since appending to the string is likely more common) - and as such resolving
// in this fashion should minimize work queue size. (If we built the queue forwards
// we would likely have to place all of the constituent StringImpls into the
// Vector before performing any concatenation, but by working backwards we likely
// only fill the queue with the number of substrings at any given level in a
// rope-of-ropes.)
void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
{
LChar* position = buffer + m_length; // We will be working backwards over the rope.
Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
workQueue.append(m_fibers[i].get());
while (!workQueue.isEmpty()) {
JSString* currentFiber = workQueue.last();
workQueue.removeLast();
if (currentFiber->isRope()) {
JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
workQueue.append(currentFiberAsRope->m_fibers[i].get());
continue;
}
StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
unsigned length = string->length();
position -= length;
StringImpl::copyChars(position, string->characters8(), length);
}
ASSERT(buffer == position);
}
示例5: CloneObject
static JSObject *
CloneObject(JSContext *cx, HandleObject selfHostedObject)
{
AutoCycleDetector detect(cx, selfHostedObject);
if (!detect.init())
return nullptr;
if (detect.foundCycle()) {
JS_ReportError(cx, "SelfHosted cloning cannot handle cyclic object graphs.");
return nullptr;
}
RootedObject clone(cx);
if (selfHostedObject->is<JSFunction>()) {
RootedFunction selfHostedFunction(cx, &selfHostedObject->as<JSFunction>());
bool hasName = selfHostedFunction->atom() != nullptr;
// Arrow functions use the first extended slot for their lexical |this| value.
JS_ASSERT(!selfHostedFunction->isArrow());
js::gc::AllocKind kind = hasName
? JSFunction::ExtendedFinalizeKind
: selfHostedFunction->getAllocKind();
clone = CloneFunctionObject(cx, selfHostedFunction, cx->global(), kind, TenuredObject);
// To be able to re-lazify the cloned function, its name in the
// self-hosting compartment has to be stored on the clone.
if (clone && hasName)
clone->as<JSFunction>().setExtendedSlot(0, StringValue(selfHostedFunction->atom()));
} else if (selfHostedObject->is<RegExpObject>()) {
RegExpObject &reobj = selfHostedObject->as<RegExpObject>();
RootedAtom source(cx, reobj.getSource());
JS_ASSERT(source->isPermanentAtom());
clone = RegExpObject::createNoStatics(cx, source, reobj.getFlags(), nullptr);
} else if (selfHostedObject->is<DateObject>()) {
clone = JS_NewDateObjectMsec(cx, selfHostedObject->as<DateObject>().UTCTime().toNumber());
} else if (selfHostedObject->is<BooleanObject>()) {
clone = BooleanObject::create(cx, selfHostedObject->as<BooleanObject>().unbox());
} else if (selfHostedObject->is<NumberObject>()) {
clone = NumberObject::create(cx, selfHostedObject->as<NumberObject>().unbox());
} else if (selfHostedObject->is<StringObject>()) {
JSString *selfHostedString = selfHostedObject->as<StringObject>().unbox();
if (!selfHostedString->isFlat())
MOZ_CRASH();
RootedString str(cx, js_NewStringCopyN<CanGC>(cx,
selfHostedString->asFlat().chars(),
selfHostedString->asFlat().length()));
if (!str)
return nullptr;
clone = StringObject::create(cx, str);
} else if (selfHostedObject->is<ArrayObject>()) {
clone = NewDenseEmptyArray(cx, nullptr, TenuredObject);
} else {
JS_ASSERT(selfHostedObject->isNative());
clone = NewObjectWithGivenProto(cx, selfHostedObject->getClass(), nullptr, cx->global(),
selfHostedObject->tenuredGetAllocKind(),
SingletonObject);
}
if (!clone)
return nullptr;
if (!CloneProperties(cx, selfHostedObject, clone))
return nullptr;
return clone;
}
示例6: JS_ASSERT
void
Shape::dump(JSContext *cx, FILE *fp) const
{
jsid propid = this->propid();
JS_ASSERT(!JSID_IS_VOID(propid));
if (JSID_IS_INT(propid)) {
fprintf(fp, "[%ld]", (long) JSID_TO_INT(propid));
} else if (JSID_IS_DEFAULT_XML_NAMESPACE(propid)) {
fprintf(fp, "<default XML namespace>");
} else {
JSLinearString *str;
if (JSID_IS_ATOM(propid)) {
str = JSID_TO_ATOM(propid);
} else {
JS_ASSERT(JSID_IS_OBJECT(propid));
JSString *s = ToStringSlow<CanGC>(cx, IdToValue(propid));
fputs("object ", fp);
str = s ? s->ensureLinear(cx) : NULL;
}
if (!str)
fputs("<error>", fp);
else
FileEscapedString(fp, str, '"');
}
fprintf(fp, " g/s %p/%p slot %d attrs %x ",
JS_FUNC_TO_DATA_PTR(void *, base()->rawGetter),
JS_FUNC_TO_DATA_PTR(void *, base()->rawSetter),
hasSlot() ? slot() : -1, attrs);
if (attrs) {
int first = 1;
fputs("(", fp);
#define DUMP_ATTR(name, display) if (attrs & JSPROP_##name) fputs(&(" " #display)[first], fp), first = 0
DUMP_ATTR(ENUMERATE, enumerate);
DUMP_ATTR(READONLY, readonly);
DUMP_ATTR(PERMANENT, permanent);
DUMP_ATTR(GETTER, getter);
DUMP_ATTR(SETTER, setter);
DUMP_ATTR(SHARED, shared);
#undef DUMP_ATTR
fputs(") ", fp);
}
fprintf(fp, "flags %x ", flags);
if (flags) {
int first = 1;
fputs("(", fp);
#define DUMP_FLAG(name, display) if (flags & name) fputs(&(" " #display)[first], fp), first = 0
DUMP_FLAG(HAS_SHORTID, has_shortid);
DUMP_FLAG(IN_DICTIONARY, in_dictionary);
#undef DUMP_FLAG
fputs(") ", fp);
}
fprintf(fp, "shortid %d\n", maybeShortid());
}
示例7: js_AtomizeChars
JSAtom *
js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, uintN flags)
{
JSString str;
str.initFlat((jschar *)chars, length);
return js_AtomizeString(cx, &str, ATOM_TMPSTR | flags);
}
示例8: suppress
void
Shape::dump(JSContext *cx, FILE *fp) const
{
/* This is only used from gdb, so allowing GC here would just be confusing. */
gc::AutoSuppressGC suppress(cx);
jsid propid = this->propid();
JS_ASSERT(!JSID_IS_VOID(propid));
if (JSID_IS_INT(propid)) {
fprintf(fp, "[%ld]", (long) JSID_TO_INT(propid));
} else {
JSLinearString *str;
if (JSID_IS_ATOM(propid)) {
str = JSID_TO_ATOM(propid);
} else {
JS_ASSERT(JSID_IS_OBJECT(propid));
Value v = IdToValue(propid);
JSString *s = ToStringSlow<NoGC>(cx, v);
fputs("object ", fp);
str = s ? s->ensureLinear(cx) : nullptr;
}
if (!str)
fputs("<error>", fp);
else
FileEscapedString(fp, str, '"');
}
fprintf(fp, " g/s %p/%p slot %d attrs %x ",
JS_FUNC_TO_DATA_PTR(void *, base()->rawGetter),
JS_FUNC_TO_DATA_PTR(void *, base()->rawSetter),
hasSlot() ? slot() : -1, attrs);
if (attrs) {
int first = 1;
fputs("(", fp);
#define DUMP_ATTR(name, display) if (attrs & JSPROP_##name) fputs(&(" " #display)[first], fp), first = 0
DUMP_ATTR(ENUMERATE, enumerate);
DUMP_ATTR(READONLY, readonly);
DUMP_ATTR(PERMANENT, permanent);
DUMP_ATTR(GETTER, getter);
DUMP_ATTR(SETTER, setter);
DUMP_ATTR(SHARED, shared);
#undef DUMP_ATTR
fputs(") ", fp);
}
fprintf(fp, "flags %x ", flags);
if (flags) {
int first = 1;
fputs("(", fp);
#define DUMP_FLAG(name, display) if (flags & name) fputs(&(" " #display)[first], fp), first = 0
DUMP_FLAG(IN_DICTIONARY, in_dictionary);
#undef DUMP_FLAG
fputs(") ", fp);
}
}
示例9: CloneObject
static JSObject *
CloneObject(JSContext *cx, JSObject *selfHostedObject, CloneMemory &clonedObjects)
{
DependentAddPtr<CloneMemory> p(cx, clonedObjects, selfHostedObject);
if (p)
return p->value();
RootedObject clone(cx);
if (selfHostedObject->is<JSFunction>()) {
JSFunction *selfHostedFunction = &selfHostedObject->as<JSFunction>();
bool hasName = selfHostedFunction->atom() != nullptr;
js::gc::AllocKind kind = hasName
? JSFunction::ExtendedFinalizeKind
: selfHostedFunction->getAllocKind();
clone = CloneFunctionObject(cx, HandleFunction::fromMarkedLocation(&selfHostedFunction),
cx->global(), kind, TenuredObject);
// To be able to re-lazify the cloned function, its name in the
// self-hosting compartment has to be stored on the clone.
if (clone && hasName)
clone->as<JSFunction>().setExtendedSlot(0, StringValue(selfHostedFunction->atom()));
} else if (selfHostedObject->is<RegExpObject>()) {
RegExpObject &reobj = selfHostedObject->as<RegExpObject>();
RootedAtom source(cx, reobj.getSource());
JS_ASSERT(source->isPermanentAtom());
clone = RegExpObject::createNoStatics(cx, source, reobj.getFlags(), nullptr);
} else if (selfHostedObject->is<DateObject>()) {
clone = JS_NewDateObjectMsec(cx, selfHostedObject->as<DateObject>().UTCTime().toNumber());
} else if (selfHostedObject->is<BooleanObject>()) {
clone = BooleanObject::create(cx, selfHostedObject->as<BooleanObject>().unbox());
} else if (selfHostedObject->is<NumberObject>()) {
clone = NumberObject::create(cx, selfHostedObject->as<NumberObject>().unbox());
} else if (selfHostedObject->is<StringObject>()) {
JSString *selfHostedString = selfHostedObject->as<StringObject>().unbox();
if (!selfHostedString->isFlat())
MOZ_CRASH();
RootedString str(cx, js_NewStringCopyN<CanGC>(cx,
selfHostedString->asFlat().chars(),
selfHostedString->asFlat().length()));
if (!str)
return nullptr;
clone = StringObject::create(cx, str);
} else if (selfHostedObject->is<ArrayObject>()) {
clone = NewDenseEmptyArray(cx, nullptr, TenuredObject);
} else {
JS_ASSERT(selfHostedObject->isNative());
clone = NewObjectWithGivenProto(cx, selfHostedObject->getClass(), nullptr, cx->global(),
selfHostedObject->tenuredGetAllocKind(),
SingletonObject);
}
if (!clone)
return nullptr;
if (!p.add(cx, clonedObjects, selfHostedObject, clone))
return nullptr;
if (!CloneProperties(cx, selfHostedObject, clone, clonedObjects)) {
clonedObjects.remove(selfHostedObject);
return nullptr;
}
return clone;
}
示例10: getReservedSlot
JSAtom *
SavedFrame::getFunctionDisplayName()
{
const Value &v = getReservedSlot(JSSLOT_FUNCTIONDISPLAYNAME);
if (v.isNull())
return nullptr;
JSString *s = v.toString();
return &s->asAtom();
}
示例11: getOwnPropertySlotByIndex
bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
{
JSString* thisObject = jsCast<JSString*>(cell);
// The semantics here are really getPropertySlot, not getOwnPropertySlot.
// This function should only be called by JSValue::get.
if (thisObject->getStringPropertySlot(exec, propertyName, slot))
return true;
return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
}
示例12: asString
void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSString* thisObject = asString(cell);
Base::visitChildren(thisObject, visitor);
if (thisObject->isRope())
static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
if (StringImpl* impl = thisObject->m_value.impl())
visitor.reportExtraMemoryVisited(impl->costDuringGC());
}
示例13: AtomizeString
JSAtom *
js::ToAtom(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType v)
{
if (!v.isString())
return ToAtomSlow<allowGC>(cx, v);
JSString *str = v.toString();
if (str->isAtom())
return &str->asAtom();
return AtomizeString(cx, str);
}
示例14: equalToStringImpl
static TriState equalToStringImpl(JSValue value, StringImpl* stringImpl)
{
if (!value.isString())
return FalseTriState;
JSString* jsString = asString(value);
const StringImpl* string = jsString->tryGetValueImpl();
if (!string)
return MixedTriState;
return triState(WTF::equal(stringImpl, string));
}
示例15: EvalScriptVersion16
JSBool
EvalScriptVersion16(JSContext *cx, uintN argc, jsval *vp)
{
JS_ASSERT(argc == 1);
jsval *argv = JS_ARGV(cx, vp);
JS_ASSERT(JSVAL_IS_STRING(argv[0]));
JSString *str = JSVAL_TO_STRING(argv[0]);
const jschar *chars = str->getChars(cx);
JS_ASSERT(chars);
size_t len = str->length();
return callbackData->evalVersion(chars, len, JSVERSION_1_6);
}