Changeset 199619 in webkit for trunk/Source/JavaScriptCore/bindings/ScriptValue.cpp
- Timestamp:
- Apr 15, 2016, 7:25:56 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/bindings/ScriptValue.cpp
r199320 r199619 38 38 using namespace JSC; 39 39 using namespace Inspector; 40 41 namespace Inspector { 42 43 static RefPtr<InspectorValue> jsToInspectorValue(ExecState& scriptState, JSValue value, int maxDepth) 44 { 45 if (!value) { 46 ASSERT_NOT_REACHED(); 47 return nullptr; 48 } 49 50 if (!maxDepth) 51 return nullptr; 52 53 maxDepth--; 54 55 if (value.isUndefinedOrNull()) 56 return InspectorValue::null(); // FIXME: Why is it OK to turn undefined into null? 57 if (value.isBoolean()) 58 return InspectorValue::create(value.asBoolean()); 59 if (value.isNumber() && value.isDouble()) 60 return InspectorValue::create(value.asNumber()); 61 if (value.isNumber() && value.isMachineInt()) 62 return InspectorValue::create(static_cast<int>(value.asMachineInt())); 63 if (value.isString()) 64 return InspectorValue::create(value.getString(&scriptState)); 65 66 if (value.isObject()) { 67 if (isJSArray(value)) { 68 auto inspectorArray = InspectorArray::create(); 69 auto& array = *asArray(value); 70 unsigned length = array.length(); 71 for (unsigned i = 0; i < length; i++) { 72 auto elementValue = jsToInspectorValue(scriptState, array.getIndex(&scriptState, i), maxDepth); 73 if (!elementValue) 74 return nullptr; 75 inspectorArray->pushValue(WTFMove(elementValue)); 76 } 77 return WTFMove(inspectorArray); 78 } 79 auto inspectorObject = InspectorObject::create(); 80 auto& object = *value.getObject(); 81 PropertyNameArray propertyNames(&scriptState, PropertyNameMode::Strings); 82 object.methodTable()->getOwnPropertyNames(&object, &scriptState, propertyNames, EnumerationMode()); 83 for (auto& name : propertyNames) { 84 auto inspectorValue = jsToInspectorValue(scriptState, object.get(&scriptState, name), maxDepth); 85 if (!inspectorValue) 86 return nullptr; 87 inspectorObject->setValue(name.string(), WTFMove(inspectorValue)); 88 } 89 return WTFMove(inspectorObject); 90 } 91 92 ASSERT_NOT_REACHED(); 93 return nullptr; 94 } 95 96 RefPtr<InspectorValue> toInspectorValue(ExecState& state, JSValue value) 97 { 98 // FIXME: Maybe we should move the JSLockHolder stuff to the callers since this function takes a JSValue directly. 99 // Doing the locking here made sense when we were trying to abstract the difference between multiple JavaScript engines. 100 JSLockHolder holder(&state); 101 return jsToInspectorValue(state, value, InspectorValue::maxDepth); 102 } 103 104 } // namespace Inspector 40 105 41 106 namespace Deprecated { … … 98 163 } 99 164 100 static RefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)101 {102 if (!value) {103 ASSERT_NOT_REACHED();104 return nullptr;105 }106 107 if (!maxDepth)108 return nullptr;109 110 maxDepth--;111 112 if (value.isNull() || value.isUndefined())113 return InspectorValue::null();114 if (value.isBoolean())115 return InspectorValue::create(value.asBoolean());116 if (value.isNumber() && value.isDouble())117 return InspectorValue::create(value.asNumber());118 if (value.isNumber() && value.isMachineInt())119 return InspectorValue::create(static_cast<int>(value.asMachineInt()));120 if (value.isString())121 return InspectorValue::create(value.getString(scriptState));122 123 if (value.isObject()) {124 if (isJSArray(value)) {125 Ref<InspectorArray> inspectorArray = InspectorArray::create();126 JSArray* array = asArray(value);127 unsigned length = array->length();128 for (unsigned i = 0; i < length; i++) {129 JSValue element = array->getIndex(scriptState, i);130 RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth);131 if (!elementValue)132 return nullptr;133 inspectorArray->pushValue(WTFMove(elementValue));134 }135 return WTFMove(inspectorArray);136 }137 Ref<InspectorObject> inspectorObject = InspectorObject::create();138 JSObject* object = value.getObject();139 PropertyNameArray propertyNames(scriptState, PropertyNameMode::Strings);140 object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, EnumerationMode());141 for (size_t i = 0; i < propertyNames.size(); i++) {142 const Identifier& name = propertyNames[i];143 JSValue propertyValue = object->get(scriptState, name);144 RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth);145 if (!inspectorValue)146 return nullptr;147 inspectorObject->setValue(name.string(), WTFMove(inspectorValue));148 }149 return WTFMove(inspectorObject);150 }151 152 ASSERT_NOT_REACHED();153 return nullptr;154 }155 156 165 RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const 157 166 { 158 167 JSLockHolder holder(scriptState); 159 return jsToInspectorValue( scriptState, m_value.get(), InspectorValue::maxDepth);168 return jsToInspectorValue(*scriptState, m_value.get(), InspectorValue::maxDepth); 160 169 } 161 170
Note:
See TracChangeset
for help on using the changeset viewer.