diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
commit | da4c7e7ed675c3bf405668739c3012d140856109 (patch) | |
tree | cdd868dba063fecba609a1d819de271f0d51b23e /toolkit/components/uniffi-fixture-refcounts/src | |
parent | Adding upstream version 125.0.3. (diff) | |
download | firefox-da4c7e7ed675c3bf405668739c3012d140856109.tar.xz firefox-da4c7e7ed675c3bf405668739c3012d140856109.zip |
Adding upstream version 126.0.upstream/126.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-fixture-refcounts/src')
-rw-r--r-- | toolkit/components/uniffi-fixture-refcounts/src/lib.rs | 28 | ||||
-rw-r--r-- | toolkit/components/uniffi-fixture-refcounts/src/refcounts.udl | 8 |
2 files changed, 36 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-fixture-refcounts/src/lib.rs b/toolkit/components/uniffi-fixture-refcounts/src/lib.rs new file mode 100644 index 0000000000..e453b22a4a --- /dev/null +++ b/toolkit/components/uniffi-fixture-refcounts/src/lib.rs @@ -0,0 +1,28 @@ +/* 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/. */ + +/// This crate exists to test managing the Rust Arc strong counts as JS objects are +/// created/destroyed. See `test_refcounts.js` for how it's used. + +use std::sync::{Arc, Mutex}; + +pub struct SingletonObject; + +impl SingletonObject { + pub fn method(&self) { } +} + +static SINGLETON: Mutex<Option<Arc<SingletonObject>>> = Mutex::new(None); + +pub fn get_singleton() -> Arc<SingletonObject> { + Arc::clone(SINGLETON.lock().unwrap().get_or_insert_with(|| Arc::new(SingletonObject))) +} + +pub fn get_js_refcount() -> i32 { + // Subtract 2: one for the reference in the Mutex and one for the temporary reference that + // we're calling Arc::strong_count on. + (Arc::strong_count(&get_singleton()) as i32) - 2 +} + +include!(concat!(env!("OUT_DIR"), "/refcounts.uniffi.rs")); diff --git a/toolkit/components/uniffi-fixture-refcounts/src/refcounts.udl b/toolkit/components/uniffi-fixture-refcounts/src/refcounts.udl new file mode 100644 index 0000000000..25ec83cfcc --- /dev/null +++ b/toolkit/components/uniffi-fixture-refcounts/src/refcounts.udl @@ -0,0 +1,8 @@ +namespace refcounts { + SingletonObject get_singleton(); + i32 get_js_refcount(); +}; + +interface SingletonObject { + void method(); +}; |