Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / base / JSExecutionUtils.cpp
blob941fa0914ff87d162ad270ee4fc2dd97f620d62e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /**
8 * This is not a generated file. It contains common utility functions
9 * invoked from the JavaScript code generated from IDL interfaces.
10 * The goal of the utility functions is to cut down on the size of
11 * the generated code itself.
14 #include "mozilla/dom/JSExecutionUtils.h"
16 #include <utility> // std::move
17 #include "ErrorList.h" // NS_ERROR_OUT_OF_MEMORY, NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW, NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW_UNCATCHABLE
18 #include "js/CompilationAndEvaluation.h" // JS::UpdateDebugMetadata
19 #include "js/experimental/JSStencil.h" // JS::Stencil, JS::CompileGlobalScriptToStencil
20 #include "js/SourceText.h" // JS::SourceText, JS::SourceOwnership
21 #include "jsapi.h" // JS_IsExceptionPending
22 #include "nsTPromiseFlatString.h" // PromiseFlatString
24 #if !defined(DEBUG) && !defined(MOZ_ENABLE_JS_DUMP)
25 # include "mozilla/StaticPrefs_browser.h"
26 #endif
28 using namespace mozilla;
30 namespace mozilla::dom {
32 nsresult EvaluationExceptionToNSResult(ErrorResult& aRv) {
33 if (aRv.IsJSContextException()) {
34 aRv.SuppressException();
35 return NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW;
37 if (aRv.IsUncatchableException()) {
38 aRv.SuppressException();
39 return NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW_UNCATCHABLE;
41 // cases like NS_OK, NS_ERROR_DOM_JS_DECODING_ERROR and NS_ERROR_OUT_OF_MEMORY
42 return aRv.StealNSResult();
45 void Compile(JSContext* aCx, JS::CompileOptions& aCompileOptions,
46 const nsAString& aScript, RefPtr<JS::Stencil>& aStencil,
47 ErrorResult& aRv) {
48 const nsPromiseFlatString& flatScript = PromiseFlatString(aScript);
49 JS::SourceText<char16_t> srcBuf;
50 if (!srcBuf.init(aCx, flatScript.get(), flatScript.Length(),
51 JS::SourceOwnership::Borrowed)) {
52 aRv.NoteJSContextException(aCx);
53 return;
56 aStencil = CompileGlobalScriptToStencil(aCx, aCompileOptions, srcBuf);
57 if (!aStencil) {
58 aRv.NoteJSContextException(aCx);
62 } // namespace mozilla::dom