Skip to content

Commit

Permalink
Cleanup Browser Runtime Code
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Aug 16, 2022
1 parent 7b96832 commit 83c9b87
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 444 deletions.
66 changes: 66 additions & 0 deletions src/runtime/wasm/ecs/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use bevy::{ecs::component::ComponentId, prelude::*, utils::HashSet};
use wasm_bindgen::prelude::*;

use crate::runtime::{
types::{JsComponentInfo, JsEntity},
wasm::{BevyModJsScripting, WORLD_RID},
};

#[wasm_bindgen]
impl BevyModJsScripting {
pub fn op_world_tostring(&self, rid: u32) -> String {
assert_eq!(rid, WORLD_RID);
let state = self.state();
let world = &state.world;

format!("{world:?}")
}

pub fn op_world_components(&self, rid: u32) -> Result<JsValue, JsValue> {
assert_eq!(rid, WORLD_RID);
let state = self.state();
let world = &state.world;

let resource_components: HashSet<ComponentId> =
world.archetypes().resource().components().collect();

let infos = world
.components()
.iter()
.filter(|info| !resource_components.contains(&info.id()))
.map(JsComponentInfo::from)
.collect::<Vec<_>>();

Ok(serde_wasm_bindgen::to_value(&infos)?)
}

pub fn op_world_resources(&self, rid: u32) -> Result<JsValue, JsValue> {
assert_eq!(rid, WORLD_RID);
let state = self.state();
let world = &state.world;

let infos = world
.archetypes()
.resource()
.components()
.map(|id| world.components().get_info(id).unwrap())
.map(JsComponentInfo::from)
.collect::<Vec<_>>();

Ok(serde_wasm_bindgen::to_value(&infos)?)
}

pub fn op_world_entities(&self, rid: u32) -> Result<JsValue, JsValue> {
assert_eq!(rid, WORLD_RID);
let mut state = self.state();
let world = &mut state.world;

let entities = world
.query::<Entity>()
.iter(world)
.map(JsEntity::from)
.collect::<Vec<_>>();

Ok(serde_wasm_bindgen::to_value(&entities)?)
}
}
5 changes: 5 additions & 0 deletions src/runtime/wasm/ecs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

mod info;
mod query;
mod resource;
mod value;
49 changes: 49 additions & 0 deletions src/runtime/wasm/ecs/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use bevy::ecs::component::ComponentId;
use bevy_ecs_dynamic::reflect_value_ref::{query::EcsValueRefQuery, ReflectValueRef};
use wasm_bindgen::prelude::*;

use crate::runtime::{
types::QueryDescriptor,
wasm::{BevyModJsScripting, JsQueryItem, JsRuntimeState, JsValueRef, WORLD_RID},
};

#[wasm_bindgen]
impl BevyModJsScripting {
pub fn op_world_query(&self, rid: u32, query: JsValue) -> Result<JsValue, JsValue> {
assert_eq!(rid, WORLD_RID);
let mut state = self.state();
let JsRuntimeState {
world, value_refs, ..
} = &mut *state;

let descriptor: QueryDescriptor = serde_wasm_bindgen::from_value(query)?;

let components: Vec<ComponentId> = descriptor
.components
.iter()
.map(ComponentId::from)
.collect();

let mut query = EcsValueRefQuery::new(world, &components);
let results = query
.iter(world)
.map(|item| {
let components = item
.items
.into_iter()
.map(|value| JsValueRef {
key: value_refs.insert(ReflectValueRef::ecs_ref(value)),
function: None,
})
.collect();

JsQueryItem {
entity: item.entity.into(),
components,
}
})
.collect::<Vec<_>>();

Ok(serde_wasm_bindgen::to_value(&results)?)
}
}
35 changes: 35 additions & 0 deletions src/runtime/wasm/ecs/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use bevy_ecs_dynamic::reflect_value_ref::{EcsValueRef, ReflectValueRef};
use wasm_bindgen::prelude::*;

use crate::runtime::{
types::ComponentIdOrBevyType,
wasm::{BevyModJsScripting, JsRuntimeState, JsValueRef, WORLD_RID},
ToJsErr,
};

#[wasm_bindgen]
impl BevyModJsScripting {
pub fn op_world_get_resource(
&self,
rid: u32,
component_id: JsValue,
) -> Result<JsValue, JsValue> {
assert_eq!(rid, WORLD_RID);
let mut state = self.state();
let JsRuntimeState {
world, value_refs, ..
} = &mut *state;

let component_id: ComponentIdOrBevyType = serde_wasm_bindgen::from_value(component_id)?;
let component_id = component_id.component_id(world).to_js_error()?;

let value_ref = EcsValueRef::resource(world, component_id).to_js_error()?;

let value_ref = JsValueRef {
key: value_refs.insert(ReflectValueRef::ecs_ref(value_ref)),
function: None,
};

Ok(serde_wasm_bindgen::to_value(&value_ref)?)
}
}
Loading

0 comments on commit 83c9b87

Please sign in to comment.