We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c4d815f commit 27f60c5Copy full SHA for 27f60c5
zenlang/src/ast/function.rs
@@ -65,10 +65,12 @@ impl Compile for AstFunction {
65
let module = compiler.get_module();
66
{
67
let name = self.name.to_string();
68
+ let ctor = self.attrs.contains(&FunctionAttribute::Ctor);
69
module.functions.push(ModuleFunction::new(
70
name,
71
module.opcodes.len() as u32,
72
self.args.len() as u64,
73
+ ctor,
74
));
75
}
76
zenlang/src/func_attr.rs
@@ -3,12 +3,15 @@ use alloc::string::*;
3
#[derive(PartialEq)]
4
pub enum FunctionAttribute {
5
Naked,
6
+ Ctor,
7
8
9
impl FunctionAttribute {
10
pub fn map(name: String) -> Option<FunctionAttribute> {
11
if name == "naked" {
12
return Some(FunctionAttribute::Naked);
13
+ } else if name == "ctor" {
14
+ return Some(FunctionAttribute::Ctor);
15
16
return None;
17
zenlang/src/module.rs
@@ -21,14 +21,17 @@ pub struct ModuleFunction {
21
pub addr: u32,
22
/// Argument count
23
pub args_count: u64,
24
+ /// Is a constructor function
25
+ pub ctor: bool,
26
27
28
impl ModuleFunction {
- pub fn new(name: String, addr: u32, args_count: u64) -> ModuleFunction {
29
+ pub fn new(name: String, addr: u32, args_count: u64, ctor: bool) -> ModuleFunction {
30
return ModuleFunction {
31
name: name,
32
addr: addr,
33
args_count: args_count,
34
+ ctor: ctor,
35
};
36
37
zenlang/src/stdlib.rs
@@ -113,6 +113,7 @@ fn number str {
113
"_vmcall_ret_unsafe_1".into(),
114
115
1,
116
+ false,
117
118
module.opcodes.push(Opcode::Dynvmcall());
119
module.opcodes.push(Opcode::Ret());
@@ -121,6 +122,7 @@ fn number str {
121
122
"_vmcall_ret_unsafe_2".into(),
123
124
2,
125
126
127
128
@@ -129,6 +131,7 @@ fn number str {
129
131
"_vmcall_ret_unsafe_3".into(),
130
132
133
3,
134
135
136
137
@@ -137,6 +140,7 @@ fn number str {
140
"_vmcall_ret_unsafe_4".into(),
138
141
139
142
4,
143
144
145
146
zenlang/src/vm/vm.rs
@@ -46,8 +46,38 @@ impl VM {
46
47
48
pub fn load_module(&mut self, module: &Module) -> Result<(), String> {
49
+ // check if already loaded
50
+ for m in self.modules.iter_mut() {
51
+ if m.name == module.name {
52
+ return Ok(());
53
+ }
54
55
+
56
self.modules.push(module.clone());
57
58
+ for func in module.functions.iter() {
59
+ if func.ctor {
60
+ self.check_stack_overflow();
61
+ self.pc.set_low(func.addr);
62
+ self.pc.set_high((self.modules.len() - 1) as u32);
63
+ self.add_scope();
64
+ loop {
+ if !self.step() {
+ break;
+ if !self.error.is_empty() {
+ return Err(format!(
+ "in constructor of module {}: {}",
+ module.name, self.error
+ ));
77
+ self.halted = false;
78
79
80
81
for var in module.globals.iter() {
82
if self.global_scope.get(var).is_some() {
83
return Err(format!(
0 commit comments