summaryrefslogtreecommitdiff
path: root/Userland/Utilities/js.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-04-01 22:14:05 +0200
committerAndreas Kling <kling@serenityos.org>2021-04-02 10:47:40 +0200
commit9da13302abf18fdef962507c14568ede817ca43d (patch)
tree94f10a1cc09c070a06d223eb4c56600b67326870 /Userland/Utilities/js.cpp
parentade3adcc7ad5847c6dd8d2182ba05a650e77e7af (diff)
downloadserenity-9da13302abf18fdef962507c14568ede817ca43d.zip
js: Add REPL pretty-printing handler for Promises
Diffstat (limited to 'Userland/Utilities/js.cpp')
-rw-r--r--Userland/Utilities/js.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp
index 6339ecf53a..5fe1e4a16f 100644
--- a/Userland/Utilities/js.cpp
+++ b/Userland/Utilities/js.cpp
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
+ * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -47,6 +47,7 @@
#include <LibJS/Runtime/NumberObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PrimitiveString.h>
+#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/ProxyObject.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/ScriptFunction.h>
@@ -271,6 +272,32 @@ static void print_proxy_object(const JS::Object& object, HashTable<JS::Object*>&
print_value(&proxy_object.handler(), seen_objects);
}
+static void print_promise(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
+{
+ auto& promise = static_cast<const JS::Promise&>(object);
+ print_type("Promise");
+ switch (promise.state()) {
+ case JS::Promise::State::Pending:
+ out("\n state: ");
+ out("\033[36;1mPending\033[0m");
+ break;
+ case JS::Promise::State::Fulfilled:
+ out("\n state: ");
+ out("\033[32;1mFulfilled\033[0m");
+ out("\n result: ");
+ print_value(promise.result(), seen_objects);
+ break;
+ case JS::Promise::State::Rejected:
+ out("\n state: ");
+ out("\033[31;1mRejected\033[0m");
+ out("\n result: ");
+ print_value(promise.result(), seen_objects);
+ break;
+ default:
+ VERIFY_NOT_REACHED();
+ }
+}
+
static void print_array_buffer(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
{
auto& array_buffer = static_cast<const JS::ArrayBuffer&>(object);
@@ -367,6 +394,8 @@ static void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
return print_regexp_object(object, seen_objects);
if (is<JS::ProxyObject>(object))
return print_proxy_object(object, seen_objects);
+ if (is<JS::Promise>(object))
+ return print_promise(object, seen_objects);
if (is<JS::ArrayBuffer>(object))
return print_array_buffer(object, seen_objects);
if (object.is_typed_array())