From eca185aa790526938aabde258d314e5598cb0421 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Mon, 3 Mar 2025 21:59:04 +0100 Subject: [PATCH] Issue #2692 - Part 3: Split out JS compiled script transcoding to its own header. --- js/public/Transcoding.h | 85 +++++++++++++++++++++++++++++++++++++++++ js/src/jsapi.h | 58 +--------------------------- js/src/moz.build | 1 + js/src/vm/Xdr.h | 1 + 4 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 js/public/Transcoding.h diff --git a/js/public/Transcoding.h b/js/public/Transcoding.h new file mode 100644 index 0000000000..f90d4663a3 --- /dev/null +++ b/js/public/Transcoding.h @@ -0,0 +1,85 @@ +/* -*- 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/. */ + +/* + * Structures and functions for transcoding compiled scripts and functions to + * and from memory. + */ + +#ifndef js_Transcoding_h +#define js_Transcoding_h + +#include "mozilla/Range.h" // mozilla::Range +#include "mozilla/Vector.h" // mozilla::Vector + +#include // size_t +#include // uint8_t, uint32_t + +#include "js/RootingAPI.h" // JS::Handle, JS::MutableHandle + +struct JSContext; +class JSFunction; +class JSObject; +class JSScript; + +namespace JS { + +// typedef mozilla::Vector TranscodeBuffer; +using TranscodeBuffer = mozilla::Vector; + +enum TranscodeResult +{ + // Successful encoding / decoding. + TranscodeResult_Ok = 0, + + // A warning message, is set to the message out-param. + TranscodeResult_Failure = 0x100, + TranscodeResult_Failure_BadBuildId = TranscodeResult_Failure | 0x1, + TranscodeResult_Failure_RunOnceNotSupported = TranscodeResult_Failure | 0x2, + TranscodeResult_Failure_AsmJSNotSupported = TranscodeResult_Failure | 0x3, + TranscodeResult_Failure_BadDecode = TranscodeResult_Failure | 0x4, + + TranscodeResult_Failure_WrongCompileOption = TranscodeResult_Failure | 0x5, + TranscodeResult_Failure_NotInterpretedFun = TranscodeResult_Failure | 0x6, + + // There is a pending exception on the context. + TranscodeResult_Throw = 0x200 +}; + +extern JS_PUBLIC_API(TranscodeResult) +EncodeScript(JSContext* cx, TranscodeBuffer& buffer, JS::HandleScript script); + +extern JS_PUBLIC_API(TranscodeResult) +EncodeInterpretedFunction(JSContext* cx, TranscodeBuffer& buffer, JS::HandleObject funobj); + +extern JS_PUBLIC_API(TranscodeResult) +DecodeScript(JSContext* cx, TranscodeBuffer& buffer, JS::MutableHandleScript scriptp, + size_t cursorIndex = 0); + +extern JS_PUBLIC_API(TranscodeResult) +DecodeInterpretedFunction(JSContext* cx, TranscodeBuffer& buffer, JS::MutableHandleFunction funp, + size_t cursorIndex = 0); + +// Register an encoder on the given 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. +// +// The |script| argument of |StartIncrementalEncoding| and +// |FinishIncrementalEncoding| should be the top-level script returned either as +// an out-param of any of the |Compile| functions, or the result of +// |FinishOffThreadScript|. +// +// The |buffer| argument of |FinishIncrementalEncoding| is used for appending +// the encoded bytecode into the buffer. If any of these functions failed, the +// content of |buffer| would be undefined. +extern JS_PUBLIC_API(bool) +StartIncrementalEncoding(JSContext* cx, JS::HandleScript script); + +extern JS_PUBLIC_API(bool) +FinishIncrementalEncoding(JSContext* cx, JS::HandleScript script, TranscodeBuffer& buffer); + +} /* namespace JS */ + +#endif /* js_Transcoding_h */ diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 496af32b41..9ae22ddacc 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -39,6 +39,7 @@ #include "js/SourceBufferHolder.h" #include "js/Stream.h" #include "js/TracingAPI.h" +#include "js/Transcoding.h" #include "js/UniquePtr.h" #include "js/Utility.h" #include "js/Value.h" @@ -5710,63 +5711,6 @@ class MOZ_RAII AutoHideScriptedCaller MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER }; -/* - * Encode/Decode interpreted scripts and functions to/from memory. - */ - -typedef mozilla::Vector TranscodeBuffer; - -enum TranscodeResult -{ - // Successful encoding / decoding. - TranscodeResult_Ok = 0, - - // A warning message, is set to the message out-param. - TranscodeResult_Failure = 0x100, - TranscodeResult_Failure_BadBuildId = TranscodeResult_Failure | 0x1, - TranscodeResult_Failure_RunOnceNotSupported = TranscodeResult_Failure | 0x2, - TranscodeResult_Failure_AsmJSNotSupported = TranscodeResult_Failure | 0x3, - TranscodeResult_Failure_BadDecode = TranscodeResult_Failure | 0x4, - - TranscodeResult_Failure_WrongCompileOption = TranscodeResult_Failure | 0x5, - TranscodeResult_Failure_NotInterpretedFun = TranscodeResult_Failure | 0x6, - - // There is a pending exception on the context. - TranscodeResult_Throw = 0x200 -}; - -extern JS_PUBLIC_API(TranscodeResult) -EncodeScript(JSContext* cx, TranscodeBuffer& buffer, JS::HandleScript script); - -extern JS_PUBLIC_API(TranscodeResult) -EncodeInterpretedFunction(JSContext* cx, TranscodeBuffer& buffer, JS::HandleObject funobj); - -extern JS_PUBLIC_API(TranscodeResult) -DecodeScript(JSContext* cx, TranscodeBuffer& buffer, JS::MutableHandleScript scriptp, - size_t cursorIndex = 0); - -extern JS_PUBLIC_API(TranscodeResult) -DecodeInterpretedFunction(JSContext* cx, TranscodeBuffer& buffer, JS::MutableHandleFunction funp, - size_t cursorIndex = 0); - -// Register an encoder on the given 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. -// -// The |script| argument of |StartIncrementalEncoding| and -// |FinishIncrementalEncoding| should be the top-level script returned either as -// an out-param of any of the |Compile| functions, or the result of -// |FinishOffThreadScript|. -// -// The |buffer| argument of |FinishIncrementalEncoding| is used for appending -// the encoded bytecode into the buffer. If any of these functions failed, the -// content of |buffer| would be undefined. -extern JS_PUBLIC_API(bool) -StartIncrementalEncoding(JSContext* cx, JS::HandleScript script); - -extern JS_PUBLIC_API(bool) -FinishIncrementalEncoding(JSContext* cx, JS::HandleScript script, TranscodeBuffer& buffer); - } /* namespace JS */ namespace js { diff --git a/js/src/moz.build b/js/src/moz.build index 3421fe9e99..9670d64bb5 100644 --- a/js/src/moz.build +++ b/js/src/moz.build @@ -100,6 +100,7 @@ EXPORTS.js += [ '../public/TraceKind.h', '../public/TracingAPI.h', '../public/TrackedOptimizationInfo.h', + '../public/Transcoding.h', '../public/TypeDecls.h', '../public/UbiNode.h', '../public/UbiNodeBreadthFirst.h', diff --git a/js/src/vm/Xdr.h b/js/src/vm/Xdr.h index 419b0dc86b..483a9aeabc 100644 --- a/js/src/vm/Xdr.h +++ b/js/src/vm/Xdr.h @@ -9,6 +9,7 @@ #include "mozilla/EndianUtils.h" #include "mozilla/TypeTraits.h" +#include "js/Transcoding.h" #include "jsatom.h" #include "jsfriendapi.h"