Skip to content

Commit d25623a

Browse files
committed
Migrate linit
1 parent 13f32c6 commit d25623a

File tree

7 files changed

+133
-70
lines changed

7 files changed

+133
-70
lines changed

build/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ fn main() {
55
.define("LUA_USE_POSIX", None)
66
// Lua core
77
.file("build/lapi.c")
8+
.file("build/lauxlib.c")
89
.file("build/lcode.c")
910
.file("build/ldump.c")
1011
.file("build/lgc.c")
11-
.file("build/linit.c")
1212
.file("build/llex.c")
1313
.file("build/loadlib.c")
1414
.file("build/lparser.c")
1515
.file("build/lundump.c")
1616
.file("build/lvm.c")
1717
// Non core libs
18-
.file("build/lauxlib.c")
1918
.file("build/lbaselib.c")
2019
.file("build/lbitlib.c")
2120
.file("build/lcorolib.c")

build/linit.c

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/lapi.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
** Lua API
33
*/
44

5+
use libc::c_int;
6+
57
use crate::lstate::lua_State;
68
use crate::types::{lua_Number, LUA_MULTRET};
79

@@ -23,6 +25,11 @@ pub(crate) unsafe fn api_checknelems(L: *mut lua_State, n: i32) {
2325
);
2426
}
2527

28+
pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
29+
lua_settop(L, -n - 1)
30+
}
31+
2632
extern "C" {
2733
pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
34+
pub fn lua_settop(L: *mut lua_State, idx: c_int);
2835
}

src/lauxlib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
** Auxiliary functions for building Lua libraries
3+
*/
4+
5+
use libc::{c_char, c_int};
6+
7+
use crate::{lstate::lua_State, types::lua_CFunction};
8+
9+
#[derive(Copy, Clone)]
10+
#[repr(C)]
11+
pub struct luaL_Reg {
12+
pub name: *const c_char,
13+
pub func: lua_CFunction,
14+
}
15+
16+
extern "C" {
17+
pub fn luaL_requiref(
18+
L: *mut lua_State,
19+
modname: *const c_char,
20+
openf: lua_CFunction,
21+
glb: c_int,
22+
);
23+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ pub(crate) mod lstate;
1515
pub(crate) mod lobject;
1616

1717
pub(crate) mod lapi;
18+
pub(crate) mod lauxlib;
1819
pub(crate) mod ldebug;
1920
pub(crate) mod ldo;
2021
pub(crate) mod lfunc;
2122
pub(crate) mod lgc;
23+
pub(crate) mod libs;
24+
pub(crate) mod linit;
2225
pub(crate) mod llex;
2326
pub(crate) mod llimits;
2427
pub(crate) mod lmem;

src/libs/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use libc::c_int;
2+
3+
use crate::lstate::lua_State;
4+
5+
extern "C" {
6+
pub fn luaopen_base(L: *mut lua_State) -> c_int;
7+
pub fn luaopen_package(L: *mut lua_State) -> c_int;
8+
pub fn luaopen_coroutine(L: *mut lua_State) -> c_int;
9+
pub fn luaopen_table(L: *mut lua_State) -> c_int;
10+
pub fn luaopen_io(L: *mut lua_State) -> c_int;
11+
pub fn luaopen_os(L: *mut lua_State) -> c_int;
12+
pub fn luaopen_string(L: *mut lua_State) -> c_int;
13+
pub fn luaopen_math(L: *mut lua_State) -> c_int;
14+
pub fn luaopen_utf8(L: *mut lua_State) -> c_int;
15+
pub fn luaopen_debug(L: *mut lua_State) -> c_int;
16+
}

src/linit.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
** Initialization of libraries
3+
*/
4+
5+
use crate::lapi::lua_pop;
6+
use crate::lauxlib::{luaL_Reg, luaL_requiref};
7+
use crate::libs::{
8+
luaopen_base, luaopen_coroutine, luaopen_debug, luaopen_io, luaopen_math, luaopen_os,
9+
luaopen_package, luaopen_string, luaopen_table, luaopen_utf8,
10+
};
11+
use crate::lstate::lua_State;
12+
13+
/*
14+
** If you embed Lua in your program and need to open the standard
15+
** libraries, call luaL_openlibs in your program. If you need a
16+
** different set of libraries, copy this file to your project and edit
17+
** it to suit your needs.
18+
**
19+
** You can also *preload* libraries, so that a later 'require' can
20+
** open the library, which is already linked to the application.
21+
** For that, do the following code:
22+
**
23+
** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
24+
** lua_pushcfunction(L, luaopen_modname);
25+
** lua_setfield(L, -2, modname);
26+
** lua_pop(L, 1); // remove PRELOAD table
27+
*/
28+
29+
/*
30+
** these libs are loaded by lua.c and are readily available to any Lua
31+
** program
32+
*/
33+
const loadedlibs: [luaL_Reg; 10] = [
34+
luaL_Reg {
35+
name: cstr!("_G"),
36+
func: Some(luaopen_base),
37+
},
38+
luaL_Reg {
39+
name: cstr!("package"), // LUA_LOADLIBNAME
40+
func: Some(luaopen_package),
41+
},
42+
luaL_Reg {
43+
name: cstr!("coroutine"), // LUA_COLIBNAME
44+
func: Some(luaopen_coroutine),
45+
},
46+
luaL_Reg {
47+
name: cstr!("table"), // LUA_TABLIBNAME
48+
func: Some(luaopen_table),
49+
},
50+
luaL_Reg {
51+
name: cstr!("io"), // LUA_IOLIBNAME
52+
func: Some(luaopen_io),
53+
},
54+
luaL_Reg {
55+
name: cstr!("os"), // LUA_OSLIBNAME
56+
func: Some(luaopen_os),
57+
},
58+
luaL_Reg {
59+
name: cstr!("string"), // LUA_STRLIBNAME
60+
func: Some(luaopen_string),
61+
},
62+
luaL_Reg {
63+
name: cstr!("math"), // LUA_MATHLIBNAME
64+
func: Some(luaopen_math),
65+
},
66+
luaL_Reg {
67+
name: cstr!("utf8"), // LUA_UTF8LIBNAME
68+
func: Some(luaopen_utf8),
69+
},
70+
luaL_Reg {
71+
name: cstr!("debug"), // LUA_DBLIBNAME
72+
func: Some(luaopen_debug),
73+
},
74+
];
75+
76+
#[no_mangle]
77+
pub unsafe extern "C" fn luaL_openlibs(L: *mut lua_State) {
78+
/* "require" functions from 'loadedlibs' and set results to global table */
79+
for lib in &loadedlibs {
80+
luaL_requiref(L, lib.name, lib.func, 1);
81+
lua_pop(L, 1);
82+
}
83+
}

0 commit comments

Comments
 (0)