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"
14 #include "util/Text.h"
19 JS_PUBLIC_API
BackEdge::Ptr
BackEdge::clone() const {
20 auto clone
= js::MakeUnique
<BackEdge
>();
25 clone
->predecessor_
= predecessor();
27 clone
->name_
= js::DuplicateString(name().get());
37 static int32_t js_fputs(const char16_t
* s
, FILE* f
) {
39 if (fputwc(wchar_t(*s
), f
) == static_cast<wint_t>(WEOF
)) {
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
);
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();
65 ok
= targets
.putNew(node
);
68 auto paths
= ShortestPaths::Create(cx
, nogc
, maxNumPaths
, &rootList
,
70 MOZ_ASSERT(paths
.isSome());
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();
83 name
= u
"<no edge name>";
85 js_fputs(name
, stderr
);
86 fprintf(stderr
, "'\n");
88 fprintf(stderr
, " |\n");
89 fprintf(stderr
, " V\n");
99 fprintf(stderr
, "No retaining paths found.\n");