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
|
/* -*- 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 "js/experimental/CompileScript.h"
#include "frontend/BytecodeCompilation.h" // frontend::CompileGlobalScriptToStencil
#include "frontend/BytecodeCompiler.h" // frontend::ParseModuleToStencil
#include "frontend/CompilationStencil.h" // frontend::{CompilationStencil,CompilationInput}
#include "frontend/FrontendContext.h" // frontend::FrontendContext
#include "frontend/ScopeBindingCache.h" // frontend::NoScopeBindingCache
#include "js/SourceText.h" // JS::SourceText
using namespace js;
using namespace js::frontend;
JS_PUBLIC_API FrontendContext* JS::NewFrontendContext() {
MOZ_ASSERT(JS::detail::libraryInitState == JS::detail::InitState::Running,
"must call JS_Init prior to creating any FrontendContexts");
return js::NewFrontendContext();
}
JS_PUBLIC_API void JS::DestroyFrontendContext(FrontendContext* fc) {
return js::DestroyFrontendContext(fc);
}
JS_PUBLIC_API void JS::SetNativeStackQuota(JS::FrontendContext* fc,
JS::NativeStackSize stackSize) {
fc->setStackQuota(stackSize);
}
JS_PUBLIC_API bool JS::SetSupportedImportAssertions(
FrontendContext* fc,
const JS::ImportAssertionVector& supportedImportAssertions) {
return fc->setSupportedImportAssertions(supportedImportAssertions);
}
JS::CompilationStorage::~CompilationStorage() {
if (input_ && !isBorrowed_) {
js_delete(input_);
input_ = nullptr;
}
}
size_t JS::CompilationStorage::sizeOfIncludingThis(
mozilla::MallocSizeOf mallocSizeOf) const {
size_t sizeOfCompilationInput =
input_ ? input_->sizeOfExcludingThis(mallocSizeOf) : 0;
return mallocSizeOf(this) + sizeOfCompilationInput;
}
bool JS::CompilationStorage::allocateInput(
FrontendContext* fc, const JS::ReadOnlyCompileOptions& options) {
MOZ_ASSERT(!input_);
input_ = fc->getAllocator()->new_<frontend::CompilationInput>(options);
return !!input_;
}
void JS::CompilationStorage::trace(JSTracer* trc) {
if (input_) {
input_->trace(trc);
}
}
template <typename CharT>
static already_AddRefed<JS::Stencil> CompileGlobalScriptToStencilImpl(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options,
JS::SourceText<CharT>& srcBuf, JS::CompilationStorage& compilationStorage) {
ScopeKind scopeKind =
options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global;
JS::SourceText<CharT> data(std::move(srcBuf));
compilationStorage.allocateInput(fc, options);
if (!compilationStorage.hasInput()) {
return nullptr;
}
frontend::NoScopeBindingCache scopeCache;
LifoAlloc tempLifoAlloc(JSContext::TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE);
RefPtr<frontend::CompilationStencil> stencil_ =
frontend::CompileGlobalScriptToStencil(nullptr, fc, tempLifoAlloc,
compilationStorage.getInput(),
&scopeCache, data, scopeKind);
return stencil_.forget();
}
template <typename CharT>
static already_AddRefed<JS::Stencil> CompileModuleScriptToStencilImpl(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& optionsInput,
JS::SourceText<CharT>& srcBuf, JS::CompilationStorage& compilationStorage) {
JS::CompileOptions options(nullptr, optionsInput);
options.setModule();
compilationStorage.allocateInput(fc, options);
if (!compilationStorage.hasInput()) {
return nullptr;
}
NoScopeBindingCache scopeCache;
js::LifoAlloc tempLifoAlloc(JSContext::TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE);
RefPtr<JS::Stencil> stencil =
ParseModuleToStencil(nullptr, fc, tempLifoAlloc,
compilationStorage.getInput(), &scopeCache, srcBuf);
if (!stencil) {
return nullptr;
}
// Convert the UniquePtr to a RefPtr and increment the count (to 1).
return stencil.forget();
}
already_AddRefed<JS::Stencil> JS::CompileGlobalScriptToStencil(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options,
JS::SourceText<mozilla::Utf8Unit>& srcBuf,
JS::CompilationStorage& compileStorage) {
return CompileGlobalScriptToStencilImpl(fc, options, srcBuf, compileStorage);
}
already_AddRefed<JS::Stencil> JS::CompileGlobalScriptToStencil(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, JS::CompilationStorage& compileStorage) {
return CompileGlobalScriptToStencilImpl(fc, options, srcBuf, compileStorage);
}
already_AddRefed<JS::Stencil> JS::CompileModuleScriptToStencil(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& optionsInput,
JS::SourceText<mozilla::Utf8Unit>& srcBuf,
JS::CompilationStorage& compileStorage) {
return CompileModuleScriptToStencilImpl(fc, optionsInput, srcBuf,
compileStorage);
}
already_AddRefed<JS::Stencil> JS::CompileModuleScriptToStencil(
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& optionsInput,
JS::SourceText<char16_t>& srcBuf, JS::CompilationStorage& compileStorage) {
return CompileModuleScriptToStencilImpl(fc, optionsInput, srcBuf,
compileStorage);
}
#ifdef DEBUG
// We don't need to worry about GC if the CompilationInput has no GC pointers
static bool isGCSafe(js::frontend::CompilationInput& input) {
bool isGlobalOrModule =
input.target == CompilationInput::CompilationTarget::Global ||
input.target == CompilationInput::CompilationTarget::Module;
bool scopeHasNoGC =
input.enclosingScope.isStencil() || input.enclosingScope.isNull();
bool scriptHasNoGC =
input.lazyOuterScript().isStencil() || input.lazyOuterScript().isNull();
bool cacheHasNoGC = input.atomCache.empty();
return isGlobalOrModule && scopeHasNoGC && scriptHasNoGC && cacheHasNoGC;
}
#endif // DEBUG
bool JS::PrepareForInstantiate(JS::FrontendContext* fc,
JS::CompilationStorage& compileStorage,
JS::Stencil& stencil,
JS::InstantiationStorage& storage) {
MOZ_ASSERT(compileStorage.hasInput());
MOZ_ASSERT(isGCSafe(compileStorage.getInput()));
if (!storage.gcOutput_) {
storage.gcOutput_ =
fc->getAllocator()->new_<js::frontend::CompilationGCOutput>();
if (!storage.gcOutput_) {
return false;
}
}
return CompilationStencil::prepareForInstantiate(
fc, compileStorage.getInput().atomCache, stencil, *storage.gcOutput_);
}
|