[gjs/ewlsh/enable-weak-refs] global: Enable WeakRef




commit 1439ee46a3e1fef9abd8bf151b3e4533f4f1aa48
Author: Evan Welsh <contact evanwelsh com>
Date:   Sun Jan 30 15:54:16 2022 -0800

    global: Enable WeakRef
    
    Add a test suite to validate all expected global objects are in
    fact defined.

 gjs/global.cpp                   |  6 ++++++
 installed-tests/js/meson.build   |  1 +
 installed-tests/js/testGlobal.js | 42 ++++++++++++++++++++++++++++++++++++++++
 installed-tests/js/testself.js   |  4 ++++
 4 files changed, 53 insertions(+)
---
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 912d5a84e..3ed6ac5f3 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -44,6 +44,12 @@ union Utf8Unit;
 class GjsBaseGlobal {
     static JSObject* base(JSContext* cx, const JSClass* clasp,
                           JS::RealmCreationOptions options) {
+        // Enable WeakRef without the cleanupSome specification
+        // Re-evaluate if cleanupSome is standardized
+        // See: https://github.com/tc39/proposal-cleanup-some
+        options.setWeakRefsEnabled(
+            JS::WeakRefSpecifier::EnabledWithoutCleanupSome);
+
         JS::RealmBehaviors behaviors;
         JS::RealmOptions compartment_options(options, behaviors);
 
diff --git a/installed-tests/js/meson.build b/installed-tests/js/meson.build
index 6a5c40b50..f531fd356 100644
--- a/installed-tests/js/meson.build
+++ b/installed-tests/js/meson.build
@@ -236,6 +236,7 @@ modules_tests = [
     'ESModules',
     'Encoding',
     'GLibLogWriter',
+    'Global',
     'Timers',
 ]
 if build_cairo
diff --git a/installed-tests/js/testGlobal.js b/installed-tests/js/testGlobal.js
new file mode 100644
index 000000000..d1f6b3744
--- /dev/null
+++ b/installed-tests/js/testGlobal.js
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2022 Evan Welsh <contact evanwelsh com>
+
+describe('globalThis', () => {
+    function itIsDefined(value, message) {
+        it(`${message ? `${message} ` : ''}is defined`, function () {
+            expect(value).toBeDefined();
+        });
+    }
+
+    it('is equal to window', function () {
+        expect(globalThis.window).toBe(globalThis);
+        expect(window.globalThis).toBe(globalThis);
+    });
+
+    describe('WeakRef', () => {
+        itIsDefined(globalThis.WeakRef);
+    });
+
+    describe('console', () => {
+        itIsDefined(globalThis.console);
+    });
+
+    describe('TextEncoder', () => {
+        itIsDefined(globalThis.TextEncoder);
+    });
+
+    describe('TextDecoder', () => {
+        itIsDefined(globalThis.TextDecoder);
+    });
+
+    describe('ARGV', () => {
+        itIsDefined(globalThis.ARGV);
+    });
+
+    describe('print function', () => {
+        itIsDefined(globalThis.log, 'log');
+        itIsDefined(globalThis.print, 'print');
+        itIsDefined(globalThis.printerr, 'printerr');
+        itIsDefined(globalThis.logError, 'logError');
+    });
+});
diff --git a/installed-tests/js/testself.js b/installed-tests/js/testself.js
index 85332e75c..967548610 100644
--- a/installed-tests/js/testself.js
+++ b/installed-tests/js/testself.js
@@ -54,4 +54,8 @@ describe('SpiderMonkey features check', function () {
     it('Intl API was compiled into SpiderMonkey', function () {
         expect(Intl).toBeDefined();
     });
+
+    it('WeakRef is enabled', function () {
+        expect(WeakRef).toBeDefined();
+    });
 });


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