#include "config.h"
#include "ScriptValue.h"
#include "APICast.h"
#include "InspectorValues.h"
#include "JSLock.h"
#include "StructureInlines.h"
using namespace JSC;
using namespace Inspector;
namespace Deprecated {
ScriptValue::~ScriptValue()
{
}
bool ScriptValue::getString(ExecState* scriptState, String& result) const
{
if (!m_value)
return false;
JSLockHolder lock(scriptState);
if (!m_value.get().getString(scriptState, result))
return false;
return true;
}
String ScriptValue::toString(ExecState* scriptState) const
{
String result = m_value.get().toString(scriptState)->value(scriptState);
if (scriptState->hadException())
scriptState->clearException();
return result;
}
bool ScriptValue::isEqual(ExecState* scriptState, const ScriptValue& anotherValue) const
{
if (hasNoValue())
return anotherValue.hasNoValue();
return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), nullptr);
}
bool ScriptValue::isNull() const
{
if (!m_value)
return false;
return m_value.get().isNull();
}
bool ScriptValue::isUndefined() const
{
if (!m_value)
return false;
return m_value.get().isUndefined();
}
bool ScriptValue::isObject() const
{
if (!m_value)
return false;
return m_value.get().isObject();
}
bool ScriptValue::isFunction() const
{
CallData callData;
return getCallData(m_value.get(), callData) != CallTypeNone;
}
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 InspectorBasicValue::create(value.asBoolean());
if (value.isNumber() && value.isDouble())
return InspectorBasicValue::create(value.asNumber());
if (value.isNumber() && value.isMachineInt())
return InspectorBasicValue::create(static_cast<int>(value.asMachineInt()));
if (value.isString())
return InspectorString::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(WTF::move(elementValue));
}
return WTF::move(inspectorArray);
}
Ref<InspectorObject> inspectorObject = InspectorObject::create();
JSObject* object = value.getObject();
PropertyNameArray propertyNames(scriptState);
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(), WTF::move(inspectorValue));
}
return WTF::move(inspectorObject);
}
ASSERT_NOT_REACHED();
return nullptr;
}
RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
{
JSLockHolder holder(scriptState);
return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth);
}
}