diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 966d045f..50f3cb01 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -183,15 +183,15 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream { } pub fn gen_funcs_info(storage: &mut ModuleStorage) { - storage.class_table[Self::export_info()].functions = Self::function_export(storage); + storage.access_class_mut(Self::export_info()).functions = Self::function_export(storage); } pub fn gen_overrides_info(storage: &mut ModuleStorage) { - storage.class_table[Self::export_info()].overrides = Self::override_export(); + storage.access_class_mut(Self::export_info()).overrides = Self::override_export(); } pub fn modify_shadow_name(storage: &mut ModuleStorage, name: &'static str) { - storage.class_table[Self::export_info()].name = name; + storage.access_class_mut(Self::export_info()).name = name; } pub fn export_info() -> usize { @@ -264,12 +264,12 @@ pub fn def_module_export(_t: TokenStream) -> TokenStream { } #[no_mangle] - pub fn get_lib() -> &'static libcore::Module { + pub extern "C" fn get_lib() -> &'static libcore::Module { import_lib().0 } #[no_mangle] - pub fn get_storage() -> &'static libcore::ModuleStorage { + pub extern "C" fn get_storage() -> &'static libcore::ModuleStorage { import_lib().1 } ) diff --git a/libcore/src/dynadata.rs b/libcore/src/dynadata.rs index d02930ab..3d4dff3e 100644 --- a/libcore/src/dynadata.rs +++ b/libcore/src/dynadata.rs @@ -1,4 +1,4 @@ -use std::{any::TypeId, mem::size_of, sync::OnceLock}; +use std::{mem::size_of, sync::OnceLock}; use crate::gc::GcMgr; @@ -23,7 +23,7 @@ pub struct DynaData { // 变量已经使用的内存空间大小 var_used: usize, #[cfg(debug_assertions)] - type_used: Vec<(TypeId, &'static str)>, + type_used: Vec<&'static str>, } impl DynaData { @@ -61,8 +61,7 @@ impl DynaData { self.stack_ptr += size_of::(); #[cfg(debug_assertions)] { - self.type_used - .push((TypeId::of::(), std::any::type_name::())); + self.type_used.push(std::any::type_name::()); } } @@ -75,13 +74,13 @@ impl DynaData { let sz = size_of::(); #[cfg(debug_assertions)] { - let info = TypeId::of::(); + let info = std::any::type_name::(); let info_stack = self.type_used.pop().unwrap(); - if info_stack.0 != info { + if info_stack != info { panic!( "pop data type error.Expected get {}.Actually has {}", std::any::type_name::(), - info_stack.1 + info_stack ); } debug_assert!(self.stack_ptr >= sz); @@ -99,13 +98,13 @@ impl DynaData { let sz = size_of::(); #[cfg(debug_assertions)] { - let info = TypeId::of::(); + let info = std::any::type_name::(); let info_stack = self.type_used.last().unwrap(); - if info_stack.0 != info { + if *info_stack != info { panic!( "pop data type error.Expected get {}.Actually has {}", std::any::type_name::(), - info_stack.1 + info_stack ); } debug_assert!(self.stack_ptr >= sz); diff --git a/libcore/src/libbasic.rs b/libcore/src/libbasic.rs index 70fd2b00..5bd5a213 100644 --- a/libcore/src/libbasic.rs +++ b/libcore/src/libbasic.rs @@ -242,8 +242,8 @@ impl Display for RustClass { #[derive(Default)] #[repr(C)] pub struct ModuleStorage { - pub func_table: Vec, - pub class_table: Vec, + pub(crate) func_table: Vec, + pub(crate) class_table: Vec, } impl ModuleStorage { @@ -268,6 +268,22 @@ impl ModuleStorage { self.class_table.push(c); self.class_table.len() - 1 } + + pub fn access_func(&self, id: usize) -> RustlibFunc { + self.func_table[id] + } + + pub fn access_class(&self, id: usize) -> &RustClass { + &self.class_table[id] + } + + pub fn func_table(&self) -> &[RustlibFunc] { + &self.func_table + } + + pub fn access_class_mut(&mut self, idx: usize) -> &mut RustClass { + &mut self.class_table[idx] + } } impl RustFunction { diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index e0abb372..412ae19a 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -274,14 +274,14 @@ impl SymScope { fn fix_func(&self, io: &mut IOType, storage: &ModuleStorage, pool: &ValuePool) { for i in &mut io.argvs_type { *i = self - .get_type_id_by_token(pool.name_pool[storage.class_table[*i].name]) + .get_type_id_by_token(pool.name_pool[storage.access_class(*i).name]) .unwrap(); } match io.return_type { None => {} Some(t) => { io.return_type = Some( - self.get_type_id_by_token(pool.name_pool[storage.class_table[t].name]) + self.get_type_id_by_token(pool.name_pool[storage.access_class(t).name]) .unwrap(), ); } @@ -300,7 +300,7 @@ impl SymScope { let mut obj_vec = vec![]; for i in types { let idx = self.insert_sym_with_error(const_pool.name_pool[i.0], i.0)?; - let obj_rc = libstorage.class_table[*i.1].clone(); + let obj_rc = libstorage.access_class(*i.1).clone(); let classid = match self.alloc_type_id(idx) { Err(_) => return Err(symbol_redefined(i.0)), Ok(t) => t, diff --git a/src/tvm.rs b/src/tvm.rs index 2a217f69..cd794dac 100644 --- a/src/tvm.rs +++ b/src/tvm.rs @@ -10,7 +10,7 @@ pub struct Vm<'a> { run_context: Context, dynadata: DydataWrap, static_data: &'a libcore::codegen::StaticData, - imported_modules: Vec, + imported_modules: Vec<(String, libloading::Library)>, } #[derive(Debug, Clone)] @@ -154,7 +154,7 @@ impl<'a> Vm<'a> { if i >= self.imported_modules.len() { break; } - if *j != self.imported_modules[i] { + if *j != self.imported_modules[i].0 { should_be_reloaded = true; break; } @@ -167,14 +167,14 @@ impl<'a> Vm<'a> { .iter() .skip(self.imported_modules.len()) { - self.imported_modules.push(i.clone()); - self.import_module(i.clone())?; + let lib = self.import_module(i.clone())?; + self.imported_modules.push((i.clone(), lib)); } } else { self.imported_modules.clear(); for i in &self.static_data.dll_module_should_loaded { - self.imported_modules.push(i.clone()); - self.import_module(i.clone())?; + let lib = self.import_module(i.clone())?; + self.imported_modules.push((i.clone(), lib)); } } Ok(()) @@ -249,11 +249,12 @@ impl<'a> Vm<'a> { Opcode::SelfNegative => { operator_opcode!(self_negative, self); } - Opcode::CallNative => unsafe { - // let tmp = - // STD_FUNC_TABLE[self.static_data.inst[*pc].operand](&mut self.dynadata.dydata); - // self.throw_err_info(tmp)?; - }, + Opcode::CallNative => { + let tmp = self.dynadata.imported_func[self.static_data.inst[*pc].operand]( + &mut self.dynadata.dydata, + ); + self.convert_err_info(tmp)?; + } Opcode::AddInt => { let (first, second) = impl_opcode!(TrcIntInternal, self, 2); self.dynadata.dydata.push_data(first + second); @@ -641,6 +642,7 @@ impl<'a> Vm<'a> { Ok(()) } + /// 运行代码 pub fn run(&mut self) -> RuntimeResult<()> { self.reset()?; let mut pc = 0; @@ -658,7 +660,7 @@ impl<'a> Vm<'a> { } /// 导入一个dll模块 - fn import_module(&mut self, i: String) -> Result<(), RuntimeError> { + fn import_module(&mut self, i: String) -> Result { let lib = unsafe { match libloading::Library::new(i.clone()) { Ok(lib) => lib, @@ -667,9 +669,11 @@ impl<'a> Vm<'a> { } } }; - let (module, storage) = crate::base::dll::load_module_storage(&lib); - for i in &storage.func_table {} - Ok(()) + let (_module, storage) = crate::base::dll::load_module_storage(&lib); + for i in storage.func_table() { + self.dynadata.imported_func.push(*i) + } + Ok(lib) } } diff --git a/stdlib/Cargo.toml b/stdlib/Cargo.toml index fd5d0bf7..035bdf89 100644 --- a/stdlib/Cargo.toml +++ b/stdlib/Cargo.toml @@ -12,4 +12,4 @@ rust-i18n = "3" [lib] name = "stdlib" -crate-type = ["dylib"] +crate-type = ["cdylib"] diff --git a/stdlib/src/prelude.rs b/stdlib/src/prelude.rs index a0b2621a..966f2076 100644 --- a/stdlib/src/prelude.rs +++ b/stdlib/src/prelude.rs @@ -4,6 +4,7 @@ use rust_i18n::t; use std::io::{self, Write}; #[trc_function(var_params = true)] +#[no_mangle] pub fn print(fmt_string: str) -> void { let mut iter = va_list.iter(); let mut output_iter = unsafe { (*fmt_string).chars() }; @@ -26,6 +27,7 @@ pub fn print(fmt_string: str) -> void { } #[trc_function(var_params = true)] +#[no_mangle] pub fn println(fmt_string: str) -> void { let mut iter = va_list.iter(); let mut output_iter = unsafe { (*fmt_string).chars() };