[DevTools] Improve ConsoleAPI functions string description

BUG=638742
R=dgozman@chromium.org

Review-Url: https://codereview.chromium.org/2269843002
Cr-Original-Commit-Position: refs/heads/master@{#413968}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 149d9e717953a1979d186a99ef7075e184253bd7
diff --git a/InjectedScript.cpp b/InjectedScript.cpp
index dfe5b92..de4e74b 100644
--- a/InjectedScript.cpp
+++ b/InjectedScript.cpp
@@ -42,6 +42,7 @@
 #include "platform/v8_inspector/V8InspectorSessionImpl.h"
 #include "platform/v8_inspector/V8StackTraceImpl.h"
 #include "platform/v8_inspector/V8StringUtil.h"
+#include "platform/v8_inspector/V8ValueCopier.h"
 #include "platform/v8_inspector/public/V8InspectorClient.h"
 
 using blink::protocol::Array;
@@ -159,7 +160,7 @@
     return remoteObject;
 }
 
-bool InjectedScript::wrapObjectProperty(ErrorString* errorString, v8::Local<v8::Object> object, v8::Local<v8::Value> key, const String16& groupName, bool forceValueType, bool generatePreview) const
+bool InjectedScript::wrapObjectProperty(ErrorString* errorString, v8::Local<v8::Object> object, v8::Local<v8::Name> key, const String16& groupName, bool forceValueType, bool generatePreview) const
 {
     v8::Local<v8::Value> property;
     if (hasInternalError(errorString, !object->Get(m_context->context(), key).ToLocal(&property)))
@@ -167,7 +168,7 @@
     v8::Local<v8::Value> wrappedProperty;
     if (!wrapValue(errorString, property, groupName, forceValueType, generatePreview).ToLocal(&wrappedProperty))
         return false;
-    v8::Maybe<bool> success = object->Set(m_context->context(), key, wrappedProperty);
+    v8::Maybe<bool> success = createDataProperty(m_context->context(), object, key, wrappedProperty);
     if (hasInternalError(errorString, success.IsNothing() || !success.FromJust()))
         return false;
     return true;
diff --git a/InjectedScript.h b/InjectedScript.h
index 9c674d3..9f99aa7 100644
--- a/InjectedScript.h
+++ b/InjectedScript.h
@@ -62,7 +62,7 @@
     void releaseObject(const String16& objectId);
 
     std::unique_ptr<protocol::Runtime::RemoteObject> wrapObject(ErrorString*, v8::Local<v8::Value>, const String16& groupName, bool forceValueType = false, bool generatePreview = false) const;
-    bool wrapObjectProperty(ErrorString*, v8::Local<v8::Object>, v8::Local<v8::Value> key, const String16& groupName, bool forceValueType = false, bool generatePreview = false) const;
+    bool wrapObjectProperty(ErrorString*, v8::Local<v8::Object>, v8::Local<v8::Name> key, const String16& groupName, bool forceValueType = false, bool generatePreview = false) const;
     bool wrapPropertyInArray(ErrorString*, v8::Local<v8::Array>, v8::Local<v8::String> property, const String16& groupName, bool forceValueType = false, bool generatePreview = false) const;
     bool wrapObjectsInArray(ErrorString*, v8::Local<v8::Array>, const String16& groupName, bool forceValueType = false, bool generatePreview = false) const;
     std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(v8::Local<v8::Value> table, v8::Local<v8::Value> columns) const;
diff --git a/InspectedContext.cpp b/InspectedContext.cpp
index f2bf952..7129103 100644
--- a/InspectedContext.cpp
+++ b/InspectedContext.cpp
@@ -8,6 +8,7 @@
 #include "platform/v8_inspector/V8Console.h"
 #include "platform/v8_inspector/V8InspectorImpl.h"
 #include "platform/v8_inspector/V8StringUtil.h"
+#include "platform/v8_inspector/V8ValueCopier.h"
 #include "platform/v8_inspector/public/V8ContextInfo.h"
 #include "platform/v8_inspector/public/V8InspectorClient.h"
 
diff --git a/V8Console.cpp b/V8Console.cpp
index 73a90e2..fdcd662 100644
--- a/V8Console.cpp
+++ b/V8Console.cpp
@@ -15,6 +15,7 @@
 #include "platform/v8_inspector/V8RuntimeAgentImpl.h"
 #include "platform/v8_inspector/V8StackTraceImpl.h"
 #include "platform/v8_inspector/V8StringUtil.h"
+#include "platform/v8_inspector/V8ValueCopier.h"
 #include "platform/v8_inspector/public/V8InspectorClient.h"
 
 namespace v8_inspector {
@@ -276,10 +277,9 @@
         v8::Local<v8::String> returnValue = toV8String(context->GetIsolate(), description);
         v8::Local<v8::Function> toStringFunction;
         if (V8_FUNCTION_NEW_REMOVE_PROTOTYPE(context, returnDataCallback, returnValue, 0).ToLocal(&toStringFunction))
-            func->Set(toV8StringInternalized(context->GetIsolate(), "toString"), toStringFunction);
+            createDataProperty(context, func, toV8StringInternalized(context->GetIsolate(), "toString"), toStringFunction);
     }
-    if (!console->Set(context, funcName, func).FromMaybe(false))
-        return;
+    createDataProperty(context, console, funcName, func);
 }
 
 } // namespace
