Skip to content

Commit

Permalink
feat: native using和get_parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 8, 2024
1 parent 9b24da7 commit 4a04238
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "key-lang"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
description = "To be the prettiest and simpliest script"
license = "MPL-2.0"
4 changes: 3 additions & 1 deletion samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod D:\code\rs\tst\target\debug\tstlib.dll> m;

log(m-:A::new().t());
// 直接调用模块上的公共函数
let a = m-.new();
a.test(); // 打印:
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static mut LINE:usize = 1;
static mut PLACE:String = String::new();

/// 标志解释器的版本
static VERSION:usize = 100001;
static VERSION:usize = 100003;

/// 解释器发行者(用于区分主版本和魔改版)
///
Expand Down
14 changes: 9 additions & 5 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub type NativeMethod = fn(&mut NativeInstance, args:Vec<CalcRef>, Scope)-> Litr
#[repr(C)]
pub struct NativeMod {
pub funcs: Vec<(Interned, NativeFn)>,
pub classes: Vec<*mut NativeClassDef>
pub classes: Vec<*const NativeClassDef>
}

#[derive(Debug, Clone)]
Expand All @@ -35,7 +35,7 @@ pub struct NativeClassDef {
#[repr(C)]
struct NativeInterface {
funcs: *mut Vec<(Interned, NativeFn)>,
classes: *mut Vec<*mut NativeClassDef>
classes: *mut Vec<*const NativeClassDef>
}

/// 传进premain的函数表, 保证原生模块能使用Key解释器上下文的函数
Expand All @@ -46,9 +46,11 @@ struct FuncTable {
find_var: fn(Scope, Interned)-> Option<CalcRef>,
let_var: fn(Scope, Interned, Litr),
const_var: fn(Scope, Interned),
using: fn(Scope, Interned, *const NativeClassDef),
call_local: fn(&LocalFunc, Vec<Litr>)-> Litr,
call_at: fn(Scope, *mut Litr, &LocalFunc, Vec<Litr>)-> Litr,
get_self: fn(Scope)-> *mut Litr,
get_parent: fn(Scope)-> Option<Scope>,
outlive_inc: fn(Scope),
outlive_dec: fn(Scope)
}
Expand All @@ -60,12 +62,14 @@ static FUNCTABLE:FuncTable = FuncTable {
locked:false, name, v
}),
const_var: |cx, name|cx.lock(name),
using: |mut cx, name, cls| cx.class_uses.push((name, crate::runtime::Class::Native(cls))),
call_local: |f, args| f.scope.call_local(f, args),
call_at: |cx, kself, f, args|{
let f = LocalFunc::new(f.ptr, cx);
Scope::call_local_with_self(&f, args, kself)
},
get_self: |cx|cx.kself,
get_parent: |cx|cx.parent,
outlive_inc: outlive::increase_scope_count,
outlive_dec: outlive::decrease_scope_count,
};
Expand All @@ -76,7 +80,7 @@ static FUNCTABLE:FuncTable = FuncTable {
pub struct NativeInstance {
pub v: usize,
pub w: usize,
pub cls: *mut NativeClassDef,
pub cls: *const NativeClassDef,
}
impl Clone for NativeInstance {
/// 调用自定义clone (key-native库中的默认clone行为也可用)
Expand All @@ -99,11 +103,11 @@ pub fn parse(path:&[u8])-> *const NativeMod {
unsafe {
// 预备main, 将原生模块需要用的解释器的函数传过去
// 没有extern前缀!
let premain: fn(&FuncTable) = std::mem::transmute(lib.get(b"premain").expect("模块需要'premain'函数初始化Key原生模块函数表"));
let premain: fn(&FuncTable) = std::mem::transmute(lib.get(b"premain").expect("请为你的项目添加'key_native'库. 入门教程请参见: https://docs.subkey.top/native/1.start"));
premain(&FUNCTABLE);

// 运行用户函数
let main: fn(&mut NativeInterface) = std::mem::transmute(lib.get(b"main").expect("模块需要'main'函数作为主运行函数"));
let main: fn(&mut NativeInterface) = std::mem::transmute(lib.get(b"main").expect("需要为main函数添加符号链接'#[no_mangle]'. 入门教程请参见: https://docs.subkey.top/native/1.start"));
main(&mut NativeInterface {
funcs: &mut m.funcs, classes: &mut m.classes
});
Expand Down
2 changes: 1 addition & 1 deletion src/primitive/litr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub struct ExternFunc {
/// 类实例
#[derive(Debug)]
pub struct Instance {
pub cls: *mut ClassDef,
pub cls: *const ClassDef,
pub v: Box<[Litr]>
}

Expand Down
8 changes: 4 additions & 4 deletions src/runtime/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Scope {
_=> panic!("构建实例::左侧必须是类型名")
};
if let Class::Local(cls) = cls {
let cls = unsafe {&mut *cls};
let cls = unsafe {&*cls};
let mut v = vec![Litr::Uninit;cls.props.len()];
/// 记录哪个属性没有写入
let mut writen = vec![false; cls.props.len()];
Expand Down Expand Up @@ -444,7 +444,7 @@ fn expr_set(mut this: Scope, left: &Expr, right: Litr) {
match left {
Litr::Inst(inst)=> {
let fname = intern(b"@index_set");
let cls = unsafe{&mut *inst.cls};
let cls = unsafe{&*inst.cls};
let opt = cls.methods.iter().find(|v|v.name == fname);
match opt {
Some(f)=> {
Expand Down Expand Up @@ -540,7 +540,7 @@ fn expr_set_diff(mut this: Scope, left: &Expr, f:impl Fn(&Litr)-> Litr) {
let i = this.calc_ref(i);
match left {
Litr::Inst(inst)=> {
let cls = unsafe{&mut *inst.cls};
let cls = unsafe{&*inst.cls};
let write = {
let fname = intern(b"@index_get");
let opt = cls.methods.iter().find(|v|v.name == fname);
Expand Down Expand Up @@ -623,7 +623,7 @@ fn get_index(mut left:CalcRef, i:CalcRef)-> CalcRef {
let left = &mut *left;
if let Litr::Inst(inst) = left {
let fname = intern(b"@index_get");
let cls = unsafe{&mut *inst.cls};
let cls = unsafe{&*inst.cls};
let opt = cls.methods.iter().find(|v|v.name == fname);
if let Some(f) = opt {
let f = LocalFunc::new(&f.f, cls.cx);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub enum Module {
/// 类声明,分为本地和原生类声明
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Class {
Native(*mut NativeClassDef),
Local(*mut ClassDef)
Native(*const NativeClassDef),
Local(*const ClassDef)
}

#[derive(Debug, Clone)]
Expand Down
6 changes: 5 additions & 1 deletion src/scan/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,12 @@ impl Scanner<'_> {
assert!(dot!=0, "未知模块类型");
let suffix = &self.src[dot..i];
match suffix {
b".ksm"|b".dll"=> {
b".ksm"|b".dll"=> unsafe{
/// 让mod过程出错时知道是原生模块的锅
let mut place = std::mem::take(&mut crate::PLACE);
crate::PLACE = format!("'{}'中:\n 解析原生模块'{}'时出现错误", place, path);
let module = crate::native::parse(path.as_bytes());
crate::PLACE = std::mem::take(&mut place);
Stmt::NativeMod(name, module)
}
b".ks"=> {
Expand Down

0 comments on commit 4a04238

Please sign in to comment.