Skip to content

Commit

Permalink
fix(tvm):fix stdlib call
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 10, 2024
1 parent 1386fbc commit 086e66e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 36 deletions.
10 changes: 5 additions & 5 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
)
Expand Down
19 changes: 9 additions & 10 deletions libcore/src/dynadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::TypeId, mem::size_of, sync::OnceLock};
use std::{mem::size_of, sync::OnceLock};

use crate::gc::GcMgr;

Expand All @@ -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 {
Expand Down Expand Up @@ -61,8 +61,7 @@ impl DynaData {
self.stack_ptr += size_of::<T>();
#[cfg(debug_assertions)]
{
self.type_used
.push((TypeId::of::<T>(), std::any::type_name::<T>()));
self.type_used.push(std::any::type_name::<T>());
}
}

Expand All @@ -75,13 +74,13 @@ impl DynaData {
let sz = size_of::<T>();
#[cfg(debug_assertions)]
{
let info = TypeId::of::<T>();
let info = std::any::type_name::<T>();
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::<T>(),
info_stack.1
info_stack
);
}
debug_assert!(self.stack_ptr >= sz);
Expand All @@ -99,13 +98,13 @@ impl DynaData {
let sz = size_of::<T>();
#[cfg(debug_assertions)]
{
let info = TypeId::of::<T>();
let info = std::any::type_name::<T>();
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::<T>(),
info_stack.1
info_stack
);
}
debug_assert!(self.stack_ptr >= sz);
Expand Down
20 changes: 18 additions & 2 deletions libcore/src/libbasic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ impl Display for RustClass {
#[derive(Default)]
#[repr(C)]
pub struct ModuleStorage {
pub func_table: Vec<RustlibFunc>,
pub class_table: Vec<RustClass>,
pub(crate) func_table: Vec<RustlibFunc>,
pub(crate) class_table: Vec<RustClass>,
}

impl ModuleStorage {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
Expand All @@ -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,
Expand Down
34 changes: 19 additions & 15 deletions src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Vm<'a> {
run_context: Context,
dynadata: DydataWrap,
static_data: &'a libcore::codegen::StaticData,
imported_modules: Vec<String>,
imported_modules: Vec<(String, libloading::Library)>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -641,6 +642,7 @@ impl<'a> Vm<'a> {
Ok(())
}

/// 运行代码
pub fn run(&mut self) -> RuntimeResult<()> {
self.reset()?;
let mut pc = 0;
Expand All @@ -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<libloading::Library, RuntimeError> {
let lib = unsafe {
match libloading::Library::new(i.clone()) {
Ok(lib) => lib,
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ rust-i18n = "3"

[lib]
name = "stdlib"
crate-type = ["dylib"]
crate-type = ["cdylib"]
2 changes: 2 additions & 0 deletions stdlib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand All @@ -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() };
Expand Down

0 comments on commit 086e66e

Please sign in to comment.