Skip to content

Commit 693d7d4

Browse files
committed
feat(rusile): Enable embedding of Rusile module
1 parent 19d68bc commit 693d7d4

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

Makefile.am

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ nodist_man_MANS =
6464
dist_man_MANS = sile-lua.1
6565
sile_SOURCES = src/bin/sile.rs src/lib.rs src/cli.rs src/types.rs src/types/semver.rs
6666
EXTRA_sile_SOURCES =
67-
if EMBEDDED_RESOURCES
68-
noinst_LIBRARIES = librusile.a
69-
librusile_a_SOURCES = $(RUSILE_SOURCES)
70-
else !EMBEDDED_RESOURCES
67+
if !EMBEDDED_RESOURCES
7168
nobase_dist_pkgdata_DATA = $(SILEDATA) $(LUALIBRARIES)
7269
nobase_nodist_pkgdata_DATA = $(BUILT_SOURCES_LUA) $(LUAMODULES)
7370
pkglib_LIBRARIES = rusile.so
@@ -128,9 +125,7 @@ $(CARGO_BIN): justenough/.libs/justenoughicu.a
128125
$(CARGO_BIN): justenough/.libs/justenoughlibtexpdf.a
129126
$(CARGO_BIN): justenough/.libs/svg.a
130127
$(CARGO_BIN): libtexpdf/.libs/libtexpdf.a
131-
if EMBEDDED_RESOURCES
132-
$(CARGO_BIN): librusile.a
133-
else !EMBEDDED_RESOURCES
128+
if !EMBEDDED_RESOURCES
134129
$(CARGO_BIN): rusile.so
135130
endif !EMBEDDED_RESOURCES
136131

@@ -176,10 +171,6 @@ rusile.so: $(rusile_so_SOURCES) $(bin_PROGRAMS)
176171
$(CARGO_ENV) $(CARGO) build $(CARGO_VERBOSE) $(RUSILE_FEATURE_ARG) $(CARGO_RELEASE_ARGS) -p rusile
177172
$(INSTALL) @builddir@/target/@RUST_TARGET_SUBDIR@/lib$@ $@
178173

179-
librusile.a:
180-
$(CARGO_ENV) $(CARGO) build $(CARGO_VERBOSE) $(RUSILE_FEATURE_ARG) $(CARGO_RELEASE_ARGS) -p rusile
181-
$(INSTALL) @builddir@/target/@RUST_TARGET_SUBDIR@/$@ $@
182-
183174
DEPDIR := .deps
184175
LOCALFONTS := FONTCONFIG_FILE=$(PWD)/fontconfig.conf
185176
LOCALPATHS := SILE_PATH="$(PWD);libtexpdf/.libs;justenough/.libs"

