v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
source-positions.cc
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <fstream>
8#include "src/torque/utils.h"
9
10EXPORT_CONTEXTUAL_VARIABLE(v8::internal::torque::CurrentSourceFile)
12
13namespace v8 {
14namespace internal {
15namespace torque {
16
17// static
18const std::string& SourceFileMap::PathFromV8Root(SourceId file) {
19 CHECK(file.IsValid());
20 return Get().sources_[file.id_];
21}
22
23// static
25 const std::string& root_path = PathFromV8Root(file);
26 if (StringStartsWith(root_path, "file://")) return root_path;
27 return Get().v8_root_ + "/" + PathFromV8Root(file);
28}
29
30// static
32 std::string path_from_root = PathFromV8Root(file);
33 if (!StringEndsWith(path_from_root, ".tq")) {
34 Error("Not a .tq file: ", path_from_root).Throw();
35 }
36 path_from_root.resize(path_from_root.size() - strlen(".tq"));
37 return path_from_root;
38}
39
40// static
42 Get().sources_.push_back(std::move(path));
43 return SourceId(static_cast<int>(Get().sources_.size()) - 1);
44}
45
46// static
47SourceId SourceFileMap::GetSourceId(const std::string& path) {
48 for (size_t i = 0; i < Get().sources_.size(); ++i) {
49 if (Get().sources_[i] == path) {
50 return SourceId(static_cast<int>(i));
51 }
52 }
53 return SourceId::Invalid();
54}
55
56// static
57std::vector<SourceId> SourceFileMap::AllSources() {
58 SourceFileMap& self = Get();
59 std::vector<SourceId> result;
60 result.reserve(static_cast<int>(self.sources_.size()));
61 for (int i = 0; i < static_cast<int>(self.sources_.size()); ++i) {
62 result.push_back(SourceId(i));
63 }
64 return result;
65}
66
67// static
68bool SourceFileMap::FileRelativeToV8RootExists(const std::string& path) {
69 const std::string file = Get().v8_root_ + "/" + path;
70 std::ifstream stream(file);
71 return stream.good();
72}
73
74} // namespace torque
75} // namespace internal
76} // namespace v8
static VarType & Get()
Definition contextual.h:64
static bool FileRelativeToV8RootExists(const std::string &path)
static std::vector< SourceId > AllSources()
static const std::string & PathFromV8Root(SourceId file)
static SourceId AddSource(std::string path)
std::vector< std::string > sources_
static std::string PathFromV8RootWithoutExtension(SourceId file)
static SourceId GetSourceId(const std::string &path)
static std::string AbsolutePath(SourceId file)
#define EXPORT_CONTEXTUAL_VARIABLE(VarName)
Definition contextual.h:94
ZoneVector< RpoNumber > & result
bool StringEndsWith(const std::string &s, const std::string &suffix)
Definition utils.h:361
bool StringStartsWith(const std::string &s, const std::string &prefix)
Definition utils.h:357
MessageBuilder Error(Args &&... args)
Definition utils.h:81
#define CHECK(condition)
Definition logging.h:124