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
|
/* -*- 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/OffThreadScriptCompilation.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Range.h" // mozilla::Range
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "mozilla/Vector.h" // mozilla::Vector
#include <stddef.h> // size_t
#include "jspubtd.h" // js::CurrentThreadCanAccessRuntime
#include "jstypes.h" // JS_PUBLIC_API
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions
#include "js/experimental/JSStencil.h" // JS::CompileToStencilOffThread, JS::FinishCompileToStencilOffThread
#include "js/SourceText.h" // JS::SourceText
#include "vm/HelperThreadState.h" // js::StartOffThreadParseScript
#include "vm/JSContext.h" // JSContext
#include "vm/Runtime.h" // js::CanUseExtraThreads
using namespace js;
using mozilla::Utf8Unit;
using JS::ReadOnlyCompileOptions;
enum class OffThread { Compile, Decode };
template <typename OptionT>
static bool CanDoOffThread(JSContext* cx, const OptionT& options,
size_t length) {
static const size_t TINY_LENGTH = 5 * 1000;
// These are heuristics which the caller may choose to ignore (e.g., for
// testing purposes).
if (!options.forceAsync) {
// Compiling off the main thread inolves significant overheads.
// Don't bother if the script is tiny.
if (length < TINY_LENGTH) {
return false;
}
}
return cx->runtime()->canUseParallelParsing() && CanUseExtraThreads();
}
JS_PUBLIC_API bool JS::CanCompileOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options, size_t length) {
return CanDoOffThread(cx, options, length);
}
JS_PUBLIC_API JS::OffThreadToken* JS::CompileToStencilOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadCompileToStencil(cx, options, srcBuf, callback,
callbackData);
}
JS_PUBLIC_API JS::OffThreadToken* JS::CompileToStencilOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadCompileToStencil(cx, options, srcBuf, callback,
callbackData);
}
JS_PUBLIC_API JS::OffThreadToken* JS::CompileModuleToStencilOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadCompileModuleToStencil(cx, options, srcBuf, callback,
callbackData);
}
JS_PUBLIC_API JS::OffThreadToken* JS::CompileModuleToStencilOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadCompileModuleToStencil(cx, options, srcBuf, callback,
callbackData);
}
JS_PUBLIC_API JS::OffThreadToken* JS::DecodeStencilOffThread(
JSContext* cx, const DecodeOptions& options, const TranscodeBuffer& buffer,
size_t cursor, OffThreadCompileCallback callback, void* callbackData) {
JS::TranscodeRange range(buffer.begin() + cursor, buffer.length() - cursor);
MOZ_ASSERT(CanDecodeOffThread(cx, options, range.length()));
return StartOffThreadDecodeStencil(cx, options, range, callback,
callbackData);
}
JS_PUBLIC_API JS::OffThreadToken* JS::DecodeStencilOffThread(
JSContext* cx, const DecodeOptions& options, const TranscodeRange& range,
OffThreadCompileCallback callback, void* callbackData) {
MOZ_ASSERT(CanDecodeOffThread(cx, options, range.length()));
return StartOffThreadDecodeStencil(cx, options, range, callback,
callbackData);
}
JS_PUBLIC_API already_AddRefed<JS::Stencil> JS::FinishOffThreadStencil(
JSContext* cx, JS::OffThreadToken* token,
JS::InstantiationStorage* storage /* = nullptr */) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
RefPtr<JS::Stencil> stencil =
HelperThreadState().finishStencilTask(cx, token, storage);
return stencil.forget();
}
JS_PUBLIC_API void JS::CancelOffThreadToken(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
HelperThreadState().cancelParseTask(cx->runtime(), token);
}
JS_PUBLIC_API bool JS::CanDecodeOffThread(JSContext* cx,
const DecodeOptions& options,
size_t length) {
return CanDoOffThread(cx, options, length);
}
JS_PUBLIC_API JS::OffThreadToken* JS::DecodeMultiStencilsOffThread(
JSContext* cx, const DecodeOptions& options, TranscodeSources& sources,
OffThreadCompileCallback callback, void* callbackData) {
#ifdef DEBUG
size_t length = 0;
for (auto& source : sources) {
length += source.range.length();
}
MOZ_ASSERT(CanDecodeOffThread(cx, options, length));
#endif
return StartOffThreadDecodeMultiStencils(cx, options, sources, callback,
callbackData);
}
JS_PUBLIC_API bool JS::FinishDecodeMultiStencilsOffThread(
JSContext* cx, JS::OffThreadToken* token,
mozilla::Vector<RefPtr<JS::Stencil>>* stencils) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
return HelperThreadState().finishMultiStencilsDecodeTask(cx, token, stencils);
}
|