More work on WASM handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-08-20 12:22:12 +02:00
parent 703fd2c147
commit 2d4253e155
36 changed files with 922 additions and 87 deletions

View File

@@ -1,12 +1,13 @@
use std::any::Any;
use std::mem::{align_of, size_of};
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize};
use std::sync::Weak;
use std::sync::{Arc, Weak};
use hashbrown::HashSet;
use wasmer::NativeFunc;
use crate::dynamic_data::{DynamicLibrary, Script};
use crate::script_implementations::wasm::extern_ref::ExternRef;
use crate::dynamic_data::{DynamicLibrary, Pokemon, 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;
use crate::static_data::EffectParameter;
@@ -24,11 +25,11 @@ pub struct WebAssemblyScript {
/// we will not execute its methods. This holds the number of suppressions on the script.
suppressed_count: AtomicUsize,
/// The owner of this script (where the script is attached to)
owner_ptr: AtomicPtr<u8>,
_owner_ptr: AtomicPtr<u8>,
/// Pointer inside WebAssembly memory where the data is for this script.
self_ptr: u32,
/// Capabilities define which functions we actually implement.
capabilities: AtomicPtr<HashSet<WebAssemblyScriptCapabilities>>,
capabilities: Arc<HashSet<WebAssemblyScriptCapabilities>>,
/// The global runtime environment data.
environment: Weak<WebAssemblyEnvironmentData>,
}
@@ -38,7 +39,7 @@ impl WebAssemblyScript {
pub fn new(
owner_ptr: *mut u8,
self_ptr: u32,
capabilities: *mut HashSet<WebAssemblyScriptCapabilities>,
capabilities: Arc<HashSet<WebAssemblyScriptCapabilities>>,
environment: Weak<WebAssemblyEnvironmentData>,
name: StringKey,
) -> Self {
@@ -46,9 +47,9 @@ impl WebAssemblyScript {
name,
marked_for_deletion: Default::default(),
suppressed_count: Default::default(),
owner_ptr: AtomicPtr::new(owner_ptr),
_owner_ptr: AtomicPtr::new(owner_ptr),
self_ptr,
capabilities: AtomicPtr::new(capabilities),
capabilities,
environment,
}
}
@@ -67,20 +68,56 @@ impl Script for WebAssemblyScript {
&self.suppressed_count
}
fn on_initialize(&self, library: &DynamicLibrary, _pars: &[EffectParameter]) {
fn on_initialize(&self, library: &DynamicLibrary, pars: &[EffectParameter]) {
if !self.capabilities.contains(&WebAssemblyScriptCapabilities::Initialize) {
return;
}
let env = self.environment.upgrade().unwrap();
let func = env.script_function_cache().on_initialize(&env);
if let Some(func) = func {
func.call(
self.self_ptr,
ExternRef::new(env.as_ref(), library),
VecExternRef::new(env.as_ref(), pars),
)
.unwrap();
}
}
fn on_before_turn(&self, choice: &TurnChoice) {
if !self.capabilities.contains(&WebAssemblyScriptCapabilities::OnBeforeTurn) {
return;
}
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();
}
}
fn change_speed(&self, choice: &TurnChoice, speed: &mut u32) {
if !self.capabilities.contains(&WebAssemblyScriptCapabilities::ChangeSpeed) {
return;
}
let env = self.environment.upgrade().unwrap();
let exported = env.exported_functions();
if let Some(f) = exported.get(&"script_on_initialize".into()) {
let func: NativeFunc<(u32, ExternRef<DynamicLibrary>, u32), ()> = f.native().unwrap();
func.call(self.self_ptr, ExternRef::new(env.as_ref(), library), 0)
if let Some(f) = exported.get::<StringKey>(&"script_change_speed".into()) {
let func: NativeFunc<(u32, ExternRef<TurnChoice>, u32), ()> = f.native().unwrap();
let ptr = env.temp_allocate_mem_typed::<u32>();
func.call(self.self_ptr, ExternRef::new(env.as_ref(), choice), ptr.wasm_pointer)
.unwrap();
unsafe {
*speed = *ptr.ptr;
}
}
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}