|
5 | 5 |
|
6 | 6 | use std::rc::Rc; |
7 | 7 |
|
8 | | -use crate::lib_registry::{LibraryModule, get_arg, get_args, require_arg}; |
| 8 | +use crate::lib_registry::{LibraryModule, LibraryEntry, get_arg, get_args, require_arg}; |
9 | 9 | use crate::lua_value::{LuaValue, LuaValueKind, MultiValue}; |
10 | 10 | use crate::lua_vm::{LuaResult, LuaVM}; |
11 | 11 |
|
12 | 12 | pub fn create_basic_lib() -> LibraryModule { |
13 | | - crate::lib_module!("_G", { |
14 | | - "print" => lua_print, |
15 | | - "type" => lua_type, |
16 | | - "assert" => lua_assert, |
17 | | - "error" => lua_error, |
18 | | - "tonumber" => lua_tonumber, |
19 | | - "tostring" => lua_tostring, |
20 | | - "select" => lua_select, |
21 | | - "ipairs" => lua_ipairs, |
22 | | - "pairs" => lua_pairs, |
23 | | - "next" => lua_next, |
24 | | - "pcall" => lua_pcall, |
25 | | - "xpcall" => lua_xpcall, |
26 | | - "getmetatable" => lua_getmetatable, |
27 | | - "setmetatable" => lua_setmetatable, |
28 | | - "rawget" => lua_rawget, |
29 | | - "rawset" => lua_rawset, |
30 | | - "rawlen" => lua_rawlen, |
31 | | - "rawequal" => lua_rawequal, |
32 | | - "collectgarbage" => lua_collectgarbage, |
33 | | - "_VERSION" => lua_version, |
34 | | - "require" => lua_require, |
35 | | - "load" => lua_load, |
36 | | - "loadfile" => lua_loadfile, |
37 | | - "dofile" => lua_dofile, |
38 | | - "warn" => lua_warn, |
39 | | - }) |
| 13 | + let mut module = LibraryModule::new("_G"); |
| 14 | + |
| 15 | + // Functions |
| 16 | + module.entries.push(("print", LibraryEntry::Function(lua_print))); |
| 17 | + module.entries.push(("type", LibraryEntry::Function(lua_type))); |
| 18 | + module.entries.push(("assert", LibraryEntry::Function(lua_assert))); |
| 19 | + module.entries.push(("error", LibraryEntry::Function(lua_error))); |
| 20 | + module.entries.push(("tonumber", LibraryEntry::Function(lua_tonumber))); |
| 21 | + module.entries.push(("tostring", LibraryEntry::Function(lua_tostring))); |
| 22 | + module.entries.push(("select", LibraryEntry::Function(lua_select))); |
| 23 | + module.entries.push(("ipairs", LibraryEntry::Function(lua_ipairs))); |
| 24 | + module.entries.push(("pairs", LibraryEntry::Function(lua_pairs))); |
| 25 | + module.entries.push(("next", LibraryEntry::Function(lua_next))); |
| 26 | + module.entries.push(("pcall", LibraryEntry::Function(lua_pcall))); |
| 27 | + module.entries.push(("xpcall", LibraryEntry::Function(lua_xpcall))); |
| 28 | + module.entries.push(("getmetatable", LibraryEntry::Function(lua_getmetatable))); |
| 29 | + module.entries.push(("setmetatable", LibraryEntry::Function(lua_setmetatable))); |
| 30 | + module.entries.push(("rawget", LibraryEntry::Function(lua_rawget))); |
| 31 | + module.entries.push(("rawset", LibraryEntry::Function(lua_rawset))); |
| 32 | + module.entries.push(("rawlen", LibraryEntry::Function(lua_rawlen))); |
| 33 | + module.entries.push(("rawequal", LibraryEntry::Function(lua_rawequal))); |
| 34 | + module.entries.push(("collectgarbage", LibraryEntry::Function(lua_collectgarbage))); |
| 35 | + module.entries.push(("require", LibraryEntry::Function(lua_require))); |
| 36 | + module.entries.push(("load", LibraryEntry::Function(lua_load))); |
| 37 | + module.entries.push(("loadfile", LibraryEntry::Function(lua_loadfile))); |
| 38 | + module.entries.push(("dofile", LibraryEntry::Function(lua_dofile))); |
| 39 | + module.entries.push(("warn", LibraryEntry::Function(lua_warn))); |
| 40 | + |
| 41 | + // Values |
| 42 | + module.entries.push(("_VERSION", LibraryEntry::Value(|vm| vm.create_string("Lua 5.4")))); |
| 43 | + |
| 44 | + module |
40 | 45 | } |
41 | 46 |
|
42 | 47 | /// print(...) - Print values to stdout |
@@ -694,12 +699,6 @@ fn lua_collectgarbage(vm: &mut LuaVM) -> LuaResult<MultiValue> { |
694 | 699 | } |
695 | 700 | } |
696 | 701 |
|
697 | | -/// _VERSION - Lua version string |
698 | | -fn lua_version(vm: &mut LuaVM) -> LuaResult<MultiValue> { |
699 | | - let version = vm.create_string("Lua 5.4"); |
700 | | - Ok(MultiValue::single(version)) |
701 | | -} |
702 | | - |
703 | 702 | /// require(modname) - Load a module |
704 | 703 | fn lua_require(vm: &mut LuaVM) -> LuaResult<MultiValue> { |
705 | 704 | let modname_value = require_arg(vm, 1, "require")?; |
|
0 commit comments