[gjs/esm/static-imports: 2/3] esm: Add additional builtin ES modules




commit 18dd7fc8a53598fe7a84c8ceac8b1946c8a53e54
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.
    
    'gettext' and 'system' have named exports because it's expected that the
    functions might be used individually that way. That doesn't make as much
    sense for Cairo which is to be understood more as if it were a GI module.

 doc/ESModules.md                    | 16 ++++++++++++++++
 doc/Modules.md                      | 20 +++++++++++---------
 installed-tests/js/testESModules.js | 32 ++++++++++++++++++++++++++++++++
 js.gresource.xml                    |  4 ++++
 modules/esm/cairo.js                | 10 ++++++++++
 modules/esm/gettext.js              | 30 ++++++++++++++++++++++++++++++
 modules/esm/system.js               | 26 ++++++++++++++++++++++++++
 7 files changed, 129 insertions(+), 9 deletions(-)
---
diff --git a/doc/ESModules.md b/doc/ESModules.md
index 80017a37..5c88ef98 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/installed-tests/js/testESModules.js b/installed-tests/js/testESModules.js
index 39ec46bb..fb5f2e23 100644
--- a/installed-tests/js/testESModules.js
+++ b/installed-tests/js/testESModules.js
@@ -1,8 +1,13 @@
 // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
 // SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
 
+import Cairo from 'cairo';
+import gettext from 'gettext';
+import {ngettext as N_} from 'gettext';
 import gi from 'gi';
 import Gio from 'gi://Gio';
+import system from 'system';
+import {exit} from 'system';
 
 import $ from 'resource:///org/gjs/jsunit/modules/exports.js';
 import {NamedExport, data} from 'resource:///org/gjs/jsunit/modules/exports.js';
@@ -33,3 +38,30 @@ describe('ES module imports', function () {
         expect(data).toEqual(Uint8Array.from('test data\n', c => c.codePointAt()));
     });
 });
+
+describe('Builtin ES modules', function () {
+    it('cairo default import', function () {
+        // one from cairoNative, one from cairo JS.
+        expect(typeof Cairo.Context).toBe('function');
+        expect(typeof Cairo.Format).toBe('object');
+    });
+
+    // cairo doesn't have named exports
+
+    it('gettext default import', function () {
+        expect(typeof gettext.ngettext).toBe('function');
+    });
+
+    it('gettext named import', function () {
+        expect(typeof N_).toBe('function');
+    });
+
+    it('system default import', function () {
+        expect(typeof system.exit).toBe('function');
+    });
+
+    it('system named import', function () {
+        expect(typeof exit).toBe('function');
+        expect(exit).toBe(system.exit);
+    });
+});
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..812d9305
--- /dev/null
+++ b/modules/esm/gettext.js
@@ -0,0 +1,30 @@
+// 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;
+
+export default {
+    setlocale,
+    textdomain,
+    bindtextdomain,
+    gettext,
+    dgettext,
+    dcgettext,
+    ngettext,
+    dngettext,
+    pgettext,
+    dpgettext,
+    domain,
+};
diff --git a/modules/esm/system.js b/modules/esm/system.js
new file mode 100644
index 00000000..07cf6c2c
--- /dev/null
+++ b/modules/esm/system.js
@@ -0,0 +1,26 @@
+// 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;
+
+export default {
+    addressOf,
+    refcount,
+    breakpoint,
+    gc,
+    exit,
+    version,
+    programInvocationName,
+    clearDateCaches,
+};


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