@@ -522,8 +522,7 @@
         v8::Local<v8::Value> value;
         if (!obj->Get(context, key).ToLocal(&value))
             continue;
-        if (!values->Set(context, i, value).FromMaybe(false))
-            continue;
+        createDataProperty(context, values, i, value);
     }
     info.GetReturnValue().Set(values);
 }
diff --git a/V8Debugger.cpp b/V8Debugger.cpp
index 65fa210..4b932fa 100644
--- a/V8Debugger.cpp
+++ b/V8Debugger.cpp
@@ -611,12 +611,12 @@
         v8::Local<v8::Function> function = value.As<v8::Function>();
         v8::Local<v8::Value> location = functionLocation(context, function);
         if (location->IsObject()) {
-            properties->Set(properties->Length(), toV8StringInternalized(m_isolate, "[[FunctionLocation]]"));
-            properties->Set(properties->Length(), location);
+            createDataProperty(context, properties, properties->Length(), toV8StringInternalized(m_isolate, "[[FunctionLocation]]"));
+            createDataProperty(context, properties, properties->Length(), location);
         }
         if (function->IsGeneratorFunction()) {
-            properties->Set(properties->Length(), toV8StringInternalized(m_isolate, "[[IsGenerator]]"));
-            properties->Set(properties->Length(), v8::True(m_isolate));
+            createDataProperty(context, properties, properties->Length(), toV8StringInternalized(m_isolate, "[[IsGenerator]]"));
+            createDataProperty(context, properties, properties->Length(), v8::True(m_isolate));
         }
     }
     if (!enabled())
@@ -624,15 +624,15 @@
     if (value->IsMap() || value->IsWeakMap() || value->IsSet() || value->IsWeakSet() || value->IsSetIterator() || value->IsMapIterator()) {
         v8::Local<v8::Value> entries = collectionEntries(context, v8::Local<v8::Object>::Cast(value));
         if (entries->IsArray()) {
-            properties->Set(properties->Length(), toV8StringInternalized(m_isolate, "[[Entries]]"));
-            properties->Set(properties->Length(), entries);
+            createDataProperty(context, properties, properties->Length(), toV8StringInternalized(m_isolate, "[[Entries]]"));
+            createDataProperty(context, properties, properties->Length(), entries);
         }
     }
     if (value->IsGeneratorObject()) {
         v8::Local<v8::Value> location = generatorObjectLocation(context, v8::Local<v8::Object>::Cast(value));
         if (location->IsObject()) {
-            properties->Set(properties->Length(), toV8StringInternalized(m_isolate, "[[GeneratorLocation]]"));
-            properties->Set(properties->Length(), location);
+            createDataProperty(context, properties, properties->Length(), toV8StringInternalized(m_isolate, "[[GeneratorLocation]]"));
+            createDataProperty(context, properties, properties->Length(), location);
         }
     }
     if (value->IsFunction()) {
@@ -640,8 +640,8 @@
         v8::Local<v8::Value> boundFunction = function->GetBoundFunction();
         v8::Local<v8::Value> scopes;
         if (boundFunction->IsUndefined() && functionScopes(context, function).ToLocal(&scopes)) {
-            properties->Set(properties->Length(), toV8StringInternalized(m_isolate, "[[Scopes]]"));
-            properties->Set(properties->Length(), scopes);
+            createDataProperty(context, properties, properties->Length(), toV8StringInternalized(m_isolate, "[[Scopes]]"));
+            createDataProperty(context, properties, properties->Length(), scopes);
         }
     }
     return properties;
@@ -691,11 +691,11 @@
     v8::Local<v8::Object> location = v8::Object::New(m_isolate);
     if (!location->SetPrototype(context, v8::Null(m_isolate)).FromMaybe(false))
         return v8::Null(m_isolate);
-    if (!location->Set(context, toV8StringInternalized(m_isolate, "scriptId"), toV8String(m_isolate, String16::fromInteger(scriptId))).FromMaybe(false))
+    if (!createDataProperty(context, location, toV8StringInternalized(m_isolate, "scriptId"), toV8String(m_isolate, String16::fromInteger(scriptId))).FromMaybe(false))
         return v8::Null(m_isolate);
