当前位置: 首页>>代码示例>>C++>>正文


C++ JSLinearString::chars方法代码示例

本文整理汇总了C++中JSLinearString::chars方法的典型用法代码示例。如果您正苦于以下问题:C++ JSLinearString::chars方法的具体用法?C++ JSLinearString::chars怎么用?C++ JSLinearString::chars使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JSLinearString的用法示例。


在下文中一共展示了JSLinearString::chars方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: reviver

JSBool
js_json_parse(JSContext *cx, uintN argc, Value *vp)
{
    JSString *s = NULL;
    Value *argv = vp + 2;
    AutoValueRooter reviver(cx);

    if (!JS_ConvertArguments(cx, argc, Jsvalify(argv), "S / v", &s, reviver.addr()))
        return JS_FALSE;

    JSLinearString *linearStr = s->ensureLinear(cx);
    if (!linearStr)
        return JS_FALSE;

    JSONParser *jp = js_BeginJSONParse(cx, vp);
    JSBool ok = jp != NULL;
    if (ok) {
        const jschar *chars = linearStr->chars();
        size_t length = linearStr->length();
        ok = js_ConsumeJSONText(cx, jp, chars, length);
        ok &= !!js_FinishJSONParse(cx, jp, reviver.value());
    }

    return ok;
}
开发者ID:3l13,项目名称:APE_Server,代码行数:25,代码来源:json.cpp

示例2: sb

JSFlatString *
RegExpObject::toString(JSContext *cx) const
{
    JSLinearString *src = getSource();
    StringBuffer sb(cx);
    if (size_t len = src->length()) {
        if (!sb.reserve(len + 2))
            return NULL;
        sb.infallibleAppend('/');
        sb.infallibleAppend(src->chars(), len);
        sb.infallibleAppend('/');
    } else {
        if (!sb.append("/(?:)/"))
            return NULL;
    }
    if (global() && !sb.append('g'))
        return NULL;
    if (ignoreCase() && !sb.append('i'))
        return NULL;
    if (multiline() && !sb.append('m'))
        return NULL;
    if (sticky() && !sb.append('y'))
        return NULL;

    return sb.finishString();
}
开发者ID:vingtetun,项目名称:mozilla-central,代码行数:26,代码来源:RegExpObject.cpp

示例3: reviver

/* ES5 15.12.2. */
JSBool
js_json_parse(JSContext *cx, unsigned argc, Value *vp)
{
    CallArgs args = CallArgsFromVp(argc, vp);

    /* Step 1. */
    JSLinearString *linear;
    if (argc >= 1) {
        JSString *str = ToString(cx, args[0]);
        if (!str)
            return false;
        linear = str->ensureLinear(cx);
        if (!linear)
            return false;
    } else {
        linear = cx->names().undefined;
    }
    JS::Anchor<JSString *> anchor(linear);

    RootedValue reviver(cx, (argc >= 2) ? args[1] : UndefinedValue());

    /* Steps 2-5. */
    return ParseJSONWithReviver(cx, linear->chars(), linear->length(), reviver, args.rval());
}
开发者ID:mokerjoke,项目名称:mozilla-central,代码行数:25,代码来源:json.cpp

示例4: replacer


//.........这里部分代码省略.........

                /* Step 4b(iv)(2). */
                if (!JSObject::getElement(cx, replacer, replacer, i, &v))
                    return false;

                RootedId id(cx);
                if (v.isNumber()) {
                    /* Step 4b(iv)(4). */
                    int32_t n;
                    if (v.isNumber() && ValueFitsInInt32(v, &n) && INT_FITS_IN_JSID(n)) {
                        id = INT_TO_JSID(n);
                    } else {
                        if (!ValueToId<CanGC>(cx, v, &id))
                            return false;
                    }
                } else if (v.isString() ||
                           IsObjectWithClass(v, ESClass_String, cx) ||
                           IsObjectWithClass(v, ESClass_Number, cx))
                {
                    /* Step 4b(iv)(3), 4b(iv)(5). */
                    if (!ValueToId<CanGC>(cx, v, &id))
                        return false;
                } else {
                    continue;
                }

                /* Step 4b(iv)(6). */
                HashSet<jsid, JsidHasher>::AddPtr p = idSet.lookupForAdd(id);
                if (!p) {
                    /* Step 4b(iv)(6)(a). */
                    if (!idSet.add(p, id) || !propertyList.append(id))
                        return false;
                }
            }
        } else {
            replacer = nullptr;
        }
    }

    /* Step 5. */
    if (space.isObject()) {
        RootedObject spaceObj(cx, &space.toObject());
        if (ObjectClassIs(spaceObj, ESClass_Number, cx)) {
            double d;
            if (!ToNumber(cx, space, &d))
                return false;
            space = NumberValue(d);
        } else if (ObjectClassIs(spaceObj, ESClass_String, cx)) {
            JSString *str = ToStringSlow<CanGC>(cx, space);
            if (!str)
                return false;
            space = StringValue(str);
        }
    }

    StringBuffer gap(cx);

    if (space.isNumber()) {
        /* Step 6. */
        double d;
        JS_ALWAYS_TRUE(ToInteger(cx, space, &d));
        d = Min(10.0, d);
        if (d >= 1 && !gap.appendN(' ', uint32_t(d)))
            return false;
    } else if (space.isString()) {
        /* Step 7. */
        JSLinearString *str = space.toString()->ensureLinear(cx);
        if (!str)
            return false;
        JS::Anchor<JSString *> anchor(str);
        size_t len = Min(size_t(10), space.toString()->length());
        if (!gap.append(str->chars(), len))
            return false;
    } else {
        /* Step 8. */
        JS_ASSERT(gap.empty());
    }

    /* Step 9. */
    RootedObject wrapper(cx, NewBuiltinClassInstance(cx, &JSObject::class_));
    if (!wrapper)
        return false;

    /* Step 10. */
    RootedId emptyId(cx, NameToId(cx->names().empty));
    if (!DefineNativeProperty(cx, wrapper, emptyId, vp, JS_PropertyStub, JS_StrictPropertyStub,
                              JSPROP_ENUMERATE, 0, 0))
    {
        return false;
    }

    /* Step 11. */
    StringifyContext scx(cx, sb, gap, replacer, propertyList);
    if (!PreprocessValue(cx, wrapper, HandleId(emptyId), vp, &scx))
        return false;
    if (IsFilteredValue(vp))
        return true;

    return Str(cx, vp, &scx);
}
开发者ID:jinevening,项目名称:releases-mozilla-central,代码行数:101,代码来源:json.cpp

