diff --git a/js/public/OffThreadScriptCompilation.h b/js/public/OffThreadScriptCompilation.h new file mode 100644 index 0000000000..2194791bab --- /dev/null +++ b/js/public/OffThreadScriptCompilation.h @@ -0,0 +1,93 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* 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/Vector.h" // mozilla::Vector + +#include // 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 JSContext; +class JSScript; + +namespace JS { + +class SourceBufferHolder; + +} // namespace JS + +namespace JS { + +using OffThreadCompileCallback = void (*)(void* token, void* callbackData); + +extern JS_PUBLIC_API(bool) +CanCompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, size_t length); + +/* + * 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 the main thread, + * so must ensure that its operations are thread safe. Afterwards, one of the + * following functions must be invoked on the 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) +CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, + OffThreadCompileCallback callback, void* callbackData); + +extern JS_PUBLIC_API(JSScript*) +FinishOffThreadScript(JSContext* cx, void* token); + +extern JS_PUBLIC_API(void) +CancelOffThreadScript(JSContext* cx, void* token); + +extern JS_PUBLIC_API(bool) +CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, + OffThreadCompileCallback callback, void* callbackData); + +extern JS_PUBLIC_API(JSObject*) +FinishOffThreadModule(JSContext* cx, void* token); + +extern JS_PUBLIC_API(void) +CancelOffThreadModule(JSContext* cx, void* token); + +extern JS_PUBLIC_API(bool) +DecodeOffThreadScript(JSContext* cx, const ReadOnlyCompileOptions& options, + mozilla::Vector& buffer /* TranscodeBuffer& */, size_t cursor, + OffThreadCompileCallback callback, void* callbackData); + +extern JS_PUBLIC_API(JSScript*) +FinishOffThreadScriptDecoder(JSContext* cx, void* token); + +extern JS_PUBLIC_API(void) +CancelOffThreadScriptDecoder(JSContext* cx, void* token); + +} // namespace JS + +#endif /* js_OffThreadScriptCompilation_h */ \ No newline at end of file diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 9ae22ddacc..39828d25ff 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -33,6 +33,7 @@ #include "js/GCVector.h" #include "js/HashTable.h" #include "js/Id.h" +#include "js/OffThreadScriptCompilation.h" #include "js/Principals.h" #include "js/Realm.h" #include "js/RootingAPI.h" @@ -3678,59 +3679,6 @@ extern JS_PUBLIC_API(bool) CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options, const char* filename, JS::MutableHandleScript script); -extern JS_PUBLIC_API(bool) -CanCompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, size_t length); - -/* - * 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 the main thread, - * so must ensure that its operations are thread safe. Afterwards, one of the - * following functions must be invoked on the 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) -CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, - const char16_t* chars, size_t length, - OffThreadCompileCallback callback, void* callbackData); - -extern JS_PUBLIC_API(JSScript*) -FinishOffThreadScript(JSContext* cx, void* token); - -extern JS_PUBLIC_API(void) -CancelOffThreadScript(JSContext* cx, void* token); - -extern JS_PUBLIC_API(bool) -CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options, - const char16_t* chars, size_t length, - OffThreadCompileCallback callback, void* callbackData); - -extern JS_PUBLIC_API(JSObject*) -FinishOffThreadModule(JSContext* cx, void* token); - -extern JS_PUBLIC_API(void) -CancelOffThreadModule(JSContext* cx, void* token); - -extern JS_PUBLIC_API(bool) -DecodeOffThreadScript(JSContext* cx, const ReadOnlyCompileOptions& options, - mozilla::Vector& buffer /* TranscodeBuffer& */, size_t cursor, - OffThreadCompileCallback callback, void* callbackData); - -extern JS_PUBLIC_API(JSScript*) -FinishOffThreadScriptDecoder(JSContext* cx, void* token); - -extern JS_PUBLIC_API(void) -CancelOffThreadScriptDecoder(JSContext* cx, void* token); - /** * Compile a function with envChain plus the global as its scope chain. * envChain must contain objects in the current compartment of cx. The actual diff --git a/js/src/jspubtd.h b/js/src/jspubtd.h index b2be737904..21318af6c9 100644 --- a/js/src/jspubtd.h +++ b/js/src/jspubtd.h @@ -150,8 +150,6 @@ class JS_PUBLIC_API(AutoEnterCycleCollection); class JS_PUBLIC_API(AutoAssertOnBarrier); struct JS_PUBLIC_API(PropertyDescriptor); -typedef void (*OffThreadCompileCallback)(void* token, void* callbackData); - enum class HeapState { Idle, // doing nothing with the GC heap Tracing, // tracing the GC heap without collecting, e.g. IterateCompartments() diff --git a/js/src/moz.build b/js/src/moz.build index 9670d64bb5..383ea8233f 100644 --- a/js/src/moz.build +++ b/js/src/moz.build @@ -84,6 +84,7 @@ EXPORTS.js += [ '../public/Initialization.h', '../public/LegacyIntTypes.h', '../public/MemoryMetrics.h', + '../public/OffThreadScriptCompilation.h', '../public/Principals.h', '../public/ProfilingFrameIterator.h', '../public/ProfilingStack.h',