-    if (!location->Set(context, toV8StringInternalized(m_isolate, "lineNumber"), v8::Integer::New(m_isolate, lineNumber)).FromMaybe(false))
+    if (!createDataProperty(context, location, toV8StringInternalized(m_isolate, "lineNumber"), v8::Integer::New(m_isolate, lineNumber)).FromMaybe(false))
         return v8::Null(m_isolate);
-    if (!location->Set(context, toV8StringInternalized(m_isolate, "columnNumber"), v8::Integer::New(m_isolate, columnNumber)).FromMaybe(false))
+    if (!createDataProperty(context, location, toV8StringInternalized(m_isolate, "columnNumber"), v8::Integer::New(m_isolate, columnNumber)).FromMaybe(false))
         return v8::Null(m_isolate);
     if (!markAsInternal(context, location, V8InternalValueType::kLocation))
         return v8::Null(m_isolate);
diff --git a/V8InjectedScriptHost.cpp b/V8InjectedScriptHost.cpp
index 782e5bb..c308575 100644
--- a/V8InjectedScriptHost.cpp
+++ b/V8InjectedScriptHost.cpp
@@ -10,6 +10,7 @@
 #include "platform/v8_inspector/V8InspectorImpl.h"
 #include "platform/v8_inspector/V8InternalValueType.h"
 #include "platform/v8_inspector/V8StringUtil.h"
+#include "platform/v8_inspector/V8ValueCopier.h"
 #include "platform/v8_inspector/public/V8InspectorClient.h"
 
 namespace v8_inspector {
@@ -23,8 +24,7 @@
     if (!V8_FUNCTION_NEW_REMOVE_PROTOTYPE(context, callback, external, 0).ToLocal(&func))
         return;
     func->SetName(funcName);
-    if (!obj->Set(context, funcName, func).FromMaybe(false))
-        return;
+    createDataProperty(context, obj, funcName, func);
 }
 
 V8InspectorImpl* unwrapInspector(const v8::FunctionCallbackInfo<v8::Value>& info)
diff --git a/V8ValueCopier.cpp b/V8ValueCopier.cpp
index 3347abe..d95da61 100644
--- a/V8ValueCopier.cpp
+++ b/V8ValueCopier.cpp
@@ -40,7 +40,7 @@
                 v8::Local<v8::Value> copied;
                 if (!copy(item, depth + 1).ToLocal(&copied))
                     return v8::MaybeLocal<v8::Value>();
-                if (!result->Set(m_to, i, copied).FromMaybe(false))
+                if (!createDataProperty(m_to, result, i, copied).FromMaybe(false))
                     return v8::MaybeLocal<v8::Value>();
             }
             return result;
@@ -63,7 +63,7 @@
             v8::Local<v8::Value> copied;
             if (!copy(property, depth + 1).ToLocal(&copied))
                 return v8::MaybeLocal<v8::Value>();
-            if (!result->Set(m_to, name, copied).FromMaybe(false))
+            if (!createDataProperty(m_to, result, v8::Local<v8::String>::Cast(name), copied).FromMaybe(false))
                 return v8::MaybeLocal<v8::Value>();
         }
         return result;
@@ -87,4 +87,18 @@
     return copier.copy(value, 0);
 }
 
+v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, v8::Local<v8::Object> object, v8::Local<v8::Name> key, v8::Local<v8::Value> value)
+{
+    v8::TryCatch tryCatch(context->GetIsolate());
+    v8::Isolate::DisallowJavascriptExecutionScope throwJs(context->GetIsolate(), v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
+    return object->CreateDataProperty(context, key, value);
+}
+
+v8::Maybe<bool> createDataProperty(v8::Local<v8::Context> context, v8::Local<v8::Array> array, int index, v8::Local<v8::Value> value)
+{
+    v8::TryCatch tryCatch(context->GetIsolate());
+    v8::Isolate::DisallowJavascriptExecutionScope throwJs(context->GetIsolate(), v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
+    return array->CreateDataProperty(context, index, value);
+}
+
 } // namespace v8_inspector
diff --git a/V8ValueCopier.h b/V8ValueCopier.h
index 9cd650c..4aeadb4 100644
--- a/V8ValueCopier.h
+++ b/V8ValueCopier.h
@@ -10,6 +10,8 @@
 namespace v8_inspector {
 
 v8::MaybeLocal<v8::Value> copyValueFromDebuggerContext(v8::Isolate*, v8::Local<v8::Context> debuggerContext, v8::Local<v8::Context> toContext, v8::Local<v8::Value>);
+v8::Maybe<bool> createDataProperty(v8::Local<v8::Context>, v8::Local<v8::Object>, v8::Local<v8::Name> key, v8::Local<v8::Value>);
+v8::Maybe<bool> createDataProperty(v8::Local<v8::Context>, v8::Local<v8::Array>, int index, v8::Local<v8::Value>);
 
 } // namespace v8_inspector