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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "builtin/TestingUtility.h"
#include <stdint.h> // uint32_t
#include "jsapi.h" // JS_NewPlainObject, JS_WrapValue
#include "frontend/CompilationStencil.h" // js::frontend::CompilationStencil
#include "js/CharacterEncoding.h" // JS_EncodeStringToUTF8
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
#include "js/CompileOptions.h" // JS::CompileOptions
#include "js/Conversions.h" // JS::ToBoolean, JS::ToString, JS::ToUint32, JS::ToInt32
#include "js/PropertyAndElement.h" // JS_GetProperty, JS_DefineProperty
#include "js/PropertyDescriptor.h" // JSPROP_ENUMERATE
#include "js/RealmOptions.h" // JS::RealmBehaviors
#include "js/RootingAPI.h" // JS::Rooted, JS::Handle
#include "js/Utility.h" // JS::UniqueChars
#include "js/Value.h" // JS::Value, JS::StringValue
#include "vm/JSContext.h" // JS::ReportUsageErrorASCII
#include "vm/JSScript.h"
#include "vm/Realm.h" // JS::Realm
bool js::ParseCompileOptions(JSContext* cx, JS::CompileOptions& options,
JS::Handle<JSObject*> opts,
JS::UniqueChars* fileNameBytes) {
JS::Rooted<JS::Value> v(cx);
JS::Rooted<JSString*> s(cx);
if (!JS_GetProperty(cx, opts, "isRunOnce", &v)) {
return false;
}
if (!v.isUndefined()) {
options.setIsRunOnce(JS::ToBoolean(v));
}
if (!JS_GetProperty(cx, opts, "noScriptRval", &v)) {
return false;
}
if (!v.isUndefined()) {
options.setNoScriptRval(JS::ToBoolean(v));
}
if (!JS_GetProperty(cx, opts, "fileName", &v)) {
return false;
}
if (v.isNull()) {
options.setFile(nullptr);
} else if (!v.isUndefined()) {
s = JS::ToString(cx, v);
if (!s) {
return false;
}
if (fileNameBytes) {
*fileNameBytes = JS_EncodeStringToUTF8(cx, s);
if (!*fileNameBytes) {
return false;
}
options.setFile(fileNameBytes->get());
}
}
if (!JS_GetProperty(cx, opts, "skipFileNameValidation", &v)) {
return false;
}
if (!v.isUndefined()) {
options.setSkipFilenameValidation(JS::ToBoolean(v));
}
if (!JS_GetProperty(cx, opts, "lineNumber", &v)) {
return false;
}
if (!v.isUndefined()) {
uint32_t u;
if (!JS::ToUint32(cx, v, &u)) {
return false;
}
options.setLine(u);
}
if (!JS_GetProperty(cx, opts, "columnNumber", &v)) {
return false;
}
if (!v.isUndefined()) {
int32_t c;
if (!JS::ToInt32(cx, v, &c)) {
return false;
}
if (c < 1) {
c = 1;
}
options.setColumn(JS::ColumnNumberOneOrigin(c));
}
if (!JS_GetProperty(cx, opts, "sourceIsLazy", &v)) {
return false;
}
if (v.isBoolean()) {
options.setSourceIsLazy(v.toBoolean());
}
if (!JS_GetProperty(cx, opts, "forceFullParse", &v)) {
return false;
}
bool forceFullParseIsSet = !v.isUndefined();
if (v.isBoolean() && v.toBoolean()) {
options.setForceFullParse();
}
if (!JS_GetProperty(cx, opts, "eagerDelazificationStrategy", &v)) {
return false;
}
if (forceFullParseIsSet && !v.isUndefined()) {
JS_ReportErrorASCII(
cx, "forceFullParse and eagerDelazificationStrategy are both set.");
return false;
}
if (v.isString()) {
s = JS::ToString(cx, v);
if (!s) {
return false;
}
JSLinearString* str = JS_EnsureLinearString(cx, s);
if (!str) {
return false;
}
bool found = false;
JS::DelazificationOption strategy = JS::DelazificationOption::OnDemandOnly;
#define MATCH_AND_SET_STRATEGY_(NAME) \
if (!found && JS_LinearStringEqualsLiteral(str, #NAME)) { \
strategy = JS::DelazificationOption::NAME; \
found = true; \
}
FOREACH_DELAZIFICATION_STRATEGY(MATCH_AND_SET_STRATEGY_);
#undef MATCH_AND_SET_STRATEGY_
#undef FOR_STRATEGY_NAMES
if (!found) {
JS_ReportErrorASCII(cx,
"eagerDelazificationStrategy does not match any "
"DelazificationOption.");
return false;
}
options.setEagerDelazificationStrategy(strategy);
}
return true;
}
bool js::ParseSourceOptions(JSContext* cx, JS::Handle<JSObject*> opts,
JS::MutableHandle<JSString*> displayURL,
JS::MutableHandle<JSString*> sourceMapURL) {
JS::Rooted<JS::Value> v(cx);
if (!JS_GetProperty(cx, opts, "displayURL", &v)) {
return false;
}
if (!v.isUndefined()) {
displayURL.set(ToString(cx, v));
if (!displayURL) {
return false;
}
}
if (!JS_GetProperty(cx, opts, "sourceMapURL", &v)) {
return false;
}
if (!v.isUndefined()) {
sourceMapURL.set(ToString(cx, v));
if (!sourceMapURL) {
return false;
}
}
return true;
}
bool js::SetSourceOptions(JSContext* cx, FrontendContext* fc,
ScriptSource* source,
JS::Handle<JSString*> displayURL,
JS::Handle<JSString*> sourceMapURL) {
if (displayURL && !source->hasDisplayURL()) {
JS::UniqueTwoByteChars chars = JS_CopyStringCharsZ(cx, displayURL);
if (!chars) {
return false;
}
if (!source->setDisplayURL(fc, std::move(chars))) {
return false;
}
}
if (sourceMapURL && !source->hasSourceMapURL()) {
JS::UniqueTwoByteChars chars = JS_CopyStringCharsZ(cx, sourceMapURL);
if (!chars) {
return false;
}
if (!source->setSourceMapURL(fc, std::move(chars))) {
return false;
}
}
return true;
}
JSObject* js::CreateScriptPrivate(JSContext* cx,
JS::Handle<JSString*> path /* = nullptr */) {
JS::Rooted<JSObject*> info(cx, JS_NewPlainObject(cx));
if (!info) {
return nullptr;
}
if (path) {
JS::Rooted<JS::Value> pathValue(cx, JS::StringValue(path));
if (!JS_DefineProperty(cx, info, "path", pathValue, JSPROP_ENUMERATE)) {
return nullptr;
}
}
return info;
}
bool js::ParseDebugMetadata(JSContext* cx, JS::Handle<JSObject*> opts,
JS::MutableHandle<JS::Value> privateValue,
JS::MutableHandle<JSString*> elementAttributeName) {
JS::Rooted<JS::Value> v(cx);
JS::Rooted<JSString*> s(cx);
if (!JS_GetProperty(cx, opts, "element", &v)) {
return false;
}
if (v.isObject()) {
JS::Rooted<JSObject*> infoObject(cx, CreateScriptPrivate(cx));
if (!infoObject) {
return false;
}
JS::Rooted<JS::Value> elementValue(cx, v);
if (!JS_WrapValue(cx, &elementValue)) {
return false;
}
if (!JS_DefineProperty(cx, infoObject, "element", elementValue, 0)) {
return false;
}
privateValue.set(JS::ObjectValue(*infoObject));
}
if (!JS_GetProperty(cx, opts, "elementAttributeName", &v)) {
return false;
}
if (!v.isUndefined()) {
s = ToString(cx, v);
if (!s) {
return false;
}
elementAttributeName.set(s);
}
return true;
}
JS::UniqueChars js::StringToLocale(JSContext* cx, JS::Handle<JSObject*> callee,
JS::Handle<JSString*> str_) {
Rooted<JSLinearString*> str(cx, str_->ensureLinear(cx));
if (!str) {
return nullptr;
}
if (!StringIsAscii(str)) {
ReportUsageErrorASCII(cx, callee,
"First argument contains non-ASCII characters");
return nullptr;
}
UniqueChars locale = JS_EncodeStringToASCII(cx, str);
if (!locale) {
return nullptr;
}
bool containsOnlyValidBCP47Characters =
mozilla::IsAsciiAlpha(locale[0]) &&
std::all_of(locale.get(), locale.get() + str->length(), [](auto c) {
return mozilla::IsAsciiAlphanumeric(c) || c == '-';
});
if (!containsOnlyValidBCP47Characters) {
ReportUsageErrorASCII(cx, callee,
"First argument should be a BCP47 language tag");
return nullptr;
}
return locale;
}
bool js::ValidateLazinessOfStencilAndGlobal(
JSContext* cx, const js::frontend::CompilationStencil& stencil) {
if (cx->realm()->behaviors().discardSource() && stencil.canLazilyParse) {
JS_ReportErrorASCII(cx,
"Stencil compiled with with lazy parse option cannot "
"be used in a realm with discardSource");
return false;
}
return true;
}
bool js::ValidateModuleCompileOptions(JSContext* cx,
JS::CompileOptions& options) {
if (options.lineno == 0) {
JS_ReportErrorASCII(cx, "Module cannot be compiled with lineNumber == 0");
return false;
}
if (!options.filename()) {
JS_ReportErrorASCII(cx, "Module should have filename");
return false;
}
return true;
}
|