Commit bfd0eb30 authored by Jan de Mooij's avatar Jan de Mooij
Browse files

Bug 1851854 - Use SetPrefableContextOptions for worklet JS ContextOptions. r=mccr8, a=RyanVM

`SetPrefableContextOptions` currently has to be called on the main thread, so store
the options in `StartModuleLoadRunnable` and copy them on the worklet thread.

This doesn't update the options when the prefs are changed, but we've been moving
to read-on-startup for prefs so we probably don't need that.

Differential Revision: https://phabricator.services.mozilla.com/D187767
parent 6ab60d13
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include "WorkletFetchHandler.h"

#include "js/loader/ModuleLoadRequest.h"
#include "js/ContextOptions.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Fetch.h"
#include "mozilla/dom/Request.h"
@@ -24,6 +25,7 @@
#include "mozilla/TaskQueue.h"
#include "nsIInputStreamPump.h"
#include "nsIThreadRetargetableRequest.h"
#include "xpcpublic.h"

using JS::loader::ModuleLoadRequest;
using JS::loader::ScriptFetchOptions;
@@ -49,6 +51,7 @@ class StartModuleLoadRunnable final : public Runnable {
            JS_GetParentRuntime(CycleCollectedJSContext::Get()->Context())) {
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_ASSERT(mParentRuntime);
    xpc::SetPrefableContextOptions(mContextOptions);
  }

  ~StartModuleLoadRunnable() = default;
@@ -64,6 +67,7 @@ class StartModuleLoadRunnable final : public Runnable {
  nsCOMPtr<nsIURI> mReferrer;
  const nsTArray<nsString>& mLocalizedStrs;
  JSRuntime* mParentRuntime;
  JS::ContextOptions mContextOptions;
};

NS_IMETHODIMP
@@ -78,7 +82,7 @@ StartModuleLoadRunnable::Run() {

NS_IMETHODIMP StartModuleLoadRunnable::RunOnWorkletThread() {
  // This can be called on a GraphRunner thread or a DOM Worklet thread.
  WorkletThread::EnsureCycleCollectedJSContext(mParentRuntime);
  WorkletThread::EnsureCycleCollectedJSContext(mParentRuntime, mContextOptions);

  WorkletGlobalScope* globalScope = mWorkletImpl->GetGlobalScope();
  if (!globalScope) {
+5 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/EventQueue.h"
#include "mozilla/ThreadEventQueue.h"
#include "js/ContextOptions.h"
#include "js/Exception.h"
#include "js/Initialization.h"
#include "XPCSelfHostedShmem.h"
@@ -346,7 +347,8 @@ static bool DispatchToEventLoop(void* aClosure,
}

// static
void WorkletThread::EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime) {
void WorkletThread::EnsureCycleCollectedJSContext(
    JSRuntime* aParentRuntime, const JS::ContextOptions& aOptions) {
  CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::Get();
  if (ccjscx) {
    MOZ_ASSERT(ccjscx->GetAsWorkletJSContext());
@@ -360,6 +362,8 @@ void WorkletThread::EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime) {
    return;
  }

  JS::ContextOptionsRef(context->Context()) = aOptions;

  JS_SetGCParameter(context->Context(), JSGC_MAX_BYTES, uint32_t(-1));

  // FIXME: JS_SetDefaultLocale
+6 −1
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@

class nsIRunnable;

namespace JS {
class ContextOptions;
};  // namespace JS

namespace mozilla::dom {

class WorkletThread final : public nsThread, public nsIObserver {
@@ -29,7 +33,8 @@ class WorkletThread final : public nsThread, public nsIObserver {
  // Threads that call EnsureCycleCollectedJSContext must call
  // DeleteCycleCollectedJSContext::Get() before terminating.  Clients of
  // Create() do not need to do this as Terminate() will ensure this happens.
  static void EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime);
  static void EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime,
                                            const JS::ContextOptions& aOptions);
  static void DeleteCycleCollectedJSContext();

  static bool IsOnWorkletThread();