Title: [283527] trunk/Source/_javascript_Core
Revision
283527
Author
[email protected]
Date
2021-10-04 17:33:56 -0700 (Mon, 04 Oct 2021)

Log Message

Display return values in nicer way in the jsc REPL and add a prettyPrint function
https://bugs.webkit.org/show_bug.cgi?id=230931
<rdar://problem/83698777>

Reviewed by Tadeu Zagallo.

Currently, print(1), print("1"), and print([1]) all print to stdout
simply as "1" (without the quotes). Same for values when running the
REPL. This isn't super helpful. Let's print quotes for strings, and
brackets for arrays. This patch adds a prettyPrint function to do
that, since we have a lot of tests that depend on the old print behavior.

This patch also makes values printed in the REPL the new pretty style.

* jsc.cpp:
(toCString):
(printInternal):
(JSC_DEFINE_HOST_FUNCTION):
(runInteractive):
(cStringFromViewWithString): Deleted.
* runtime/JSCJSValue.cpp:
(JSC::JSValue::toWTFStringForConsole const):
* runtime/JSCJSValue.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (283526 => 283527)


--- trunk/Source/_javascript_Core/ChangeLog	2021-10-04 23:50:37 UTC (rev 283526)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-10-05 00:33:56 UTC (rev 283527)
@@ -1,5 +1,32 @@
 2021-10-04  Saam Barati  <[email protected]>
 
+        Display return values in nicer way in the jsc REPL and add a prettyPrint function
+        https://bugs.webkit.org/show_bug.cgi?id=230931
+        <rdar://problem/83698777>
+
+        Reviewed by Tadeu Zagallo.
+
+        Currently, print(1), print("1"), and print([1]) all print to stdout
+        simply as "1" (without the quotes). Same for values when running the
+        REPL. This isn't super helpful. Let's print quotes for strings, and
+        brackets for arrays. This patch adds a prettyPrint function to do
+        that, since we have a lot of tests that depend on the old print behavior.
+        
+        This patch also makes values printed in the REPL the new pretty style.
+
+
+        * jsc.cpp:
+        (toCString):
+        (printInternal):
+        (JSC_DEFINE_HOST_FUNCTION):
+        (runInteractive):
+        (cStringFromViewWithString): Deleted.
+        * runtime/JSCJSValue.cpp:
+        (JSC::JSValue::toWTFStringForConsole const):
+        * runtime/JSCJSValue.h:
+
+2021-10-04  Saam Barati  <[email protected]>
+
         IntrinsicGetterAccessCase implementation of __proto__ needs to handle get_by_id_with_this
         https://bugs.webkit.org/show_bug.cgi?id=229951
         <rdar://problem/82787527>

Modified: trunk/Source/_javascript_Core/jsc.cpp (283526 => 283527)


--- trunk/Source/_javascript_Core/jsc.cpp	2021-10-04 23:50:37 UTC (rev 283526)
+++ trunk/Source/_javascript_Core/jsc.cpp	2021-10-05 00:33:56 UTC (rev 283527)
@@ -278,6 +278,7 @@
 
 static JSC_DECLARE_HOST_FUNCTION(functionPrintStdOut);
 static JSC_DECLARE_HOST_FUNCTION(functionPrintStdErr);
+static JSC_DECLARE_HOST_FUNCTION(functionPrettyPrint);
 static JSC_DECLARE_HOST_FUNCTION(functionDebug);
 static JSC_DECLARE_HOST_FUNCTION(functionDescribe);
 static JSC_DECLARE_HOST_FUNCTION(functionDescribeArray);
@@ -527,6 +528,7 @@
         addFunction(vm, "describeArray", functionDescribeArray, 1);
         addFunction(vm, "print", functionPrintStdOut, 1);
         addFunction(vm, "printErr", functionPrintStdErr, 1);
+        addFunction(vm, "prettyPrint", functionPrettyPrint, 1);
         addFunction(vm, "quit", functionQuit, 0);
         addFunction(vm, "gc", functionGCAndSweep, 0);
         addFunction(vm, "fullGC", functionFullGC, 0);
@@ -1238,9 +1240,10 @@
     return metaProperties;
 }
 
