Fixes memory issue
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-07-18 10:49:58 +02:00
parent 7682704945
commit 9472c1cec2
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 19 additions and 16 deletions

View File

@ -11,6 +11,7 @@
#![feature(is_some_with)]
#![feature(core_ffi_c)]
#![feature(new_uninit)]
#![feature(get_mut_unchecked)]
//! PkmnLib
//! PkmnLib is a full featured implementation of Pokemon. while currently focused on implementing

View File

@ -1,9 +1,7 @@
use core::ffi::c_char;
use std::borrow::Cow;
use std::ffi::CString;
use std::mem::{align_of, size_of};
use std::mem::align_of;
use wasmer::wasmparser::Data;
use wasmer::{Exports, Function, Store};
use crate::dynamic_data::DynamicLibrary;
@ -82,7 +80,7 @@ fn const_string_get_hash(env: &WebAssemblyEnv, string_key: ExternRef<StringKey>)
fn const_string_get_str(env: &WebAssemblyEnv, string_key: ExternRef<StringKey>) -> u32 {
let string_key = string_key.value(env).str();
let mut s: CString = CString::new(string_key.as_bytes()).unwrap();
let s: CString = CString::new(string_key.as_bytes()).unwrap();
let wasm_string_ptr = env
.resolver()
.allocate_mem(string_key.len() as u32, align_of::<CString>() as u32);

View File

@ -1,6 +1,4 @@
use std::any::{Any, TypeId};
use std::fmt::{Debug, Formatter};
use std::pin::Pin;
use std::sync::Arc;
use hashbrown::{HashMap, HashSet};
@ -46,7 +44,7 @@ struct ScriptCapabilitiesKey {
impl WebAssemblyScriptResolver {
/// Instantiates a new WebAssemblyScriptResolver.
pub fn new() -> Self {
pub fn new() -> Box<WebAssemblyScriptResolver> {
let config = Cranelift::default();
let mut features = Features::new();
features.multi_value = true;
@ -54,7 +52,7 @@ impl WebAssemblyScriptResolver {
let universal = Universal::new(config).features(features);
let engine = universal.engine();
let store = Store::new(&engine);
Self {
let s = Self {
engine,
store,
module: Default::default(),
@ -69,7 +67,8 @@ impl WebAssemblyScriptResolver {
extern_ref_pointers: Default::default(),
extern_ref_pointers_lookup: Default::default(),
extern_ref_type_lookup: Default::default(),
}
};
Box::new(s)
}
/// Load a compiled WASM module.
@ -146,7 +145,7 @@ impl WebAssemblyScriptResolver {
/// its proper value, validates its type, and returns the value.
pub fn get_extern_ref_value<T: UniqueTypeId<u64>>(&self, index: u32) -> &T {
let read_guard = self.extern_ref_pointers.read();
let ptr = read_guard.get(index as usize).unwrap();
let ptr = read_guard.get((index - 1) as usize).unwrap();
let expected_type_id = self.extern_ref_type_lookup.read()[&ptr];
if expected_type_id != T::id().0 {
panic!("Extern ref was accessed with wrong type");

View File

@ -9,10 +9,10 @@ use project_root::get_project_root;
use serde_json::Value;
use pkmn_lib::defines::LevelInt;
use pkmn_lib::dynamic_data::DynamicLibrary;
use pkmn_lib::dynamic_data::Gen7BattleStatCalculator;
use pkmn_lib::dynamic_data::Gen7DamageLibrary;
use pkmn_lib::dynamic_data::Gen7MiscLibrary;
use pkmn_lib::dynamic_data::{DynamicLibrary, EmptyScriptResolver};
use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver;
use pkmn_lib::static_data::{
Ability, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, Item,
@ -34,14 +34,14 @@ pub fn load_library() -> DynamicLibrary {
load_moves(&path, &mut data);
load_species(&path, &mut data);
let mut resolver = WebAssemblyScriptResolver::new();
load_wasm(&path, &mut resolver);
load_wasm(&path, resolver.as_mut());
let dynamic = DynamicLibrary::new(
data,
Box::new(Gen7BattleStatCalculator {}),
Box::new(Gen7DamageLibrary::new(false)),
Box::new(Gen7MiscLibrary::new()),
Box::new(resolver),
resolver,
);
dynamic
}
@ -276,10 +276,10 @@ pub fn load_species(path: &String, library: &mut StaticData) {
}
fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) {
let mut file = File::open(path.to_string() + "gen7_scripts_rs.wasm").unwrap();
let file = File::open(path.to_string() + "gen7_scripts_rs.wasm").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer);
reader.read_to_end(&mut buffer).unwrap();
library.load_wasm_from_bytes(&buffer);
}

View File

@ -30,7 +30,7 @@ fn get_library<'a>() -> &'a DynamicLibrary {
#[cfg_attr(miri, ignore)]
fn validate_library_load() {
let start_time = chrono::Utc::now();
let lib = library_loader::load_library();
library_loader::load_library();
let end_time = chrono::Utc::now();
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
}

View File

@ -3,3 +3,8 @@ MoveData = 1
StringKey = 2
DynamicLibrary = 3
StaticData = 4
DynamicLibrary = 0
MoveLibrary = 1
StaticData = 2
MoveData = 3
StringKey = 4