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/ScriptState.h"
36#include "bindings/v8/V8Binding.h"
37#include "platform/JSONValues.h"
38
39namespace WebCore {
40
41ScriptValue::~ScriptValue()
42{
43}
44
45bool ScriptValue::getString(String& result) const
46{
47    if (hasNoValue())
48        return false;
49
50    v8::HandleScope handleScope(m_isolate);
51    v8::Handle<v8::Value> string = v8Value();
52    if (string.IsEmpty() || !string->IsString())
53        return false;
54    result = toCoreString(string.As<v8::String>());
55    return true;
56}
57
58String ScriptValue::toString() const
59{
60    v8::TryCatch block;
61    v8::Handle<v8::String> string = v8Value()->ToString();
62    if (block.HasCaught())
63        return String();
64    return v8StringToWebCoreString<String>(string, DoNotExternalize);
65}
66
67static PassRefPtr<JSONValue> v8ToJSONValue(v8::Handle<v8::Value> value, int maxDepth, v8::Isolate* isolate)
68{
69    if (value.IsEmpty()) {
70        ASSERT_NOT_REACHED();
71        return 0;
72    }
73
74    if (!maxDepth)
75        return 0;
76    maxDepth--;
77
78    if (value->IsNull() || value->IsUndefined())
79        return JSONValue::null();
80    if (value->IsBoolean())
81        return JSONBasicValue::create(value->BooleanValue());
82    if (value->IsNumber())
83        return JSONBasicValue::create(value->NumberValue());
84    if (value->IsString())
85        return JSONString::create(toCoreString(value.As<v8::String>()));
86    if (value->IsArray()) {
87        v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(value);
88        RefPtr<JSONArray> inspectorArray = JSONArray::create();
89        uint32_t length = array->Length();
90        for (uint32_t i = 0; i < length; i++) {
91            v8::Local<v8::Value> value = array->Get(v8::Int32::New(i, isolate));
92            RefPtr<JSONValue> element = v8ToJSONValue(value, maxDepth, isolate);
93            if (!element)
94                return 0;
95            inspectorArray->pushValue(element);
96        }
97        return inspectorArray;
98    }
99    if (value->IsObject()) {
100        RefPtr<JSONObject> jsonObject = JSONObject::create();
101        v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
102        v8::Local<v8::Array> propertyNames = object->GetPropertyNames();
103        uint32_t length = propertyNames->Length();
104        for (uint32_t i = 0; i < length; i++) {
105            v8::Local<v8::Value> name = propertyNames->Get(v8::Int32::New(i, isolate));
106            // FIXME(yurys): v8::Object should support GetOwnPropertyNames
107            if (name->IsString() && !object->HasRealNamedProperty(v8::Handle<v8::String>::Cast(name)))
108                continue;
109            RefPtr<JSONValue> propertyValue = v8ToJSONValue(object->Get(name), maxDepth, isolate);
110            if (!propertyValue)
111                return 0;
112            V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<WithNullCheck>, nameString, name, 0);
113            jsonObject->setValue(nameString, propertyValue);
114        }
115        return jsonObject;
116    }
117    ASSERT_NOT_REACHED();
118    return 0;
119}
120
121PassRefPtr<JSONValue> ScriptValue::toJSONValue(ScriptState* scriptState) const
122{
123    v8::HandleScope handleScope(scriptState->isolate());
124    // v8::Object::GetPropertyNames() expects current context to be not null.
125    v8::Context::Scope contextScope(scriptState->context());
126    return v8ToJSONValue(v8Value(), JSONValue::maxDepth, scriptState->isolate());
127}
128
129} // namespace WebCore
130