From 9da13302abf18fdef962507c14568ede817ca43d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 1 Apr 2021 22:14:05 +0200 Subject: [PATCH] js: Add REPL pretty-printing handler for Promises --- Userland/Utilities/js.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 - * Copyright (c) 2020, Linus Groh + * Copyright (c) 2020-2021, Linus Groh * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -271,6 +272,32 @@ static void print_proxy_object(const JS::Object& object, HashTable& print_value(&proxy_object.handler(), seen_objects); } +static void print_promise(const JS::Object& object, HashTable& seen_objects) +{ + auto& promise = static_cast(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& seen_objects) { auto& array_buffer = static_cast(object); @@ -367,6 +394,8 @@ static void print_value(JS::Value value, HashTable& seen_objects) return print_regexp_object(object, seen_objects); if (is(object)) return print_proxy_object(object, seen_objects); + if (is(object)) + return print_promise(object, seen_objects); if (is(object)) return print_array_buffer(object, seen_objects); if (object.is_typed_array())