@raviqqe/stak 0.3.8 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -4
- package/package.json +1 -1
- package/stak_wasm_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -37,46 +37,147 @@ cargo install stak-interpret
|
|
|
37
37
|
|
|
38
38
|
## Examples
|
|
39
39
|
|
|
40
|
-
###
|
|
40
|
+
### Embedding Scheme scripts in Rust
|
|
41
|
+
|
|
42
|
+
First, prepare a Scheme script at `src/hello.scm`.
|
|
43
|
+
|
|
44
|
+
```scheme
|
|
45
|
+
(import (scheme base))
|
|
46
|
+
|
|
47
|
+
(write-string "Hello, world!\n")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then, add a build script at `build.rs` to build the Scheme source file into bytecodes.
|
|
51
|
+
|
|
52
|
+
```rust no_run
|
|
53
|
+
use stak_build::{build_r7rs, BuildError};
|
|
54
|
+
|
|
55
|
+
fn main() -> Result<(), BuildError> {
|
|
56
|
+
build_r7rs()
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Now, you can include the Scheme script into a program in Rust using [the `stak::include_module` macro](https://docs.rs/stak/latest/stak/macro.include_bytecode.html).
|
|
41
61
|
|
|
42
62
|
```rust
|
|
43
63
|
use core::error::Error;
|
|
44
64
|
use stak::{
|
|
45
|
-
build::include_bytecode,
|
|
46
65
|
device::StdioDevice,
|
|
47
66
|
file::VoidFileSystem,
|
|
67
|
+
include_module,
|
|
48
68
|
process_context::VoidProcessContext,
|
|
69
|
+
module::{Module, UniversalModule},
|
|
49
70
|
r7rs::{SmallError, SmallPrimitiveSet},
|
|
50
71
|
time::VoidClock,
|
|
51
72
|
vm::Vm,
|
|
52
73
|
};
|
|
53
74
|
|
|
54
75
|
const HEAP_SIZE: usize = 1 << 16;
|
|
55
|
-
|
|
76
|
+
|
|
77
|
+
// Include a Scheme script in the bytecode format built by the build script above.
|
|
78
|
+
static MODULE: UniversalModule = include_module!("hello.scm");
|
|
56
79
|
|
|
57
80
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
58
|
-
run(
|
|
81
|
+
run(&MODULE.bytecode())?;
|
|
59
82
|
|
|
60
83
|
Ok(())
|
|
61
84
|
}
|
|
62
85
|
|
|
63
86
|
fn run(bytecodes: &[u8]) -> Result<(), SmallError> {
|
|
87
|
+
// Prepare a heap memory of a virtual machine.
|
|
64
88
|
let mut heap = [Default::default(); HEAP_SIZE];
|
|
89
|
+
// Create a virtual machine with its heap memory primitive procedures.
|
|
65
90
|
let mut vm = Vm::new(
|
|
66
91
|
&mut heap,
|
|
67
92
|
SmallPrimitiveSet::new(
|
|
93
|
+
// Attach standard input, output, and error of this process to a virtual machine.
|
|
68
94
|
StdioDevice::new(),
|
|
95
|
+
// Use void system interfaces for security because we don't need them for this example.
|
|
69
96
|
VoidFileSystem::new(),
|
|
70
97
|
VoidProcessContext::new(),
|
|
71
98
|
VoidClock::new(),
|
|
72
99
|
),
|
|
73
100
|
)?;
|
|
74
101
|
|
|
102
|
+
// Initialize a virtual machine with bytecodes.
|
|
75
103
|
vm.initialize(bytecodes.iter().copied())?;
|
|
104
|
+
// Run bytecodes on a virtual machine.
|
|
76
105
|
vm.run()
|
|
77
106
|
}
|
|
78
107
|
```
|
|
79
108
|
|
|
109
|
+
### Communication between Scheme and Rust
|
|
110
|
+
|
|
111
|
+
Currently, in-memory standard input (`stdin`) and output (`stdout`) to Scheme scripts are the only way to communicate information between Rust programs and Scheme scripts.
|
|
112
|
+
|
|
113
|
+
```rust
|
|
114
|
+
use core::{error::Error, ffi::CStr, str::FromStr};
|
|
115
|
+
use stak::{
|
|
116
|
+
device::ReadWriteDevice,
|
|
117
|
+
file::VoidFileSystem,
|
|
118
|
+
include_module,
|
|
119
|
+
process_context::VoidProcessContext,
|
|
120
|
+
module::{Module, UniversalModule},
|
|
121
|
+
r7rs::{SmallError, SmallPrimitiveSet},
|
|
122
|
+
time::VoidClock,
|
|
123
|
+
vm::Vm,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const BUFFER_SIZE: usize = 1 << 8;
|
|
127
|
+
const HEAP_SIZE: usize = 1 << 16;
|
|
128
|
+
|
|
129
|
+
static MODULE: UniversalModule = include_module!("fibonacci.scm");
|
|
130
|
+
|
|
131
|
+
fn main() -> Result<(), Box<dyn Error>> {
|
|
132
|
+
let mut input = 24;
|
|
133
|
+
let mut output = [0u8; BUFFER_SIZE];
|
|
134
|
+
let mut error = [0u8; BUFFER_SIZE];
|
|
135
|
+
|
|
136
|
+
run(&MODULE.bytecode(), input.to_string().as_bytes(), &mut output, &mut error)?;
|
|
137
|
+
|
|
138
|
+
let error = decode_buffer(&error)?;
|
|
139
|
+
|
|
140
|
+
// If stderr is not empty, we assume that some error has occurred.
|
|
141
|
+
if !error.is_empty() {
|
|
142
|
+
return Err(error.into());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Decode and print the output.
|
|
146
|
+
println!("Answer: {}", isize::from_str(&decode_buffer(&output)?)?);
|
|
147
|
+
|
|
148
|
+
Ok(())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fn run(
|
|
152
|
+
bytecodes: &[u8],
|
|
153
|
+
input: &[u8],
|
|
154
|
+
output: &mut [u8],
|
|
155
|
+
error: &mut [u8],
|
|
156
|
+
) -> Result<(), SmallError> {
|
|
157
|
+
let mut heap = [Default::default(); HEAP_SIZE];
|
|
158
|
+
let mut vm = Vm::new(
|
|
159
|
+
&mut heap,
|
|
160
|
+
SmallPrimitiveSet::new(
|
|
161
|
+
// Create and attach an in-memory I/O device.
|
|
162
|
+
ReadWriteDevice::new(input, output, error),
|
|
163
|
+
VoidFileSystem::new(),
|
|
164
|
+
VoidProcessContext::new(),
|
|
165
|
+
VoidClock::new(),
|
|
166
|
+
),
|
|
167
|
+
)?;
|
|
168
|
+
|
|
169
|
+
vm.initialize(bytecodes.iter().copied())?;
|
|
170
|
+
vm.run()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn decode_buffer(buffer: &[u8]) -> Result<String, Box<dyn Error>> {
|
|
174
|
+
Ok(CStr::from_bytes_until_nul(buffer)
|
|
175
|
+
.map_err(|error| error.to_string())?
|
|
176
|
+
.to_string_lossy()
|
|
177
|
+
.into())
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
80
181
|
## License
|
|
81
182
|
|
|
82
183
|
[MIT](https://github.com/raviqqe/stak/blob/main/LICENSE)
|
package/package.json
CHANGED
package/stak_wasm_bg.wasm
CHANGED
|
Binary file
|