1/*
2 * Copyright (C) 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
3 * Copyright (c) 2011 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "ScriptValue.h"
32
33#include "APICast.h"
34#include "CatchScope.h"
35#include "JSCInlines.h"
36#include "JSLock.h"
37
38using namespace JSC;
39using namespace Inspector;
40
41namespace Inspector {
42
43static RefPtr<JSON::Value> jsToInspectorValue(ExecState& scriptState, JSValue value, int maxDepth)
44{
45 if (!value) {
46 ASSERT_NOT_REACHED();
47 return nullptr;
48 }
49
50 if (!maxDepth)
51 return nullptr;
52
53 maxDepth--;
54
55 if (value.isUndefinedOrNull())
56 return JSON::Value::null();
57 if (value.isBoolean())
58 return JSON::Value::create(value.asBoolean());
59 if (value.isNumber() && value.isDouble())
60 return JSON::Value::create(value.asNumber());
61 if (value.isNumber() && value.isAnyInt())
62 return JSON::Value::create(static_cast<int>(value.asAnyInt()));
63 if (value.isString())
64 return JSON::Value::create(asString(value)->value(&scriptState));
65
66 if (value.isObject()) {
67 if (isJSArray(value)) {
68 auto inspectorArray = JSON::Array::create();
69 auto& array = *asArray(value);
70 unsigned length = array.length();
71 for (unsigned i = 0; i < length; i++) {
72 auto elementValue = jsToInspectorValue(scriptState, array.getIndex(&scriptState, i), maxDepth);
73 if (!elementValue)
74 return nullptr;
75 inspectorArray->pushValue(WTFMove(elementValue));
76 }
77 return inspectorArray;
78 }
79 VM& vm = scriptState.vm();
80 auto inspectorObject = JSON::Object::create();
81 auto& object = *value.getObject();
82 PropertyNameArray propertyNames(&vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
83 object.methodTable(vm)->getOwnPropertyNames(&object, &scriptState, propertyNames, EnumerationMode());
84 for (auto& name : propertyNames) {
85 auto inspectorValue = jsToInspectorValue(scriptState, object.get(&scriptState, name), maxDepth);
86 if (!inspectorValue)
87 return nullptr;
88 inspectorObject->setValue(name.string(), WTFMove(inspectorValue));
89 }
90 return inspectorObject;
91 }
92
93 ASSERT_NOT_REACHED();
94 return nullptr;
95}
96
97RefPtr<JSON::Value> toInspectorValue(ExecState& state, JSValue value)
98{
99 // FIXME: Maybe we should move the JSLockHolder stuff to the callers since this function takes a JSValue directly.
100 // Doing the locking here made sense when we were trying to abstract the difference between multiple JavaScript engines.
101 JSLockHolder holder(&state);
102 return jsToInspectorValue(state, value, JSON::Value::maxDepth);
103}
104
105} // namespace Inspector
106