Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / src / vm / UbiNodeShortestPaths.cpp
blobf11cf19035bb490f09be6abf0ef49863cf9c1280
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "js/UbiNodeShortestPaths.h"
9 #include "mozilla/Maybe.h"
11 #include <stdio.h>
12 #include <utility>
14 #include "util/Text.h"
16 namespace JS {
17 namespace ubi {
19 JS_PUBLIC_API BackEdge::Ptr BackEdge::clone() const {
20 auto clone = js::MakeUnique<BackEdge>();
21 if (!clone) {
22 return nullptr;
25 clone->predecessor_ = predecessor();
26 if (name()) {
27 clone->name_ = js::DuplicateString(name().get());
28 if (!clone->name_) {
29 return nullptr;
32 return clone;
35 #ifdef DEBUG
37 static int32_t js_fputs(const char16_t* s, FILE* f) {
38 while (*s != 0) {
39 if (fputwc(wchar_t(*s), f) == static_cast<wint_t>(WEOF)) {
40 return WEOF;
42 s++;
44 return 1;
47 static void dumpNode(const JS::ubi::Node& node) {
48 fprintf(stderr, " %p ", (void*)node.identifier());
49 js_fputs(node.typeName(), stderr);
50 if (node.coarseType() == JS::ubi::CoarseType::Object) {
51 if (const char* clsName = node.jsObjectClassName()) {
52 fprintf(stderr, " [object %s]", clsName);
55 fputc('\n', stderr);
58 JS_PUBLIC_API void dumpPaths(JSContext* cx, Node node,
59 uint32_t maxNumPaths /* = 10 */) {
60 JS::ubi::RootList rootList(cx, true);
61 auto [ok, nogc] = rootList.init();
62 MOZ_ASSERT(ok);
64 NodeSet targets;
65 ok = targets.putNew(node);
66 MOZ_ASSERT(ok);
68 auto paths = ShortestPaths::Create(cx, nogc, maxNumPaths, &rootList,
69 std::move(targets));
70 MOZ_ASSERT(paths.isSome());
72 int i = 0;
73 ok = paths->forEachPath(node, [&](Path& path) {
74 fprintf(stderr, "Path %d:\n", i++);
75 for (auto backEdge : path) {
76 dumpNode(backEdge->predecessor());
77 fprintf(stderr, " |\n");
78 fprintf(stderr, " |\n");
79 fprintf(stderr, " '");
81 const char16_t* name = backEdge->name().get();
82 if (!name) {
83 name = u"<no edge name>";
85 js_fputs(name, stderr);
86 fprintf(stderr, "'\n");
88 fprintf(stderr, " |\n");
89 fprintf(stderr, " V\n");
92 dumpNode(node);
93 fputc('\n', stderr);
94 return true;
95 });
96 MOZ_ASSERT(ok);
98 if (i == 0) {
99 fprintf(stderr, "No retaining paths found.\n");
102 #endif
104 } // namespace ubi
105 } // namespace JS