tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

JSExecutionUtils.cpp (2341B)


      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/. */
      6 
      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.
     12 */
     13 
     14 #include "mozilla/dom/JSExecutionUtils.h"
     15 
     16 #include <utility>  // std::move
     17 
     18 #include "ErrorList.h"  // NS_ERROR_OUT_OF_MEMORY, NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW, NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW_UNCATCHABLE
     19 #include "js/CompilationAndEvaluation.h"  // JS::UpdateDebugMetadata
     20 #include "js/SourceText.h"                // JS::SourceText, JS::SourceOwnership
     21 #include "js/experimental/JSStencil.h"  // JS::Stencil, JS::CompileGlobalScriptToStencil
     22 #include "jsapi.h"                      // JS_IsExceptionPending
     23 #include "nsTPromiseFlatString.h"  // PromiseFlatString
     24 
     25 #if !defined(DEBUG) && !defined(MOZ_ENABLE_JS_DUMP)
     26 #  include "mozilla/StaticPrefs_browser.h"
     27 #endif
     28 
     29 using namespace mozilla;
     30 
     31 namespace mozilla::dom {
     32 
     33 nsresult EvaluationExceptionToNSResult(ErrorResult& aRv) {
     34  if (aRv.IsJSContextException()) {
     35    aRv.SuppressException();
     36    return NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW;
     37  }
     38  if (aRv.IsUncatchableException()) {
     39    aRv.SuppressException();
     40    return NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW_UNCATCHABLE;
     41  }
     42  // cases like NS_OK, NS_ERROR_DOM_JS_DECODING_ERROR and NS_ERROR_OUT_OF_MEMORY
     43  return aRv.StealNSResult();
     44 }
     45 
     46 void Compile(JSContext* aCx, JS::CompileOptions& aCompileOptions,
     47             const nsAString& aScript, RefPtr<JS::Stencil>& aStencil,
     48             ErrorResult& aRv) {
     49  const nsPromiseFlatString& flatScript = PromiseFlatString(aScript);
     50  JS::SourceText<char16_t> srcBuf;
     51  if (!srcBuf.init(aCx, flatScript.get(), flatScript.Length(),
     52                   JS::SourceOwnership::Borrowed)) {
     53    aRv.NoteJSContextException(aCx);
     54    return;
     55  }
     56 
     57  aStencil = CompileGlobalScriptToStencil(aCx, aCompileOptions, srcBuf);
     58  if (!aStencil) {
     59    aRv.NoteJSContextException(aCx);
     60  }
     61 }
     62 
     63 }  // namespace mozilla::dom