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

Commit 8707401

Browse files
authored
Merge pull request #197 from lumen/runtime_on_alloc
Runtime on alloc
2 parents 9706e66 + 429fbd9 commit 8707401

File tree

645 files changed

+19304
-22331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

645 files changed

+19304
-22331
lines changed

Cargo.lock

Lines changed: 124 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/spawn-chain/Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ edition = "2018"
88
crate-type = ["cdylib", "rlib"]
99

1010
[features]
11-
default = ["console_error_panic_hook"]
11+
default = ["console_error_panic_hook", "time_web_sys"]
12+
time_web_sys = ["parking_lot_core/time_web_sys", "lumen_runtime/time_web_sys"]
1213

1314
[dependencies]
1415
# The `console_error_panic_hook` crate provides better debugging of panics by
@@ -17,6 +18,7 @@ default = ["console_error_panic_hook"]
1718
# code size when deploying.
1819
console_error_panic_hook = { version = "0.1.1", optional = true }
1920

21+
liblumen_alloc = { path = "../../liblumen_alloc" }
2022
lumen_runtime = { path = "../../lumen_runtime" }
2123
num-bigint = "0.2.2"
2224
wasm-bindgen = "0.2"
@@ -28,9 +30,14 @@ wasm-bindgen = "0.2"
2830
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
2931
wee_alloc = { version = "0.4.2", optional = true }
3032

33+
[dependencies.parking_lot_core]
34+
git = "https://github.com/KronicDeth/parking_lot.git"
35+
branch = "wasm32-time_web_sys"
36+
features = ["nightly"]
37+
3138
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
3239
version = "0.3.20"
33-
features = ['console', 'Performance', 'Window']
40+
features = ['console']
3441

3542
[dev-dependencies]
3643
time-test = "0.2.1"

examples/spawn-chain/src/code.rs

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,48 @@
11
use std::convert::TryInto;
22
use std::sync::Arc;
33

4-
use lumen_runtime::process::stack::frame::Frame;
5-
use lumen_runtime::process::{ModuleFunctionArity, Process};
6-
use lumen_runtime::term::Term;
4+
use liblumen_alloc::erts::exception::runtime;
5+
use liblumen_alloc::erts::process::code::stack::frame::Frame;
6+
use liblumen_alloc::erts::process::code::Result;
7+
use liblumen_alloc::erts::process::ProcessControlBlock;
8+
use liblumen_alloc::erts::term::{Atom, Term, TypedTerm};
9+
use liblumen_alloc::erts::ModuleFunctionArity;
710

811
use crate::elixir;
912

