Skip to content

Commit

Permalink
Public/Private access
Browse files Browse the repository at this point in the history
This is paving the way to custom types having fields that have access
permissions.
  • Loading branch information
ecton committed Apr 14, 2024
1 parent e063e79 commit b1063d4
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 54 deletions.
2 changes: 1 addition & 1 deletion examples/fib-vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() {

let mut context = VmContext::new(&vm, &mut guard);
context
.declare_function(dbg!(Function::new("fib").when(1, fib)))
.declare_function(Function::new("fib").when(1, fib))
.unwrap();
dbg!(context.execute(&code).unwrap());
}
64 changes: 49 additions & 15 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use syntax::{

use crate::runtime::symbol::Symbol;
use crate::vm::bitcode::{
BinaryKind, BitcodeBlock, BitcodeFunction, FaultKind, Label, Op, OpDestination, ValueOrSource,
Access, BinaryKind, BitcodeBlock, BitcodeFunction, FaultKind, Label, Op, OpDestination,
ValueOrSource,
};
use crate::vm::{Code, Register, Stack};

Expand Down Expand Up @@ -471,6 +472,10 @@ impl<'a> Scope<'a> {
}
}

fn is_module_root(&self) -> bool {
self.module && self.depth == 0
}

fn function_root(compiler: &'a mut Compiler) -> Self {
compiler.scopes.push(ScopeInfo {
kind: ScopeKind::Function,
Expand Down Expand Up @@ -770,10 +775,17 @@ impl<'a> Scope<'a> {
self.compiler
.code
.load_module(instance, OpDestination::Stack(stack));
if module.publish.is_some() {
if self.is_module_root() || module.publish.is_some() {
self.ensure_in_module(module.name.1);

self.compiler.code.declare(name.clone(), false, stack, dest);
let access = if module.publish.is_some() {
Access::Public
} else {
Access::Private
};
self.compiler
.code
.declare(name.clone(), false, access, stack, dest);
} else {
self.declare_local(name.clone(), false, stack, dest);
}
Expand Down Expand Up @@ -958,22 +970,28 @@ impl<'a> Scope<'a> {
}
}

match (&decl.name, decl.publish.is_some()) {
(Some(name), true) => {
self.ensure_in_module(name.1);
self.compiler.code.declare(name.0.clone(), false, fun, dest);
match (&decl.name, decl.publish.is_some(), self.is_module_root()) {
(Some(name), true, _) | (Some(name), _, true) => {
let access = if decl.publish.is_some() {
Access::Public
} else {
Access::Private
};
self.compiler
.code
.declare(name.0.clone(), false, access, fun, dest);
}
(Some(name), false) => {
(Some(name), false, _) => {
let stack = self.new_temporary();
self.compiler.code.copy(fun, stack);
self.declare_local(name.0.clone(), false, stack, dest);
}
(None, true) => {
(None, true, _) => {
self.compiler
.errors
.push(Ranged::new(range, Error::PublicFunctionRequiresName));
}
(None, false) => {
(None, false, _) => {
self.compiler.code.copy(fun, dest);
}
}
Expand Down Expand Up @@ -1034,12 +1052,18 @@ impl<'a> Scope<'a> {
self.compiler.code.invoke(matches, Symbol::get_symbol(), 1);
self.compiler.code.copy(Register(0), variable);

if bindings.publish {
if self.is_module_root() || bindings.publish {
self.ensure_in_module(range);
let access = if bindings.publish {
Access::Public
} else {
Access::Private
};

self.compiler.code.declare(
name.clone(),
bindings.mutable,
access,
variable,
(),
);
Expand Down Expand Up @@ -1090,10 +1114,19 @@ impl<'a> Scope<'a> {
if let Some(name) = name {
self.check_bound_name(Ranged::new(pattern.range(), name.clone()), bindings);

if bindings.publish {
self.compiler
.code
.declare(name.clone(), bindings.mutable, source, ());
if self.is_module_root() || bindings.publish {
let access = if bindings.publish {
Access::Public
} else {
Access::Private
};
self.compiler.code.declare(
name.clone(),
bindings.mutable,
access,
source,
(),
);
} else {
let stack = self.new_temporary();
self.compiler.code.copy(source, stack);
Expand Down Expand Up @@ -2161,6 +2194,7 @@ impl<'a> Scope<'a> {
let base = self.compile_source(&base.expression);

self.compile_function_args(&call.parameters.enclosed, arity);
self.compiler.code.set_current_source_range(range);
self.compiler
.code
.invoke(base, lookup.name.0.clone(), arity);
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,15 @@ where
}
}

impl<T> PartialEq for Dynamic<T>
where
T: CustomType + Trace,
{
fn eq(&self, other: &Self) -> bool {
self.0.as_any() == other.0.as_any()
}
}

impl<T> From<Dynamic<T>> for AnyRef
where
T: CustomType + Trace,
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn module_budgeting() {
}
println!("Executed in {ops} steps");
assert!(ops > 6);
assert!(ops < MAX_OPS);
assert!(ops <= MAX_OPS);
}

#[test]
Expand Down
Loading

0 comments on commit b1063d4

Please sign in to comment.