repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

feat: P2c LuaJIT system link — proves LuaJIT works on Android

d3e776e683227bcc7fb90b9c7efde86c444f21ca

HallucyGenie Agent <agent@hallucygenie.local> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (3)

+551 / −0 text lines

Diff layout

prototypes/p2c-luajit-system/Cargo.lock

Added
new file mode 100644

+319 / −0 text lines

1# This file is automatically @generated by Cargo.
2# It is not intended for manual editing.
3version = 4
4
5[[package]]
6name = "autocfg"
7version = "1.5.1"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
11[[package]]
12name = "bitflags"
13version = "2.11.1"
14source = "registry+https://github.com/rust-lang/crates.io-index"
15checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
17[[package]]
18name = "bstr"
19version = "1.12.1"
20source = "registry+https://github.com/rust-lang/crates.io-index"
21checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab"
22dependencies = [
23 "memchr",
24 "serde",
25]
27[[package]]
28name = "cc"
29version = "1.2.62"
30source = "registry+https://github.com/rust-lang/crates.io-index"
31checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98"
32dependencies = [
33 "find-msvc-tools",
34 "shlex",
35]
37[[package]]
38name = "cfg-if"
39version = "1.0.4"
40source = "registry+https://github.com/rust-lang/crates.io-index"
41checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
43[[package]]
44name = "either"
45version = "1.16.0"
46source = "registry+https://github.com/rust-lang/crates.io-index"
47checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
49[[package]]
50name = "erased-serde"
51version = "0.4.10"
52source = "registry+https://github.com/rust-lang/crates.io-index"
53checksum = "d2add8a07dd6a8d93ff627029c51de145e12686fbc36ecb298ac22e74cf02dec"
54dependencies = [
55 "serde",
56 "serde_core",
57 "typeid",
58]
60[[package]]
61name = "find-msvc-tools"
62version = "0.1.9"
63source = "registry+https://github.com/rust-lang/crates.io-index"
64checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
66[[package]]
67name = "itoa"
68version = "1.0.18"
69source = "registry+https://github.com/rust-lang/crates.io-index"
70checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
72[[package]]
73name = "libc"
74version = "0.2.186"
75source = "registry+https://github.com/rust-lang/crates.io-index"
76checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
78[[package]]
79name = "lock_api"
80version = "0.4.14"
81source = "registry+https://github.com/rust-lang/crates.io-index"
82checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
83dependencies = [
84 "scopeguard",
85]
87[[package]]
88name = "memchr"
89version = "2.8.0"
90source = "registry+https://github.com/rust-lang/crates.io-index"
91checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
93[[package]]
94name = "mlua"
95version = "0.10.5"
96source = "registry+https://github.com/rust-lang/crates.io-index"
97checksum = "c1f5f8fbebc7db5f671671134b9321c4b9aa9adeafccfd9a8c020ae45c6a35d0"
98dependencies = [
99 "bstr",
100 "either",
101 "erased-serde",
102 "mlua-sys",
103 "num-traits",
104 "parking_lot",
105 "rustc-hash",
106 "rustversion",
107 "serde",
108 "serde-value",
109]
111[[package]]
112name = "mlua-sys"
113version = "0.6.8"
114source = "registry+https://github.com/rust-lang/crates.io-index"
115checksum = "380c1f7e2099cafcf40e51d3a9f20a346977587aa4d012eae1f043149a728a93"
116dependencies = [
117 "cc",
118 "cfg-if",
119 "pkg-config",
120]
122[[package]]
123name = "num-traits"
124version = "0.2.19"
125source = "registry+https://github.com/rust-lang/crates.io-index"
126checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
127dependencies = [
128 "autocfg",
129]
131[[package]]
132name = "ordered-float"
133version = "2.10.1"
134source = "registry+https://github.com/rust-lang/crates.io-index"
135checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c"
136dependencies = [
137 "num-traits",
138]
140[[package]]
141name = "p2c-luajit-system"
142version = "0.1.0"
143dependencies = [
144 "mlua",
145 "serde_json",
146]
148[[package]]
149name = "parking_lot"
150version = "0.12.5"
151source = "registry+https://github.com/rust-lang/crates.io-index"
152checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
153dependencies = [
154 "lock_api",
155 "parking_lot_core",
156]
158[[package]]
159name = "parking_lot_core"
160version = "0.9.12"
161source = "registry+https://github.com/rust-lang/crates.io-index"
162checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
163dependencies = [
164 "cfg-if",
165 "libc",
166 "redox_syscall",
167 "smallvec",
168 "windows-link",
169]
171[[package]]
172name = "pkg-config"
173version = "0.3.33"
174source = "registry+https://github.com/rust-lang/crates.io-index"
175checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e"
177[[package]]
178name = "proc-macro2"
179version = "1.0.106"
180source = "registry+https://github.com/rust-lang/crates.io-index"
181checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
182dependencies = [
183 "unicode-ident",
184]
186[[package]]
187name = "quote"
188version = "1.0.45"
189source = "registry+https://github.com/rust-lang/crates.io-index"
190checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
191dependencies = [
192 "proc-macro2",
193]
195[[package]]
196name = "redox_syscall"
197version = "0.5.18"
198source = "registry+https://github.com/rust-lang/crates.io-index"
199checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
200dependencies = [
201 "bitflags",
202]
204[[package]]
205name = "rustc-hash"
206version = "2.1.2"
207source = "registry+https://github.com/rust-lang/crates.io-index"
208checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"
210[[package]]
211name = "rustversion"
212version = "1.0.22"
213source = "registry+https://github.com/rust-lang/crates.io-index"
214checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
216[[package]]
217name = "scopeguard"
218version = "1.2.0"
219source = "registry+https://github.com/rust-lang/crates.io-index"
220checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
222[[package]]
223name = "serde"
224version = "1.0.228"
225source = "registry+https://github.com/rust-lang/crates.io-index"
226checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
227dependencies = [
228 "serde_core",
229]
231[[package]]
232name = "serde-value"
233version = "0.7.0"
234source = "registry+https://github.com/rust-lang/crates.io-index"
235checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c"
236dependencies = [
237 "ordered-float",
238 "serde",
239]
241[[package]]
242name = "serde_core"
243version = "1.0.228"
244source = "registry+https://github.com/rust-lang/crates.io-index"
245checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
246dependencies = [
247 "serde_derive",
248]
250[[package]]
251name = "serde_derive"
252version = "1.0.228"
253source = "registry+https://github.com/rust-lang/crates.io-index"
254checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
255dependencies = [
256 "proc-macro2",
257 "quote",
258 "syn",
259]
261[[package]]
262name = "serde_json"
263version = "1.0.150"
264source = "registry+https://github.com/rust-lang/crates.io-index"
265checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
266dependencies = [
267 "itoa",
268 "memchr",
269 "serde",
270 "serde_core",
271 "zmij",
272]
274[[package]]
275name = "shlex"
276version = "1.3.0"
277source = "registry+https://github.com/rust-lang/crates.io-index"
278checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
280[[package]]
281name = "smallvec"
282version = "1.15.1"
283source = "registry+https://github.com/rust-lang/crates.io-index"
284checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
286[[package]]
287name = "syn"
288version = "2.0.117"
289source = "registry+https://github.com/rust-lang/crates.io-index"
290checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
291dependencies = [
292 "proc-macro2",
293 "quote",
294 "unicode-ident",
295]
297[[package]]
298name = "typeid"
299version = "1.0.3"
300source = "registry+https://github.com/rust-lang/crates.io-index"
301checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
303[[package]]
304name = "unicode-ident"
305version = "1.0.24"
306source = "registry+https://github.com/rust-lang/crates.io-index"
307checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
309[[package]]
310name = "windows-link"
311version = "0.2.1"
312source = "registry+https://github.com/rust-lang/crates.io-index"
313checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
315[[package]]
316name = "zmij"
317version = "1.0.21"
318source = "registry+https://github.com/rust-lang/crates.io-index"
319checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

