bool Dictionary::getOwnPropertyNames(Vector<String>& names) const { if (!m_dictionary.isValid()) return false; JSObject* object = m_dictionary.initializerObject(); ExecState* exec = m_dictionary.execState(); PropertyNameArray propertyNames(exec); JSObject::getOwnPropertyNames(object, exec, propertyNames, EnumerationMode()); for (PropertyNameArray::const_iterator it = propertyNames.begin(); it != propertyNames.end(); ++it) { String stringKey = it->string(); if (!stringKey.isEmpty()) names.append(stringKey); } return true; }
bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& map) const { if (!m_dictionary.isValid()) return false; JSObject* object = m_dictionary.initializerObject(); ExecState* exec = m_dictionary.execState(); PropertyNameArray propertyNames(exec); JSObject::getOwnPropertyNames(object, exec, propertyNames, EnumerationMode()); for (PropertyNameArray::const_iterator it = propertyNames.begin(); it != propertyNames.end(); ++it) { String stringKey = it->string(); if (stringKey.isEmpty()) continue; JSValue value = object->get(exec, *it); if (exec->hadException()) continue; String stringValue = value.toString(exec)->value(exec); if (!exec->hadException()) map.set(stringKey, stringValue); } return true; }
bool NPJSObject::enumerate(NPIdentifier** identifiers, uint32_t* identifierCount) { ExecState* exec = m_objectMap->globalExec(); if (!exec) return false; JSLockHolder lock(exec); PropertyNameArray propertyNames(exec, PropertyNameMode::Strings); m_jsObject->methodTable()->getPropertyNames(m_jsObject.get(), exec, propertyNames, EnumerationMode()); NPIdentifier* nameIdentifiers = npnMemNewArray<NPIdentifier>(propertyNames.size()); for (size_t i = 0; i < propertyNames.size(); ++i) nameIdentifiers[i] = static_cast<NPIdentifier>(IdentifierRep::get(propertyNames[i].string().utf8().data())); *identifiers = nameIdentifiers; *identifierCount = propertyNames.size(); return true; }
static RefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth) { if (!value) { ASSERT_NOT_REACHED(); return nullptr; } if (!maxDepth) return nullptr; maxDepth--; if (value.isNull() || value.isUndefined()) return InspectorValue::null(); if (value.isBoolean()) return InspectorValue::create(value.asBoolean()); if (value.isNumber() && value.isDouble()) return InspectorValue::create(value.asNumber()); if (value.isNumber() && value.isMachineInt()) return InspectorValue::create(static_cast<int>(value.asMachineInt())); if (value.isString()) return InspectorValue::create(value.getString(scriptState)); if (value.isObject()) { if (isJSArray(value)) { Ref<InspectorArray> inspectorArray = InspectorArray::create(); JSArray* array = asArray(value); unsigned length = array->length(); for (unsigned i = 0; i < length; i++) { JSValue element = array->getIndex(scriptState, i); RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth); if (!elementValue) return nullptr; inspectorArray->pushValue(WTFMove(elementValue)); } return WTFMove(inspectorArray); } Ref<InspectorObject> inspectorObject = InspectorObject::create(); JSObject* object = value.getObject(); PropertyNameArray propertyNames(scriptState, PropertyNameMode::Strings); object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, EnumerationMode()); for (size_t i = 0; i < propertyNames.size(); i++) { const Identifier& name = propertyNames[i]; JSValue propertyValue = object->get(scriptState, name); RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth); if (!inspectorValue) return nullptr; inspectorObject->setValue(name.string(), WTFMove(inspectorValue)); } return WTFMove(inspectorObject); } ASSERT_NOT_REACHED(); return nullptr; }