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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/*
* Types and functions related to the compilation of JavaScript off the
* direct JSAPI-using thread.
*/
#ifndef js_OffThreadScriptCompilation_h
#define js_OffThreadScriptCompilation_h
#include "mozilla/Range.h" // mozilla::Range
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "mozilla/Vector.h" // mozilla::Vector
#include <stddef.h> // size_t
#include "jstypes.h" // JS_PUBLIC_API
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions
#include "js/GCVector.h" // JS::GCVector
#include "js/Transcoding.h" // JS::TranscodeSource
struct JS_PUBLIC_API JSContext;
class JS_PUBLIC_API JSScript;
namespace JS {
template <typename UnitT>
class SourceText;
} // namespace JS
namespace JS {
class OffThreadToken;
using OffThreadCompileCallback = void (*)(OffThreadToken* token,
void* callbackData);
/*
* Off thread compilation control flow.
*
* After successfully triggering an off thread compile of a script, the
* callback will eventually be invoked with the specified data and a token
* for the compilation. The callback will be invoked while off thread,
* so must ensure that its operations are thread safe. Afterwards, one of the
* following functions must be invoked on the runtime's main thread:
*
* - FinishOffThreadScript, to get the result script (or nullptr on failure).
* - CancelOffThreadScript, to free the resources without creating a script.
*
* The characters passed in to CompileOffThread must remain live until the
* callback is invoked, and the resulting script will be rooted until the call
* to FinishOffThreadScript.
*/
extern JS_PUBLIC_API bool CanCompileOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options, size_t length);
extern JS_PUBLIC_API OffThreadToken* CompileOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<char16_t>& srcBuf, OffThreadCompileCallback callback,
void* callbackData);
// NOTE: Unlike for the normal sync compilation functions, this function NEVER
// INFLATES TO UTF-16. Therefore, it is ALWAYS invoking experimental
// UTF-8 support. Inflate to UTF-16 yourself and use the other overload
// if you're unable to take a risk using unstable functionality.
extern JS_PUBLIC_API OffThreadToken* CompileOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<mozilla::Utf8Unit>& srcBuf, OffThreadCompileCallback callback,
void* callbackData);
// Finish the off-thread parse/decode task and return the script. Return the
// script on success, or return null on failure (usually with an error reported)
extern JS_PUBLIC_API JSScript* FinishOffThreadScript(JSContext* cx,
OffThreadToken* token);
// Finish the off-thread parse/decode task and return the script, and register
// an encoder on its script source, such that all functions can be encoded as
// they are parsed. This strategy is used to avoid blocking the main thread in
// a non-interruptible way.
//
// See also JS::FinishIncrementalEncoding.
//
// Return the script on success, or return null on failure (usually with an
// error reported)
extern JS_PUBLIC_API JSScript* FinishOffThreadScriptAndStartIncrementalEncoding(
JSContext* cx, OffThreadToken* token);
extern JS_PUBLIC_API void CancelOffThreadScript(JSContext* cx,
OffThreadToken* token);
extern JS_PUBLIC_API OffThreadToken* CompileOffThreadModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<char16_t>& srcBuf, OffThreadCompileCallback callback,
void* callbackData);
// NOTE: Unlike for the normal sync compilation functions, this function NEVER
// INFLATES TO UTF-16. Therefore, it is ALWAYS invoking experimental
// UTF-8 support. Inflate to UTF-16 yourself and use the other overload
// if you're unable to take a risk using unstable functionality.
extern JS_PUBLIC_API OffThreadToken* CompileOffThreadModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
SourceText<mozilla::Utf8Unit>& srcBuf, OffThreadCompileCallback callback,
void* callbackData);
extern JS_PUBLIC_API JSObject* FinishOffThreadModule(JSContext* cx,
OffThreadToken* token);
extern JS_PUBLIC_API void CancelOffThreadModule(JSContext* cx,
OffThreadToken* token);
extern JS_PUBLIC_API bool CanDecodeOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options, size_t length);
// If options.useOffThreadParseGlobal is true,
// decode JSScript from the buffer.
//
// If options.useOffThreadParseGlobal is false,
// decode stencil from the buffer and instantiate JSScript from it.
//
// The start of `buffer` and `cursor` should meet
// IsTranscodingBytecodeAligned and IsTranscodingBytecodeOffsetAligned.
// (This should be handled while encoding).
//
// `buffer` should be alive until the end of `FinishOffThreadScriptDecoder`.
extern JS_PUBLIC_API OffThreadToken* DecodeOffThreadScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
mozilla::Vector<uint8_t>& buffer /* TranscodeBuffer& */, size_t cursor,
OffThreadCompileCallback callback, void* callbackData);
// The start of `range` should be meet IsTranscodingBytecodeAligned and
// AlignTranscodingBytecodeOffset.
// (This should be handled while encoding).
//
// `range` should be alive until the end of `FinishOffThreadScriptDecoder`.
extern JS_PUBLIC_API OffThreadToken* DecodeOffThreadScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
const mozilla::Range<uint8_t>& range /* TranscodeRange& */,
OffThreadCompileCallback callback, void* callbackData);
extern JS_PUBLIC_API JSScript* FinishOffThreadScriptDecoder(
JSContext* cx, OffThreadToken* token);
extern JS_PUBLIC_API void CancelOffThreadScriptDecoder(JSContext* cx,
OffThreadToken* token);
// Decode multiple JSScript from the sources.
extern JS_PUBLIC_API OffThreadToken* DecodeMultiOffThreadScripts(
JSContext* cx, const ReadOnlyCompileOptions& options,
mozilla::Vector<TranscodeSource>& sources,
OffThreadCompileCallback callback, void* callbackData);
extern JS_PUBLIC_API bool FinishMultiOffThreadScriptsDecoder(
JSContext* cx, OffThreadToken* token,
MutableHandle<GCVector<JSScript*>> scripts);
extern JS_PUBLIC_API void CancelMultiOffThreadScriptsDecoder(
JSContext* cx, OffThreadToken* token);
// Tell off-thread compilation to/not to use the parse global.
// The default value is true.
//
// If set to false, off-thread compilation will compile to stencil, and
// instantiate the stencil on main-thread.
extern JS_PUBLIC_API void SetUseOffThreadParseGlobal(bool value);
} // namespace JS
namespace js {
// Returns the value set by JS::SetUseOffThreadParseGlobal.
// The default value is true.
//
// This value is consumed internally, and public API consumer shouldn't
// directly use this value.
extern bool UseOffThreadParseGlobal();
} // namespace js
#endif /* js_OffThreadScriptCompilation_h */
|