Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "bindings/v8/ScriptValue.h"
     33 
     34 #include "bindings/v8/ScriptScope.h"
     35 #include "bindings/v8/SerializedScriptValue.h"
     36 #include "bindings/v8/V8Binding.h"
     37 #include "core/dom/MessagePort.h"
     38 #include "core/platform/JSONValues.h"
     39 #include "wtf/ArrayBuffer.h"
     40 
     41 namespace WebCore {
     42 
     43 ScriptValue::~ScriptValue()
     44 {
     45 }
     46 
     47 PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState)
     48 {
     49     ScriptScope scope(scriptState);
     50     return SerializedScriptValue::create(v8Value(), scriptState->isolate());
     51 }
     52 
     53 PassRefPtr<SerializedScriptValue> ScriptValue::serialize(ScriptState* scriptState, MessagePortArray* messagePorts, ArrayBufferArray* arrayBuffers, bool& didThrow)
     54 {
     55     ScriptScope scope(scriptState);
     56     return SerializedScriptValue::create(v8Value(), messagePorts, arrayBuffers, didThrow, scriptState->isolate());
     57 }
     58 
     59 ScriptValue ScriptValue::deserialize(ScriptState* scriptState, SerializedScriptValue* value)
     60 {
     61     ScriptScope scope(scriptState);
     62     return ScriptValue(value->deserialize());
     63 }
     64 
     65 bool ScriptValue::getString(String& result, v8::Isolate* isolate) const
     66 {
     67     if (hasNoValue())
     68         return false;
     69 
     70     v8::HandleScope handleScope(isolate);
     71     v8::Handle<v8::Value> string = v8Value();
     72     if (string.IsEmpty() || !string->IsString())
     73         return false;
     74     result = toWebCoreString(string);
     75     return true;
     76 }
     77 
     78 String ScriptValue::toString(ScriptState*) const
     79 {
     80     v8::TryCatch block;
     81     v8::Handle<v8::String> string = v8Value()->ToString();
     82     if (block.HasCaught())
     83         return String();
     84     return v8StringToWebCoreString<String>(string, DoNotExternalize);
     85 }
     86 
     87 static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxDepth)
     88 {
     89     if (value.IsEmpty()) {
     90         ASSERT_NOT_REACHED();
     91         return 0;
     92     }
     93 
     94     if (!maxDepth)
     95         return 0;
     96     maxDepth--;
     97 
     98     if (value->IsNull() || value->IsUndefined())
     99         return JSONValue::null();
    100     if (value->IsBoolean())
    101         return JSONBasicValue::create(value->BooleanValue());
    102     if (value->IsNumber())
    103         return JSONBasicValue::create(value->NumberValue());
    104     if (value->IsString())
    105         return JSONString::create(toWebCoreString(value));
    106     if (value->IsArray()) {
    107         v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value);
    108         RefPtr<JSONArray> inspectorArray = JSONArray::create();
    109         uint32_t length = array->Length();
    110         for (uint32_t i = 0; i < length; i++) {
    111             v8::Local<v8::Value> value = array->Get(v8::Int32::New(i));
    112             RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth);
    113             if (!element)
    114                 return 0;
    115             inspectorArray->pushValue(element);
    116         }
    117         return inspectorArray;
    118     }
    119     if (value->IsObject()) {
    120         RefPtr<JSONObject> jsonObject = JSONObject::create();
    121         v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
    122         v8::Local<v8::Array> propertyNames = object->GetPropertyNames();
    123         uint32_t length = propertyNames->Length();
    124         for (uint32_t i = 0; i < length; i++) {
    125             v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(i));
    126             // FIXME(yurys): v8::Object should support GetOwnPropertyNames
    127             if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8::String>::Cast(name)))
    128                 continue;
    129             RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), maxDepth);
    130             if (!propertyValue)
    131                 return 0;
    132             jsonObject->setValue(toWebCoreStringWithNullCheck(name), propertyValue);
    133         }
    134         return jsonObject;
    135     }
    136     ASSERT_NOT_REACHED();
    137     return 0;
    138 }
    139 
    140 PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const
    141 {
    142     v8::HandleScope handleScope(scriptState->isolate());
    143     // v8::Object::GetPropertyNames() expects current context to be not null.
    144     v8::Context::Scope contextScope(scriptState->context());
    145     return v8ToJSONValue(v8Value(), JSONValue::maxDepth);
    146 }
    147 
    148 } // namespace WebCore
    149