[gjs/esm/static-imports: 7/9] esm: Add additional builtin ES modules




commit 83eeeefc4af04edffbee50fffee0f994ddd15029
Author: Evan Welsh <contact evanwelsh com>
Date:   Wed Feb 3 17:02:35 2021 -0800

    esm: Add additional builtin ES modules
    
    Adds cairo, gettext, and system modules, to match the builtin legacy
    modules that GJS already had.

 doc/ESModules.md       | 16 ++++++++++++++++
 doc/Modules.md         | 20 +++++++++++---------
 js.gresource.xml       |  4 ++++
 modules/esm/cairo.js   | 10 ++++++++++
 modules/esm/gettext.js | 32 ++++++++++++++++++++++++++++++++
 modules/esm/system.js  | 28 ++++++++++++++++++++++++++++
 6 files changed, 101 insertions(+), 9 deletions(-)
---
diff --git a/doc/ESModules.md b/doc/ESModules.md
index a2566e67..4adb055f 100644
--- a/doc/ESModules.md
+++ b/doc/ESModules.md
@@ -154,6 +154,22 @@ import Gdk from 'gi://Gdk';
 import Hdy from 'gi://Hdy';
 ```
 
+## Built-in modules
+
+Built-in modules provide a default export with all their exported functions and properties. Most modules 
provide named exports too. `cairo` does not provide named exports of its API.
+
+Modifying the values of the default export _does not_ change the values of named exports.
+
+```js
+import system from 'system';
+system.exit(1);
+```
+
+```js
+import { ngettext as _ } from 'gettext';
+_('Hello!');
+```
+
 ## `import()` expressions
 
 Dynamic [`import()` statements][] are not currently supported in GJS.
diff --git a/doc/Modules.md b/doc/Modules.md
index 4c077bf5..c6bdac3e 100644
--- a/doc/Modules.md
+++ b/doc/Modules.md
@@ -63,14 +63,13 @@ import Gtk from 'gi://Gtk?version=3.0';
 
 ## Cairo
 
-**Import with `const Cairo = imports.cairo;`**
+**Import with `import Cairo from 'cairo';`**
 
 Mostly API compatible with [cairo](https://www.cairographics.org/documentation/), but using camelCase 
function names. There is list of constants in [cairo.js][cairo-const] and functions for each object in its 
corresponding C++ file (eg. [cairo-context.cpp][cairo-func]). A simple example drawing a 32x32 red circle:
 
 ```js
-imports.gi.versions.Gtk = "3.0";
-const Gtk = imports.gi.Gtk;
-const Cairo = imports.cairo;
+import Gtk from 'gi://Gtk?version=3.0';
+import Cairo from 'cairo';
 
 let drawingArea = new Gtk.DrawingArea({
     height_request: 32,
@@ -125,12 +124,16 @@ Gettext.ngettext("I have %d apple", "I have %d apples", num).format(num);
 
 ## [Gettext](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/script/gettext.js)
 
-**Import with `const Gettext = imports.gettext;`**
+**Import with `import gettext from 'gettext';`**
 
 Helper functions for gettext. See also [examples/gettext.js][example-gettext] for usage.
 
 [example-gettext]: https://gitlab.gnome.org/GNOME/gjs/blob/master/examples/gettext.js
 
+### Legacy Imports (`imports.gettext`)
+
+Gettext is also exposed via `imports.gettext` on the global `imports` object.
+
 ## [jsUnit](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/script/jsUnit.js)
 
 **DEPRECATED**
@@ -218,7 +221,7 @@ obj.disconnectAll();
 
 ## [System](https://gitlab.gnome.org/GNOME/gjs/blob/master/modules/system.cpp)
 
-**Import with `const System = imports.system;`**
+**Import with `import system from 'system';`**
 
 The System module offers a number of useful functions and properties for debugging and shell interaction 
(eg. ARGV):
 
@@ -262,9 +265,8 @@ The System module offers a number of useful functions and properties for debuggi
  [examples/gtk-application.js][example-application]):
 
     ```js
-    imports.gi.versions.Gtk = "3.0";
-    const Gtk = imports.gi.Gtk;
-    const System = imports.system;
+    import Gtk from 'gi://Gtk?version=3.0';
+    import system from 'system';
 
     let myApp = new Gtk.Application();
     myApp.connect("activate", () => log("activated"));
diff --git a/js.gresource.xml b/js.gresource.xml
index c8e9f7cc..adc8edde 100644
--- a/js.gresource.xml
+++ b/js.gresource.xml
@@ -10,7 +10,11 @@
     <file>modules/internal/modules/gi.js</file>
 
     <!-- ESM-based modules -->
+    <file>modules/esm/cairo.js</file>
+    <file>modules/esm/gettext.js</file>
     <file>modules/esm/gi.js</file>
+    <file>modules/esm/system.js</file>
+
     <!-- Script-based Modules -->
     <file>modules/script/_bootstrap/debugger.js</file>
     <file>modules/script/_bootstrap/default.js</file>
diff --git a/modules/esm/cairo.js b/modules/esm/cairo.js
new file mode 100644
index 00000000..d6127ff1
--- /dev/null
+++ b/modules/esm/cairo.js
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
+
+const cairo = import.meta.importSync('cairoNative');
+
+export default Object.assign(
+    {},
+    imports._cairo,
+    cairo
+);
diff --git a/modules/esm/gettext.js b/modules/esm/gettext.js
new file mode 100644
index 00000000..79b66718
--- /dev/null
+++ b/modules/esm/gettext.js
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
+
+export let {
+    setlocale,
+    textdomain,
+    bindtextdomain,
+    gettext,
+    dgettext,
+    dcgettext,
+    ngettext,
+    dngettext,
+    pgettext,
+    dpgettext,
+    domain,
+} = imports._gettext;
+
+let _ = {
+    setlocale,
+    textdomain,
+    bindtextdomain,
+    gettext,
+    dgettext,
+    dcgettext,
+    ngettext,
+    dngettext,
+    pgettext,
+    dpgettext,
+    domain,
+};
+
+export default _;
diff --git a/modules/esm/system.js b/modules/esm/system.js
new file mode 100644
index 00000000..e0b43e57
--- /dev/null
+++ b/modules/esm/system.js
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
+
+const system = import.meta.importSync('system');
+
+export let {
+    addressOf,
+    refcount,
+    breakpoint,
+    gc,
+    exit,
+    version,
+    programInvocationName,
+    clearDateCaches,
+} = system;
+
+let _ = {
+    addressOf,
+    refcount,
+    breakpoint,
+    gc,
+    exit,
+    version,
+    programInvocationName,
+    clearDateCaches,
+};
+
+export default _;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]