void GetDynamicName(JSContext *cx, JSObject *scopeChain, JSString *str, Value *vp) { // Lookup a string on the scope chain, returning either the value found or // undefined through rval. This function is infallible, and cannot GC or // invalidate. JSAtom *atom; if (str->isAtom()) { atom = &str->asAtom(); } else { atom = AtomizeString(cx, str); if (!atom) { vp->setUndefined(); return; } } if (!frontend::IsIdentifier(atom) || frontend::IsKeyword(atom)) { vp->setUndefined(); return; } Shape *shape = nullptr; JSObject *scope = nullptr, *pobj = nullptr; if (LookupNameNoGC(cx, atom->asPropertyName(), scopeChain, &scope, &pobj, &shape)) { if (FetchNameNoGC(pobj, shape, MutableHandleValue::fromMarkedLocation(vp))) return; } vp->setUndefined(); }
bool JSRuntime::initializeAtoms(JSContext *cx) { atoms_ = cx->new_<AtomSet>(); if (!atoms_ || !atoms_->init(JS_STRING_HASH_COUNT)) return false; if (parentRuntime) { staticStrings = parentRuntime->staticStrings; commonNames = parentRuntime->commonNames; emptyString = parentRuntime->emptyString; permanentAtoms = parentRuntime->permanentAtoms; return true; } permanentAtoms = cx->new_<AtomSet>(); if (!permanentAtoms || !permanentAtoms->init(JS_STRING_HASH_COUNT)) return false; staticStrings = cx->new_<StaticStrings>(); if (!staticStrings || !staticStrings->init(cx)) return false; static const CommonNameInfo cachedNames[] = { #define COMMON_NAME_INFO(idpart, id, text) { js_##idpart##_str, sizeof(text) - 1 }, FOR_EACH_COMMON_PROPERTYNAME(COMMON_NAME_INFO) #undef COMMON_NAME_INFO #define COMMON_NAME_INFO(name, code, init, clasp) { js_##name##_str, sizeof(#name) - 1 }, JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INFO) #undef COMMON_NAME_INFO }; commonNames = cx->new_<JSAtomState>(); if (!commonNames) return false; FixedHeapPtr<PropertyName> *names = reinterpret_cast<FixedHeapPtr<PropertyName> *>(commonNames); for (size_t i = 0; i < ArrayLength(cachedNames); i++, names++) { JSAtom *atom = Atomize(cx, cachedNames[i].str, cachedNames[i].length, InternAtom); if (!atom) return false; names->init(atom->asPropertyName()); } JS_ASSERT(uintptr_t(names) == uintptr_t(commonNames + 1)); emptyString = commonNames->empty; return true; }
bool js_InitCommonAtoms(JSContext *cx) { JSAtomState *state = &cx->runtime->atomState; JSAtom **atoms = state->commonAtomsStart(); for (size_t i = 0; i < ArrayLength(js_common_atom_names); i++, atoms++) { JSAtom *atom = js_Atomize(cx, js_common_atom_names[i], strlen(js_common_atom_names[i]), InternAtom); if (!atom) return false; *atoms = atom->asPropertyName(); } state->clearLazyAtoms(); cx->runtime->emptyString = state->emptyAtom; return true; }
const StructField * StructTypeRepresentation::fieldNamed(jsid id) const { if (!JSID_IS_ATOM(id)) return nullptr; uint32_t unused; JSAtom *atom = JSID_TO_ATOM(id); if (atom->isIndex(&unused)) return nullptr; PropertyName *name = atom->asPropertyName(); for (size_t i = 0; i < fieldCount(); i++) { if (field(i).propertyName.get() == name) return &field(i); } return nullptr; }
bool js::InitCommonNames(JSContext *cx) { static const CommonNameInfo cachedNames[] = { #define COMMON_NAME_INFO(idpart, id, text) { js_##idpart##_str, sizeof(text) - 1 }, FOR_EACH_COMMON_PROPERTYNAME(COMMON_NAME_INFO) #undef COMMON_NAME_INFO #define COMMON_NAME_INFO(name, code, init) { js_##name##_str, sizeof(#name) - 1 }, JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INFO) #undef COMMON_NAME_INFO }; FixedHeapPtr<PropertyName> *names = &cx->runtime->firstCachedName; for (size_t i = 0; i < ArrayLength(cachedNames); i++, names++) { JSAtom *atom = Atomize(cx, cachedNames[i].str, cachedNames[i].length, InternAtom); if (!atom) return false; names->init(atom->asPropertyName()); } JS_ASSERT(uintptr_t(names) == uintptr_t(&cx->runtime->atomState + 1)); cx->runtime->emptyString = cx->names().empty; return true; }
JSObject * ParallelArrayObject::initClass(JSContext *cx, HandleObject obj) { JS_ASSERT(obj->isNative()); // Cache constructor names. { const char *ctorStrs[NumCtors] = { "ParallelArrayConstructEmpty", "ParallelArrayConstructFromArray", "ParallelArrayConstructFromFunction", "ParallelArrayConstructFromFunctionMode" }; for (uint32_t i = 0; i < NumCtors; i++) { JSAtom *atom = Atomize(cx, ctorStrs[i], strlen(ctorStrs[i]), InternAtom); if (!atom) return NULL; ctorNames[i].init(atom->asPropertyName()); } } Rooted<GlobalObject *> global(cx, &obj->asGlobal()); RootedObject proto(cx, global->createBlankPrototype(cx, &protoClass)); if (!proto) return NULL; JSProtoKey key = JSProto_ParallelArray; RootedFunction ctor(cx, global->createConstructor(cx, construct, cx->names().ParallelArray, 0)); if (!ctor || !LinkConstructorAndPrototype(cx, ctor, proto) || !DefinePropertiesAndBrand(cx, proto, NULL, methods) || !DefineConstructorAndPrototype(cx, global, key, ctor, proto)) { return NULL; } // Define the length getter. { const char lengthStr[] = "ParallelArrayLength"; JSAtom *atom = Atomize(cx, lengthStr, strlen(lengthStr)); if (!atom) return NULL; Rooted<PropertyName *> lengthProp(cx, atom->asPropertyName()); RootedValue lengthValue(cx); if (!cx->global()->getIntrinsicValue(cx, lengthProp, &lengthValue)) return NULL; RootedObject lengthGetter(cx, &lengthValue.toObject()); if (!lengthGetter) return NULL; RootedId lengthId(cx, AtomToId(cx->names().length)); unsigned flags = JSPROP_PERMANENT | JSPROP_SHARED | JSPROP_GETTER; RootedValue value(cx, UndefinedValue()); if (!DefineNativeProperty(cx, proto, lengthId, value, JS_DATA_TO_FUNC_PTR(PropertyOp, lengthGetter.get()), NULL, flags, 0, 0)) { return NULL; } } return proto; }
bool JSRuntime::initializeAtoms(JSContext *cx) { atoms_ = cx->new_<AtomSet>(); if (!atoms_ || !atoms_->init(JS_STRING_HASH_COUNT)) return false; // |permanentAtoms| hasn't been created yet. MOZ_ASSERT(!permanentAtoms); if (parentRuntime) { staticStrings = parentRuntime->staticStrings; commonNames = parentRuntime->commonNames; emptyString = parentRuntime->emptyString; permanentAtoms = parentRuntime->permanentAtoms; wellKnownSymbols = parentRuntime->wellKnownSymbols; return true; } staticStrings = cx->new_<StaticStrings>(); if (!staticStrings || !staticStrings->init(cx)) return false; static const CommonNameInfo cachedNames[] = { #define COMMON_NAME_INFO(idpart, id, text) { js_##idpart##_str, sizeof(text) - 1 }, FOR_EACH_COMMON_PROPERTYNAME(COMMON_NAME_INFO) #undef COMMON_NAME_INFO #define COMMON_NAME_INFO(name, code, init, clasp) { js_##name##_str, sizeof(#name) - 1 }, JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INFO) #undef COMMON_NAME_INFO }; commonNames = cx->new_<JSAtomState>(); if (!commonNames) return false; ImmutablePropertyNamePtr *names = reinterpret_cast<ImmutablePropertyNamePtr *>(commonNames); for (size_t i = 0; i < ArrayLength(cachedNames); i++, names++) { JSAtom *atom = Atomize(cx, cachedNames[i].str, cachedNames[i].length, InternAtom); if (!atom) return false; names->init(atom->asPropertyName()); } MOZ_ASSERT(uintptr_t(names) == uintptr_t(commonNames + 1)); emptyString = commonNames->empty; // Create the well-known symbols. wellKnownSymbols = cx->new_<WellKnownSymbols>(); if (!wellKnownSymbols) return false; ImmutablePropertyNamePtr *descriptions = commonNames->wellKnownSymbolDescriptions(); ImmutableSymbolPtr *symbols = reinterpret_cast<ImmutableSymbolPtr *>(wellKnownSymbols); for (size_t i = 0; i < JS::WellKnownSymbolLimit; i++) { JS::Symbol *symbol = JS::Symbol::new_(cx, JS::SymbolCode(i), descriptions[i]); if (!symbol) { ReportOutOfMemory(cx); return false; } symbols[i].init(symbol); } return true; }