Skip to content
Permalink
Browse files
Print values in a nicer way in the jsc shell
https://bugs.webkit.org/show_bug.cgi?id=230931

Reviewed by Tadeu Zagallo.

JSTests:

* ChakraCore/test/jsc-lib.js:

Source/JavaScriptCore:

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.

Some tests rely on the old print behavior. Those tests now use the legacyPrint
instead.

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

LayoutTests:

* resources/standalone-pre.js:


Canonical link: https://commits.webkit.org/242318@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@283293 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
saambarati committed Sep 30, 2021
1 parent 058858d commit c8702a205db555ed0dbd8cd8febf4d08f35f8bc4
Showing 11 changed files with 84 additions and 15 deletions.
@@ -1,3 +1,5 @@
print = legacyPrint;

WScript = {
_jscGC: gc,
_jscLoad: load,
@@ -1,3 +1,12 @@
2021-09-29 Saam Barati <sbarati@apple.com>

Print values in a nicer way in the jsc shell
https://bugs.webkit.org/show_bug.cgi?id=230931

Reviewed by Tadeu Zagallo.

* ChakraCore/test/jsc-lib.js:

2021-09-29 Saam Barati <sbarati@apple.com>

We need to load the baseline JIT's constant pool register after OSR exit to checkpoints if we return to baseline code
@@ -358,5 +358,5 @@ DisplArea = null;

})();
} catch (e) {
print("JSC EXCEPTION FUZZ: Caught exception: " + e);
legacyPrint("JSC EXCEPTION FUZZ: Caught exception: " + e);
}
@@ -424,5 +424,5 @@ for (i = 0; i < 4000; ++i) {

})();
} catch (e) {
print("JSC EXCEPTION FUZZ: Caught exception: " + e);
legacyPrint("JSC EXCEPTION FUZZ: Caught exception: " + e);
}
@@ -4684,6 +4684,6 @@ for (var i = 0; i < 4; ++i) {
}
})();
} catch (e) {
print("JSC EXCEPTION FUZZ: Caught exception: " + e);
legacyPrint("JSC EXCEPTION FUZZ: Caught exception: " + e);
}

@@ -1,3 +1,12 @@
2021-09-29 Saam Barati <sbarati@apple.com>

Print values in a nicer way in the jsc shell
https://bugs.webkit.org/show_bug.cgi?id=230931

Reviewed by Tadeu Zagallo.

* resources/standalone-pre.js:

2021-09-29 Chris Dumez <cdumez@apple.com>

[ iOS Debug ] http/tests/xmlhttprequest/access-control-preflight-credential-sync.html is a flaky crash
@@ -13,6 +13,8 @@ silentTestPass = false;
didPassSomeTestsSilently = false;
didFailSomeTests = false;

print = legacyPrint;

function description(msg)
{
print(msg);
@@ -1,3 +1,28 @@
2021-09-29 Saam Barati <sbarati@apple.com>

Print values in a nicer way in the jsc shell
https://bugs.webkit.org/show_bug.cgi?id=230931

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.

Some tests rely on the old print behavior. Those tests now use the legacyPrint
instead.

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

2021-09-29 Saam Barati <sbarati@apple.com>

We need to load the baseline JIT's constant pool register after OSR exit to checkpoints if we return to baseline code
@@ -278,6 +278,7 @@ static JSC_DECLARE_HOST_FUNCTION(functionIsHeapBigInt);

static JSC_DECLARE_HOST_FUNCTION(functionPrintStdOut);
static JSC_DECLARE_HOST_FUNCTION(functionPrintStdErr);
static JSC_DECLARE_HOST_FUNCTION(functionLegacyPrint);
static JSC_DECLARE_HOST_FUNCTION(functionDebug);
static JSC_DECLARE_HOST_FUNCTION(functionDescribe);
static JSC_DECLARE_HOST_FUNCTION(functionDescribeArray);
@@ -527,6 +528,7 @@ class GlobalObject final : public JSGlobalObject {
addFunction(vm, "describeArray", functionDescribeArray, 1);
addFunction(vm, "print", functionPrintStdOut, 1);
addFunction(vm, "printErr", functionPrintStdErr, 1);
addFunction(vm, "legacyPrint", functionLegacyPrint, 1);
addFunction(vm, "quit", functionQuit, 0);
addFunction(vm, "gc", functionGCAndSweep, 0);
addFunction(vm, "fullGC", functionFullGC, 0);
@@ -1238,9 +1240,10 @@ JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObject* g
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 @@ static CString cStringFromViewWithString(JSGlobalObject* globalObject, ThrowScop
return { };
}

static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* callFrame, FILE* out)
static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* callFrame, FILE* out, bool legacy)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
@@ -1277,13 +1280,11 @@ static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* cal
if (EOF == fputc(' ', out))
goto fail;

auto* jsString = callFrame->uncheckedArgument(i).toString(globalObject);
String string = legacy ? callFrame->uncheckedArgument(i).toWTFString(globalObject) : callFrame->uncheckedArgument(i).toWTFStringForConsole(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,12 +1297,17 @@ static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* cal

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(functionLegacyPrint, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return printInternal(globalObject, callFrame, stdout, true);
}

JSC_DEFINE_HOST_FUNCTION(functionDebug, (JSGlobalObject* globalObject, CallFrame* callFrame))
@@ -1312,7 +1318,7 @@ JSC_DEFINE_HOST_FUNCTION(functionDebug, (JSGlobalObject* globalObject, CallFrame
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 @@ static void runInteractive(GlobalObject* globalObject)
fputs("Exception: ", stdout);
utf8 = evaluationException->value().toWTFString(globalObject).tryGetUtf8();
} else
utf8 = returnValue.toWTFString(globalObject).tryGetUtf8();
utf8 = returnValue.toWTFStringForConsole(globalObject).tryGetUtf8();

CString result;
if (utf8)
@@ -471,4 +471,19 @@ NEVER_INLINE void ensureStillAliveHere(JSValue)
}
#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
@@ -289,6 +289,7 @@ class JSValue {
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.

0 comments on commit c8702a2

Please sign in to comment.