| 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/. */ |
| 6 |
|
| 7 |
#include "js/UbiNodeShortestPaths.h" |
| 8 |
|
| 9 |
#include "mozilla/Maybe.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "util/Text.h" |
| 15 |
|
| 16 |
namespace JS { |
| 17 |
namespace ubi { |
| 18 |
|
| 19 |
JS_PUBLIC_API BackEdge::Ptr BackEdge::clone() const { |
| 20 |
auto clone = js::MakeUnique<BackEdge>(); |
| 21 |
if (!clone) { |
| 22 |
return nullptr; |
| 23 |
} |
| 24 |
|
| 25 |
clone->predecessor_ = predecessor(); |
| 26 |
if (name()) { |
| 27 |
clone->name_ = js::DuplicateString(name().get()); |
| 28 |
if (!clone->name_) { |
| 29 |
return nullptr; |
| 30 |
} |
| 31 |
} |
| 32 |
return clone; |
| 33 |
} |
| 34 |
|
| 35 |
#ifdef DEBUG |
| 36 |
|
| 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; |
| 41 |
} |
| 42 |
s++; |
| 43 |
} |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 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); |
| 53 |
} |
| 54 |
} |
| 55 |
fputc('\n', stderr); |
| 56 |
} |
| 57 |
|
| 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); |
| 63 |
|
| 64 |
NodeSet targets; |
| 65 |
ok = targets.putNew(node); |
| 66 |
MOZ_ASSERT(ok); |
| 67 |
|
| 68 |
auto paths = ShortestPaths::Create(cx, nogc, maxNumPaths, &rootList, |
| 69 |
std::move(targets)); |
| 70 |
MOZ_ASSERT(paths.isSome()); |
| 71 |
|
| 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, " '"); |
| 80 |
|
| 81 |
const char16_t* name = backEdge->name().get(); |
| 82 |
if (!name) { |
| 83 |
name = u"<no edge name>"; |
| 84 |
} |
| 85 |
js_fputs(name, stderr); |
| 86 |
fprintf(stderr, "'\n"); |
| 87 |
|
| 88 |
fprintf(stderr, " |\n"); |
| 89 |
fprintf(stderr, " V\n"); |
| 90 |
} |
| 91 |
|
| 92 |
dumpNode(node); |
| 93 |
fputc('\n', stderr); |
| 94 |
return true; |
| 95 |
}); |
| 96 |
MOZ_ASSERT(ok); |
| 97 |
|
| 98 |
if (i == 0) { |
| 99 |
fprintf(stderr, "No retaining paths found.\n"); |
| 100 |
} |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
} // namespace ubi |
| 105 |
} // namespace JS |