diff --git a/src/glua/lib.gleam b/src/glua/lib.gleam new file mode 100644 index 0000000..56ef076 --- /dev/null +++ b/src/glua/lib.gleam @@ -0,0 +1,107 @@ +//// Bindings to the functions under `_G` + +import glua.{type Value} + +/// Returns the `assert` function. +@external(erlang, "glua_stdlib_ffi", "assert") +pub fn assert_() -> Value + +/// Returns the `collectgarbage` function. +@external(erlang, "glua_stdlib_ffi", "collectgarbage") +pub fn collect_garbage() -> Value + +/// Returns the `dofile` function. +@external(erlang, "glua_stdlib_ffi", "dofile") +pub fn do_file() -> Value + +/// Returns the `eprint` function. +@external(erlang, "glua_stdlib_ffi", "eprint") +pub fn eprint() -> Value + +/// Returns the `error` function. +@external(erlang, "glua_stdlib_ffi", "error") +pub fn error() -> Value + +/// Returns the `getmetatable` function. +@external(erlang, "glua_stdlib_ffi", "getmetatable") +pub fn get_meta_table() -> Value + +/// Returns the `ipairs` function. +@external(erlang, "glua_stdlib_ffi", "ipairs") +pub fn ipairs() -> Value + +/// Returns the `load` function. +@external(erlang, "glua_stdlib_ffi", "load") +pub fn load() -> Value + +/// Returns the `loadfile` function. +@external(erlang, "glua_stdlib_ffi", "loadfile") +pub fn load_file() -> Value + +/// Returns the `loadstring` function. +/// +/// As of Lua 5.2, `loadstring` is deprecated. +@external(erlang, "glua_stdlib_ffi", "loadstring") +pub fn load_string() -> Value + +/// Returns the `next` function. +@external(erlang, "glua_stdlib_ffi", "next") +pub fn next() -> Value + +/// Returns the `pairs` function. +@external(erlang, "glua_stdlib_ffi", "pairs") +pub fn pairs() -> Value + +/// Returns the `pcall` function. +@external(erlang, "glua_stdlib_ffi", "pcall") +pub fn pcall() -> Value + +/// Returns the `print` function. +@external(erlang, "glua_stdlib_ffi", "print") +pub fn print() -> Value + +/// Returns the `rawequal` function. +@external(erlang, "glua_stdlib_ffi", "rawequal") +pub fn raw_equal() -> Value + +/// Returns the `rawget` function. +@external(erlang, "glua_stdlib_ffi", "rawget") +pub fn raw_get() -> Value + +/// Returns the `rawlen` function. +@external(erlang, "glua_stdlib_ffi", "rawlen") +pub fn raw_len() -> Value + +/// Returns the `rawset` function. +@external(erlang, "glua_stdlib_ffi", "rawset") +pub fn raw_set() -> Value + +/// Returns the `select` function. +@external(erlang, "glua_stdlib_ffi", "select") +pub fn select() -> Value + +/// Returns the `setmetatable` function. +@external(erlang, "glua_stdlib_ffi", "setmetatable") +pub fn set_meta_table() -> Value + +/// Returns the `tonumber` function. +@external(erlang, "glua_stdlib_ffi", "tonumber") +pub fn to_number() -> Value + +/// Returns the `tostring` function. +@external(erlang, "glua_stdlib_ffi", "tostring") +pub fn to_string() -> Value + +/// Returns the `type` function. +@external(erlang, "glua_stdlib_ffi", "type") +pub fn type_() -> Value + +/// Returns the `unpack` function. +/// +/// As of Lua 5.2, `unpack` was moved to `table.unpack` +@external(erlang, "glua_stdlib_ffi", "unpack") +pub fn unpack() -> Value + +/// Returns the `require` function. +@external(erlang, "glua_stdlib_ffi", "require") +pub fn require() -> Value diff --git a/src/glua/lib/bit32.gleam b/src/glua/lib/bit32.gleam new file mode 100644 index 0000000..cc66d30 --- /dev/null +++ b/src/glua/lib/bit32.gleam @@ -0,0 +1,53 @@ +//// Bindings to the functions under `_G.bit32`. +//// +//// As of Lua 5.3, the entire bit32 library is deprecated. + +import glua.{type Value} + +/// Returns the `bit32.band` function. +@external(erlang, "glua_stdlib_ffi", "bit32_band") +pub fn band() -> Value + +/// Returns the `bit32.bnot` function. +@external(erlang, "glua_stdlib_ffi", "bit32_bnot") +pub fn bnot() -> Value + +/// Returns the `bit32.bor` function. +@external(erlang, "glua_stdlib_ffi", "bit32_bor") +pub fn bor() -> Value + +/// Returns the `bit32.btest` function. +@external(erlang, "glua_stdlib_ffi", "bit32_btest") +pub fn btest() -> Value + +/// Returns the `bit32.bxor` function. +@external(erlang, "glua_stdlib_ffi", "bit32_bxor") +pub fn bxor() -> Value + +/// Returns the `bit32.lshift` function. +@external(erlang, "glua_stdlib_ffi", "bit32_lshift") +pub fn lshift() -> Value + +/// Returns the `bit32.rshift` function. +@external(erlang, "glua_stdlib_ffi", "bit32_rshift") +pub fn rshift() -> Value + +/// Returns the `bit32.arshift` function. +@external(erlang, "glua_stdlib_ffi", "bit32_arshift") +pub fn arshift() -> Value + +/// Returns the `bit32.lrotate` function. +@external(erlang, "glua_stdlib_ffi", "bit32_lrotate") +pub fn lrotate() -> Value + +/// Returns the `bit32.rrotate` function. +@external(erlang, "glua_stdlib_ffi", "bit32_rrotate") +pub fn rrotate() -> Value + +/// Returns the `bit32.extract` function. +@external(erlang, "glua_stdlib_ffi", "bit32_extract") +pub fn extract() -> Value + +/// Returns the `bit32.replace` function. +@external(erlang, "glua_stdlib_ffi", "bit32_replace") +pub fn replace() -> Value diff --git a/src/glua/lib/debug.gleam b/src/glua/lib/debug.gleam new file mode 100644 index 0000000..2501a97 --- /dev/null +++ b/src/glua/lib/debug.gleam @@ -0,0 +1,19 @@ +//// Bindings to the functions under `_G.debug` + +import glua.{type Value} + +/// Returns the `getmetatable` function. +@external(erlang, "glua_stdlib_ffi", "debug_getmetatable") +pub fn get_meta_table() -> Value + +/// Returns the `getuservalue` function. +@external(erlang, "glua_stdlib_ffi", "debug_getuservalue") +pub fn get_user_value() -> Value + +/// Returns the `setmetatable` function. +@external(erlang, "glua_stdlib_ffi", "debug_setmetatable") +pub fn set_meta_table() -> Value + +/// Returns the `setuservalue` function. +@external(erlang, "glua_stdlib_ffi", "debug_setuservalue") +pub fn set_user_value() -> Value diff --git a/src/glua/lib/io.gleam b/src/glua/lib/io.gleam new file mode 100644 index 0000000..501d342 --- /dev/null +++ b/src/glua/lib/io.gleam @@ -0,0 +1,9 @@ +import glua.{type Value} + +/// Returns the `io.flush` function. +@external(erlang, "glua_stdlib_ffi", "io_flush") +pub fn flush() -> Value + +/// Returns the `io.write` function. +@external(erlang, "glua_stdlib_ffi", "io_write") +pub fn write() -> Value diff --git a/src/glua/lib/math.gleam b/src/glua/lib/math.gleam new file mode 100644 index 0000000..7ed69bf --- /dev/null +++ b/src/glua/lib/math.gleam @@ -0,0 +1,145 @@ +//// Bindings to the functions under `_G.math` + +import glua.{type Value} + +pub const huge = 1.7976931348623157e308 + +pub const max_integer = 9_223_372_036_854_775_807 + +pub const min_integer = -9_223_372_036_854_775_808 + +pub const pi = 3.141592653589793 + +/// Returns the `math.abs` function. +@external(erlang, "glua_stdlib_ffi", "math_abs") +pub fn abs() -> Value + +/// Returns the `math.acos` function. +@external(erlang, "glua_stdlib_ffi", "math_acos") +pub fn acos() -> Value + +/// Returns the `math.asin` function. +@external(erlang, "glua_stdlib_ffi", "math_asin") +pub fn asin() -> Value + +/// Returns the `math.atan` function. +@external(erlang, "glua_stdlib_ffi", "math_atan") +pub fn atan() -> Value + +/// Returns the `math.atan2` function. +/// +/// As of Lua 5.3, `math.atan2` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_atan2") +pub fn atan2() -> Value + +/// Returns the `math.ceil` function. +@external(erlang, "glua_stdlib_ffi", "math_ceil") +pub fn ceil() -> Value + +/// Returns the `math.cos` function. +@external(erlang, "glua_stdlib_ffi", "math_cos") +pub fn cos() -> Value + +/// Returns the `math.cosh` function. +/// +/// As of Lua 5.3, `math.cosh` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_cosh") +pub fn cosh() -> Value + +/// Returns the `math.deg` function. +@external(erlang, "glua_stdlib_ffi", "math_deg") +pub fn deg() -> Value + +/// Returns the `math.exp` function. +@external(erlang, "glua_stdlib_ffi", "math_exp") +pub fn exp() -> Value + +/// Returns the `math.floor` function. +@external(erlang, "glua_stdlib_ffi", "math_floor") +pub fn floor() -> Value + +/// Returns the `math.fmod` function. +@external(erlang, "glua_stdlib_ffi", "math_fmod") +pub fn fmod() -> Value + +/// Returns the `math.frexp` function. +/// +/// As of Lua 5.3, `math.frexp` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_frexp") +pub fn frexp() -> Value + +/// Returns the `math.ldexp` function. +/// +/// As of Lua 5.3, `math.ldexp` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_ldexp") +pub fn ldexp() -> Value + +/// Returns the `math.log` function. +@external(erlang, "glua_stdlib_ffi", "math_log") +pub fn log() -> Value + +/// Returns the `math.log10` function. +@external(erlang, "glua_stdlib_ffi", "math_log10") +pub fn log10() -> Value + +/// Returns the `math.max` function. +@external(erlang, "glua_stdlib_ffi", "math_max") +pub fn max() -> Value + +/// Returns the `math.min` function. +@external(erlang, "glua_stdlib_ffi", "math_min") +pub fn min() -> Value + +/// Returns the `math.modf` function. +@external(erlang, "glua_stdlib_ffi", "math_modf") +pub fn modf() -> Value + +/// Returns the `math.pow` function. +/// +/// As of Lua 5.3, `math.pow` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_pow") +pub fn pow() -> Value + +/// Returns the `math.rad` function. +@external(erlang, "glua_stdlib_ffi", "math_rad") +pub fn rad() -> Value + +/// Returns the `math.random` function. +@external(erlang, "glua_stdlib_ffi", "math_random") +pub fn random() -> Value + +/// Returns the `math.randomseed` function. +@external(erlang, "glua_stdlib_ffi", "math_randomseed") +pub fn random_seed() -> Value + +/// Returns the `math.sin` function. +@external(erlang, "glua_stdlib_ffi", "math_sin") +pub fn sin() -> Value + +/// Returns the `math.sinh` function. +/// +/// As of Lua 5.3, `math.sinh` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_sinh") +pub fn sinh() -> Value + +/// Returns the `math.sqrt` function. +@external(erlang, "glua_stdlib_ffi", "math_sqrt") +pub fn sqrt() -> Value + +/// Returns the `math.tan` function. +@external(erlang, "glua_stdlib_ffi", "math_tan") +pub fn tan() -> Value + +/// Returns the `math.tanh` function. +/// +/// As of Lua 5.3, `math.tanh` is deprecated +@external(erlang, "glua_stdlib_ffi", "math_tanh") +pub fn tanh() -> Value + +/// Returns the `math.tointeger` function. +@external(erlang, "glua_stdlib_ffi", "math_tointeger") +pub fn to_integer() -> Value + +/// Returns the `math.type` function. +@external(erlang, "glua_stdlib_ffi", "math_type") +pub fn type_() -> Value diff --git a/src/glua/lib/os.gleam b/src/glua/lib/os.gleam new file mode 100644 index 0000000..4a96846 --- /dev/null +++ b/src/glua/lib/os.gleam @@ -0,0 +1,43 @@ +//// Bindings to the functions under `_G.os` + +import glua.{type Value} + +/// Returns the `os.clock` function. +@external(erlang, "glua_stdlib_ffi", "os_clock") +pub fn clock() -> Value + +/// Returns the `os.date` function. +@external(erlang, "glua_stdlib_ffi", "os_date") +pub fn date() -> Value + +/// Returns the `os.difftime` function. +@external(erlang, "glua_stdlib_ffi", "os_difftime") +pub fn difftime() -> Value + +/// Returns the `os.execute` function. +@external(erlang, "glua_stdlib_ffi", "os_execute") +pub fn execute() -> Value + +/// Returns the `os.exit` function. +@external(erlang, "glua_stdlib_ffi", "os_exit") +pub fn exit() -> Value + +/// Returns the `os.getenv` function. +@external(erlang, "glua_stdlib_ffi", "os_getenv") +pub fn getenv() -> Value + +/// Returns the `os.remove` function. +@external(erlang, "glua_stdlib_ffi", "os_remove") +pub fn remove() -> Value + +/// Returns the `os.rename` function. +@external(erlang, "glua_stdlib_ffi", "os_rename") +pub fn rename() -> Value + +/// Returns the `os.time` function. +@external(erlang, "glua_stdlib_ffi", "os_time") +pub fn time() -> Value + +/// Returns the `os.tmpname` function. +@external(erlang, "glua_stdlib_ffi", "os_tmpname") +pub fn tmpname() -> Value diff --git a/src/glua/lib/package.gleam b/src/glua/lib/package.gleam new file mode 100644 index 0000000..4d10838 --- /dev/null +++ b/src/glua/lib/package.gleam @@ -0,0 +1,15 @@ +//// Bindings to the functions under `_G.package` + +import glua.{type Value} + +/// Returns the `package.searchpath` function. +@external(erlang, "glua_stdlib_ffi", "package_searchpath") +pub fn search_path() -> Value + +/// Returns the `package.searchers[1]` function. +@external(erlang, "glua_stdlib_ffi", "package_preload_searcher") +pub fn preload_searcher() -> Value + +/// Returns the `package.searchers[2]` function. +@external(erlang, "glua_stdlib_ffi", "package_lua_searcher") +pub fn lua_searcher() -> Value diff --git a/src/glua/lib/require.gleam b/src/glua/lib/require.gleam new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/glua/lib/require.gleam @@ -0,0 +1 @@ + diff --git a/src/glua/lib/string.gleam b/src/glua/lib/string.gleam new file mode 100644 index 0000000..8e8cac2 --- /dev/null +++ b/src/glua/lib/string.gleam @@ -0,0 +1,59 @@ +//// The all the functions under `_G.string` + +import glua.{type Value} + +/// Returns the `string.byte` function. +@external(erlang, "glua_stdlib_ffi", "string_byte") +pub fn byte() -> Value + +/// Returns the `string.char` function. +@external(erlang, "glua_stdlib_ffi", "string_char") +pub fn char() -> Value + +/// Returns the `string.dump` function. +@external(erlang, "glua_stdlib_ffi", "string_dump") +pub fn dump() -> Value + +/// Returns the `string.find` function. +@external(erlang, "glua_stdlib_ffi", "string_find") +pub fn find() -> Value + +/// Returns the `string.format` function. +@external(erlang, "glua_stdlib_ffi", "string_format") +pub fn format() -> Value + +/// Returns the `string.gmatch` function. +@external(erlang, "glua_stdlib_ffi", "string_gmatch") +pub fn gmatch() -> Value + +/// Returns the `string.gsub` function. +@external(erlang, "glua_stdlib_ffi", "string_gsub") +pub fn gsub() -> Value + +/// Returns the `string.len` function. +@external(erlang, "glua_stdlib_ffi", "string_len") +pub fn len() -> Value + +/// Returns the `string.lower` function. +@external(erlang, "glua_stdlib_ffi", "string_lower") +pub fn lower() -> Value + +/// Returns the `string.match` function. +@external(erlang, "glua_stdlib_ffi", "string_match") +pub fn match() -> Value + +/// Returns the `string.rep` function. +@external(erlang, "glua_stdlib_ffi", "string_rep") +pub fn rep() -> Value + +/// Returns the `string.reverse` function. +@external(erlang, "glua_stdlib_ffi", "string_reverse") +pub fn reverse() -> Value + +/// Returns the `string.sub` function. +@external(erlang, "glua_stdlib_ffi", "string_sub") +pub fn sub() -> Value + +/// Returns the `string.upper` function. +@external(erlang, "glua_stdlib_ffi", "string_upper") +pub fn upper() -> Value diff --git a/src/glua/lib/table.gleam b/src/glua/lib/table.gleam new file mode 100644 index 0000000..88d1dea --- /dev/null +++ b/src/glua/lib/table.gleam @@ -0,0 +1,27 @@ +//// Bindings to the functions under `_G.table` + +import glua.{type Value} + +/// Returns the `table.concat` function. +@external(erlang, "glua_stdlib_ffi", "table_concat") +pub fn concat() -> Value + +/// Returns the `table.insert` function. +@external(erlang, "glua_stdlib_ffi", "table_insert") +pub fn insert() -> Value + +/// Returns the `table.pack` function. +@external(erlang, "glua_stdlib_ffi", "table_pack") +pub fn pack() -> Value + +/// Returns the `table.remove` function. +@external(erlang, "glua_stdlib_ffi", "table_remove") +pub fn remove() -> Value + +/// Returns the `table.sort` function. +@external(erlang, "glua_stdlib_ffi", "table_sort") +pub fn sort() -> Value + +/// Returns the `table.unpack` function. +@external(erlang, "glua_stdlib_ffi", "table_unpack") +pub fn unpack() -> Value diff --git a/src/glua/lib/utf8.gleam b/src/glua/lib/utf8.gleam new file mode 100644 index 0000000..e4b53c5 --- /dev/null +++ b/src/glua/lib/utf8.gleam @@ -0,0 +1,23 @@ +//// Bindings to the functions under `_G.utf8` + +import glua.{type Value} + +/// Returns the `utf8.char` function. +@external(erlang, "glua_stdlib_ffi", "utf8_char") +pub fn char() -> Value + +/// Returns the `utf8.codes` function. +@external(erlang, "glua_stdlib_ffi", "utf8_codes") +pub fn codes() -> Value + +/// Returns the `utf8.codepoint` function. +@external(erlang, "glua_stdlib_ffi", "utf8_codepoint") +pub fn codepoint() -> Value + +/// Returns the `utf8.len` function. +@external(erlang, "glua_stdlib_ffi", "utf8_len") +pub fn len() -> Value + +/// Returns the `utf8.offset` function. +@external(erlang, "glua_stdlib_ffi", "utf8_offset") +pub fn offset() -> Value diff --git a/src/glua_stdlib_ffi.erl b/src/glua_stdlib_ffi.erl new file mode 100644 index 0000000..6335fc1 --- /dev/null +++ b/src/glua_stdlib_ffi.erl @@ -0,0 +1,522 @@ +-module(glua_stdlib_ffi). + +-include_lib("luerl/include/luerl.hrl"). + +-export([ + %% luerl_lib_basic (top-level) + assert/0, + collectgarbage/0, + dofile/0, + eprint/0, + error/0, + getmetatable/0, + ipairs/0, + load/0, + loadfile/0, + loadstring/0, + next/0, + pairs/0, + pcall/0, + print/0, + rawequal/0, + rawget/0, + rawlen/0, + rawset/0, + select/0, + setmetatable/0, + tonumber/0, + tostring/0, + type/0, + unpack/0, + + %% luerl_lib_package + require/0, + package_searchpath/0, + package_preload_searcher/0, + package_lua_searcher/0, + + %% luerl_lib_math + math_abs/0, + math_acos/0, + math_asin/0, + math_atan/0, + math_atan2/0, + math_ceil/0, + math_cos/0, + math_cosh/0, + math_deg/0, + math_exp/0, + math_floor/0, + math_fmod/0, + math_frexp/0, + math_ldexp/0, + math_log/0, + math_log10/0, + math_max/0, + math_min/0, + math_modf/0, + math_pow/0, + math_rad/0, + math_random/0, + math_randomseed/0, + math_sin/0, + math_sinh/0, + math_sqrt/0, + math_tan/0, + math_tanh/0, + math_tointeger/0, + math_type/0, + + %% luerl_lib_os + os_clock/0, + os_date/0, + os_difftime/0, + os_execute/0, + os_exit/0, + os_getenv/0, + os_remove/0, + os_rename/0, + os_time/0, + os_tmpname/0, + + %% luerl_lib_string + string_byte/0, + string_char/0, + string_dump/0, + string_find/0, + string_format/0, + string_gmatch/0, + string_gsub/0, + string_len/0, + string_lower/0, + string_match/0, + string_rep/0, + string_reverse/0, + string_sub/0, + string_upper/0, + + %% luerl_lib_utf8 + utf8_char/0, + utf8_codes/0, + utf8_codepoint/0, + utf8_len/0, + utf8_offset/0, + + %% luerl_lib_table + table_concat/0, + table_insert/0, + table_pack/0, + table_remove/0, + table_sort/0, + table_unpack/0, + + %% luerl_lib_debug + debug_getmetatable/0, + debug_getuservalue/0, + debug_setmetatable/0, + debug_setuservalue/0, + + %% luerl_lib_bit32 + bit32_band/0, + bit32_bnot/0, + bit32_bor/0, + bit32_btest/0, + bit32_bxor/0, + bit32_lshift/0, + bit32_rshift/0, + bit32_arshift/0, + bit32_lrotate/0, + bit32_rrotate/0, + bit32_extract/0, + bit32_replace/0, + + %% luerl_lib_io + io_flush/0, + io_write/0 +]). + +%% ------------------------------------------------------------------ +%% luerl_lib_basic (top level) +%% ------------------------------------------------------------------ + +assert() -> + #erl_mfa{m=luerl_lib_basic,f=assert}. + +collectgarbage() -> + #erl_mfa{m=luerl_lib_basic,f=collectgarbage}. + +dofile() -> + #erl_mfa{m=luerl_lib_basic,f=dofile}. + +eprint() -> + #erl_mfa{m=luerl_lib_basic,f=eprint}. + +error() -> + #erl_mfa{m=luerl_lib_basic,f=error_call}. + +getmetatable() -> + #erl_mfa{m=luerl_lib_basic,f=getmetatable}. + +ipairs() -> + #erl_mfa{m=luerl_lib_basic,f=ipairs}. + +load() -> + #erl_mfa{m=luerl_lib_basic,f=load}. + +loadfile() -> + #erl_mfa{m=luerl_lib_basic,f=loadfile}. + +%% For Lua 5.1 compatibility +loadstring() -> + #erl_mfa{m=luerl_lib_basic,f=loadstring}. + +next() -> + #erl_mfa{m=luerl_lib_basic,f=next}. + +pairs() -> + #erl_mfa{m=luerl_lib_basic,f=pairs}. + +pcall() -> + #erl_mfa{m=luerl_lib_basic,f=pcall}. + +print() -> + #erl_mfa{m=luerl_lib_basic,f=print}. + +rawequal() -> + #erl_mfa{m=luerl_lib_basic,f=rawequal}. + +rawget() -> + #erl_mfa{m=luerl_lib_basic,f=rawget}. + +rawlen() -> + #erl_mfa{m=luerl_lib_basic,f=rawlen}. + +rawset() -> + #erl_mfa{m=luerl_lib_basic,f=rawset}. + +select() -> + #erl_mfa{m=luerl_lib_basic,f=select}. + +setmetatable() -> + #erl_mfa{m=luerl_lib_basic,f=setmetatable}. + +tonumber() -> + #erl_mfa{m=luerl_lib_basic,f=tonumber}. + +tostring() -> + #erl_mfa{m=luerl_lib_basic,f=tostring}. + +type() -> + #erl_mfa{m=luerl_lib_basic,f=type}. + +%% For Lua 5.1 compatibility +unpack() -> + #erl_mfa{m=luerl_lib_basic,f=unpack}. + +%% ------------------------------------------------------------------ +%% luerl_lib_package +%% ------------------------------------------------------------------ + +require() -> + #erl_mfa{m=luerl_lib_package,f=require}. + +package_searchpath() -> + #erl_mfa{m=luerl_lib_package,f=searchpath}. + +%% package.searchers[1] +package_preload_searcher() -> + #erl_mfa{m=luerl_lib_package,f=preload_searcher}. + +%% package.searchers[2] +package_lua_searcher() -> + #erl_mfa{m=luerl_lib_package,f=lua_searcher}. + + +%% ------------------------------------------------------------------ +%% luerl_lib_math +%% ------------------------------------------------------------------ + +math_abs() -> + #erl_mfa{m=luerl_lib_math,f=abs}. + +math_acos() -> + #erl_mfa{m=luerl_lib_math,f=acos}. + +math_asin() -> + #erl_mfa{m=luerl_lib_math,f=asin}. + +math_atan() -> + #erl_mfa{m=luerl_lib_math,f=atan}. + +%% Deprecated: For 5.2 backwards compatibility +math_atan2() -> + #erl_mfa{m=luerl_lib_math,f=atan2}. + +math_ceil() -> + #erl_mfa{m=luerl_lib_math,f=ceil}. + +math_cos() -> + #erl_mfa{m=luerl_lib_math,f=cos}. + +%% For 5.2 backwards compatibility +math_cosh() -> + #erl_mfa{m=luerl_lib_math,f=cosh}. + +math_deg() -> + #erl_mfa{m=luerl_lib_math,f=deg}. + +math_exp() -> + #erl_mfa{m=luerl_lib_math,f=exp}. + +math_floor() -> + #erl_mfa{m=luerl_lib_math,f=floor}. + +math_fmod() -> + #erl_mfa{m=luerl_lib_math,f=fmod}. + +%% For 5.2 backwards compatibility +math_frexp() -> + #erl_mfa{m=luerl_lib_math,f=frexp}. + +%% For 5.2 backwards compatibility +math_ldexp() -> + #erl_mfa{m=luerl_lib_math,f=ldexp}. + +math_log() -> + #erl_mfa{m=luerl_lib_math,f=log}. + +%% For 5.1 backwards compatibility +math_log10() -> + #erl_mfa{m=luerl_lib_math,f=log10}. + +math_max() -> + #erl_mfa{m=luerl_lib_math,f=max}. + +math_min() -> + #erl_mfa{m=luerl_lib_math,f=min}. + +math_modf() -> + #erl_mfa{m=luerl_lib_math,f=modf}. + +math_pow() -> + #erl_mfa{m=luerl_lib_math,f=pow}. + +math_rad() -> + #erl_mfa{m=luerl_lib_math,f=rad}. + +math_random() -> + #erl_mfa{m=luerl_lib_math,f=random}. + +math_randomseed() -> + #erl_mfa{m=luerl_lib_math,f=randomseed}. + +math_sin() -> + #erl_mfa{m=luerl_lib_math,f=sin}. + +%% For 5.2 backwards compatibility +math_sinh() -> + #erl_mfa{m=luerl_lib_math,f=sinh}. + +math_sqrt() -> + #erl_mfa{m=luerl_lib_math,f=sqrt}. + +math_tan() -> + #erl_mfa{m=luerl_lib_math,f=tan}. + +%% For 5.2 backwards compatibility +math_tanh() -> + #erl_mfa{m=luerl_lib_math,f=tanh}. + +math_tointeger() -> + #erl_mfa{m=luerl_lib_math,f=tointeger}. + +math_type() -> + #erl_mfa{m=luerl_lib_math,f=type}. + +%% ------------------------------------------------------------------ +%% luerl_lib_os +%% ------------------------------------------------------------------ + +os_clock() -> + #erl_mfa{m=luerl_lib_os,f=clock}. + +os_date() -> + #erl_mfa{m=luerl_lib_os,f=date}. + +os_difftime() -> + #erl_mfa{m=luerl_lib_os,f=difftime}. + +os_execute() -> + #erl_mfa{m=luerl_lib_os,f=execute}. + +os_exit() -> + #erl_mfa{m=luerl_lib_os,f=lua_exit}. + +os_getenv() -> + #erl_mfa{m=luerl_lib_os,f=getenv}. + +os_remove() -> + #erl_mfa{m=luerl_lib_os,f=remove}. + +os_rename() -> + #erl_mfa{m=luerl_lib_os,f=rename}. + +os_time() -> + #erl_mfa{m=luerl_lib_os,f=time}. + +os_tmpname() -> + #erl_mfa{m=luerl_lib_os,f=tmpname}. + +%% ------------------------------------------------------------------ +%% luerl_lib_string +%% ------------------------------------------------------------------ + +string_byte() -> + #erl_mfa{m=luerl_lib_string,f=byte}. + +string_char() -> + #erl_mfa{m=luerl_lib_string,f=char}. + +string_dump() -> + #erl_mfa{m=luerl_lib_string,f=dump}. + +string_find() -> + #erl_mfa{m=luerl_lib_string,f=find}. + +string_format() -> + #erl_mfa{m=luerl_lib_string,f=format}. + +string_gmatch() -> + #erl_mfa{m=luerl_lib_string,f=gmatch}. + +string_gsub() -> + #erl_mfa{m=luerl_lib_string,f=gsub}. + +string_len() -> + #erl_mfa{m=luerl_lib_string,f=len}. + +string_lower() -> + #erl_mfa{m=luerl_lib_string,f=lower}. + +string_match() -> + #erl_mfa{m=luerl_lib_string,f=match}. + +string_rep() -> + #erl_mfa{m=luerl_lib_string,f=rep}. + +string_reverse() -> + #erl_mfa{m=luerl_lib_string,f=reverse}. + +string_sub() -> + #erl_mfa{m=luerl_lib_string,f=sub}. + +string_upper() -> + #erl_mfa{m=luerl_lib_string,f=upper}. + +%% ------------------------------------------------------------------ +%% luerl_lib_utf8 +%% ------------------------------------------------------------------ + +utf8_char() -> + #erl_mfa{m=luerl_lib_utf8,f=utf8_char}. + +utf8_codes() -> + #erl_mfa{m=luerl_lib_utf8,f=codes}. + +utf8_codepoint() -> + #erl_mfa{m=luerl_lib_utf8,f=codepoint}. + +utf8_len() -> + #erl_mfa{m=luerl_lib_utf8,f=utf8_len}. + +utf8_offset() -> + #erl_mfa{m=luerl_lib_utf8,f=offset}. + +%% ------------------------------------------------------------------ +%% luerl_lib_table +%% ------------------------------------------------------------------ + +table_concat() -> + #erl_mfa{m=luerl_lib_table,f=concat}. + +table_insert() -> + #erl_mfa{m=luerl_lib_table,f=insert}. + +table_pack() -> + #erl_mfa{m=luerl_lib_table,f=pack}. + +table_remove() -> + #erl_mfa{m=luerl_lib_table,f=remove}. + +table_sort() -> + #erl_mfa{m=luerl_lib_table,f=sort}. + +table_unpack() -> + #erl_mfa{m=luerl_lib_table,f=unpack}. + +%% ------------------------------------------------------------------ +%% luerl_lib_debug +%% ------------------------------------------------------------------ + +debug_getmetatable() -> + #erl_mfa{m=luerl_lib_debug,f=getmetatable}. + +debug_getuservalue() -> + #erl_mfa{m=luerl_lib_debug,f=getuservalue}. + +debug_setmetatable() -> + #erl_mfa{m=luerl_lib_debug,f=setmetatable}. + +debug_setuservalue() -> + #erl_mfa{m=luerl_lib_debug,f=setuservalue}. + +%% ------------------------------------------------------------------ +%% luerl_lib_bit32 +%% ------------------------------------------------------------------ + +bit32_band() -> + #erl_mfa{m=luerl_lib_bit32,f=fband}. + +bit32_bnot() -> + #erl_mfa{m=luerl_lib_bit32,f=fbnot}. + +bit32_bor() -> + #erl_mfa{m=luerl_lib_bit32,f=fbor}. + +bit32_btest() -> + #erl_mfa{m=luerl_lib_bit32,f=fbtest}. + +bit32_bxor() -> + #erl_mfa{m=luerl_lib_bit32,f=fbxor}. + +bit32_lshift() -> + #erl_mfa{m=luerl_lib_bit32,f=flshift}. + +bit32_rshift() -> + #erl_mfa{m=luerl_lib_bit32,f=frshift}. + +bit32_arshift() -> + #erl_mfa{m=luerl_lib_bit32,f=farshift}. + +bit32_lrotate() -> + #erl_mfa{m=luerl_lib_bit32,f=flrotate}. + +bit32_rrotate() -> + #erl_mfa{m=luerl_lib_bit32,f=frrotate}. + +bit32_extract() -> + #erl_mfa{m=luerl_lib_bit32,f=fextract}. + +bit32_replace() -> + #erl_mfa{m=luerl_lib_bit32,f=freplace}. + +%% ------------------------------------------------------------------ +%% luerl_lib_io +%% ------------------------------------------------------------------ + +io_flush() -> + #erl_mfa{m=luerl_lib_io,f=flush}. + +io_write() -> + #erl_mfa{m=luerl_lib_io,f=write}. + diff --git a/test/lib_test.gleam b/test/lib_test.gleam new file mode 100644 index 0000000..fecab2a --- /dev/null +++ b/test/lib_test.gleam @@ -0,0 +1,170 @@ +import glua +import glua/lib +import glua/lib/bit32 +import glua/lib/debug +import glua/lib/io +import glua/lib/math +import glua/lib/os +import glua/lib/package +import glua/lib/string +import glua/lib/table +import glua/lib/utf8 + +/// Checks to see if a value matches a value at the given path. +fn check(val: glua.Value, path: List(String)) { + let assert Ok(found) = glua.get(glua.new(), path) + assert val == found +} + +pub fn lib_basic_test() { + check(lib.assert_(), ["assert"]) + check(lib.collect_garbage(), ["collectgarbage"]) + check(lib.do_file(), ["dofile"]) + check(lib.eprint(), ["eprint"]) + check(lib.error(), ["error"]) + check(lib.get_meta_table(), ["getmetatable"]) + check(lib.ipairs(), ["ipairs"]) + check(lib.load(), ["load"]) + check(lib.load_file(), ["loadfile"]) + check(lib.load_string(), ["loadstring"]) + check(lib.next(), ["next"]) + check(lib.pairs(), ["pairs"]) + check(lib.pcall(), ["pcall"]) + check(lib.print(), ["print"]) + check(lib.raw_equal(), ["rawequal"]) + check(lib.raw_get(), ["rawget"]) + check(lib.raw_len(), ["rawlen"]) + check(lib.raw_set(), ["rawset"]) + check(lib.select(), ["select"]) + check(lib.set_meta_table(), ["setmetatable"]) + check(lib.to_number(), ["tonumber"]) + check(lib.to_string(), ["tostring"]) + check(lib.type_(), ["type"]) + check(lib.unpack(), ["unpack"]) + check(lib.require(), ["require"]) +} + +pub fn lib_package_test() { + // luerl:get_table_keys/2 uses ints to index lists + check(package.preload_searcher(), ["package", "searchers", coerce_string(1)]) + check(package.lua_searcher(), [ + "package", + "searchers", + coerce_string(2), + ]) + check(package.search_path(), ["package", "searchpath"]) +} + +@external(erlang, "glua_ffi", "coerce") +fn coerce_string(item: Int) -> String + +pub fn lib_bit32_test() { + check(bit32.band(), ["bit32", "band"]) + check(bit32.bnot(), ["bit32", "bnot"]) + check(bit32.bor(), ["bit32", "bor"]) + check(bit32.btest(), ["bit32", "btest"]) + check(bit32.bxor(), ["bit32", "bxor"]) + check(bit32.lshift(), ["bit32", "lshift"]) + check(bit32.rshift(), ["bit32", "rshift"]) + check(bit32.arshift(), ["bit32", "arshift"]) + check(bit32.lrotate(), ["bit32", "lrotate"]) + check(bit32.rrotate(), ["bit32", "rrotate"]) + check(bit32.extract(), ["bit32", "extract"]) + check(bit32.replace(), ["bit32", "replace"]) +} + +pub fn lib_io_test() { + check(io.flush(), ["io", "flush"]) + check(io.write(), ["io", "write"]) +} + +pub fn lib_math_test() { + check(glua.float(math.huge), ["math", "huge"]) + check(glua.float(math.pi), ["math", "pi"]) + check(glua.int(math.max_integer), ["math", "maxinteger"]) + check(glua.int(math.min_integer), ["math", "mininteger"]) + check(math.abs(), ["math", "abs"]) + check(math.acos(), ["math", "acos"]) + check(math.asin(), ["math", "asin"]) + check(math.atan(), ["math", "atan"]) + check(math.atan2(), ["math", "atan2"]) + check(math.ceil(), ["math", "ceil"]) + check(math.cos(), ["math", "cos"]) + check(math.cosh(), ["math", "cosh"]) + check(math.deg(), ["math", "deg"]) + check(math.exp(), ["math", "exp"]) + check(math.floor(), ["math", "floor"]) + check(math.fmod(), ["math", "fmod"]) + check(math.frexp(), ["math", "frexp"]) + check(math.ldexp(), ["math", "ldexp"]) + check(math.log(), ["math", "log"]) + check(math.log10(), ["math", "log10"]) + check(math.max(), ["math", "max"]) + check(math.min(), ["math", "min"]) + check(math.modf(), ["math", "modf"]) + check(math.pow(), ["math", "pow"]) + check(math.rad(), ["math", "rad"]) + check(math.random(), ["math", "random"]) + check(math.random_seed(), ["math", "randomseed"]) + check(math.sin(), ["math", "sin"]) + check(math.sinh(), ["math", "sinh"]) + check(math.sqrt(), ["math", "sqrt"]) + check(math.tan(), ["math", "tan"]) + check(math.tanh(), ["math", "tanh"]) + check(math.to_integer(), ["math", "tointeger"]) + check(math.type_(), ["math", "type"]) +} + +pub fn lib_debug_test() { + check(debug.get_meta_table(), ["debug", "getmetatable"]) + check(debug.get_user_value(), ["debug", "getuservalue"]) + check(debug.set_meta_table(), ["debug", "setmetatable"]) + check(debug.set_user_value(), ["debug", "setuservalue"]) +} + +pub fn lib_table_test() { + check(table.concat(), ["table", "concat"]) + check(table.insert(), ["table", "insert"]) + check(table.pack(), ["table", "pack"]) + check(table.remove(), ["table", "remove"]) + check(table.sort(), ["table", "sort"]) + check(table.unpack(), ["table", "unpack"]) +} + +pub fn lib_utf8_test() { + check(utf8.char(), ["utf8", "char"]) + check(utf8.codes(), ["utf8", "codes"]) + check(utf8.codepoint(), ["utf8", "codepoint"]) + check(utf8.len(), ["utf8", "len"]) + check(utf8.offset(), ["utf8", "offset"]) +} + +pub fn lib_string_test() { + check(string.byte(), ["string", "byte"]) + check(string.char(), ["string", "char"]) + check(string.dump(), ["string", "dump"]) + check(string.find(), ["string", "find"]) + check(string.format(), ["string", "format"]) + check(string.gmatch(), ["string", "gmatch"]) + check(string.gsub(), ["string", "gsub"]) + check(string.len(), ["string", "len"]) + check(string.lower(), ["string", "lower"]) + check(string.match(), ["string", "match"]) + check(string.rep(), ["string", "rep"]) + check(string.reverse(), ["string", "reverse"]) + check(string.sub(), ["string", "sub"]) + check(string.upper(), ["string", "upper"]) +} + +pub fn lib_os_test() { + check(os.clock(), ["os", "clock"]) + check(os.date(), ["os", "date"]) + check(os.difftime(), ["os", "difftime"]) + check(os.execute(), ["os", "execute"]) + check(os.exit(), ["os", "exit"]) + check(os.getenv(), ["os", "getenv"]) + check(os.remove(), ["os", "remove"]) + check(os.rename(), ["os", "rename"]) + check(os.time(), ["os", "time"]) + check(os.tmpname(), ["os", "tmpname"]) +}