Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 14:34:42 +02:00
parent 3058739ea0
commit feffb5f030
36 changed files with 466 additions and 274 deletions

View File

@@ -59,7 +59,7 @@ register! {
env: FunctionEnvMut<WebAssemblyEnv>,
battle: ExternRef<Battle>,
) -> ExternRef<StringKey> {
let weather = battle.value_func(&env).unwrap().weather_name();
let weather = battle.value_func(&env).unwrap().weather_name().unwrap();
if let Some(weather) = weather {
ExternRef::func_new(&env, &weather)
} else {

View File

@@ -10,14 +10,14 @@ register! {
env: FunctionEnvMut<WebAssemblyEnv>,
battle_random: ExternRef<BattleRandom>,
) -> i32 {
battle_random.value_func(&env).unwrap().get()
battle_random.value_func(&env).unwrap().get().unwrap()
}
fn battle_random_get_max(
env: FunctionEnvMut<WebAssemblyEnv>,
battle_random: ExternRef<BattleRandom>,
max: i32
) -> i32 {
battle_random.value_func(&env).unwrap().get_max(max)
battle_random.value_func(&env).unwrap().get_max(max).unwrap()
}
fn battle_random_get_between(
env: FunctionEnvMut<WebAssemblyEnv>,
@@ -25,6 +25,6 @@ register! {
min: i32,
max: i32
) -> i32 {
battle_random.value_func(&env).unwrap().get_between(min, max)
battle_random.value_func(&env).unwrap().get_between(min, max).unwrap()
}
}

View File

@@ -10,6 +10,6 @@ register! {
battle_random: ExternRef<ChoiceQueue>,
pokemon: ExternRef<Pokemon>
) -> u8 {
u8::from(battle_random.value_func(&env).unwrap().move_pokemon_choice_next(pokemon.value_func(&env).unwrap()))
u8::from(battle_random.value_func(&env).unwrap().move_pokemon_choice_next(pokemon.value_func(&env).unwrap()).unwrap())
}
}

View File

@@ -108,7 +108,7 @@ register! {
source: u8
) {
unsafe{
pokemon.value_func(&env).unwrap().damage(damage, transmute(source));
pokemon.value_func(&env).unwrap().damage(damage, transmute(source)).unwrap();
}
}

View File

@@ -1,3 +1,6 @@
// allow these for now until we have a good result interface defined.
#[allow(clippy::unwrap_used)]
#[allow(clippy::expect_used)]
/// The export registry module deals with registering all functions we require in WebAssembly.
mod export_registry;
/// A hacky extern ref implementation to ensure the client does not do things it is not allowed to do.

View File

@@ -3,6 +3,8 @@ use std::fmt::{Debug, Formatter};
use std::mem::{align_of, forget, size_of};
use std::sync::{Arc, Weak};
use anyhow::Result;
use anyhow_ext::{anyhow, ensure};
use hashbrown::{HashMap, HashSet};
use parking_lot::lock_api::RwLockReadGuard;
use parking_lot::{RawRwLock, RwLock};
@@ -19,7 +21,7 @@ use crate::script_implementations::wasm::script_function_cache::ScriptFunctionCa
use crate::script_implementations::wasm::temp_wasm_allocator::{AllocatedObject, TempWasmAllocator};
use crate::script_implementations::wasm::WebAssemblyScriptCapabilities;
use crate::static_data::Item;
use crate::{PkmnResult, ScriptCategory, StringKey, ValueIdentifiable, ValueIdentifier};
use crate::{ScriptCategory, StringKey, ValueIdentifiable, ValueIdentifier};
/// A WebAssembly script resolver implements the dynamic scripts functionality with WebAssembly.
pub struct WebAssemblyScriptResolver {
@@ -175,21 +177,20 @@ impl ScriptResolver for WebAssemblyScriptResolver {
owner: ScriptOwnerData,
category: ScriptCategory,
script_key: &StringKey,
) -> PkmnResult<Option<Arc<dyn Script>>> {
) -> Result<Option<Arc<dyn Script>>> {
let script = self
.load_script_fn
.as_ref()
.unwrap()
.ok_or(anyhow!("Load script function was not found"))?
.call(
&mut self.store_mut(),
category as u8,
ExternRef::new_with_resolver(self, script_key),
)
.unwrap();
)?;
self.environment_data.setup_script(script, category, script_key, owner)
}
fn load_item_script(&self, _key: &dyn Item) -> PkmnResult<Option<Arc<dyn ItemScript>>> {
fn load_item_script(&self, _key: &dyn Item) -> Result<Option<Arc<dyn ItemScript>>> {
todo!()
}
}
@@ -354,7 +355,7 @@ impl WebAssemblyEnvironmentData {
.unwrap()
.call(
&mut self.store_mut(),
(size_of::<T>() * v.len()) as u32,
(std::mem::size_of_val(v)) as u32,
align_of::<T>() as u32,
)
.unwrap();
@@ -484,7 +485,7 @@ impl WebAssemblyEnvironmentData {
category: ScriptCategory,
script_key: &StringKey,
owner: ScriptOwnerData,
) -> PkmnResult<Option<Arc<dyn Script>>> {
) -> Result<Option<Arc<dyn Script>>> {
if script_ptr == 0 {
return Ok(None);
}
@@ -502,13 +503,12 @@ impl WebAssemblyEnvironmentData {
.read()
.get::<StringKey>(&"get_script_capabilities".into())
{
let res = get_cap
.call(&mut self.store_mut(), &[Value::I32(script_ptr as i32)])
.unwrap();
let res = get_cap.call(&mut self.store_mut(), &[Value::I32(script_ptr as i32)])?;
let ptr =
(self.memory() as *const WebAssemblyScriptCapabilities).offset(res[0].i32().unwrap() as isize);
let length = res[1].i32().unwrap() as usize;
for i in 0..length {
let length = res[1].i32();
ensure!(length.is_some(), "Script capabilities length was not a valid i32");
for i in 0..length.unwrap() as usize {
capabilities.insert(*ptr.add(i));
}
}