prototypes/p2c-luajit-system/Cargo.toml

Added
new file mode 100644

+8 / −0 text lines

1[package]
2name = "p2c-luajit-system"
3version = "0.1.0"
4edition = "2024"
5
6[dependencies]
7mlua = { version = "0.10", features = ["luajit", "serialize"] }
8serde_json = "1"

prototypes/p2c-luajit-system/src/main.rs

Added
new file mode 100644

+224 / −0 text lines

1//! Prototype P2c: mlua + system LuaJIT (NOT vendored) on Android/Termux.
2//! Proves LuaJIT works when linked against Termux shared lib.
3//! P2 failed: vendored static compile → __clear_cache unresolved on ARM.
4//! P2b workaround: Lua 5.4 (pure C) works but slower.
5//! P2c: system LuaJIT via pkg-config → dynamic link → __clear_cache inside .so.
6
7use mlua::{Lua, Result, Value};
8use std::time::Instant;
9
10fn main() -> Result<()> {
11 eprintln!("=== P2c: System LuaJIT on Android/Termux ===");
12 eprintln!();
14 let lua = Lua::new();
16 // === Test 1: Basic eval ===
17 test_basic_eval(&lua)?;
18 // === Test 2: Sandbox globals ===
19 test_sandbox(&lua)?;
20 // === Test 3: Table serialization ===
21 test_table_serialize(&lua)?;
22 // === Test 4: Rust → Lua values ===
23 test_rust_to_lua(&lua)?;
24 // === Test 5: Coroutines ===
25 test_coroutines(&lua)?;
26 // === Test 6: Error propagation ===
27 test_error_propagation(&lua)?;
28 // === Test 7: Coroutine benchmark ===
29 bench_coroutines(&lua)?;
31 eprintln!();
32 eprintln!("=== ALL P2c TESTS PASSED (7/7) ===");
33 Ok(())
34}
36fn test_basic_eval(lua: &Lua) -> Result<()> {
37 eprintln!("--- Test 1: Basic eval ---");
38 let result: i32 = lua.load("return 2 + 3").eval()?;
39 assert_eq!(result, 5);
40 eprintln!("[OK] eval: 2+3 = {}", result);
42 let s: String = lua.load("return 'hello'").eval()?;
43 assert_eq!(s, "hello");
44 eprintln!("[OK] eval string: {:?}", s);
45 Ok(())
46}
48fn test_sandbox(lua: &Lua) -> Result<()> {
49 eprintln!("--- Test 2: Sandbox globals ---");
50 lua.load(r#"
51 local raw_os = os
52 local raw_io = io
53 local blocked = { execute = true, exit = true, remove = true, rename = true }
54 os = setmetatable({}, {
55 __index = function(_, k)
56 if blocked[k] then return nil end
57 return raw_os[k]
58 end,
59 __newindex = function(_, k) error("sandbox: cannot set os." .. k) end
60 })
61 local io_blocked = { open = true, popen = true, read = true, write = true }
62 io = setmetatable({}, {
63 __index = function(_, k)
64 if io_blocked[k] then return nil end
65 return raw_io[k]
66 end,
67 __newindex = function(_, k) error("sandbox: cannot set io." .. k) end
68 })
69 "#).exec()?;
71 let res = lua.load(r#"return os.execute("echo pwned")"#).eval::<Value>();
72 match res {
73 Err(_) => eprintln!("[OK] sandbox blocked os.execute"),
74 Ok(v) => panic!("Sandbox FAILED: os.execute returned {:?}", v),
75 }
77 let res = lua.load(r#"return io.open("/etc/passwd", "r")"#).eval::<Value>();
78 match res {
79 Err(_) => eprintln!("[OK] sandbox blocked io.open"),
80 Ok(v) => panic!("Sandbox FAILED: io.open returned {:?}", v),
81 }
83 let result: i32 = lua.load(r#"return math.floor(3.7)"#).eval()?;
84 assert_eq!(result, 3);
85 eprintln!("[OK] sandbox allows math.floor(3.7) = {}", result);
86 Ok(())
87}
89fn test_table_serialize(lua: &Lua) -> Result<()> {
90 eprintln!("--- Test 3: Table serialization ---");
91 let code = r#"return { name = "test", count = 42, items = {1, 2, 3} }"#;
92 let lua_tbl: mlua::Table = lua.load(code).eval()?;
94 let name: String = lua_tbl.get("name")?;
95 let count: i32 = lua_tbl.get("count")?;
96 assert_eq!(name, "test");
97 assert_eq!(count, 42);
98 eprintln!("[OK] table: name={:?}, count={}", name, count);
100 let items: mlua::Table = lua_tbl.get("items")?;
101 let mut item_vec = Vec::new();
102 for pair in items.sequence_values::<i32>() {
103 item_vec.push(pair?);
104 }
105 let json_val = serde_json::json!({
106 "name": name,
107 "count": count,
108 "items": item_vec
109 });
110 assert_eq!(json_val["name"], "test");
111 assert_eq!(json_val["count"], 42);
112 eprintln!("[OK] serde roundtrip: {:?}", json_val);
113 Ok(())
114}
116fn test_rust_to_lua(lua: &Lua) -> Result<()> {
117 eprintln!("--- Test 4: Rust → Lua values ---");
118 let globals = lua.globals();
119 let data = lua.create_table()?;
120 data.set("key", "value")?;
121 data.set("num", 99)?;
122 globals.set("rust_data", data)?;
124 let key: String = lua.load(r#"return rust_data.key"#).eval()?;
125 let num: i32 = lua.load(r#"return rust_data.num"#).eval()?;
126 assert_eq!(key, "value");
127 assert_eq!(num, 99);
128 eprintln!("[OK] rust→lua: key={:?}, num={}", key, num);
129 Ok(())
130}
132fn test_coroutines(lua: &Lua) -> Result<()> {
133 eprintln!("--- Test 5: Coroutines ---");
134 let gen_fn: mlua::Function = lua.load(r#"
135 local function gen(n)
136 local co = coroutine.create(function()
137 for i = 1, n do coroutine.yield(i) end
138 end)
139 local results = {}
140 while true do
141 local ok, val = coroutine.resume(co)
142 if not ok or not val then break end
143 results[#results + 1] = val
144 end
145 return results
146 end
147 return gen
148 "#).eval()?;
150 let results: Vec<i32> = gen_fn.call(3)?;
151 assert_eq!(results, vec![1, 2, 3]);
152 eprintln!("[OK] coroutine generator: {:?}", results);
153 Ok(())
154}
156fn test_error_propagation(lua: &Lua) -> Result<()> {
157 eprintln!("--- Test 6: Error propagation ---");
158 let res = lua.load(r#"error("code=42 msg=boom")"#).eval::<Value>();
159 match res {
160 Err(e) => {
161 let msg = e.to_string();
162 assert!(msg.contains("42"), "error should contain code 42, got: {}", msg);
163 assert!(msg.contains("boom"), "error should contain boom, got: {}", msg);
164 eprintln!("[OK] error propagated: {}", msg);
165 }
166 Ok(v) => panic!("Expected error, got {:?}", v),
167 }
168 Ok(())
169}
171fn bench_coroutines(lua: &Lua) -> Result<()> {
172 eprintln!("--- Test 7: Coroutine benchmark ---");
174 // Warm up JIT
175 let warmup: Vec<i32> = lua.load(r#"
176 local function gen(n)
177 local co = coroutine.create(function()
178 for i = 1, n do coroutine.yield(i) end
179 end)
180 local results = {}
181 while true do
182 local ok, val = coroutine.resume(co)
183 if not ok or not val then break end
184 results[#results + 1] = val
185 end
186 return results
187 end
188 return gen(100)
189 "#).eval()?;
190 assert_eq!(warmup.len(), 100);
192 let n = 100_000;
193 let bench_fn: mlua::Function = lua.load(r#"
194 local function bench(n)
195 local total = 0
196 for _ = 1, n do
197 local co = coroutine.create(function()
198 coroutine.yield(1)
199 coroutine.yield(2)
200 coroutine.yield(3)
201 end)
202 local ok, v = coroutine.resume(co)
203 if ok then total = total + v end
204 ok, v = coroutine.resume(co)
205 if ok then total = total + v end
206 ok, v = coroutine.resume(co)
207 if ok then total = total + v end
208 end
209 return total
210 end
211 return bench
212 "#).eval()?;
214 let start = Instant::now();
215 let total: i64 = bench_fn.call(n)?;
216 let elapsed = start.elapsed();
218 assert_eq!(total, n as i64 * 6);
219 let us_per_yield = elapsed.as_micros() as f64 / (n as f64 * 3.0);
220 eprintln!("[OK] {} coroutines × 3 yields = {} total in {:?}", n, total, elapsed);
221 eprintln!("[OK] {:.2} μs per yield", us_per_yield);
222 Ok(())
223}