Skip to content

Commit 0ae83b7

Browse files
author
NICK
committed
Add Variant helpers
1 parent 6ed8736 commit 0ae83b7

File tree

5 files changed

+399
-40
lines changed

5 files changed

+399
-40
lines changed

macros/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn native_object(_args: TokenStream, input: TokenStream) -> TokenStream {
9696
unsafe extern "C" fn get_prop_name<T: IComponentBase + IComponentInit>(_0: &T, num: i64, alias: i64) -> *const u16 {
9797
let _0 = &*((((_0 as *const T) as *const u8) as usize - 8) as *const T);
9898
let prop_name = _0.get_prop_name(num, alias);
99-
memory_manager().alloc_utf16_str(prop_name)
99+
memory_manager().copy_utf16_str(prop_name)
100100
}
101101
get_prop_name::<#ident>
102102
},
@@ -149,7 +149,7 @@ pub fn native_object(_args: TokenStream, input: TokenStream) -> TokenStream {
149149
unsafe extern "C" fn get_method_name<T: IComponentBase + IComponentInit>(_0: &T, num: i64, alias: i64) -> *const u16 {
150150
let _0 = &*((((_0 as *const T) as *const u8) as usize - 8) as *const T);
151151
let method_name = _0.get_method_name(num, alias);
152-
memory_manager().alloc_utf16_str(method_name)
152+
memory_manager().copy_utf16_str(method_name)
153153
}
154154
get_method_name::<#ident>
155155
},
@@ -182,7 +182,7 @@ pub fn native_object(_args: TokenStream, input: TokenStream) -> TokenStream {
182182
true => None,
183183
false => Some(std::slice::from_raw_parts_mut(params, size))
184184
};
185-
_0.call_as_proc(num, params, size)
185+
_0.call_as_proc(num, params)
186186
}
187187
call_as_proc::<#ident>
188188
},
@@ -195,7 +195,7 @@ pub fn native_object(_args: TokenStream, input: TokenStream) -> TokenStream {
195195
};
196196
let ret = &mut *ret;
197197

