Skip to content

Commit

Permalink
feat: 模块外禁止属性不全的class构造
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 1, 2024
1 parent e19e46e commit 32ebd4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 1 addition & 3 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

mod samples/testmod.ks> m;

class Priv{}
let a = m-:A::ok();
a.a = Priv::{};
let a = m-:A::{b:9};
24 changes: 11 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ static VERSION:usize = 100000;
static DISTRIBUTION:&str = "Subkey";

fn main()-> ExitCode {
// 参数类型检查
// 模块外newInst如果属性不全不让构造
// let [] = x
// let a=0,b=0
// prelude mod 让模块本身帮你初始化上下文
Expand Down Expand Up @@ -80,17 +78,17 @@ fn main()-> ExitCode {

// 自定义报错
unsafe {PLACE = path.clone()}
// std::panic::set_hook(Box::new(|inf| {
// use crate::utils::date;
// let line = unsafe{LINE};
// let place = unsafe{&*PLACE};
// let s = if let Some(mes) = inf.payload().downcast_ref::<&'static str>() {
// mes
// }else if let Some(mes) = inf.payload().downcast_ref::<String>() {
// mes
// }else{"错误"};
// println!("\n> {}\n {}:第{}行\n\n> Key Script CopyLeft by Subkey\n {}\n", s, place, line, date());
// }));
std::panic::set_hook(Box::new(|inf| {
use crate::utils::date;
let line = unsafe{LINE};
let place = unsafe{&*PLACE};
let s = if let Some(mes) = inf.payload().downcast_ref::<&'static str>() {
mes
}else if let Some(mes) = inf.payload().downcast_ref::<String>() {
mes
}else{"错误"};
println!("\n> {}\n {}:第{}行\n\n> Key Script CopyLeft by Subkey\n {}\n", s, place, line, date());
}));

// 运行并返回
let scanned = scan::scan(&fs::read(&path).unwrap_or_else(|e|
Expand Down
18 changes: 17 additions & 1 deletion src/runtime/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ impl Scope {
if let Class::Local(cls) = cls {
let cls = unsafe {&mut *cls};
let mut v = vec![Litr::Uninit;cls.props.len()];
/// 记录哪个属性没有写入
let mut writen = vec![false; cls.props.len()];
/// 确认你在模块内还是模块外
let can_access_private = self.exports == cls.cx.exports;
'a: for (id, e) in val.iter() {
for (n, prop) in cls.props.iter().enumerate() {
Expand All @@ -159,12 +162,25 @@ impl Scope {
let right = self.calc(e);
assert!(prop.typ.is(&right, cls.cx), "'{}'属性要求{:?}类型, 但传入了{:?}", id, prop.typ, right);
// 写入值
unsafe{*v.get_unchecked_mut(n) = right;}
unsafe{
*v.get_unchecked_mut(n) = right;
*writen.get_unchecked_mut(n) = true;
}
continue 'a;
}
}
panic!("'{}'类型不存在'{}'属性", cls.name, id.str())
}
// 如果你在模块外, 就不能缺省属性
if !can_access_private {
let strs = writen.iter().enumerate().filter_map(|(n,b)|if !*b {
Some(unsafe{cls.props.get_unchecked(n).name}.str())
}else {None}).collect::<Vec<String>>();
// 如果有一个属性没写就报错
if strs.len() > 0 {
panic!("正在创建'{}'类型, 但以下属性{}的值未定义", cls.name, strs.join(", "));
}
}
Litr::Inst(Instance {cls, v:v.into()})
}else {
panic!("无法直接构建原生类型'{}'", clsname.str())
Expand Down

0 comments on commit 32ebd4f

Please sign in to comment.