@raviqqe/stak 0.3.24 → 0.3.25

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 CHANGED
@@ -19,6 +19,14 @@ For more information and usage, visit [the full documentation](https://raviqqe.g
19
19
 
20
20
  ## Install
21
21
 
22
+ ### Interpreter
23
+
24
+ To install the Scheme interpreter as a command, run:
25
+
26
+ ```sh
27
+ cargo install stak
28
+ ```
29
+
22
30
  ### Libraries
23
31
 
24
32
  To install Stak Scheme as a library in your Rust project, run:
@@ -31,14 +39,6 @@ cargo install stak-compile
31
39
 
32
40
  For full examples, see [the `examples` directory](https://github.com/raviqqe/stak/tree/main/examples).
33
41
 
34
- ### Command line tools
35
-
36
- To install the Scheme interpreter as a command, run:
37
-
38
- ```sh
39
- cargo install stak
40
- ```
41
-
42
42
  ## Examples
43
43
 
44
44
  ### Dynamic scripting in Rust
@@ -46,25 +46,42 @@ cargo install stak
46
46
  First, prepare a Scheme script named `src/fight.scm`:
47
47
 
48
48
  ```scheme
49
+ ; Import a base library and the library named `(stak rust)` for Rust integration.
49
50
  (import (scheme base) (stak rust))
50
51
 
52
+ ; Use the `define-rust` procedure to import native functions written in Rust.
53
+ ; The order of the functions should match the ones passed into the `Engine::new()`
54
+ ; function in Rust.
51
55
  (define-rust
52
56
  make-person
53
- person-throw-pie
54
- person-wasted)
57
+ person-pies
58
+ person-wasted
59
+ person-throw-pie)
55
60
 
61
+ ; Make two people with a number of pies they have and their dodge rates.
56
62
  (define me (make-person 4 0.2))
57
- (define you (make-person 2 0.6))
58
-
59
- (person-throw-pie me you)
60
- (person-throw-pie you me)
61
- (person-throw-pie me you)
62
- (person-throw-pie you me)
63
-
64
- (when (person-wasted you)
65
- (write-string "Congrats!"))
66
- (when (person-wasted me)
67
- (write-string "Oh, no!"))
63
+ (define friend (make-person 2 0.6))
64
+
65
+ ; The fight begins. Let's throw pies to each other!
66
+ (do ()
67
+ ((or
68
+ (person-wasted me)
69
+ (person-wasted friend)
70
+ (and
71
+ (zero? (person-pies me))
72
+ (zero? (person-pies friend)))))
73
+ (person-throw-pie me friend)
74
+ (person-throw-pie friend me))
75
+
76
+ ; Output the winner.
77
+ (write-string
78
+ (cond
79
+ ((person-wasted friend)
80
+ "You won!")
81
+ ((person-wasted me)
82
+ "You lost...")
83
+ (else
84
+ "Draw...")))
68
85
  ```
69
86
 
70
87
  Then, add a build script at `build.rs` to build the Scheme source file
@@ -92,6 +109,7 @@ use stak::{
92
109
 
93
110
  const HEAP_SIZE: usize = 1 << 16;
94
111
 
112
+ /// A person who holds pies to throw.
95
113
  struct Person {
96
114
  pies: usize,
97
115
  dodge: f64,
@@ -99,6 +117,7 @@ struct Person {
99
117
  }
100
118
 
101
119
  impl Person {
120
+ /// Creates a person.
102
121
  pub fn new(pies: usize, dodge: f64) -> Self {
103
122
  Self {
104
123
  pies,
@@ -107,12 +126,19 @@ impl Person {
107
126
  }
108
127
  }
109
128
 
129
+ /// Returns a number of pies the person has.
130
+ pub fn pies(&self) -> usize {
131
+ self.pies
132
+ }
133
+
134
+ /// Returns `true` if a person is wasted.
110
135
  pub fn wasted(&self) -> bool {
111
136
  self.wasted
112
137
  }
113
138
 
139
+ /// Throws a pie to another person.
114
140
  pub fn throw_pie(&mut self, other: &mut Person) {
115
- if self.wasted {
141
+ if self.pies == 0 || self.wasted {
116
142
  return;
117
143
  }
118
144
 
@@ -125,22 +151,29 @@ impl Person {
125
151
  }
126
152
 
127
153
  fn main() -> Result<(), Box<dyn Error>> {
154
+ // Import a Scheme module of the script.
128
155
  static MODULE: UniversalModule = include_module!("fight.scm");
129
156
 
130
- run(&MODULE)?;
157
+ // Run the Scheme module.
158
+ run_scheme(&MODULE)?;
131
159
 
132
160
  Ok(())
133
161
  }
134
162
 
135
- fn run(module: &'static UniversalModule) -> Result<(), EngineError> {
163
+ fn run_scheme(module: &'static UniversalModule) -> Result<(), EngineError> {
164
+ // Initialize a heap memory for a Scheme scripting engine.
136
165
  let mut heap = [Default::default(); HEAP_SIZE];
166
+ // Define Rust functions to pass to the engine.
137
167
  let mut functions = [
138
168
  r#fn(Person::new),
139
- r#fn(Person::throw_pie),
169
+ r#fn::<(Ref<_>,), _>(Person::pies),
140
170
  r#fn::<(Ref<_>,), _>(Person::wasted),
171
+ r#fn(Person::throw_pie),
141
172
  ];
173
+ // Initialize the engine.
142
174
  let mut engine = Engine::new(&mut heap, &mut functions)?;
143
175
 
176
+ // Finally, run the module!
144
177
  engine.run(module)
145
178
  }
146
179
  ```
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@raviqqe/stak",
3
3
  "type": "module",
4
4
  "description": "Stak Scheme in WebAssembly",
5
- "version": "0.3.24",
5
+ "version": "0.3.25",
6
6
  "license": "SEE LICENSE IN ../LICENSE",
7
7
  "repository": {
8
8
  "type": "git",
package/stak_wasm_bg.wasm CHANGED
Binary file