198-
_0.call_as_func(num, ret, params, size)
198+
_0.call_as_func(num, ret, params)
199199
}
200200
call_as_func::<#ident>
201201
}
@@ -232,7 +232,7 @@ pub fn native_object(_args: TokenStream, input: TokenStream) -> TokenStream {
232232

233233
impl IComponentInit for #ident {
234234
fn register_extension_as(&mut self, name: *mut *const u16) -> bool {
235-
unsafe { *name = memory_manager().alloc_utf16_str(#ident_str); }
235+
unsafe { *name = memory_manager().copy_utf16_str(#ident_str); }
236236
true
237237
}
238238
}

src/component.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ pub trait IComponentBase {
8282
fn get_n_params(&self, method_num: i64) -> i64;
8383
fn get_param_def_value(&self, method_num: i64, param_num: i64, var_param_def_value: &mut Variant) -> bool;
8484
fn has_ret_val(&self, method_num: i64) -> bool;
85-
fn call_as_proc(&mut self, method_num: i64, params: Option<&mut [Variant]>, size: usize) -> bool;
86-
fn call_as_func(&mut self, method_num: i64, ret_vals: &mut Variant, params: Option<&mut [Variant]>, size: usize) -> bool;
85+
fn call_as_proc(&mut self, method_num: i64, params: Option<&mut [Variant]>) -> bool;
86+
fn call_as_func(&mut self, method_num: i64, ret_vals: &mut Variant, params: Option<&mut [Variant]>) -> bool;
8787

8888
// LocaleBaseVTable
8989
fn set_locale(&mut self, loc: &str);

src/connector.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ pub struct IConnector {
2626

2727
impl IConnector {
2828
pub fn add_error(&mut self, code: u16, source: &str, descr: &str, scode: i64) -> bool {
29-
let source = memory_manager().alloc_utf16_str(source);
30-
let descr = memory_manager().alloc_utf16_str(descr);
29+
let source = memory_manager().copy_utf16_str(source);
30+
let descr = memory_manager().copy_utf16_str(descr);
3131
unsafe { (self.vtable.as_mut().add_error)(self, code, source, descr, scode) }
3232
}
3333

3434
pub fn read(&mut self, prop_name: &str, value: &mut Variant, error: &mut u64, error_description: &mut String) -> bool {
35-
let prop_name = memory_manager().alloc_utf16_str(prop_name);
35+
let prop_name = memory_manager().copy_utf16_str(prop_name);
3636
let value = value as *mut Variant;
3737
let error = error as *mut u64;
3838
let mut error_description_ptr = std::ptr::null();
@@ -46,12 +46,12 @@ impl IConnector {
4646
}
4747

4848
pub fn write(&mut self, prop_name: &str, value: &Variant) -> bool {
49-
let prop_name = memory_manager().alloc_utf16_str(prop_name);
49+
let prop_name = memory_manager().copy_utf16_str(prop_name);
5050
unsafe { (self.vtable.as_mut().write)(self, prop_name, value as *const Variant) }
5151
}
5252

5353
pub fn register_profile_as(&mut self, profile_name: &str) -> bool {
54-
let profile_name = memory_manager().alloc_utf16_str(profile_name);
54+
let profile_name = memory_manager().copy_utf16_str(profile_name);
5555
unsafe { (self.vtable.as_mut().register_profile_as)(self, profile_name) }
5656
}
5757

@@ -64,9 +64,9 @@ impl IConnector {
6464
}
6565

6666
pub fn external_event(&mut self, source: &str, message: &str, data: &str) -> bool {
67-
let source = memory_manager().alloc_utf16_str(source);
68-
let message = memory_manager().alloc_utf16_str(message);
69-
let data = memory_manager().alloc_utf16_str(data);
67+
let source = memory_manager().copy_utf16_str(source);
68+
let message = memory_manager().copy_utf16_str(message);
69+
let data = memory_manager().copy_utf16_str(data);
7070
unsafe { (self.vtable.as_mut().external_event)(self, source, message, data) }
7171
}
7272

@@ -75,7 +75,7 @@ impl IConnector {
7575
}
7676

7777
pub fn set_status_line(&mut self, message: &str) -> bool {
78-
let message = memory_manager().alloc_utf16_str(message);
78+
let message = memory_manager().copy_utf16_str(message);
7979
unsafe { (self.vtable.as_mut().set_status_line)(self, message) }
8080
}
8181

src/memory.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,33 @@ impl IMemoryManager {
2626
}
2727
}
2828

29-
pub fn alloc_utf16_str(&mut self, value: &str) -> *const u16 {
29+
pub fn copy_u8_array<T>(&mut self, value: &[T]) -> *const u8 {
30+
let mut ptr = std::ptr::null_mut();
31+
let data_ptr = value.as_ptr() as *const u8;
32+
let size = value.len() * std::mem::size_of::<T>();
33+
self.alloc_memory((&mut ptr as *mut *mut u8) as *mut *const c_void, size);
34+
unsafe {
35+
if !value.is_empty() {
36+
std::ptr::copy(data_ptr, ptr, size);
37+
}
38+
}
39+
ptr
40+
}
41+
42+
pub fn copy_utf8_str(&mut self, value: &str) -> *const u8 {
43+
let mut ptr = std::ptr::null_mut();
44+
let data_ptr = value.as_ptr();
45+
self.alloc_memory((&mut ptr as *mut *mut u8) as *mut *const c_void, value.len() + 1);
46+
unsafe {
47+
if !value.is_empty() {
48+
std::ptr::copy(data_ptr, ptr, value.len() + 1);
49+
}
50+
std::ptr::write(((ptr as usize) + value.len()) as *mut u8, 0x00 as u8);
51+
}
52+
ptr
53+
}
54+
55+
pub fn copy_utf16_str(&mut self, value: &str) -> *const u16 {
3056
let data = value.encode_utf16().collect::<Vec<u16>>();
3157
let data_ptr = data.as_ptr();
3258
let mut ptr = std::ptr::null_mut();

0 commit comments

Comments
 (0)