Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit ad24024

Browse files
committed
Adjust libulem_eir_interpreter for new Code idiom in runtime
1 parent d32bb12 commit ad24024

File tree

9 files changed

+84
-86
lines changed

9 files changed

+84
-86
lines changed

liblumen_alloc/src/erts/term/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Closure {
2323
creator: Term, // pid of creator process, possible to be either Pid or ExternalPid
2424
module_function_arity: Arc<ModuleFunctionArity>,
2525
code: Code, // pointer to function entry
26-
env: Vec<Term>,
26+
pub env: Vec<Term>,
2727
}
2828

2929
impl Closure {

liblumen_eir_interpreter/src/exec/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,19 @@ impl CallExecutor {
116116
TypedTerm::Boxed(boxed) => match boxed.to_typed_term().unwrap() {
117117
TypedTerm::Closure(closure) => {
118118
//assert!(closure.env_hack.len() != 1);
119-
if closure.env_hack.len() > 0 {
120-
let env_list = proc
121-
.list_from_iter(closure.env_hack.iter().skip(1).cloned())
122-
.unwrap();
119+
if closure.env.len() > 0 {
120+
let env_list = proc.list_from_slice(&closure.env[1..]).unwrap();
123121
proc.stack_push(env_list)?;
124122

125-
let block_id = closure.env_hack[0];
123+
let block_id = closure.env[0];
126124
proc.stack_push(block_id)?;
127125
}
128126

129127
let arg_list = proc.list_from_iter(args.iter().cloned()).unwrap();
130128
proc.stack_push(arg_list)?;
131129

132-
let mfa = closure.module_function_arity();
133-
if closure.env_hack.len() > 0 {
130+
if closure.env.len() > 0 {
131+
let mfa = closure.module_function_arity();
134132
proc.stack_push(proc.integer(mfa.arity).unwrap()).unwrap();
135133
}
136134

liblumen_eir_interpreter/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use liblumen_alloc::erts::process::ProcessControlBlock;
55
use liblumen_alloc::erts::process::{heap, next_heap_size, Status};
66
use liblumen_alloc::erts::term::{atom_unchecked, Atom, Term};
77
use liblumen_alloc::erts::ModuleFunctionArity;
8-
use lumen_runtime::code::apply_fn;
8+
99
use lumen_runtime::scheduler::Scheduler;
1010
use lumen_runtime::system;
1111

@@ -64,16 +64,16 @@ pub fn call_erlang(
6464
// if this fails the entire tab is out-of-memory
6565
let heap = heap(heap_size).unwrap();
6666

67-
let run_arc_process = Scheduler::spawn(
67+
let run_arc_process = Scheduler::spawn_apply_3(
6868
&proc,
6969
module,
7070
function,
7171
arguments,
72-
apply_fn(),
7372
heap,
74-
heap_size)
75-
// if this fails, don't use `default_heap` and instead use a bigger sized heap
76-
.unwrap();
73+
heap_size
74+
)
75+
// if this fails increase heap size
76+
.unwrap();
7777

7878
loop {
7979
let ran = Scheduler::current().run_through(&run_arc_process);

liblumen_eir_interpreter/src/native/erlang.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn make_erlang() -> NativeModule {
3939
};
4040

4141
let inner_args = proc.cons(ret, proc.cons(ret, args[2]).unwrap()).unwrap();
42-
Ok(erlang::spawn_3(args[0], args[1], inner_args, proc).unwrap())
42+
Ok(erlang::spawn_3::native(proc, args[0], args[1], inner_args).unwrap())
4343
});
4444

4545
native.add_simple(Atom::try_from_str("send").unwrap(), 2, |proc, args| {
@@ -49,12 +49,12 @@ pub fn make_erlang() -> NativeModule {
4949

5050
native.add_simple(Atom::try_from_str("-").unwrap(), 2, |proc, args| {
5151
assert!(args.len() == 2);
52-
Ok(erlang::subtract_2(args[0], args[1], proc).unwrap())
52+
Ok(erlang::subtract_2::native(proc, args[0], args[1]).unwrap())
5353
});
5454

5555
native.add_simple(Atom::try_from_str("+").unwrap(), 2, |proc, args| {
5656
assert!(args.len() == 2);
57-
Ok(erlang::add_2(args[0], args[1], proc).unwrap())
57+
Ok(erlang::add_2::native(proc, args[0], args[1]).unwrap())
5858
});
5959

6060
native.add_simple(Atom::try_from_str("self").unwrap(), 0, |proc, args| {
@@ -76,7 +76,7 @@ pub fn make_erlang() -> NativeModule {
7676
0,
7777
|proc, args| {
7878
assert!(args.len() == 0);
79-
Ok(erlang::monotonic_time_0(proc).unwrap())
79+
Ok(erlang::monotonic_time_0::native(proc).unwrap())
8080
},
8181
);
8282

liblumen_eir_interpreter/src/vm.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use libeir_ir::{FunctionIdent, Module};
99
use liblumen_alloc::erts::exception;
1010
use liblumen_alloc::erts::process::{heap, next_heap_size, Status};
1111
use liblumen_alloc::erts::term::{atom_unchecked, Atom, Term};
12-
use lumen_runtime::code::apply_fn;
1312
use lumen_runtime::registry;
1413
use lumen_runtime::scheduler::Scheduler;
1514
use lumen_runtime::system;
@@ -23,7 +22,7 @@ pub struct VMState {
2322

2423
impl VMState {
2524
pub fn new() -> Self {
26-
lumen_runtime::code::set_apply_fn(crate::code::apply);
25+
lumen_runtime::otp::erlang::apply_3::set_code(crate::code::apply);
2726

2827
let arc_scheduler = Scheduler::current();
2928
let init_arc_scheduler = Arc::clone(&arc_scheduler);
@@ -62,15 +61,14 @@ impl VMState {
6261
// if this fails the entire tab is out-of-memory
6362
let heap = heap(heap_size).unwrap();
6463

65-
let run_arc_process = Scheduler::spawn(
64+
let run_arc_process = Scheduler::spawn_apply_3(
6665
&init_arc_process,
6766
module,
6867
function,
6968
arguments,
70-
apply_fn(),
7169
heap,
7270
heap_size)
73-
// if this fails, don't use `default_heap` and instead use a bigger sized heap
71+
// if this fails a bigger sized heap
7472
.unwrap();
7573

7674
loop {

lumen_runtime/src/otp/erlang/add_2.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ use liblumen_alloc::erts::process::ProcessControlBlock;
1515
use liblumen_alloc::erts::term::{Atom, Term};
1616
use liblumen_alloc::ModuleFunctionArity;
1717

18+
/// `+/2` infix operator
19+
pub fn native(process: &ProcessControlBlock, augend: Term, addend: Term) -> exception::Result {
20+
number_infix_operator!(augend, addend, process, checked_add, +)
21+
}
22+
1823
/// `+/2` infix operator
1924
pub fn place_frame_with_arguments(
2025
process: &ProcessControlBlock,
@@ -62,7 +67,3 @@ fn module_function_arity() -> Arc<ModuleFunctionArity> {
6267
arity: 0,
6368
})
6469
}
65-
66-
fn native(process: &ProcessControlBlock, augend: Term, addend: Term) -> exception::Result {
67-
number_infix_operator!(augend, addend, process, checked_add, +)
68-
}

lumen_runtime/src/otp/erlang/monotonic_time_0.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ use liblumen_alloc::ModuleFunctionArity;
1212

1313
use crate::time::{monotonic, Unit::Native};
1414

15+
pub fn native(process_control_block: &ProcessControlBlock) -> exception::Result {
16+
let big_int = monotonic::time(Native);
17+
18+
Ok(process_control_block.integer(big_int)?)
19+
}
20+
1521
pub fn place_frame(process: &ProcessControlBlock, placement: Placement) {
1622
process.place_frame(frame(), placement);
1723
}
@@ -46,9 +52,3 @@ fn module_function_arity() -> Arc<ModuleFunctionArity> {
4652
arity: 0,
4753
})
4854
}
49-
50-
fn native(process_control_block: &ProcessControlBlock) -> exception::Result {
51-
let big_int = monotonic::time(Native);
52-
53-
Ok(process_control_block.integer(big_int)?)
54-
}

lumen_runtime/src/otp/erlang/spawn_3.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,55 @@ use liblumen_alloc::ModuleFunctionArity;
1919

2020
use crate::scheduler::Scheduler;
2121

22+
pub fn native(
23+
process_control_block: &ProcessControlBlock,
24+
module: Term,
25+
function: Term,
26+
arguments: Term,
27+
) -> exception::Result {
28+
let module_atom: Atom = module.try_into()?;
29+
let function_atom: Atom = function.try_into()?;
30+
31+
let option_pid = match arguments.to_typed_term().unwrap() {
32+
TypedTerm::Nil => {
33+
let (heap, heap_size) = default_heap()?;
34+
let arc_process = Scheduler::spawn_apply_3(
35+
process_control_block,
36+
module_atom,
37+
function_atom,
38+
arguments,
39+
heap,
40+
heap_size,
41+
)?;
42+
43+
Some(arc_process.pid())
44+
}
45+
TypedTerm::List(cons) => {
46+
if cons.is_proper() {
47+
let (heap, heap_size) = default_heap()?;
48+
let arc_process = Scheduler::spawn_apply_3(
49+
process_control_block,
50+
module_atom,
51+
function_atom,
52+
arguments,
53+
heap,
54+
heap_size,
55+
)?;
56+
57+
Some(arc_process.pid())
58+
} else {
59+
None
60+
}
61+
}
62+
_ => None,
63+
};
64+
65+
match option_pid {
66+
Some(pid) => Ok(unsafe { pid.as_term() }),
67+
None => Err(badarg!().into()),
68+
}
69+
}
70+
2271
pub fn place_frame_with_arguments(
2372
process: &ProcessControlBlock,
2473
placement: Placement,
@@ -68,52 +117,3 @@ fn module_function_arity() -> Arc<ModuleFunctionArity> {
68117
arity: 3,
69118
})
70119
}
71-
72-
pub fn native(
73-
process_control_block: &ProcessControlBlock,
74-
module: Term,
75-
function: Term,
76-
arguments: Term,
77-
) -> exception::Result {
78-
let module_atom: Atom = module.try_into()?;
79-
let function_atom: Atom = function.try_into()?;
80-
81-
let option_pid = match arguments.to_typed_term().unwrap() {
82-
TypedTerm::Nil => {
83-
let (heap, heap_size) = default_heap()?;
84-
let arc_process = Scheduler::spawn_apply_3(
85-
process_control_block,
86-
module_atom,
87-
function_atom,
88-
arguments,
89-
heap,
90-
heap_size,
91-
)?;
92-
93-
Some(arc_process.pid())
94-
}
95-
TypedTerm::List(cons) => {
96-
if cons.is_proper() {
97-
let (heap, heap_size) = default_heap()?;
98-
let arc_process = Scheduler::spawn_apply_3(
99-
process_control_block,
100-
module_atom,
101-
function_atom,
102-
arguments,
103-
heap,
104-
heap_size,
105-
)?;
106-
107-
Some(arc_process.pid())
108-
} else {
109-
None
110-
}
111-
}
112-
_ => None,
113-
};
114-
115-
match option_pid {
116-
Some(pid) => Ok(unsafe { pid.as_term() }),
117-
None => Err(badarg!().into()),
118-
}
119-
}

lumen_runtime/src/otp/erlang/subtract_2.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ use liblumen_alloc::erts::process::ProcessControlBlock;
1515
use liblumen_alloc::erts::term::{Atom, Term};
1616
use liblumen_alloc::ModuleFunctionArity;
1717

18+
/// `-/2` infix operator
19+
pub fn native(process: &ProcessControlBlock, minuend: Term, subtrahend: Term) -> exception::Result {
20+
number_infix_operator!(minuend, subtrahend, process, checked_sub, -)
21+
}
22+
1823
/// `-/2` infix operator
1924
pub fn place_frame_with_arguments(
2025
process: &ProcessControlBlock,
@@ -62,7 +67,3 @@ fn module_function_arity() -> Arc<ModuleFunctionArity> {
6267
arity: 0,
6368
})
6469
}
65-
66-
fn native(process: &ProcessControlBlock, minuend: Term, subtrahend: Term) -> exception::Result {
67-
number_infix_operator!(minuend, subtrahend, process, checked_sub, -)
68-
}

0 commit comments

Comments
 (0)