示例5: 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;
}
开发者ID:3l13,项目名称:APE_Server,代码行数:86,代码来源:jsatom.cpp

示例6: WrapForSameCompartment


//.........这里部分代码省略.........

        /* Unwrap the object, but don't unwrap outer windows. */
        obj = UnwrapObject(&vp->toObject(), /* stopAtOuter = */ true, &flags);

        if (obj->compartment() == this)
            return WrapForSameCompartment(cx, obj, vp);

        if (cx->runtime->preWrapObjectCallback) {
            obj = cx->runtime->preWrapObjectCallback(cx, global, obj, flags);
            if (!obj)
                return false;
        }

        if (obj->compartment() == this)
            return WrapForSameCompartment(cx, obj, vp);
        vp->setObject(*obj);

#ifdef DEBUG
        {
            JSObject *outer = GetOuterObject(cx, obj);
            JS_ASSERT(outer && outer == obj);
        }
#endif
    }

    RootedValue key(cx, *vp);

    /* If we already have a wrapper for this value, use it. */
    if (WrapperMap::Ptr p = crossCompartmentWrappers.lookup(key)) {
        *vp = p->value;
        if (vp->isObject()) {
            RootedObject obj(cx, &vp->toObject());
            JS_ASSERT(obj->isCrossCompartmentWrapper());
            JS_ASSERT(obj->getParent() == global);
        }
        return true;
    }

    if (vp->isString()) {
        RootedValue orig(cx, *vp);
        JSLinearString *str = vp->toString()->ensureLinear(cx);
        if (!str)
            return false;
        JSString *wrapped = js_NewStringCopyN(cx, str->chars(), str->length());
        if (!wrapped)
            return false;
        vp->setString(wrapped);
        if (!putWrapper(orig, *vp))
            return false;

        if (str->compartment()->isGCMarking()) {
            /*
             * All string wrappers are dropped when collection starts, but we
             * just created a new one.  Mark the wrapped string to stop it being
             * finalized, because if it was then the pointer in this
             * compartment's wrapper map would be left dangling.
             */
            JSString *tmp = str;
            MarkStringUnbarriered(&rt->gcMarker, &tmp, "wrapped string");
            JS_ASSERT(tmp == str);
        }

        return true;
    }

    RootedObject obj(cx, &vp->toObject());

    JSObject *proto = Proxy::LazyProto;
    if (existing) {
        /* Is it possible to reuse |existing|? */
        if (!existing->getTaggedProto().isLazy() ||
            existing->getClass() != &ObjectProxyClass ||
            existing->getParent() != global ||
            obj->isCallable())
        {
            existing = NULL;
        }
    }

    /*
     * We hand in the original wrapped object into the wrap hook to allow
     * the wrap hook to reason over what wrappers are currently applied
     * to the object.
     */
    RootedObject wrapper(cx);
    wrapper = cx->runtime->wrapObjectCallback(cx, existing, obj, proto, global, flags);
    if (!wrapper)
        return false;

    // We maintain the invariant that the key in the cross-compartment wrapper
    // map is always directly wrapped by the value.
    JS_ASSERT(Wrapper::wrappedObject(wrapper) == &key.get().toObject());

    vp->setObject(*wrapper);

    if (!putWrapper(key, *vp))
        return false;

    return true;
}
开发者ID:IcepOwer,项目名称:Spidermonkey,代码行数:101,代码来源:jscompartment.cpp


注:本文中的JSLinearString::chars方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。