10-
pub fn apply(arc_process: &Arc<Process>) {
11-
let frame_argument_vec = arc_process.pop_arguments(3);
12-
let module = frame_argument_vec[0];
13-
let function = frame_argument_vec[1];
13+
pub fn apply(arc_process: &Arc<ProcessControlBlock>) -> Result {
14+
let module_term = arc_process.stack_pop().unwrap();
15+
let function_term = arc_process.stack_pop().unwrap();
1416

15-
let argument_list = frame_argument_vec[2];
16-
let argument_vec: Vec<Term> = match argument_list.try_into() {
17-
Ok(argument_vec) => argument_vec,
18-
Err(_) => {
19-
#[cfg(debug_assertions)]
20-
panic!(
21-
"Arguments ({:?}) are neither empty list nor proper list",
22-
argument_list
23-
);
24-
#[cfg(not(debug_assertions))]
25-
panic!("Arguments are neither empty list nor proper list")
17+
let argument_list = arc_process.stack_pop().unwrap();
18+
19+
let mut argument_vec: Vec<Term> = Vec::new();
20+
21+
match argument_list.to_typed_term().unwrap() {
22+
TypedTerm::Nil => (),
23+
TypedTerm::List(argument_cons) => {
24+
for result in argument_cons.into_iter() {
25+
let element = result.unwrap();
26+
27+
argument_vec.push(element);
28+
}
2629
}
27-
};
28-
let arity = argument_vec.len();
30+
_ => panic!(
31+
"In {:?}, {:?} ({:#b}) is not an argument list. Cannot call {:?}:{:?}",
32+
arc_process.pid_term(),
33+
argument_list,
34+
argument_list.as_usize(),
35+
module_term,
36+
function_term
37+
),
38+
}
39+
40+
let module: Atom = module_term.try_into().unwrap();
41+
let function: Atom = function_term.try_into().unwrap();
42+
let arity = argument_vec.len() as u8;
2943

30-
match unsafe { module.atom_to_string() }.as_ref().as_ref() {
31-
"Elixir.Chain" => match unsafe { function.atom_to_string() }.as_ref().as_ref() {
44+
match module.name() {
45+
"Elixir.Chain" => match function.name() {
3246
"counter" => match arity {
3347
1 => {
3448
let module_function_arity = Arc::new(ModuleFunctionArity {
@@ -39,16 +53,16 @@ pub fn apply(arc_process: &Arc<Process>) {
3953

4054
// Elixir.Chain.counter is a user function and not a BIF, so it is a `Code` and
4155
// run in a frame instead of directly
42-
let mut chain_counter_frame =
56+
let chain_counter_frame =
4357
Frame::new(module_function_arity, elixir::chain::counter_0_code);
44-
chain_counter_frame.push(argument_vec[0]);
58+
arc_process.stack_push(argument_vec[0])?;
4559
arc_process.replace_frame(chain_counter_frame);
4660

4761
// don't count finding the function as a reduction if it is found, only on
4862
// exception in `undef`, so that each path is at least one reduction.
49-
Process::call_code(arc_process);
63+
ProcessControlBlock::call_code(arc_process)
5064
}
51-
_ => undef(arc_process, module, function, argument_list),
65+
_ => undef(arc_process, module_term, function_term, argument_list),
5266
},
5367
"run" => match arity {
5468
1 => {
@@ -59,42 +73,33 @@ pub fn apply(arc_process: &Arc<Process>) {
5973
});
6074
// Elixir.Chain.run is a user function and not a BIF, so it is a `Code` and
6175
// run in a frame instead of directly
62-
let mut chain_run_frame =
76+
let chain_run_frame =
6377
Frame::new(module_function_arity, elixir::chain::run_0_code);
64-
chain_run_frame.push(argument_vec[0]);
78+
arc_process.stack_push(argument_vec[0])?;
6579
arc_process.replace_frame(chain_run_frame);
6680

6781
// don't count finding the function as a reduction if it is found, only on
6882
// exception in `undef`, so that each path is at least one reduction.
69-
Process::call_code(arc_process);
83+
ProcessControlBlock::call_code(arc_process)
7084
}
71-
_ => undef(arc_process, module, function, argument_list),
85+
_ => undef(arc_process, module_term, function_term, argument_list),
7286
},
73-
_ => undef(arc_process, module, function, argument_list),
87+
_ => undef(arc_process, module_term, function_term, argument_list),
7488
},
75-
_ => undef(arc_process, module, function, argument_list),
76-
}
77-
}
78-
79-
#[cfg(debug_assertions)]
80-
pub fn print_stacktrace(process: &Process) {
81-
let mut formatted_stacktrace_parts: Vec<String> = Vec::new();
82-
83-
for module_function_arity in process.stacktrace() {
84-
formatted_stacktrace_parts.push(format!("{}", module_function_arity));
89+
_ => undef(arc_process, module_term, function_term, argument_list),
8590
}
86-
87-
let formatted_stacktrace = formatted_stacktrace_parts.join("\n");
88-
89-
crate::start::log_1(formatted_stacktrace);
9091
}
9192

92-
fn undef(arc_process: &Arc<Process>, module: Term, function: Term, arguments: Term) {
93+
fn undef(
94+
arc_process: &Arc<ProcessControlBlock>,
95+
module: Term,
96+
function: Term,
97+
arguments: Term,
98+
) -> Result {
9399
arc_process.reduce();
94-
arc_process.exception(lumen_runtime::undef!(
95-
module,
96-
function,
97-
arguments,
98-
arc_process
99-
))
100+
let exception = liblumen_alloc::undef!(arc_process, module, function, arguments);
101+
let runtime_exception: runtime::Exception = exception.try_into().unwrap();
102+
arc_process.exception(runtime_exception);
103+
104+
Ok(())
100105
}

0 commit comments

Comments
 (0)