very very very very basic lua handling/mini-wrapper

This commit is contained in:
hippoz 2021-02-14 19:38:23 +02:00
parent 1ff88d6729
commit 3c721a4c1b
No known key found for this signature in database
GPG key ID: 7C52899193467641
10 changed files with 104 additions and 4 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
bin/
obj/

39
Makefile Normal file
View file

@ -0,0 +1,39 @@
LUA_VERSION := 5.3
LUA = lua$(LUA_VERSION)
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
EXE := $(BIN_DIR)/out
OBJ_OUT_DIRS := obj/Main obj/LuaHandler
SRC := $(SRC_DIR)/Main/Main.cpp $(SRC_DIR)/LuaHandler/LuaHandler.cpp
OBJ := $(OBJ_DIR)/Main/Main.o $(OBJ_DIR)/LuaHandler/LuaHandler.o
CC := g++
CPPFLAGS := -I/usr/include/$(LUA) -Iinclude -MMD -MP
CFLAGS := -Wall
LDFLAGS :=
LDLIBS := -l$(LUA) -ldl
.PHONY: all clean run
all: $(EXE)
run: $(EXE)
@echo -e "\033[1m---> \033[1;35mRunning binary from $(EXE)\033[0m"
@./$(EXE)
$(EXE): $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR) $(OBJ_OUT_DIRS)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
$(BIN_DIR) $(OBJ_DIR) $(OBJ_OUT_DIRS):
mkdir -p $@
clean:
@$(RM) -rv $(BIN_DIR) $(OBJ_DIR)
-include $(OBJ:.o=.d)

View file

@ -1,3 +1,4 @@
# game # game
yes ## dependencies
- lua5.3

1
Script.lua Normal file
View file

@ -0,0 +1 @@
libepic.say_something("lol")

View file

@ -1 +0,0 @@
git please

View file

@ -0,0 +1,18 @@
#ifndef _LUAHANDLER_H
#define _LUAHANDLER_H
extern "C"{
#include <lua.h>
#include <lauxlib.h>
}
struct LuaHandler {
lua_State* L;
LuaHandler(lua_State* L) : L(L) {};
void defineClass(const char* className);
void defineFunction(const char* className, const char* valueName, lua_CFunction func);
};
#endif

View file

@ -1 +0,0 @@
git please

View file

@ -0,0 +1,22 @@
#include <LuaHandler/LuaHandler.hpp>
void LuaHandler::defineClass(const char* className) {
// Create a new table and assign it to a global variable
lua_newtable(L);
lua_setglobal(L, className);
}
void LuaHandler::defineFunction(const char* className, const char* valueName, lua_CFunction func) {
// Get the table that should have been defined with LuaHandler::defineClass
lua_getglobal(L, className);
// Push the function name, as well as the function onto the stack
lua_pushstring(L, valueName);
lua_pushcfunction(L, func);
// Add the function inside the table, giving it the specified name
lua_settable(L, -3);
// Keep the stack balanced
lua_pop(L, 1);
}

20
src/Main/Main.cpp Normal file
View file

@ -0,0 +1,20 @@
#include <iostream>
#include <LuaHandler/LuaHandler.hpp>
static int say_something(lua_State* L) {
std::cout << "something: " << lua_tostring(L, 1) << "\n";
return 1;
}
int main() {
lua_State* L = luaL_newstate();
LuaHandler handler(L);
handler.defineClass("libepic");
handler.defineFunction("libepic", "say_something", &say_something);
luaL_dofile(handler.L, "Script.lua");
return 0;
}

View file

@ -1 +0,0 @@
git please