@@ -6,6 +6,7 @@ use crate::{
66 errors:: { Throwable , TypeError } ,
77 functions:: ZendFunction ,
88 objects:: Object ,
9+ strings:: ZendString ,
910 sys:: * ,
1011 types:: { get_type_by_const, Type } ,
1112 utils:: ensure_end_with_zero,
@@ -162,10 +163,8 @@ impl Val {
162163 pub fn as_string ( & self ) -> crate :: Result < String > {
163164 if self . get_type ( ) . is_string ( ) {
164165 unsafe {
165- let s = self . inner . value . str ;
166- let buf = from_raw_parts ( & ( * s) . val as * const c_char as * const u8 , ( * s) . len ) ;
167- let string = str:: from_utf8 ( buf) ?. to_string ( ) ;
168- Ok ( string)
166+ let zs = ZendString :: from_ptr ( self . inner . value . str ) ;
167+ Ok ( zs. as_string ( ) ?)
169168 }
170169 } else {
171170 Err ( self . must_be_type_error ( "string" ) . into ( ) )
@@ -175,10 +174,8 @@ impl Val {
175174 pub fn as_string_value ( & self ) -> Result < String , Utf8Error > {
176175 unsafe {
177176 let s = phper_zval_get_string ( & self . inner as * const _ as * mut _ ) ;
178- let buf = from_raw_parts ( & ( * s) . val as * const c_char as * const u8 , ( * s) . len ) ;
179- let result = str:: from_utf8 ( buf) . map ( ToString :: to_string) ;
180- phper_zend_string_release ( s) ;
181- result
177+ let zs = EBox :: from_raw ( s as * mut ZendString ) ;
178+ zs. as_string ( )
182179 }
183180 }
184181
@@ -218,12 +215,8 @@ impl Val {
218215 Err ( e) => e. into ( ) ,
219216 }
220217 }
221- }
222-
223- impl EAllocatable for Val { }
224218
225- impl Drop for Val {
226- fn drop ( & mut self ) {
219+ fn drop_me ( & mut self ) {
227220 let t = self . get_type ( ) ;
228221 unsafe {
229222 if t. is_string ( ) {
@@ -237,6 +230,21 @@ impl Drop for Val {
237230 }
238231}
239232
233+ impl EAllocatable for Val {
234+ fn free ( ptr : * mut Self ) {
235+ unsafe {
236+ ptr. as_mut ( ) . unwrap ( ) . drop_me ( ) ;
237+ _efree ( ptr. cast ( ) ) ;
238+ }
239+ }
240+ }
241+
242+ impl Drop for Val {
243+ fn drop ( & mut self ) {
244+ self . drop_me ( ) ;
245+ }
246+ }
247+
240248/// The trait for setting the value of [Val], mainly as the return value of
241249/// [crate::functions::Function] and [crate::functions::Method], and initializer of [Val].
242250pub trait SetVal {
0 commit comments