This source file includes following definitions.
- getString
- toString
- v8ToJSONValue
- toJSONValue
#include "config.h"
#include "bindings/v8/ScriptValue.h"
#include "bindings/v8/ScriptScope.h"
#include "bindings/v8/ScriptState.h"
#include "bindings/v8/V8Binding.h"
#include "platform/JSONValues.h"
namespace WebCore {
ScriptValue::~ScriptValue()
{
}
bool ScriptValue::getString(String& result) const
{
if (hasNoValue())
return false;
v8::HandleScope handleScope(m_isolate);
v8::Handle<v8::Value> string = v8Value();
if (string.IsEmpty() || !string->IsString())
return false;
result = toCoreString(string.As<v8::String>());
return true;
}
String ScriptValue::toString() const
{
v8::TryCatch block;
v8::Handle<v8::String> string = v8Value()->ToString();
if (block.HasCaught())
return String();
return v8StringToWebCoreString<String>(string, DoNotExternalize);
}
static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxDepth, v8::Isolate* isolate)
{
if (value.IsEmpty()) {
ASSERT_NOT_REACHED();
return nullptr;
}
if (!maxDepth)
return nullptr;
maxDepth--;
if (value->IsNull() || value->IsUndefined())
return JSONValue::null();
if (value->IsBoolean())
return JSONBasicValue::create(value->BooleanValue());
if (value->IsNumber())
return JSONBasicValue::create(value->NumberValue());
if (value->IsString())
return JSONString::create(toCoreString(value.As<v8::String>()));
if (value->IsArray()) {
v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value);
RefPtr<JSONArray> inspectorArray = JSONArray::create();
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++) {
v8::Local<v8::Value> value = array->Get(v8::Int32::New(isolate, i));
RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate);
if (!element)
return nullptr;
inspectorArray->pushValue(element);
}
return inspectorArray;
}
if (value->IsObject()) {
RefPtr<JSONObject> jsonObject = JSONObject::create();
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
v8::Local<v8::Array> propertyNames = object->GetPropertyNames();
uint32_t length = propertyNames->Length();
for (uint32_t i = 0; i < length; i++) {
v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(isolate, i));
if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8::String>::Cast(name)))
continue;
RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), maxDepth, isolate);
if (!propertyValue)
return nullptr;
V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, nameString, name, nullptr);
jsonObject->setValue(nameString, propertyValue);
}
return jsonObject;
}
ASSERT_NOT_REACHED();
return nullptr;
}
PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const
{
v8::HandleScope handleScope(scriptState->isolate());
v8::Context::Scope contextScope(scriptState->context());
return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate());
}
}