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
//! The Rust runtime stub that the AOT-produced binary calls at
//! process start. This module is **library code** today: the
//! scaffold's linked binary uses a tiny C `main` (see
//! [`crate::embed`]) that prints the bytecode length. The follow-up
//! session swaps that C entry out for this Rust stub so the binary
//! actually runs the embedded bytecode through a `Vm`.
//!
//! # Wiring (follow-up)
//!
//! Two paths to get this module's [`aot_main`] called from the
//! produced binary:
//!
//! 1. **Cargo bootstrap** (audit § Stage 6 Option A) — the embedder's
//! AOT pipeline generates a tiny crate in a tempdir whose `main.rs`
//! is:
//! ```ignore
//! fn main() -> std::process::ExitCode {
//! std::process::ExitCode::from(luna_aot::runtime_stub::aot_main() as u8)
//! }
//! ```
//! and whose `Cargo.toml` depends on `luna-aot = { path = "..." }`.
//! Then `cargo build --release` produces the final binary; the
//! bytecode object file emitted by [`crate::embed`] is linked in
//! via a `build.rs` that calls `println!("cargo:rustc-link-arg=...")`.
//!
//! 2. **Pre-built staticlib** (audit § Stage 6 Option B, v1.4) — ship
//! `libluna_core.a` per supported triple, link directly via `cc`,
//! let this stub serve as the entry point through `#[no_mangle] pub
//! extern "C" fn aot_main`.
//!
//! Either path requires the symbols [`crate::BYTECODE_START_SYMBOL`] /
//! [`crate::BYTECODE_END_SYMBOL`] to resolve at link time — both are
//! satisfied by the bytecode object [`crate::embed::embed_bytecode`]
//! writes.
use Value;
use LuaVersion;
use Vm;
unsafe extern "C"
/// Read the embedded bytecode section as a `&'static [u8]`.
///
/// # Safety
///
/// The bracket symbols must be defined by the linker, i.e. the
/// bytecode object file produced by [`crate::embed::embed_bytecode`]
/// (or an equivalent) is part of this binary's link set. Calling this
/// in a build that doesn't include the bytecode object is an
/// unresolved-symbol at link time, not a runtime failure.
/// AOT-binary entry point. Constructs a fully-loaded `Vm`, undumps
/// the embedded bytecode into the heap via `Vm::load` (which routes
/// through `luna_core::vm::dump::undump` because the dump bytes start
/// with `\x1bLua`), calls the resulting closure with no args, and
/// returns the process exit code.
///
/// # Error handling
///
/// - Load failure (corrupted bytecode / dialect mismatch) prints the
/// error to `stderr` and exits 1.
/// - Runtime errors thrown by the script (uncaught `error(...)`) are
/// formatted via `Vm::error_text` + `Vm::take_error_traceback`,
/// printed to `stderr`, and the process exits 1.
/// - Successful runs exit 0 regardless of the script's return values
/// (matches PUC `lua` behaviour: scripts don't compose with
/// process exit codes unless they call `os.exit`).