-static CString cStringFromViewWithString(JSGlobalObject* globalObject, ThrowScope& scope, StringViewWithUnderlyingString& viewWithString)
+template <typename T>
+static CString toCString(JSGlobalObject* globalObject, ThrowScope& scope, T& string)
 {
-    Expected<CString, UTF8ConversionError> expectedString = viewWithString.view.tryGetUtf8();
+    Expected<CString, UTF8ConversionError> expectedString = string.tryGetUtf8();
     if (expectedString)
         return expectedString.value();
     switch (expectedString.error()) {
@@ -1259,7 +1262,7 @@
     return { };
 }
 
-static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* callFrame, FILE* out)
+static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* callFrame, FILE* out, bool pretty)
 {
     VM& vm = globalObject->vm();
     auto scope = DECLARE_THROW_SCOPE(vm);
@@ -1277,13 +1280,11 @@
             if (EOF == fputc(' ', out))
                 goto fail;
 
-        auto* jsString = callFrame->uncheckedArgument(i).toString(globalObject);
+        String string = pretty ? callFrame->uncheckedArgument(i).toWTFStringForConsole(globalObject) : callFrame->uncheckedArgument(i).toWTFString(globalObject);
         RETURN_IF_EXCEPTION(scope, { });
-        auto viewWithString = jsString->viewWithUnderlyingString(globalObject);
+        auto cString = toCString(globalObject, scope, string);
         RETURN_IF_EXCEPTION(scope, { });
-        auto string = cStringFromViewWithString(globalObject, scope, viewWithString);
-        RETURN_IF_EXCEPTION(scope, { });
-        fwrite(string.data(), sizeof(char), string.length(), out);
+        fwrite(cString.data(), sizeof(char), cString.length(), out);
         if (ferror(out))
             goto fail;
     }
@@ -1296,14 +1297,19 @@
 
 JSC_DEFINE_HOST_FUNCTION(functionPrintStdOut, (JSGlobalObject* globalObject, CallFrame* callFrame))
 {
-    return printInternal(globalObject, callFrame, stdout);
+    return printInternal(globalObject, callFrame, stdout, false);
 }
 
 JSC_DEFINE_HOST_FUNCTION(functionPrintStdErr, (JSGlobalObject* globalObject, CallFrame* callFrame))
 {
-    return printInternal(globalObject, callFrame, stderr);
+    return printInternal(globalObject, callFrame, stderr, false);
 }
 
+JSC_DEFINE_HOST_FUNCTION(functionPrettyPrint, (JSGlobalObject* globalObject, CallFrame* callFrame))
+{
+    return printInternal(globalObject, callFrame, stdout, true);
+}
+
 JSC_DEFINE_HOST_FUNCTION(functionDebug, (JSGlobalObject* globalObject, CallFrame* callFrame))
 {
     VM& vm = globalObject->vm();
@@ -1312,7 +1318,7 @@
     RETURN_IF_EXCEPTION(scope, { });
     auto viewWithString = jsString->viewWithUnderlyingString(globalObject);
     RETURN_IF_EXCEPTION(scope, { });
-    auto string = cStringFromViewWithString(globalObject, scope, viewWithString);
+    auto string = toCString(globalObject, scope, viewWithString.view);
     RETURN_IF_EXCEPTION(scope, { });
     fputs("--> ", stderr);
     fwrite(string.data(), sizeof(char), string.length(), stderr);
@@ -3271,7 +3277,7 @@
             fputs("Exception: ", stdout);
             utf8 = evaluationException->value().toWTFString(globalObject).tryGetUtf8();
         } else
-            utf8 = returnValue.toWTFString(globalObject).tryGetUtf8();
+            utf8 = returnValue.toWTFStringForConsole(globalObject).tryGetUtf8();
 
         CString result;
         if (utf8)

Modified: trunk/Source/_javascript_Core/runtime/JSCJSValue.cpp (283526 => 283527)


--- trunk/Source/_javascript_Core/runtime/JSCJSValue.cpp	2021-10-04 23:50:37 UTC (rev 283526)
+++ trunk/Source/_javascript_Core/runtime/JSCJSValue.cpp	2021-10-05 00:33:56 UTC (rev 283527)
@@ -471,4 +471,19 @@
 }
 #endif
 
+WTF::String JSValue::toWTFStringForConsole(JSGlobalObject* globalObject) const
+{
+    VM& vm = globalObject->vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    JSString* string = toString(globalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+    String result = string->value(globalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+    if (isString())
+        return makeString("\"", result, "\"");
+    if (jsDynamicCast<JSArray*>(vm, *this))
+        return makeString("[", result, "]");
+    return result;
+}
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/JSCJSValue.h (283526 => 283527)


--- trunk/Source/_javascript_Core/runtime/JSCJSValue.h	2021-10-04 23:50:37 UTC (rev 283526)
+++ trunk/Source/_javascript_Core/runtime/JSCJSValue.h	2021-10-05 00:33:56 UTC (rev 283527)
@@ -289,6 +289,7 @@
     Identifier toPropertyKey(JSGlobalObject*) const;
     JSValue toPropertyKeyValue(JSGlobalObject*) const;
     WTF::String toWTFString(JSGlobalObject*) const;
+    JS_EXPORT_PRIVATE WTF::String toWTFStringForConsole(JSGlobalObject*) const;
     JSObject* toObject(JSGlobalObject*) const;
 
     // Integer conversions.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to