fune/js/xpconnect/loader/ModuleEnvironmentProxy.h
Tooru Fujisawa d39eaa62a9 Bug 1766761 - Part 4: Add a shim for Cu.import/ChromeUtils.import with ESM-ified module. r=jonco,Standard8
This shim redirects Cu.import("foo.jsm") to ChromeUtils.importModule("foo.mjs")
if foo.jsm is already renamed to foo.mjs.

This de-optimizes ESM component's global `var`s, in order to expose all
variables in `ModuleEnvironmentObject`, and return those variables from
`Cu.improt`'s return value.

In contrast to normal Cu.import, the return value of shim version exposes
all non-exported lexical variables.
Bug 1768060 patch aligns the normal Cu.import to expose them as well,
so that `this.foo = foo;` hack can be removed in bug 1610653.

Once not-in-tree `Cu.import` consumers are gone and `Cu.import` is removed,
this shim and the de-optimization for the `var` can be removed.

Differential Revision: https://phabricator.services.mozilla.com/D145784
2022-05-13 22:02:39 +00:00

30 lines
1.1 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_loader_ModuleEnvironmentProxy_h
#define mozilla_loader_ModuleEnvironmentProxy_h
#include "js/TypeDecls.h" // JSContext, JSObject
#include "js/RootingAPI.h" // JS::Handle
namespace mozilla {
namespace loader {
// Create an object that works in the same way as global object returned by
// `Cu.import`. This proxy exposes all global variables, including lexical
// variables.
//
// This is a temporary workaround to support not-in-tree code that depends on
// `Cu.import` return value.
//
// This will eventually be removed once ESM-ification finishes.
JSObject* CreateModuleEnvironmentProxy(JSContext* aCx,
JS::Handle<JSObject*> aModuleObj);
} // namespace loader
} // namespace mozilla
#endif // mozilla_loader_ModuleEnvironmentProxy_h