11use super :: stack:: { CallFrame , ValueStack } ;
22use crate :: store:: Globals ;
33use crate :: { Result , interpreter:: simd:: Value128 } ;
4- use tinywasm_types:: { AnyRef , ExnRef , ExternRef , FuncRef , GlobalAddr , LocalAddr , RefValue , WasmType , WasmValue } ;
4+ use tinywasm_types:: { GlobalAddr , LocalAddr , RefValue , WasmValue } ;
55
66pub ( crate ) type Value32 = u32 ;
77pub ( crate ) type Value64 = u64 ;
88
99#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
10+ /// Packed internal representation of a WebAssembly reference.
11+ ///
12+ /// Unlike the public [`RefValue`], this stores no explicit reference category.
13+ /// Converting it back therefore requires the value's canonical reference type.
1014pub ( crate ) struct ValueRef ( u32 ) ;
1115
1216impl Default for ValueRef {
@@ -71,6 +75,18 @@ impl ValueRef {
7175 }
7276}
7377
78+ impl From < RefValue > for ValueRef {
79+ fn from ( value : RefValue ) -> Self {
80+ match value {
81+ RefValue :: Null => Self :: NULL ,
82+ RefValue :: Func ( value) => Self :: from_category_addr ( value. addr ( ) ) ,
83+ RefValue :: Extern ( value) => Self :: from_raw ( value. raw ( ) ) ,
84+ RefValue :: Exn ( value) => Self :: from_category_addr ( value. addr ( ) ) ,
85+ RefValue :: Any ( value) => Self :: from_raw ( value. raw ( ) ) ,
86+ }
87+ }
88+ }
89+
7490#[ allow( private_interfaces) ]
7591#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
7692/// A untyped WebAssembly value
@@ -118,32 +134,6 @@ impl TinyWasmValue {
118134 _ => None ,
119135 }
120136 }
121-
122- /// Converts this internal value to the requested public WebAssembly type.
123- pub fn attach_type ( self , ty : WasmType ) -> Option < WasmValue > {
124- match ( self , ty) {
125- ( Self :: Value32 ( v) , WasmType :: I32 ) => Some ( WasmValue :: I32 ( v as i32 ) ) ,
126- ( Self :: Value64 ( v) , WasmType :: I64 ) => Some ( WasmValue :: I64 ( v as i64 ) ) ,
127- ( Self :: Value32 ( v) , WasmType :: F32 ) => Some ( WasmValue :: F32 ( f32:: from_bits ( v) ) ) ,
128- ( Self :: Value64 ( v) , WasmType :: F64 ) => Some ( WasmValue :: F64 ( f64:: from_bits ( v) ) ) ,
129- ( Self :: ValueRef ( v) , WasmType :: Ref ( _) ) if v. is_null ( ) => Some ( RefValue :: Null . into ( ) ) ,
130- ( Self :: ValueRef ( v) , WasmType :: Ref ( ty) ) if ty. is_func ( ) => {
131- Some ( WasmValue :: Ref ( RefValue :: Func ( FuncRef :: new ( v. addr ( ) ?) ) ) )
132- }
133- ( Self :: ValueRef ( v) , WasmType :: Ref ( ty) ) if ty. is_extern ( ) => {
134- Some ( WasmValue :: Ref ( RefValue :: Extern ( ExternRef :: from_raw ( v. raw ( ) ) ) ) )
135- }
136- ( Self :: ValueRef ( v) , WasmType :: Ref ( ty) ) if ty. is_exn ( ) => {
137- Some ( WasmValue :: Ref ( RefValue :: Exn ( ExnRef :: new ( v. addr ( ) ?) ) ) )
138- }
139- ( Self :: ValueRef ( v) , WasmType :: Ref ( _) ) => Some ( WasmValue :: Ref ( RefValue :: Any ( AnyRef :: from_raw ( v. raw ( ) ) ) ) ) ,
140- ( Self :: Value128 ( v) , WasmType :: V128 ) => Some ( WasmValue :: V128 ( v. 0 ) ) ,
141- ( _, WasmType :: I32 | WasmType :: F32 ) => None ,
142- ( _, WasmType :: I64 | WasmType :: F64 ) => None ,
143- ( _, WasmType :: Ref ( _) ) => None ,
144- ( _, WasmType :: V128 ) => None ,
145- }
146- }
147137}
148138
149139impl From < & WasmValue > for TinyWasmValue {
@@ -153,11 +143,7 @@ impl From<&WasmValue> for TinyWasmValue {
153143 WasmValue :: I64 ( v) => Self :: Value64 ( * v as u64 ) ,
154144 WasmValue :: F32 ( v) => Self :: Value32 ( v. to_bits ( ) ) ,
155145 WasmValue :: F64 ( v) => Self :: Value64 ( v. to_bits ( ) ) ,
156- WasmValue :: Ref ( RefValue :: Null ) => Self :: ValueRef ( ValueRef :: NULL ) ,
157- WasmValue :: Ref ( RefValue :: Func ( value) ) => Self :: ValueRef ( ValueRef :: from_category_addr ( value. addr ( ) ) ) ,
158- WasmValue :: Ref ( RefValue :: Extern ( value) ) => Self :: ValueRef ( ValueRef :: from_raw ( value. raw ( ) ) ) ,
159- WasmValue :: Ref ( RefValue :: Exn ( value) ) => Self :: ValueRef ( ValueRef :: from_category_addr ( value. addr ( ) ) ) ,
160- WasmValue :: Ref ( RefValue :: Any ( value) ) => Self :: ValueRef ( ValueRef :: from_raw ( value. raw ( ) ) ) ,
146+ WasmValue :: Ref ( value) => Self :: ValueRef ( ( * value) . into ( ) ) ,
161147 WasmValue :: V128 ( v) => Self :: Value128 ( ( * v) . into ( ) ) ,
162148 }
163149 }
@@ -190,9 +176,7 @@ pub(crate) trait InternalValue: sealed::Sealed + Copy + Default {
190176 fn local_set ( stack : & mut ValueStack , frame : & CallFrame , index : LocalAddr , value : Self ) ;
191177 fn local_update ( stack : & mut ValueStack , frame : & CallFrame , index : LocalAddr , f : impl FnOnce ( Self ) -> Self ) ;
192178 fn local_copy ( stack : & mut ValueStack , frame : & CallFrame , from : LocalAddr , to : LocalAddr ) ;
193- /// Gets a value from its physical global lane.
194179 fn global_get ( globals : & Globals , addr : GlobalAddr ) -> Self ;
195- /// Sets a value in its physical global lane.
196180 fn global_set ( globals : & mut Globals , addr : GlobalAddr , value : Self ) ;
197181}
198182
0 commit comments