diff options
Diffstat (limited to 'chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp')
-rw-r--r-- | chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp | 96 |
1 files changed, 24 insertions, 72 deletions
diff --git a/chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp b/chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp index 04b2287f9c2..bdfbe082258 100644 --- a/chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp +++ b/chromium/third_party/WebKit/Source/bindings/v8/ScriptValue.cpp @@ -31,7 +31,6 @@ #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" @@ -42,88 +41,41 @@ ScriptValue::~ScriptValue() { } -bool ScriptValue::getString(String& result) const +v8::Handle<v8::Value> ScriptValue::v8Value() const { - if (hasNoValue()) - return false; + if (isEmpty()) + return v8::Handle<v8::Value>(); - 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; -} + ASSERT(isolate()->InContext()); -String ScriptValue::toString() const -{ - v8::TryCatch block; - v8::Handle<v8::String> string = v8Value()->ToString(); - if (block.HasCaught()) - return String(); - return v8StringToWebCoreString<String>(string, DoNotExternalize); + // This is a check to validate that you don't return a ScriptValue to a world different + // from the world that created the ScriptValue. + // Probably this could be: + // if (&m_scriptState->world() == &DOMWrapperWorld::current(isolate())) + // return v8::Handle<v8::Value>(); + // instead of triggering RELEASE_ASSERT. + RELEASE_ASSERT(&m_scriptState->world() == &DOMWrapperWorld::current(isolate())); + return m_value->newLocal(isolate()); } -static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxDepth, v8::Isolate* isolate) +bool ScriptValue::toString(String& result) const { - if (value.IsEmpty()) { - ASSERT_NOT_REACHED(); - return 0; - } - - if (!maxDepth) - return 0; - maxDepth--; + if (isEmpty()) + return false; - 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(i, isolate)); - RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate); - if (!element) - return 0; - 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(i, isolate)); - // FIXME(yurys): v8::Object should support GetOwnPropertyNames - if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8::String>::Cast(name))) - continue; - RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), maxDepth, isolate); - if (!propertyValue) - return 0; - V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, nameString, name, 0); - jsonObject->setValue(nameString, propertyValue); - } - return jsonObject; - } - ASSERT_NOT_REACHED(); - return 0; + ScriptState::Scope scope(m_scriptState.get()); + v8::Handle<v8::Value> string = v8Value(); + if (string.IsEmpty() || !string->IsString()) + return false; + result = toCoreString(v8::Handle<v8::String>::Cast(string)); + return true; } PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const { - v8::HandleScope handleScope(scriptState->isolate()); - // v8::Object::GetPropertyNames() expects current context to be not null. - v8::Context::Scope contextScope(scriptState->context()); - return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate()); + ASSERT(!scriptState->contextIsEmpty()); + ScriptState::Scope scope(scriptState); + return v8ToJSONValue(scriptState->isolate(), v8Value(), JSONValue::maxDepth); } } // namespace WebCore |