Skip to content

Commit

Permalink
web: Use RuffleHandle as a custom key type, not a wrapper over Defaul…
Browse files Browse the repository at this point in the history
…tKey
  • Loading branch information
Dinnerbone committed Jun 1, 2024
1 parent d08334d commit f2db91e
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use ruffle_render::quality::StageQuality;
use ruffle_video_software::backend::SoftwareVideoBackend;
use ruffle_web_common::JsResult;
use serde::{Deserialize, Serialize};
use slotmap::{DefaultKey, SlotMap};
use slotmap::{new_key_type, SlotMap};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Once;
Expand All @@ -48,11 +48,19 @@ use web_sys::{

static RUFFLE_GLOBAL_PANIC: Once = Once::new();

new_key_type! {
/// An opaque handle to a `RuffleInstance` inside the pool.
///
/// This type is exported to JS, and is used to interact with the library.
#[wasm_bindgen]
pub struct RuffleHandle;
}

thread_local! {
/// We store the actual instances of the ruffle core in a static pool.
/// This gives us a clear boundary between the JS side and Rust side, avoiding
/// issues with lifetimes and type parameters (which cannot be exported with wasm-bindgen).
static INSTANCES: RefCell<SlotMap<DefaultKey, RefCell<RuffleInstance>>> = RefCell::new(SlotMap::new());
static INSTANCES: RefCell<SlotMap<RuffleHandle, RefCell<RuffleInstance>>> = RefCell::new(SlotMap::with_key());

static CURRENT_CONTEXT: RefCell<Option<*mut UpdateContext<'static, 'static>>> = const { RefCell::new(None) };
}
Expand Down Expand Up @@ -336,13 +344,6 @@ struct MovieMetadata {
uncompressed_len: i32,
}

/// An opaque handle to a `RuffleInstance` inside the pool.
///
/// This type is exported to JS, and is used to interact with the library.
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub struct RuffleHandle(DefaultKey);

#[wasm_bindgen]
impl RuffleHandle {
#[allow(clippy::new_ret_no_self)]
Expand Down Expand Up @@ -1105,7 +1106,7 @@ impl RuffleHandle {
fn add_instance(instance: RuffleInstance) -> Result<Self, RuffleInstanceError> {
INSTANCES.try_with(|instances| {
let mut instances = instances.try_borrow_mut()?;
let ruffle = Self(instances.insert(RefCell::new(instance)));
let ruffle = instances.insert(RefCell::new(instance));
Ok(ruffle)
})?
}
Expand All @@ -1114,7 +1115,7 @@ impl RuffleHandle {
fn remove_instance(&self) -> Result<RuffleInstance, RuffleInstanceError> {
INSTANCES.try_with(|instances| {
let mut instances = instances.try_borrow_mut()?;
if let Some(instance) = instances.remove(self.0) {
if let Some(instance) = instances.remove(*self) {
Ok(instance.into_inner())
} else {
Err(RuffleInstanceError::InstanceNotFound)
Expand All @@ -1130,7 +1131,7 @@ impl RuffleHandle {
let ret = INSTANCES
.try_with(|instances| {
let instances = instances.try_borrow()?;
if let Some(instance) = instances.get(self.0) {
if let Some(instance) = instances.get(*self) {
let instance = instance.try_borrow()?;
let _subscriber =
tracing::subscriber::set_default(instance.log_subscriber.clone());
Expand All @@ -1155,7 +1156,7 @@ impl RuffleHandle {
let ret = INSTANCES
.try_with(|instances| {
let instances = instances.try_borrow()?;
if let Some(instance) = instances.get(self.0) {
if let Some(instance) = instances.get(*self) {
let mut instance = instance.try_borrow_mut()?;
let _subscriber =
tracing::subscriber::set_default(instance.log_subscriber.clone());
Expand All @@ -1180,7 +1181,7 @@ impl RuffleHandle {
let ret = INSTANCES
.try_with(|instances| {
let instances = instances.try_borrow()?;
if let Some(instance) = instances.get(self.0) {
if let Some(instance) = instances.get(*self) {
let instance = instance.try_borrow()?;
let _subscriber =
tracing::subscriber::set_default(instance.log_subscriber.clone());
Expand Down Expand Up @@ -1212,7 +1213,7 @@ impl RuffleHandle {
let ret = INSTANCES
.try_with(|instances| {
let instances = instances.try_borrow()?;
if let Some(instance) = instances.get(self.0) {
if let Some(instance) = instances.get(*self) {
let instance = instance.try_borrow()?;
let _subscriber =
tracing::subscriber::set_default(instance.log_subscriber.clone());
Expand Down

0 comments on commit f2db91e

Please sign in to comment.