build-aux/build.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ fn main() {
3636
generate_shell_completions();
3737
#[cfg(feature = "static")]
3838
{
39-
////let dir = env::var("CARGO_TARGET_DIR").unwrap();
40-
//let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
41-
//println!(
42-
// "cargo:rustc-link-search=native={}",
43-
// Path::new(&dir).join("target").join("release").display()
44-
//);
45-
//println!("cargo:rustc-link-arg=-l:librusile.a");
46-
4739
let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
4840
println!(
4941
"cargo:rustc-link-search=native={}",

core/sile.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SILE = {}
1919
--- Load the C module that provides any and all SILE features implemented in Rust. The exports in this module are meant
2020
--- to be moved to approprate places in the Lua API as we load other things and assemble the public facing interface.
2121
--- This location is considered internal and accessing them directly from here is not supported.
22-
-- @table SILE.rusile
22+
-- @table SILE._rusile
2323
SILE._rusile = require("rusile")
2424

2525
--- Machine friendly short-form version.

rusile/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Thin wrapper around user facing functions implemented in Rust to expose them in a loadable Lua
2+
// module separate from the Rust CLI.
3+
14
#![crate_type = "cdylib"]
25
#![crate_type = "rlib"]
36
#![crate_type = "staticlib"]
@@ -6,7 +9,5 @@ use mlua::prelude::*;
69

710
#[mlua::lua_module]
811
fn rusile(lua: &Lua) -> LuaResult<LuaTable> {
9-
let exports = lua.create_table()?;
10-
exports.set("semver", LuaFunction::wrap_raw(sile::types::semver::semver))?;
11-
Ok(exports)
12+
sile::get_rusile_exports(lua)
1213
}

src/embed.rs.in

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use mlua::chunk;
22
use mlua::prelude::*;
33
use rust_embed::{EmbeddedFile, RustEmbed};
44
use std::str;
5-
//use rusile::rusile;
65

76
/// Embed everything that would otherwise be installed to datadir
87
#[derive(RustEmbed)]
@@ -46,7 +45,6 @@ pub struct SileModules;
4645
// Link Lua loader functions from C modules that Lua would otherwise be loading externally that
4746
// we've linked into the CLI binary. Linking happens in build-aux/build.rs.
4847
extern "C-unwind" {
49-
//fn luaopen_rusile(lua: *mut mlua::lua_State) -> i32;
5048
fn luaopen_fontmetrics(lua: *mut mlua::lua_State) -> i32;
5149
fn luaopen_justenoughfontconfig(lua: *mut mlua::lua_State) -> i32;
5250
fn luaopen_justenoughharfbuzz(lua: *mut mlua::lua_State) -> i32;
@@ -58,18 +56,27 @@ extern "C-unwind" {
5856
/// Register a Lua function in the loaders/searchers table to return C modules linked into the CLI
5957
/// binary and another to return embedded Lua resources as Lua modules. See discussion in mlua:
6058
/// https://github.com/khvzak/mlua/discussions/322
61-
pub fn inject_embedded_loader(lua: &Lua) {
59+
pub fn inject_embedded_loaders(lua: &Lua) {
6260
let package: LuaTable = lua.globals().get("package").unwrap();
6361
let loaders: LuaTable = match package.get("loaders").unwrap() {
6462
LuaValue::Table(loaders) => loaders,
6563
LuaValue::Nil => package.get("searchers").unwrap(),
6664
_ => panic!("Unable to find appropriate interface to inject embedded loader"),
6765
};
68-
let embedded_ffi_loader = lua.create_function(|lua, module: String| unsafe {
66+
67+
let embedded_rusile_loader = lua
68+
.create_function(|lua, module: String| match module.as_str() {
69+
"rusile" => lua
70+
.create_function(move |lua, _: ()| crate::get_rusile_exports(lua))
71+
.map(LuaValue::Function),
72+
_ => format!("Module '{module}' is embeded in Rust binary").into_lua(lua),
73+
})
74+
.unwrap();
75+
loaders.push(embedded_rusile_loader).unwrap();
76+
77+
let embedded_ffi_loader = lua
78+
.create_function(|lua, module: String| unsafe {
6979
match module.as_str() {
70-
//"rusile" => lua
71-
// .create_c_function(luaopen_rusile)
72-
// .map(LuaValue::Function),
7380
"fontmetrics" => lua
7481
.create_c_function(luaopen_fontmetrics)
7582
.map(LuaValue::Function),
@@ -88,12 +95,12 @@ pub fn inject_embedded_loader(lua: &Lua) {
8895
"svg" => lua.create_c_function(luaopen_svg).map(LuaValue::Function),
8996
_ => format!("C Module '{module}' is not linked in Rust binary").into_lua(lua),
9097
}
91-
}).unwrap();
92-
loaders
93-
.push(embedded_ffi_loader)
98+
})
9499
.unwrap();
100+
loaders.push(embedded_ffi_loader).unwrap();
95101

96-
let embedded_lua_loader = lua.create_function(|lua, module: String| {
102+
let embedded_lua_loader = lua
103+
.create_function(|lua, module: String| {
97104
let module_path = module.replace('.', "/");
98105
let luaversion: LuaString = lua
99106
.load(chunk! {
@@ -121,40 +128,40 @@ pub fn inject_embedded_loader(lua: &Lua) {
121128
}
122129
}
123130
match resource_option {
124-
Some(module) => {
125-
lua.create_function(move |lua, _: ()| {
131+
Some(module) => lua
132+
.create_function(move |lua, _: ()| {
126133
let data = str::from_utf8(module.data.as_ref())
127134
.expect("Embedded content is not valid UTF-8");
128135
lua.load(data).call::<LuaValue>(())
129-
}).map(LuaValue::Function)
130-
},
136+
})
137+
.map(LuaValue::Function),
131138

132139
None => format!("Module '{module}' is not embedded in Rust binary").into_lua(lua),
133140
}
134-
}).unwrap();
135-
loaders
136-
.push(embedded_lua_loader)
141+
})
137142
.unwrap();
143+
loaders.push(embedded_lua_loader).unwrap();
138144

139-
let embedded_ftl_loader = lua.create_function(|lua, module: String| {
145+
let embedded_ftl_loader = lua
146+
.create_function(|lua, module: String| {
140147
let module_path = module.replace('.', "/");
141148
let pattern = "?.ftl";
142149
let path = pattern.replace('?', &module_path);
143150
match SileModules::get(&path) {
144-
Some(module) => lua.create_function(move |lua, _: ()| {
145-
let data = str::from_utf8(module.data.as_ref())
146-
.expect("Embedded content is not valid UTF-8");
147-
lua.load(chunk! {
148-
return assert(fluent:add_messages($data))
151+
Some(module) => lua
152+
.create_function(move |lua, _: ()| {
153+
let data = str::from_utf8(module.data.as_ref())
154+
.expect("Embedded content is not valid UTF-8");
155+
lua.load(chunk! {
156+
return assert(fluent:add_messages($data))
157+
})
158+
.call::<LuaValue>(())
149159
})
150-
.call::<LuaValue>(())
151-
}).map(LuaValue::Function),
160+
.map(LuaValue::Function),
152161
_ => format!("FTL resource '{module_path}' is not embedded in Rust binary")
153162
.into_lua(lua),
154163
}
155-
}).unwrap();
156-
loaders
157-
.push(embedded_ftl_loader)
164+
})
158165
.unwrap();
159-
166+
loaders.push(embedded_ftl_loader).unwrap();
160167
}

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// rust-embed include attributes have issues with lots of matches...
22
#![recursion_limit = "2048"]
33

4+
#[cfg(not(feature = "static"))]
45
use mlua::chunk;
56
use mlua::prelude::*;
67
#[cfg(not(feature = "static"))]
@@ -19,7 +20,7 @@ pub type Result<T> = anyhow::Result<T>;
1920
pub fn start_luavm() -> crate::Result<Lua> {
2021
let lua = unsafe { Lua::unsafe_new() };
2122
#[cfg(feature = "static")]
22-
crate::embed::inject_embedded_loader(&lua);
23+
crate::embed::inject_embedded_loaders(&lua);
2324
inject_paths(&lua);
2425
load_sile(&lua);
2526
inject_version(&lua);
@@ -56,6 +57,12 @@ pub fn inject_paths(lua: &Lua) {
5657
}
5758
}
5859

60+
pub fn get_rusile_exports(lua: &Lua) -> LuaResult<LuaTable> {
61+
let exports = lua.create_table()?;
62+
exports.set("semver", LuaFunction::wrap_raw(types::semver::semver))?;
63+
Ok(exports)
64+
}
65+
5966
pub fn inject_version(lua: &Lua) {
6067
let sile: LuaTable = lua.globals().get("SILE").unwrap();
6168
let mut full_version: String = sile.get("full_version").unwrap();

0 commit comments

Comments
 (0)