| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2010 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 "core/inspector/ScriptArguments.h" | |
| 32 | |
| 33 #include "bindings/core/v8/ScriptValue.h" | |
| 34 #include "bindings/core/v8/V8Binding.h" | |
| 35 #include "wtf/text/StringBuilder.h" | |
| 36 #include <v8.h> | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 static const unsigned maxArrayItemsLimit = 10000; | |
| 43 static const unsigned maxStackDepthLimit = 32; | |
| 44 | |
| 45 class V8ValueStringBuilder { | |
| 46 public: | |
| 47 static String toString(v8::Local<v8::Value> value, v8::Isolate* isolate) | |
| 48 { | |
| 49 V8ValueStringBuilder builder(isolate); | |
| 50 if (!builder.append(value)) | |
| 51 return String(); | |
| 52 return builder.toString(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 enum { | |
| 57 IgnoreNull = 1 << 0, | |
| 58 IgnoreUndefined = 1 << 1, | |
| 59 }; | |
| 60 | |
| 61 V8ValueStringBuilder(v8::Isolate* isolate) | |
| 62 : m_arrayLimit(maxArrayItemsLimit) | |
| 63 , m_isolate(isolate) | |
| 64 , m_tryCatch(isolate) | |
| 65 { | |
| 66 } | |
| 67 | |
| 68 bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) | |
| 69 { | |
| 70 if (value.IsEmpty()) | |
| 71 return true; | |
| 72 if ((ignoreOptions & IgnoreNull) && value->IsNull()) | |
| 73 return true; | |
| 74 if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) | |
| 75 return true; | |
| 76 if (value->IsString()) | |
| 77 return append(v8::Local<v8::String>::Cast(value)); | |
| 78 if (value->IsStringObject()) | |
| 79 return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf()); | |
| 80 if (value->IsSymbol()) | |
| 81 return append(v8::Local<v8::Symbol>::Cast(value)); | |
| 82 if (value->IsSymbolObject()) | |
| 83 return append(v8::Local<v8::SymbolObject>::Cast(value)->ValueOf()); | |
| 84 if (value->IsNumberObject()) { | |
| 85 m_builder.appendNumber(v8::Local<v8::NumberObject>::Cast(value)->Val
ueOf()); | |
| 86 return true; | |
| 87 } | |
| 88 if (value->IsBooleanObject()) { | |
| 89 m_builder.append(v8::Local<v8::BooleanObject>::Cast(value)->ValueOf(
) ? "true" : "false"); | |
| 90 return true; | |
| 91 } | |
| 92 if (value->IsArray()) | |
| 93 return append(v8::Local<v8::Array>::Cast(value)); | |
| 94 if (value->IsProxy()) { | |
| 95 m_builder.append("[object Proxy]"); | |
| 96 return true; | |
| 97 } | |
| 98 if (value->IsObject() | |
| 99 && !value->IsDate() | |
| 100 && !value->IsFunction() | |
| 101 && !value->IsNativeError() | |
| 102 && !value->IsRegExp()) { | |
| 103 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | |
| 104 v8::Local<v8::String> stringValue; | |
| 105 if (object->ObjectProtoToString(m_isolate->GetCurrentContext()).ToLo
cal(&stringValue)) | |
| 106 return append(stringValue); | |
| 107 } | |
| 108 v8::Local<v8::String> stringValue; | |
| 109 if (!value->ToString(m_isolate->GetCurrentContext()).ToLocal(&stringValu
e)) | |
| 110 return false; | |
| 111 return append(stringValue); | |
| 112 } | |
| 113 | |
| 114 bool append(v8::Local<v8::Array> array) | |
| 115 { | |
| 116 if (m_visitedArrays.contains(array)) | |
| 117 return true; | |
| 118 uint32_t length = array->Length(); | |
| 119 if (length > m_arrayLimit) | |
| 120 return false; | |
| 121 if (m_visitedArrays.size() > maxStackDepthLimit) | |
| 122 return false; | |
| 123 | |
| 124 bool result = true; | |
| 125 m_arrayLimit -= length; | |
| 126 m_visitedArrays.append(array); | |
| 127 for (uint32_t i = 0; i < length; ++i) { | |
| 128 if (i) | |
| 129 m_builder.append(','); | |
| 130 if (!append(array->Get(i), IgnoreNull | IgnoreUndefined)) { | |
| 131 result = false; | |
| 132 break; | |
| 133 } | |
| 134 } | |
| 135 m_visitedArrays.removeLast(); | |
| 136 return result; | |
| 137 } | |
| 138 | |
| 139 bool append(v8::Local<v8::Symbol> symbol) | |
| 140 { | |
| 141 m_builder.append("Symbol("); | |
| 142 bool result = append(symbol->Name(), IgnoreUndefined); | |
| 143 m_builder.append(')'); | |
| 144 return result; | |
| 145 } | |
| 146 | |
| 147 bool append(v8::Local<v8::String> string) | |
| 148 { | |
| 149 if (m_tryCatch.HasCaught()) | |
| 150 return false; | |
| 151 if (!string.IsEmpty()) | |
| 152 m_builder.append(toCoreString(string)); | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 String toString() | |
| 157 { | |
| 158 if (m_tryCatch.HasCaught()) | |
| 159 return String(); | |
| 160 return m_builder.toString(); | |
| 161 } | |
| 162 | |
| 163 uint32_t m_arrayLimit; | |
| 164 v8::Isolate* m_isolate; | |
| 165 StringBuilder m_builder; | |
| 166 Vector<v8::Local<v8::Array>> m_visitedArrays; | |
| 167 v8::TryCatch m_tryCatch; | |
| 168 }; | |
| 169 | |
| 170 } // namespace | |
| 171 | |
| 172 ScriptArguments* ScriptArguments::create(ScriptState* scriptState, Vector<Script
Value>& arguments) | |
| 173 { | |
| 174 return new ScriptArguments(scriptState, arguments); | |
| 175 } | |
| 176 | |
| 177 ScriptArguments* ScriptArguments::create(ScriptState* scriptState, const v8::Fun
ctionCallbackInfo<v8::Value>& v8arguments, unsigned skipArgumentCount) | |
| 178 { | |
| 179 Vector<ScriptValue> arguments; | |
| 180 for (int i = skipArgumentCount; i < v8arguments.Length(); ++i) | |
| 181 arguments.append(ScriptValue(scriptState, v8arguments[i])); | |
| 182 return ScriptArguments::create(scriptState, arguments); | |
| 183 } | |
| 184 | |
| 185 ScriptArguments::ScriptArguments(ScriptState* scriptState, Vector<ScriptValue>&
arguments) | |
| 186 : m_scriptState(scriptState) | |
| 187 { | |
| 188 m_arguments.swap(arguments); | |
| 189 } | |
| 190 | |
| 191 ScriptArguments::~ScriptArguments() | |
| 192 { | |
| 193 } | |
| 194 | |
| 195 const ScriptValue &ScriptArguments::argumentAt(size_t index) const | |
| 196 { | |
| 197 ASSERT(m_arguments.size() > index); | |
| 198 return m_arguments[index]; | |
| 199 } | |
| 200 | |
| 201 bool ScriptArguments::getFirstArgumentAsString(String& result) const | |
| 202 { | |
| 203 if (!argumentCount()) | |
| 204 return false; | |
| 205 | |
| 206 const ScriptValue& value = argumentAt(0); | |
| 207 ScriptState::Scope scope(m_scriptState.get()); | |
| 208 result = V8ValueStringBuilder::toString(value.v8Value(), value.isolate()); | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 } // namespace blink | |
| OLD | NEW |