Skip to content

Commit

Permalink
feat: native函数表
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 6, 2024
1 parent 33014d3 commit 9b24da7
Show file tree
Hide file tree
Showing 6 changed files with 36 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.1"
version = "0.1.2"
edition = "2021"
description = "To be the prettiest and simpliest script"
license = "MPL-2.0"
2 changes: 1 addition & 1 deletion samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod D:\code\rs\tst\target\debug\tstlib.dll> m;

log(m-.牛魔())
log(m-:A::new().t());
4 changes: 1 addition & 3 deletions 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 = 100000;
static VERSION:usize = 100001;

/// 解释器发行者(用于区分主版本和魔改版)
///
Expand All @@ -52,9 +52,7 @@ fn main()-> ExitCode {
// extern to_raw_args
// throw catch
// 同名省略struct属性
// 如果不加分号报错会错行,记得提示用户
// 科学计数法0x 0b
// wasm版本实现
// linux macos支持
// 脚本打包exe

Expand Down
42 changes: 31 additions & 11 deletions src/native.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//! 提供Native Module的接口
use crate::{
c::Clib,
intern::{intern, Interned},
scan::stmt::LocalMod,
primitive::litr::Litr
c::Clib, intern::{intern, Interned}, primitive::litr::Litr, runtime::{outlive::{self, LocalFunc}, Variant}, scan::stmt::LocalMod
};
use crate::runtime::{calc::CalcRef, Scope};

pub type NativeFn = fn(Vec<CalcRef>, Scope)-> Litr;
pub type NativeMethod = fn(&mut NativeInstance, args:Vec<CalcRef>, Scope)-> Litr;

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

#[repr(C)]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct NativeClassDef {
pub name: Interned,
pub statics: Vec<(Interned, NativeFn)>,
pub methods: Vec<(Interned, NativeMethod)>,
pub name: Interned,
pub getter: fn(&NativeInstance, get:Interned)-> Litr,
pub setter: fn(&mut NativeInstance, set:Interned, to:Litr),
pub index_get: fn(&NativeInstance, CalcRef)-> Litr,
Expand All @@ -42,11 +40,35 @@ struct NativeInterface {

/// 传进premain的函数表, 保证原生模块能使用Key解释器上下文的函数
#[repr(C)]
struct PreMain {
struct FuncTable {
intern: fn(&[u8])-> Interned,
err: fn(&str)-> !,
find_var: fn(Scope, Interned)-> Option<CalcRef>,
let_var: fn(Scope, Interned, Litr),
const_var: fn(Scope, Interned),
call_local: fn(&LocalFunc, Vec<Litr>)-> Litr,
call_at: fn(Scope, *mut Litr, &LocalFunc, Vec<Litr>)-> Litr,
get_self: fn(Scope)-> *mut Litr,
outlive_inc: fn(Scope),
outlive_dec: fn(Scope)
}
static FUNCTABLE:FuncTable = FuncTable {
intern,
err:|s|panic!("{}",s),
find_var: Scope::var,
let_var: |mut cx, name, v|cx.vars.push(Variant {
locked:false, name, v
}),
const_var: |cx, name|cx.lock(name),
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,
outlive_inc: outlive::increase_scope_count,
outlive_dec: outlive::decrease_scope_count,
};

/// 原生类型实例
#[derive(Debug)]
Expand Down Expand Up @@ -77,10 +99,8 @@ pub fn parse(path:&[u8])-> *const NativeMod {
unsafe {
// 预备main, 将原生模块需要用的解释器的函数传过去
// 没有extern前缀!
let premain: fn(&PreMain) = std::mem::transmute(lib.get(b"premain").expect("模块需要'premain'函数初始化Key原生模块函数表"));
premain(&PreMain {
intern, err:|s|panic!("{}",s), find_var: Scope::var,
});
let premain: fn(&FuncTable) = std::mem::transmute(lib.get(b"premain").expect("模块需要'premain'函数初始化Key原生模块函数表"));
premain(&FUNCTABLE);

// 运行用户函数
let main: fn(&mut NativeInterface) = std::mem::transmute(lib.get(b"main").expect("模块需要'main'函数作为主运行函数"));
Expand Down
1 change: 1 addition & 0 deletions src/runtime/outlive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn ln()->usize{unsafe{crate::LINE}}

/// 本地函数指针
#[derive(Debug)]
#[repr(C)]
pub struct LocalFunc {
/// pointer
pub ptr:*const LocalFuncRaw,
Expand Down

0 comments on commit 9b24da7

Please sign in to comment.