Repository navigation
Expand file tree
/
Copy pathImportMapParse.cpp
More file actions
202 lines (187 loc) · 6.87 KB
/
Copy pathImportMapParse.cpp
File metadata and controls
202 lines (187 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <optional>
#include <string>
#include <utility>
#include "ns/V8Helpers.h"
#include "ns/loader/LoaderState.h"
namespace nativescript::common::loader {
namespace {
// A plain object: JSON gives no other kind, but arrays are objects to V8.
bool IsJsonObject(v8::Local<v8::Value> value) {
return value->IsObject() && !value->IsArray();
}
// The caught JSON error's message and position.
std::string DescribeJsonError(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const v8::TryCatch& tryCatch) {
if (!tryCatch.HasCaught()) {
return std::string();
}
v8::Local<v8::Message> message = tryCatch.Message();
if (message.IsEmpty()) {
return std::string();
}
std::string described = ToString(isolate, message->Get());
const int line = message->GetLineNumber(context).FromMaybe(0);
if (line > 0) {
described += " (line " + std::to_string(line) + ":" +
std::to_string(message->GetStartColumn()) + ")";
}
return described;
}
// The object's own enumerable string keys. An index-like key ("404") is a
// string to JSON too, so it is not kept as a number.
bool OwnKeys(v8::Local<v8::Context> context, v8::Local<v8::Object> object,
v8::Local<v8::Array>* keys) {
return object
->GetOwnPropertyNames(context,
static_cast<v8::PropertyFilter>(
v8::ONLY_ENUMERABLE | v8::SKIP_SYMBOLS),
v8::KeyConversionMode::kConvertToString)
.ToLocal(keys);
}
// An own key as exact UTF-8; nullopt for a non-string or a lone surrogate,
// which JSON.parse accepts but no file path or URL can hold.
std::optional<std::string> KeyAt(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Array> keys, uint32_t index,
v8::Local<v8::Value>* key) {
if (!keys->Get(context, index).ToLocal(key) || !(*key)->IsString()) {
return std::nullopt;
}
return ToExactUtf8(isolate, key->As<v8::String>());
}
bool ParseSection(v8::Isolate* isolate, v8::Local<v8::Context> context,
v8::Local<v8::Object> source, const std::string& label,
ImportMapSection* out, std::string* error) {
v8::Local<v8::Array> keys;
if (!OwnKeys(context, source, &keys)) {
*error = label + ": could not be read";
return false;
}
for (uint32_t i = 0; i < keys->Length(); i++) {
v8::Local<v8::Value> key;
const std::optional<std::string> specifier =
KeyAt(isolate, context, keys, i, &key);
if (!specifier) {
*error = label + ": every key must be a string";
return false;
}
v8::Local<v8::Value> value;
std::optional<std::string> target;
if (source->Get(context, key).ToLocal(&value) && value->IsString()) {
target = ToExactUtf8(isolate, value.As<v8::String>());
}
if (!specifier->empty() && !target) {
*error = label + ": the target for '" + *specifier + "' must be a string";
return false;
}
if (!AddImportMapEntry(*out, label, *specifier, target.value_or(""),
error)) {
return false;
}
}
return true;
}
} // namespace
bool ParseImportMap(v8::Isolate* isolate, std::string_view json,
ParsedImportMap* out, std::string* error) {
if (json.empty()) {
*error = "an import map must be a non-empty JSON object";
return false;
}
v8::HandleScope handleScope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::TryCatch tryCatch(isolate);
v8::Local<v8::Value> parsed;
if (!v8::JSON::Parse(context, ToV8String(isolate, json)).ToLocal(&parsed)) {
const std::string detail = DescribeJsonError(isolate, context, tryCatch);
*error = "an import map must be valid JSON" +
(detail.empty() ? std::string() : ": " + detail);
return false;
}
if (!IsJsonObject(parsed)) {
*error = "an import map must be a JSON object";
return false;
}
v8::Local<v8::Object> top = parsed.As<v8::Object>();
// Own keys only: a polluted Object.prototype must not add a section.
v8::Local<v8::Array> sections;
if (!OwnKeys(context, top, §ions)) {
*error = "an import map must be a JSON object";
return false;
}
v8::Local<v8::Value> imports;
v8::Local<v8::Value> scopes;
// Built aside: a rejection leaves `out` as the caller had it.
ParsedImportMap map;
for (uint32_t i = 0; i < sections->Length(); i++) {
v8::Local<v8::Value> key;
const std::string name =
KeyAt(isolate, context, sections, i, &key).value_or(std::string());
v8::Local<v8::Value>* slot = name == "imports" ? &imports
: name == "scopes" ? &scopes
: nullptr;
if (slot == nullptr) {
*error = "unsupported import-map section '" + name +
"'; only \"imports\" and \"scopes\" are supported";
return false;
}
if (!top->Get(context, key).ToLocal(slot)) {
*error = "an import map must be a JSON object";
return false;
}
}
if (!imports.IsEmpty()) {
if (!IsJsonObject(imports)) {
*error = "the \"imports\" section must be an object";
return false;
}
if (!ParseSection(isolate, context, imports.As<v8::Object>(), "imports",
&map.imports, error)) {
return false;
}
}
if (!scopes.IsEmpty()) {
if (!IsJsonObject(scopes)) {
*error = "the \"scopes\" section must be an object";
return false;
}
v8::Local<v8::Object> scopesObject = scopes.As<v8::Object>();
v8::Local<v8::Array> scopeKeys;
if (!OwnKeys(context, scopesObject, &scopeKeys)) {
*error = "the \"scopes\" section must be an object";
return false;
}
for (uint32_t i = 0; i < scopeKeys->Length(); i++) {
v8::Local<v8::Value> key;
const std::optional<std::string> scopeKey =
KeyAt(isolate, context, scopeKeys, i, &key);
if (!scopeKey) {
*error = "scopes: every scope key must be a string";
return false;
}
if (scopeKey->empty()) {
*error = "scopes: a scope key must not be empty";
return false;
}
v8::Local<v8::Value> scopeMap;
if (!scopesObject->Get(context, key).ToLocal(&scopeMap) ||
!IsJsonObject(scopeMap)) {
*error =
"scopes: the map for scope '" + *scopeKey + "' must be an object";
return false;
}
ImportMapSection section;
if (!ParseSection(isolate, context, scopeMap.As<v8::Object>(),
"scope '" + *scopeKey + "'", §ion, error)) {
return false;
}
map.scopes.emplace_back(ImportMapScopePrefix(*scopeKey),
std::move(section));
}
}
FinishImportMap(map);
*out = std::move(map);
return true;
}
} // namespace nativescript::common::loader