63 lines
1.4 KiB
C
63 lines
1.4 KiB
C
#include <lauxlib.h>
|
|
#include <lua.h>
|
|
#include <lualib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
float x, y, z;
|
|
} Vec3;
|
|
|
|
Vec3 table_to_vec3(lua_State *L, int index) {
|
|
Vec3 v;
|
|
luaL_checktype(L, index, LUA_TTABLE);
|
|
|
|
lua_getfield(L, index, "x");
|
|
v.x = luaL_checknumber(L, -1);
|
|
lua_pop(L, 1);
|
|
|
|
lua_getfield(L, index, "y");
|
|
v.y = luaL_checknumber(L, -1);
|
|
lua_pop(L, 1);
|
|
|
|
lua_getfield(L, index, "z");
|
|
v.z = luaL_checknumber(L, -1);
|
|
lua_pop(L, 1);
|
|
|
|
return v;
|
|
}
|
|
|
|
int printvec(lua_State *L) {
|
|
Vec3 v = table_to_vec3(L, 1);
|
|
// lua_pushnumber(L, a + b);
|
|
printf("{ X: %.1f, Y: %.1f, Z: %.1f }\n", v.x, v.y, v.z);
|
|
return 0; // no return values
|
|
}
|
|
|
|
int main(void) {
|
|
// Vec3 pos = {1.0, 2.0, 3.0};
|
|
|
|
// printf("{ X: %.1f, Y: %.1f, Z: %.1f }\n", pos.x, pos.y, pos.z);
|
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
luaL_openlibs(L); // Load Lua standard libraries
|
|
// luaL_requiref(L, "_G", luaopen_base, 1);
|
|
|
|
// const lua_Number ver = lua_version(NULL);
|
|
|
|
// printf("Lua version: %.2f\n", ver);
|
|
// printf("Lua version: %s\n", LUA_VERSION);
|
|
|
|
// Register the C function as a global Lua function
|
|
lua_register(L, "printvec", printvec);
|
|
|
|
// Run the Lua script
|
|
if (luaL_dofile(L, "script.lua") != LUA_OK) {
|
|
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
|
|
lua_pop(L, 1); // remove error message
|
|
}
|
|
|
|
lua_close(L);
|
|
return 0;
|
|
}
|