Remove lifetime mess, replace a lot of code with Arc instead of borrows.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This cleans up the codebase massively, and allows me to maintain some semblance of sanity.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
use std::intrinsics::transmute;
|
||||
|
||||
use crate::dynamic_data::{LearnedMove, MoveLearnMethod, Pokemon, TurnChoice};
|
||||
use crate::dynamic_data::LearnedMove;
|
||||
use crate::script_implementations::wasm::export_registry::register;
|
||||
use crate::script_implementations::wasm::extern_ref::ExternRef;
|
||||
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
|
||||
use crate::static_data::MoveData;
|
||||
|
||||
register! {
|
||||
fn learned_move_get_learn_method<'a>(
|
||||
fn learned_move_get_learn_method(
|
||||
env: &WebAssemblyEnv,
|
||||
turn_choice: ExternRef<LearnedMove>,
|
||||
) -> u8 {
|
||||
@@ -16,7 +16,7 @@ register! {
|
||||
}
|
||||
}
|
||||
|
||||
fn learned_move_get_move_data<'a>(
|
||||
fn learned_move_get_move_data(
|
||||
env: &WebAssemblyEnv,
|
||||
turn_choice: ExternRef<LearnedMove>,
|
||||
) -> ExternRef<MoveData> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::mem::transmute;
|
||||
|
||||
use crate::dynamic_data::{DamageSource, DynamicLibrary, Pokemon};
|
||||
use crate::dynamic_data::{DynamicLibrary, Pokemon};
|
||||
use crate::script_implementations::wasm::export_registry::register;
|
||||
use crate::script_implementations::wasm::extern_ref::ExternRef;
|
||||
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
|
||||
|
||||
@@ -7,10 +7,10 @@ use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
|
||||
|
||||
register! {
|
||||
|
||||
fn turn_choice_get_user<'a>(
|
||||
fn turn_choice_get_user(
|
||||
env: &WebAssemblyEnv,
|
||||
turn_choice: ExternRef<TurnChoice<'a, 'a>>,
|
||||
) -> ExternRef<Pokemon<'a, 'a>> {
|
||||
turn_choice: ExternRef<TurnChoice>,
|
||||
) -> ExternRef<Pokemon> {
|
||||
let turn_choice = turn_choice.value(env).unwrap();
|
||||
ExternRef::new(env.data().as_ref(), turn_choice.user().as_ref().deref())
|
||||
}
|
||||
@@ -28,10 +28,10 @@ register! {
|
||||
}
|
||||
}
|
||||
|
||||
fn turn_choice_move_used_move<'a>(
|
||||
fn turn_choice_move_used_move(
|
||||
env: &WebAssemblyEnv,
|
||||
turn_choice: ExternRef<TurnChoice<'a, 'a>>,
|
||||
) -> ExternRef<LearnedMove<'a>> {
|
||||
turn_choice: ExternRef<TurnChoice>,
|
||||
) -> ExternRef<LearnedMove> {
|
||||
if let TurnChoice::Move(d) = turn_choice.value(env).unwrap() {
|
||||
return ExternRef::new(env.data().as_ref(), d.used_move().as_ref());
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
use std::ffi::CString;
|
||||
use std::mem::{align_of, forget};
|
||||
use std::process::exit;
|
||||
|
||||
use wasmer::{Exports, Store};
|
||||
|
||||
pub(crate) use register;
|
||||
pub(crate) use register_func_with_env;
|
||||
|
||||
use crate::dynamic_data::DynamicLibrary;
|
||||
use crate::script_implementations::wasm::extern_ref::ExternRef;
|
||||
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnv;
|
||||
@@ -35,7 +31,7 @@ macro_rules! register_func_with_env {
|
||||
macro_rules! register {
|
||||
(
|
||||
$(
|
||||
fn $name:ident$(<$($lt:lifetime)*>)?($($par:ident: $par_type:ty),*$(,)?) $(-> $return:ty)? $block:block
|
||||
fn $name:ident($($par:ident: $par_type:ty),*$(,)?) $(-> $return:ty)? $block:block
|
||||
)*
|
||||
) => {
|
||||
pub(crate) fn register(exports: &mut crate::script_implementations::wasm::export_registry::Exports,
|
||||
@@ -43,7 +39,7 @@ macro_rules! register {
|
||||
env: crate::script_implementations::wasm::script_resolver::WebAssemblyEnv) {
|
||||
$(
|
||||
|
||||
fn $name<$($($lt)*)*>(
|
||||
fn $name(
|
||||
$(
|
||||
$par: $par_type,
|
||||
)*
|
||||
@@ -55,6 +51,9 @@ macro_rules! register {
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use register;
|
||||
pub(crate) use register_func_with_env;
|
||||
|
||||
pub(crate) fn register_webassembly_funcs(exports: &mut Exports, store: &Store, env: WebAssemblyEnv) {
|
||||
register_func_with_env!(exports, store, _print, env);
|
||||
register_func_with_env!(exports, store, _error, env);
|
||||
|
||||
@@ -21,10 +21,6 @@ impl<T: UniqueTypeId<u64>> ExternRef<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn index(&self) -> u32 {
|
||||
self.index
|
||||
}
|
||||
|
||||
/// Creates an ExternRef with a given resolver. This can be used in cases where we do not have an environment variable.
|
||||
pub(crate) fn new_with_resolver(resolver: &WebAssemblyScriptResolver, value: &T) -> Self {
|
||||
Self {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use std::any::Any;
|
||||
use std::mem::{align_of, size_of};
|
||||
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize};
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use wasmer::NativeFunc;
|
||||
|
||||
use crate::dynamic_data::{DynamicLibrary, Pokemon, Script, TurnChoice};
|
||||
use crate::dynamic_data::{DynamicLibrary, Script, TurnChoice};
|
||||
use crate::script_implementations::wasm::extern_ref::{ExternRef, VecExternRef};
|
||||
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnvironmentData;
|
||||
use crate::script_implementations::wasm::WebAssemblyScriptCapabilities;
|
||||
@@ -92,8 +91,7 @@ impl Script for WebAssemblyScript {
|
||||
let env = self.environment.upgrade().unwrap();
|
||||
let func = env.script_function_cache().on_before_turn(&env);
|
||||
if let Some(func) = func {
|
||||
func.call(self.self_ptr, ExternRef::new(env.as_ref(), choice).index())
|
||||
.unwrap();
|
||||
func.call(self.self_ptr, ExternRef::new(env.as_ref(), choice)).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,5 +58,5 @@ macro_rules! script_function_cache {
|
||||
|
||||
script_function_cache! {
|
||||
on_initialize -> NativeFunc<(u32, ExternRef<DynamicLibrary>, VecExternRef<EffectParameter>), ()>
|
||||
on_before_turn -> NativeFunc<(u32, u32), ()>
|
||||
on_before_turn -> NativeFunc<(u32, ExternRef<TurnChoice>), ()>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user