1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Structures and functions for transcoding compiled scripts and functions to
11 #ifndef js_Transcoding_h
12 #define js_Transcoding_h
14 #include "mozilla/Range.h" // mozilla::Range
15 #include "mozilla/Vector.h" // mozilla::Vector
17 #include <stddef.h> // size_t
18 #include <stdint.h> // uint8_t, uint32_t
20 #include "js/TypeDecls.h"
24 class ReadOnlyCompileOptions
;
26 using TranscodeBuffer
= mozilla::Vector
<uint8_t>;
27 using TranscodeRange
= mozilla::Range
<const uint8_t>;
29 struct TranscodeSource final
{
30 TranscodeSource(const TranscodeRange
& range_
, const char* file
, uint32_t line
)
31 : range(range_
), filename(file
), lineno(line
) {}
33 const TranscodeRange range
;
35 const uint32_t lineno
;
38 using TranscodeSources
= mozilla::Vector
<TranscodeSource
>;
40 enum class TranscodeResult
: uint8_t {
41 // Successful encoding / decoding.
44 // A warning message, is set to the message out-param.
46 Failure_BadBuildId
= Failure
| 0x1,
47 Failure_RunOnceNotSupported
= Failure
| 0x2,
48 Failure_AsmJSNotSupported
= Failure
| 0x3,
49 Failure_BadDecode
= Failure
| 0x4,
50 Failure_WrongCompileOption
= Failure
| 0x5,
51 Failure_NotInterpretedFun
= Failure
| 0x6,
53 // There is a pending exception on the context.
57 inline bool IsTranscodeFailureResult(const TranscodeResult result
) {
58 uint8_t raw_result
= static_cast<uint8_t>(result
);
59 uint8_t raw_failure
= static_cast<uint8_t>(TranscodeResult::Failure
);
60 TranscodeResult masked
=
61 static_cast<TranscodeResult
>(raw_result
& raw_failure
);
62 return masked
== TranscodeResult::Failure
;
65 static constexpr size_t BytecodeOffsetAlignment
= 4;
66 static_assert(BytecodeOffsetAlignment
<= alignof(std::max_align_t
),
67 "Alignment condition requires a custom allocator.");
69 // Align the bytecode offset for transcoding for the requirement.
70 inline size_t AlignTranscodingBytecodeOffset(size_t offset
) {
71 size_t extra
= offset
% BytecodeOffsetAlignment
;
75 size_t padding
= BytecodeOffsetAlignment
- extra
;
76 return offset
+ padding
;
79 inline bool IsTranscodingBytecodeOffsetAligned(size_t offset
) {
80 return offset
% BytecodeOffsetAlignment
== 0;
83 inline bool IsTranscodingBytecodeAligned(const void* offset
) {
84 return IsTranscodingBytecodeOffsetAligned(size_t(offset
));
87 // Encode JSScript into the buffer.
89 // If the `buffer` isn't empty, the start of the `buffer` should meet
90 // IsTranscodingBytecodeAligned, and the length should meet
91 // IsTranscodingBytecodeOffsetAligned.
93 // NOTE: As long as IsTranscodingBytecodeOffsetAligned is met, that means
94 // there's JS::BytecodeOffsetAlignment+extra bytes in the buffer,
95 // IsTranscodingBytecodeAligned should be guaranteed to meet by
96 // malloc, used by MallocAllocPolicy in mozilla::Vector.
97 extern JS_PUBLIC_API TranscodeResult
EncodeScript(JSContext
* cx
,
98 TranscodeBuffer
& buffer
,
99 Handle
<JSScript
*> script
);
101 // Decode JSScript from the buffer.
103 // The start of `buffer` and `cursorIndex` should meet
104 // IsTranscodingBytecodeAligned and IsTranscodingBytecodeOffsetAligned.
105 // (This should be handled while encoding).
106 extern JS_PUBLIC_API TranscodeResult
107 DecodeScript(JSContext
* cx
, const ReadOnlyCompileOptions
& options
,
108 TranscodeBuffer
& buffer
, MutableHandle
<JSScript
*> scriptp
,
109 size_t cursorIndex
= 0);
111 // Decode JSScript from the range.
113 // The start of `range` should meet IsTranscodingBytecodeAligned and
114 // IsTranscodingBytecodeOffsetAligned.
115 // (This should be handled while encoding).
116 extern JS_PUBLIC_API TranscodeResult
117 DecodeScript(JSContext
* cx
, const ReadOnlyCompileOptions
& options
,
118 const TranscodeRange
& range
, MutableHandle
<JSScript
*> scriptp
);
120 // If js::UseOffThreadParseGlobal is true, decode JSScript from the buffer.
122 // If js::UseOffThreadParseGlobal is false, decode CompilationStencil from the
123 // buffer and instantiate JSScript from it.
125 // options.useOffThreadParseGlobal should match JS::SetUseOffThreadParseGlobal.
127 // The start of `buffer` and `cursorIndex` should meet
128 // IsTranscodingBytecodeAligned and IsTranscodingBytecodeOffsetAligned.
129 // (This should be handled while encoding).
130 extern JS_PUBLIC_API TranscodeResult
DecodeScriptMaybeStencil(
131 JSContext
* cx
, const ReadOnlyCompileOptions
& options
,
132 TranscodeBuffer
& buffer
, MutableHandle
<JSScript
*> scriptp
,
133 size_t cursorIndex
= 0);
135 // If js::UseOffThreadParseGlobal is true, decode JSScript from the buffer.
137 // If js::UseOffThreadParseGlobal is false, decode CompilationStencil from the
138 // buffer and instantiate JSScript from it.
140 // And then register an encoder on its script source, such that all functions
141 // can be encoded as they are parsed. This strategy is used to avoid blocking
142 // the main thread in a non-interruptible way.
144 // See also JS::FinishIncrementalEncoding.
146 // options.useOffThreadParseGlobal should match JS::SetUseOffThreadParseGlobal.
148 // The start of `buffer` and `cursorIndex` should meet
149 // IsTranscodingBytecodeAligned and IsTranscodingBytecodeOffsetAligned.
150 // (This should be handled while encoding).
151 extern JS_PUBLIC_API TranscodeResult
DecodeScriptAndStartIncrementalEncoding(
152 JSContext
* cx
, const ReadOnlyCompileOptions
& options
,
153 TranscodeBuffer
& buffer
, MutableHandle
<JSScript
*> scriptp
,
154 size_t cursorIndex
= 0);
156 // Finish incremental encoding started by one of:
157 // * JS::CompileAndStartIncrementalEncoding
158 // * JS::FinishOffThreadScriptAndStartIncrementalEncoding
159 // * JS::DecodeScriptAndStartIncrementalEncoding
161 // The |script| argument of |FinishIncrementalEncoding| should be the top-level
162 // script returned from one of the above.
164 // The |buffer| argument of |FinishIncrementalEncoding| is used for appending
165 // the encoded bytecode into the buffer. If any of these functions failed, the
166 // content of |buffer| would be undefined.
168 // If js::UseOffThreadParseGlobal is true, |buffer| contains encoded JSScript.
170 // If js::UseOffThreadParseGlobal is false, |buffer| contains encoded
171 // CompilationStencil.
173 // If the `buffer` isn't empty, the start of the `buffer` should meet
174 // IsTranscodingBytecodeAligned, and the length should meet
175 // IsTranscodingBytecodeOffsetAligned.
177 // NOTE: As long as IsTranscodingBytecodeOffsetAligned is met, that means
178 // there's JS::BytecodeOffsetAlignment+extra bytes in the buffer,
179 // IsTranscodingBytecodeAligned should be guaranteed to meet by
180 // malloc, used by MallocAllocPolicy in mozilla::Vector.
181 extern JS_PUBLIC_API
bool FinishIncrementalEncoding(JSContext
* cx
,
182 Handle
<JSScript
*> script
,
183 TranscodeBuffer
& buffer
);
185 // Check if the compile options and script's flag matches.
187 // JS::DecodeScript* and JS::DecodeOffThreadScript internally check this.
189 // JS::DecodeMultiOffThreadScripts checks some options shared across multiple
190 // scripts. Caller is responsible for checking each script with this API when
191 // using the decoded script instead of compiling a new script wiht the given
193 extern JS_PUBLIC_API
bool CheckCompileOptionsMatch(
194 const ReadOnlyCompileOptions
& options
, JSScript
* script
);
198 #endif /* js_Transcoding_h */