Skip to content

Commit

Permalink
Native Class接口实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Feb 18, 2024
1 parent 14ac7db commit c2803e8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
3 changes: 2 additions & 1 deletion samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

mod D:\code\rs\key-native\target\debug\key_native.dll> m

let a = m-.test();
let a = m-:Okk::new(555);
log(a)
47 changes: 28 additions & 19 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,59 @@ use crate::{
};

pub type NativeFn = fn(Vec<Litr>)-> Litr;
pub type Getter = fn(get:&[u8]);
pub type Setter = fn(set:&[u8], to:Litr);
pub type IndexGetter = fn(get:usize)-> Litr;
pub type IndexSetter = fn(set:usize, to:Litr)-> Litr;
pub type NativeMethod = fn(kself:&mut Litr, Vec<Litr>)-> Litr;
pub type Getter = fn(get:Interned)-> Litr;
pub type Setter = fn(set:Interned, to:Litr);
pub type IndexGetter = fn(get:usize)-> Litr;
pub type IndexSetter = fn(set:usize, to:Litr);

#[derive(Debug, Clone)]
pub struct NativeMod {
pub name: Interned,
pub funcs: Vec<(Interned, NativeFn)>,
pub classes: Vec<NativeClassDef>
pub classes: Vec<*const NativeClassDef>
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct NativeClassDef {
pub name: Interned,
pub getters: Vec<(Interned, Getter)>,
pub setters: Vec<(Interned, Setter)>,
pub igetters: Vec<(Interned, IndexGetter)>,
pub isetters: Vec<(Interned, IndexSetter)>,
pub getter: Getter,
pub setter: Setter,
pub igetter: IndexGetter,
pub isetter: IndexSetter,
pub statics: Vec<(Interned, NativeFn)>,
pub methods: Vec<(Interned, NativeMethod)>
}

pub struct NativeApis<'a> {
export_fn: &'a mut dyn FnMut(&'a [u8], NativeFn),
export_cls: &'a mut dyn FnMut(NativeClassDef),
/// 传进main里的东西,作为与原生的接口
#[repr(C)]
struct NativeInterface {
intern: fn(&[u8])-> Interned,
err: fn(&str)->!,
funcs: *mut Vec<(Interned, NativeFn)>,
classes: *mut Vec<*const NativeClassDef>
}

#[repr(transparent)]
/// 原生类型实例
#[derive(Debug, Clone)]
#[repr(C)]
pub struct NativeInstance {
pub p: usize
pub cls: *mut NativeClassDef,
pub v: [usize;2],
}

pub fn parse(name:Interned,path:&[u8])-> Result<NativeMod, String> {
let lib = Clib::load(path)?;
let mut funcs = Vec::new();
let export_fn = &mut |name, f|funcs.push((intern(name), f));
let mut classes = Vec::new();
let export_cls = &mut |c|classes.push(c);
fn err(s:&str)->! {
panic!("{} \n 运行时({})", s, unsafe{crate::runtime::LINE})
}
unsafe {
let keymain:extern fn(&mut NativeApis) = std::mem::transmute(lib.get(b"keymain").ok_or("模块需要'KeyMain'作为主运行函数")?);
keymain(&mut NativeApis {
export_fn, export_cls
let keymain:extern fn(&mut NativeInterface) = std::mem::transmute(lib.get(b"keymain").ok_or("模块需要'KeyMain'作为主运行函数")?);
keymain(&mut NativeInterface {
intern, err, funcs: &mut funcs, classes: &mut classes
});
}
Ok(NativeMod{
Expand Down
1 change: 1 addition & 0 deletions src/runtime/externer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn translate(arg:Litr)-> Result<usize,String> {
List(_)=> Err("列表类型不可作为C指针传递".to_string()),
Obj=> Err("Ks对象不可作为C指针传递".to_string()),
Inst(_)=> Err("Ks实例不可作为C指针传递".to_string()),
Ninst(_)=> Err("Ks原生实例不可作为C指针传递".to_string())
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod externer;
/// 运行期追踪行号
///
/// 只有主线程会访问,不存在多线程同步问题
static mut LINE:usize = 0;
pub static mut LINE:usize = 0;
#[macro_use] macro_rules! err {($($a:expr$(,)?)*) => {
panic!("{} 运行时({})", format_args!($($a,)*), unsafe{LINE})
}}
Expand Down Expand Up @@ -226,11 +226,12 @@ impl Scope {
Module::Native(p)=> {
let m = unsafe {&*p};
for cls in m.classes.iter() {
if cls.name == s {
return Class::Native(cls);
let name = unsafe {&**cls}.name;
if name == s {
return Class::Native(*cls);
}
}
err!("模块'{}'中没有'{}'类型",modname.str(), s.str())
err!("原生模块'{}'中没有'{}'类型",modname.str(), s.str())
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/scan/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use super::{
expr::*
};

use crate::runtime::{Module, Scope};
use crate::{native::NativeInstance, runtime::{Module, Scope}};
use crate::intern::Interned;

use std::collections::HashMap;


/// 变量或字面量
#[repr(C)]
#[derive(Debug, Clone)]
pub enum Litr {
Uninit,
Expand All @@ -25,7 +25,8 @@ pub enum Litr {
Buffer (Box<Vec<u8>>),
List (Box<Vec<Litr>>),
Obj,
Inst (Box<Instance>)
Inst (Box<Instance>),
Ninst (Box<NativeInstance>)
}
impl Litr {
/// 由Key编译器提供的转字符
Expand Down Expand Up @@ -87,6 +88,12 @@ impl Litr {
str.push_str(" }");
str
}
Ninst(inst)=> {
let mut s = String::new();
s.push_str(&unsafe{&*inst.cls}.name.str());
s.push_str(" { Native }");
s
}
}
}
}
Expand Down

0 comments on commit c2803e8

Please sign in to comment.