#include "config.h"
#include "ScriptValue.h"
#include "APICast.h"
#include "CatchScope.h"
#include "InspectorValues.h"
#include "JSCInlines.h"
#include "JSLock.h"
using namespace JSC;
using namespace Inspector;
namespace Inspector {
static RefPtr<InspectorValue> jsToInspectorValue(ExecState& scriptState, JSValue value, int maxDepth)
{
if (!value) {
ASSERT_NOT_REACHED();
return nullptr;
}
if (!maxDepth)
return nullptr;
maxDepth--;
if (value.isUndefinedOrNull())
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.isAnyInt())
return InspectorValue::create(static_cast<int>(value.asAnyInt()));
if (value.isString())
return InspectorValue::create(asString(value)->value(&scriptState));
if (value.isObject()) {
if (isJSArray(value)) {
auto inspectorArray = InspectorArray::create();
auto& array = *asArray(value);
unsigned length = array.length();
for (unsigned i = 0; i < length; i++) {
auto elementValue = jsToInspectorValue(scriptState, array.getIndex(&scriptState, i), maxDepth);
if (!elementValue)
return nullptr;
inspectorArray->pushValue(WTFMove(elementValue));
}
return WTFMove(inspectorArray);
}
auto inspectorObject = InspectorObject::create();
auto& object = *value.getObject();
PropertyNameArray propertyNames(&scriptState, PropertyNameMode::Strings);
object.methodTable()->getOwnPropertyNames(&object, &scriptState, propertyNames, EnumerationMode());
for (auto& name : propertyNames) {
auto inspectorValue = jsToInspectorValue(scriptState, object.get(&scriptState, name), maxDepth);
if (!inspectorValue)
return nullptr;
inspectorObject->setValue(name.string(), WTFMove(inspectorValue));
}
return WTFMove(inspectorObject);
}
ASSERT_NOT_REACHED();
return nullptr;
}
RefPtr<InspectorValue> toInspectorValue(ExecState& state, JSValue value)
{
JSLockHolder holder(&state);
return jsToInspectorValue(state, value, InspectorValue::maxDepth);
}
}
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
{
VM& vm = scriptState->vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
String result = m_value.get().toWTFString(scriptState);
if (UNLIKELY(scope.exception()))
scope.clearException();
return result;
}
bool ScriptValue::isEqual(ExecState* scriptState, const ScriptValue& anotherValue) const
{
if (hasNoValue())
return anotherValue.hasNoValue();
return JSValueIsStrictEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()));
}
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) != CallType::None;
}
RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
{
JSLockHolder holder(scriptState);
return jsToInspectorValue(*scriptState, m_value.get(), InspectorValue::maxDepth);
}
}