diff --git a/lib/api/src/externals.rs b/lib/api/src/externals.rs index 3ffa3eb15..629825876 100644 --- a/lib/api/src/externals.rs +++ b/lib/api/src/externals.rs @@ -113,15 +113,10 @@ impl Global { Self::from_type(store, GlobalType::new(val.ty(), Mutability::Var), val).unwrap() } - pub fn from_type(store: &Store, ty: GlobalType, val: Val) -> Result { + fn from_type(store: &Store, ty: GlobalType, val: Val) -> Result { if !val.comes_from_same_store(store) { return Err(RuntimeError::new("cross-`Store` globals are not supported")); } - if val.ty() != ty.ty.clone() { - return Err(RuntimeError::new( - "value provided does not match the type of this global", - )); - } let mut definition = VMGlobalDefinition::new(); unsafe { match val { diff --git a/lib/compiler/src/translator/environ.rs b/lib/compiler/src/translator/environ.rs index a281e0043..8f94d4063 100644 --- a/lib/compiler/src/translator/environ.rs +++ b/lib/compiler/src/translator/environ.rs @@ -10,8 +10,8 @@ use wasm_common::entity::PrimaryMap; use wasm_common::FuncType; use wasm_common::{ DataIndex, DataInitializer, DataInitializerLocation, ElemIndex, ExportIndex, FuncIndex, - GlobalIndex, GlobalType, ImportIndex, LocalFuncIndex, MemoryIndex, MemoryType, SignatureIndex, - TableIndex, TableType, + GlobalIndex, GlobalInit, GlobalType, ImportIndex, LocalFuncIndex, MemoryIndex, MemoryType, + SignatureIndex, TableIndex, TableType, }; use wasmer_runtime::{Module, TableElements}; @@ -263,8 +263,13 @@ impl<'data> ModuleEnvironment<'data> { Ok(()) } - pub(crate) fn declare_global(&mut self, global: GlobalType) -> WasmResult<()> { + pub(crate) fn declare_global( + &mut self, + global: GlobalType, + initializer: GlobalInit, + ) -> WasmResult<()> { self.result.module.globals.push(global); + self.result.module.global_initializers.push(initializer); Ok(()) } diff --git a/lib/compiler/src/translator/sections.rs b/lib/compiler/src/translator/sections.rs index 98a31b364..d3d9776d3 100644 --- a/lib/compiler/src/translator/sections.rs +++ b/lib/compiler/src/translator/sections.rs @@ -130,7 +130,6 @@ pub fn parse_import_section<'data>( GlobalType { ty: wptype_to_type(ty.content_type).unwrap(), mutability: ty.mutable.into(), - initializer: GlobalInit::Import, }, module_name, field_name, @@ -254,9 +253,8 @@ pub fn parse_global_section( let global = GlobalType { ty: wptype_to_type(content_type).unwrap(), mutability: mutable.into(), - initializer, }; - environ.declare_global(global)?; + environ.declare_global(global, initializer)?; } Ok(()) diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index 91a172ae4..77ff2e391 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -1229,22 +1229,20 @@ fn initialize_memories( fn initialize_globals(instance: &Instance) { let module = Arc::clone(&instance.module); - let num_imports = module.num_imported_globals; - for (index, global) in module.globals.iter().skip(num_imports) { - let def_index = module.local_global_index(index).unwrap(); + for (index, initializer) in module.global_initializers.iter() { unsafe { - let to = instance.global_ptr(def_index); - match global.initializer { - GlobalInit::I32Const(x) => *(*to).as_i32_mut() = x, - GlobalInit::I64Const(x) => *(*to).as_i64_mut() = x, - GlobalInit::F32Const(x) => *(*to).as_f32_mut() = x, - GlobalInit::F64Const(x) => *(*to).as_f64_mut() = x, + let to = instance.global_ptr(index); + match initializer { + GlobalInit::I32Const(x) => *(*to).as_i32_mut() = *x, + GlobalInit::I64Const(x) => *(*to).as_i64_mut() = *x, + GlobalInit::F32Const(x) => *(*to).as_f32_mut() = *x, + GlobalInit::F64Const(x) => *(*to).as_f64_mut() = *x, GlobalInit::V128Const(x) => *(*to).as_u128_bits_mut() = *x.bytes(), GlobalInit::GetGlobal(x) => { - let from = if let Some(def_x) = module.local_global_index(x) { + let from = if let Some(def_x) = module.local_global_index(*x) { instance.global(def_x) } else { - *instance.imported_global(x).definition + *instance.imported_global(*x).definition }; *to = from; } diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 7e00a5ea0..bf93da633 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -10,8 +10,9 @@ use std::sync::Arc; use wasm_common::entity::{EntityRef, PrimaryMap}; use wasm_common::{ DataIndex, ElemIndex, ExportIndex, ExportType, ExternType, FuncIndex, FuncType, GlobalIndex, - GlobalType, ImportIndex, ImportType, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, - LocalTableIndex, MemoryIndex, MemoryType, Pages, SignatureIndex, TableIndex, TableType, + GlobalInit, GlobalType, ImportIndex, ImportType, LocalFuncIndex, LocalGlobalIndex, + LocalMemoryIndex, LocalTableIndex, MemoryIndex, MemoryType, Pages, SignatureIndex, TableIndex, + TableType, }; /// A WebAssembly table initializer. @@ -124,6 +125,9 @@ pub struct Module { /// WebAssembly passive data segments. pub passive_data: HashMap>, + /// WebAssembly global initializers. + pub global_initializers: PrimaryMap, + /// WebAssembly function names. pub func_names: HashMap, @@ -167,6 +171,7 @@ impl Module { table_elements: Vec::new(), passive_elements: HashMap::new(), passive_data: HashMap::new(), + global_initializers: PrimaryMap::new(), func_names: HashMap::new(), signatures: PrimaryMap::new(), functions: PrimaryMap::new(), diff --git a/lib/wasm-common/src/types.rs b/lib/wasm-common/src/types.rs index 69ffc9df9..b64b72060 100644 --- a/lib/wasm-common/src/types.rs +++ b/lib/wasm-common/src/types.rs @@ -110,20 +110,13 @@ pub enum ExternType { } fn is_global_compatible(exported: &GlobalType, imported: &GlobalType) -> bool { - match imported.initializer { - GlobalInit::Import => (), - _ => panic!("imported Global should have an Imported initializer"), - } - let GlobalType { ty: exported_ty, mutability: exported_mutability, - initializer: _exported_initializer, } = exported; let GlobalType { ty: imported_ty, mutability: imported_mutability, - initializer: _imported_initializer, } = imported; exported_ty == imported_ty && imported_mutability == exported_mutability } @@ -318,8 +311,6 @@ pub struct GlobalType { pub ty: Type, /// A flag indicating whether the value may change at runtime. pub mutability: Mutability, - /// The source of the initial value. - pub initializer: GlobalInit, } // Global Types @@ -344,7 +335,6 @@ impl GlobalType { Self { ty: ty, mutability: mutability, - initializer: GlobalInit::Import, } } }