Skip to content

Commit

Permalink
Libc process context (#1335)
Browse files Browse the repository at this point in the history
- **Fix**
- **Fix**
  • Loading branch information
raviqqe authored Jul 15, 2024
1 parent c61111d commit 17c6bfd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ jobs:
tags: not @library
- name: stak-tools
- name: mstak
tags: not @process-context
tags: not @command-line and not @environment-variable and not @library
- name: mstak-tools
tags: not @process-context
tags: not @environment-variable
exclude:
- os: ${{ github.ref == 'refs/heads/main' && 'none' || 'macos-14' }}
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 3 additions & 1 deletion cmd/minimal/interpret/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ stak-device = { version = "0.2.24", path = "../../../device", features = [
] }
stak-file = { version = "0.4.0", path = "../../../file", features = ["libc"] }
stak-primitive = { version = "0.5.0", path = "../../../primitive" }
stak-process-context = { version = "0.2.0", path = "../../../process_context" }
stak-process-context = { version = "0.2.0", path = "../../../process_context", features = [
"libc",
] }
stak-vm = { version = "0.5.0", path = "../../../vm" }

[lints]
Expand Down
4 changes: 2 additions & 2 deletions cmd/minimal/interpret/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mstak_util::Mmap;
use stak_device::libc::{ReadWriteDevice, Stderr, Stdin, Stdout};
use stak_file::LibcFileSystem;
use stak_primitive::SmallPrimitiveSet;
use stak_process_context::VoidProcessContext;
use stak_process_context::LibcProcessContext;
use stak_vm::{Value, Vm};

const HEAP_SIZE: usize = 1 << 19;
Expand All @@ -41,7 +41,7 @@ unsafe extern "C" fn main(argc: isize, argv: *const *const i8) -> isize {
SmallPrimitiveSet::new(
ReadWriteDevice::new(Stdin::new(), Stdout::new(), Stderr::new()),
LibcFileSystem::new(),
VoidProcessContext::new(),
LibcProcessContext::new(argc, argv),
),
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion features/process-context/command-line.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@process-context
@command-line
Feature: Command line
Scenario: Get an argument
Given a file named "main.scm" with:
Expand Down
2 changes: 1 addition & 1 deletion features/process-context/environment-variable.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@process-context
@environment-variable
Feature: Environment variables
Scenario: Get an environment variable
Given a file named "main.scm" with:
Expand Down
1 change: 1 addition & 0 deletions process_context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme.workspace = true
repository.workspace = true

[features]
libc = []
std = ["dep:once_cell"]

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions process_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "libc")]
mod libc;
#[cfg(feature = "std")]
mod os;
mod process_context;
mod void;

#[cfg(feature = "libc")]
pub use libc::LibcProcessContext;
#[cfg(feature = "std")]
pub use os::OsProcessContext;
pub use process_context::ProcessContext;
Expand Down
35 changes: 35 additions & 0 deletions process_context/src/libc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::ProcessContext;
use core::{ffi::CStr, slice};

/// A process context based on libc.
#[derive(Debug)]
pub struct LibcProcessContext {
arguments: &'static [*const i8],
}

impl LibcProcessContext {
/// Creates a process context.
///
/// # Safety
///
/// The `argc` and `argv` arguments should be the ones passed down as
/// arguments to the `main` function in C.
pub const unsafe fn new(argc: isize, argv: *const *const i8) -> Self {
Self {
arguments: unsafe { slice::from_raw_parts(argv, argc as _) },
}
}
}

impl ProcessContext for LibcProcessContext {
fn command_line_rev(&self) -> impl IntoIterator<Item = &str> {
self.arguments
.iter()
.rev()
.map(|&argument| unsafe { CStr::from_ptr(argument) }.to_str().unwrap())
}

fn environment_variables(&self) -> impl IntoIterator<Item = (&str, &str)> {
[]
}
}

0 comments on commit 17c6bfd

Please sign in to comment.