1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! # [`LuaVM`] Actor Messages
//!
//! Message definitions for the [`LuaVM`] actor. Other
//! actors can use these messages to communicate and interoperate with
//! the userscript environment.
//!
//! See each message for examples and usage information.
//!
use LuaVM;
use ;
use *;
/// Execute a chunk of Lua code in the virutal machine.
///
/// A request for [`LuaVM`] to execute a provided Lua code snippet in
/// the context of the userscript environment.
///
/// # Reply
///
/// After submitting an [`ExecuteChunk`] request, expect a reply from
/// [`LuaVM`] of type `Result<(), mlua::Error>`.
///
/// # Example
///
/// ```
/// # use mlua::prelude::*;
/// # use sscan::lua_vm::{LuaVM, messages::ExecuteChunk};
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Create and spawn a userscript environment.
/// let vm = kameo::spawn(LuaVM::init()?);
///
/// // Execute some code in the userscript environment.
/// let exec_request = ExecuteChunk::using(r#"
/// print("Hello, world!")
/// "#);
/// vm.ask(exec_request).await?;
/// # Ok(())
/// # }
/// ```
/// Evaluate a chunk of Lua code as an expression in the virtual machine.
///
/// This is similar to [`ExecuteChunk`], however it also requests that
/// [`LuaVM`] treat the provided chunk as an expression. As such, if the
/// expression yields a result, [`LuaVM`] returns the result to the
/// sender.
///
/// # Reply
///
/// After submitting an [`EvaluateChunk`] request, expect a reply from
/// [`LuaVM`] of type `Result<mlua::Value, mlua::Error>`.
///
/// # Example
///
/// ```
/// # use mlua::prelude::*;
/// # use sscan::lua_vm::{LuaVM, messages::EvaluateChunk};
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Create and spawn a userscript environment.
/// let vm = kameo::spawn(LuaVM::init()?);
///
/// // Evaluate an expression in the userscript environment.
/// let eval_request = EvaluateChunk::using(r#"
/// 5 + 6
/// "#);
/// let reply: LuaValue = vm.ask(eval_request).await?;
/// assert_eq!(reply, LuaValue::Integer(11));
/// # Ok(())
/// # }
/// ```
/// Checkout a table object from Lua globals.
///
/// A request for [`LuaVM`] to fetch a table object with the given name
/// from the current gloabls table.
///
/// NOTE! [`CheckoutTable`] and [`CommitTable`] requests are not atomic,
/// nor is there any locking! Another actor could potentially modify the
/// same table before the sender commits it back to Lua.
///
/// # Reply
///
/// After submitting a [`CheckoutTable`] request, expect a reply from
/// [`LuaVM`] of type `Result<mlua::Table, mlua::Error>`.
///
/// # Example
///
/// ```
/// # use mlua::prelude::*;
/// # use sscan::lua_vm::{LuaVM, messages::CheckoutTable};
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Create and spawn a userscript environment.
/// let vm = kameo::spawn(LuaVM::init()?);
///
/// // Checkout a table from lua globals.
/// let checkout_request = CheckoutTable::with_name("about");
/// let table: LuaTable = vm.ask(checkout_request).await?;
/// # Ok(())
/// # }
/// ```
/// Commmit a table object into Lua globals.
///
/// A request for [`LuaVM`] to commit a table object with a given name
/// into the gloabls table.
///
/// NOTE! [`CheckoutTable`] and [`CommitTable`] requests are not atomic,
/// nor is there any locking! Another actor could potentially modify the
/// same table before the sender commits it back to Lua.
///
/// # Reply
///
/// After submitting a [`CommitTable`] request, expect a reply from
/// [`LuaVM`] of type `Result<(), mlua::Error>`.
///
/// # Example
///
/// ```
/// # use mlua::prelude::*;
/// # use sscan::lua_vm::{LuaVM, messages::{CheckoutTable, CommitTable}};
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Create and spawn a userscript environment.
/// let vm = kameo::spawn(LuaVM::init()?);
///
/// // Checkout a table from lua globals.
/// let checkout_request = CheckoutTable::with_name("about");
/// let table: LuaTable = vm.ask(checkout_request).await?;
///
/// // Make changes to the table
/// table.set("favorite_animal", "cat")?;
///
/// // Commit changes
/// let commit_request = CommitTable::using(table, "about");
/// vm.ask(commit_request).await?;
/// # Ok(())
/// # }
/// ```