Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the 'fields' primitive. #142

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lang_tests/instance_fields.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"
VM:
status: success
stdout:
instance of Array
true
true
"

instance_fields = (
| x y |
run = (
| fds |
fds := instance_fields fields.
fds println.
(fds contains: #x) println.
(fds contains: #y) println.
)
)
29 changes: 29 additions & 0 deletions lang_tests/mutate_fields.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"
VM:
status: success
stdout:
true
true
true
true
false
"

mutate_fields = (
| x y |
run = (
| fds |
fds := mutate_fields fields.
(fds contains: #x) println.
(fds contains: #y) println.
fds at: 1 put: #z.
fds := mutate_fields fields.
(fds contains: #x) println.
(fds contains: #y) println.
(fds contains: #z) println.
)

find: sym = (
method_holder methods do: [:e | ((e signature) == sym) ifTrue: [ ^e ]].
)
)
6 changes: 5 additions & 1 deletion src/lib/vm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,11 @@ impl VM {
))
}
}
Primitive::Fields => todo!(),
Primitive::Fields => {
let fields = stry!(rcv.downcast::<Class>(self)).fields(self);
self.stack.push(fields);
SendReturn::Val
}
Primitive::FromString => todo!(),
Primitive::FullGC => todo!(),
Primitive::Global => {
Expand Down
11 changes: 10 additions & 1 deletion src/lib/vm/objects/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rboehm::Gc;
use crate::vm::{
core::VM,
error::{VMError, VMErrorKind},
objects::{Array, Method, MethodsArray, Obj, ObjType, StaticObjType, String_},
objects::{Array, Method, MethodsArray, NormalArray, Obj, ObjType, StaticObjType, String_},
val::{NotUnboxable, Val, ValKind},
};

Expand Down Expand Up @@ -164,6 +164,15 @@ impl Class {
}
}

pub fn fields(&self, vm: &mut VM) -> Val {
let field_strs = self
.inst_vars_map
.keys()
.map(|k| String_::new_sym(vm, k.clone()))
.collect();
NormalArray::from_vec(vm, field_strs)
}

pub fn methods(&self, _: &VM) -> Val {
self.methods
}
Expand Down