From 9b24da7bbb3e7ce0c3832af4b7a2a27e46554165 Mon Sep 17 00:00:00 2001 From: subkey <2822448396@qq.com> Date: Sun, 7 Apr 2024 04:58:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20native=E5=87=BD=E6=95=B0=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- samples/helloworld.ks | 2 +- src/main.rs | 4 +--- src/native.rs | 42 +++++++++++++++++++++++++++++++----------- src/runtime/outlive.rs | 1 + 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6d13bc..6600fd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "key-lang" -version = "0.1.1" +version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index 39449ba..fce70ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/samples/helloworld.ks b/samples/helloworld.ks index f6a7cb2..c79815e 100644 --- a/samples/helloworld.ks +++ b/samples/helloworld.ks @@ -1,3 +1,3 @@ mod D:\code\rs\tst\target\debug\tstlib.dll> m; -log(m-.牛魔()) +log(m-:A::new().t()); diff --git a/src/main.rs b/src/main.rs index 1dc2be2..363e9f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ static mut LINE:usize = 1; static mut PLACE:String = String::new(); /// 标志解释器的版本 -static VERSION:usize = 100000; +static VERSION:usize = 100001; /// 解释器发行者(用于区分主版本和魔改版) /// @@ -52,9 +52,7 @@ fn main()-> ExitCode { // extern to_raw_args // throw catch // 同名省略struct属性 - // 如果不加分号报错会错行,记得提示用户 // 科学计数法0x 0b - // wasm版本实现 // linux macos支持 // 脚本打包exe diff --git a/src/native.rs b/src/native.rs index 4b02f30..bffec06 100644 --- a/src/native.rs +++ b/src/native.rs @@ -1,10 +1,7 @@ //! 提供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}; @@ -12,17 +9,18 @@ pub type NativeFn = fn(Vec, Scope)-> Litr; pub type NativeMethod = fn(&mut NativeInstance, args:Vec, 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, @@ -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, + let_var: fn(Scope, Interned, Litr), + const_var: fn(Scope, Interned), + call_local: fn(&LocalFunc, Vec)-> Litr, + call_at: fn(Scope, *mut Litr, &LocalFunc, Vec)-> 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)] @@ -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'函数作为主运行函数")); diff --git a/src/runtime/outlive.rs b/src/runtime/outlive.rs index b3eeeed..765dea1 100644 --- a/src/runtime/outlive.rs +++ b/src/runtime/outlive.rs @@ -14,6 +14,7 @@ fn ln()->usize{unsafe{crate::LINE}} /// 本地函数指针 #[derive(Debug)] +#[repr(C)] pub struct LocalFunc { /// pointer pub ptr:*const LocalFuncRaw,