initial commit
This commit is contained in:
54
CMakeLists.txt
Normal file
54
CMakeLists.txt
Normal file
@@ -0,0 +1,54 @@
|
||||
cmake_minimum_required(VERSION 3.18..3.28)
|
||||
project(ReScript
|
||||
VERSION 1.1
|
||||
LANGUAGES CXX)
|
||||
|
||||
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||
message(FATAL_ERROR "In-source builds are not allowed!")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
# Enable Package Managers
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
file(GLOB_RECURSE ReScript_HEADERS "include/ReScript/*.hpp" "include/ReScript/*.h")
|
||||
file(GLOB_RECURSE ReScript_SRC "src/ReScript/*.cpp" "src/ReScript/*.c")
|
||||
|
||||
include_directories("include")
|
||||
|
||||
if (UNIX)
|
||||
add_library(ReScript SHARED ${ReScript_SRC})
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
add_library(ReScript STATIC ${ReScript_SRC})
|
||||
endif()
|
||||
|
||||
set_target_properties(ReScript PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
FIND_PACKAGE(Lua 5.3 REQUIRED)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Event
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-12.zip
|
||||
)
|
||||
|
||||
|
||||
target_include_directories(ReScript PUBLIC ${Event_SOURCE_DIR}/include)
|
||||
target_include_directories(ReScript PUBLIC ${jlog_SOURCE_DIR}/include)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
|
||||
install(FILES ${ReScript_HEADERS} DESTINATION include/${PROJECT_NAME})
|
||||
|
||||
target_link_libraries(ReScript PUBLIC Event jlog ${LUA_LIBRARY})
|
||||
|
||||
add_executable(RedactedReScriptDemo main.cpp)
|
||||
target_link_libraries(RedactedReScriptDemo PUBLIC ReScript)
|
36
include/ReScript/luaengine.hpp
Normal file
36
include/ReScript/luaengine.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <lua5.3/lua.hpp>
|
||||
|
||||
#pragma once
|
||||
|
||||
template <typename Val>
|
||||
class LuaValue {
|
||||
protected:
|
||||
Val value;
|
||||
public:
|
||||
LuaValue() {};
|
||||
LuaValue(Val value) { value = value; };
|
||||
Val Value() { return value; };
|
||||
virtual void Push(lua_State *l) = 0;
|
||||
virtual void Pull(lua_State *l, int index) = 0;
|
||||
};
|
||||
|
||||
class LuaNumber : public LuaValue<double> {
|
||||
public:
|
||||
LuaNumber() : LuaValue() {};
|
||||
LuaNumber(double value) : LuaValue<double>(value) {};
|
||||
void Push(lua_State *l) override { lua_pushnumber(l, value); };
|
||||
void Pull(lua_State *l, int index) override { value = luaL_checknumber(l, index); };
|
||||
};
|
||||
|
||||
|
||||
class LuaEngine {
|
||||
public:
|
||||
lua_State *L;
|
||||
LuaEngine() { L = luaL_newstate(); luaL_openlibs(L); };
|
||||
template<typename Val>
|
||||
void Push(Val lv) { lv.Push(L); };
|
||||
template<typename Val>
|
||||
Val Pull(int index) { Val v; v.Pull(L, index); return v; };
|
||||
bool LoadString(const char *str) { return luaL_loadstring(L, str); };
|
||||
|
||||
};
|
42
main.cpp
Normal file
42
main.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "ReScript/luaengine.hpp"
|
||||
#include <cmath>
|
||||
#include <errno.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
|
||||
static int l_sin (lua_State *L) {
|
||||
//lua_Number d = luaL_checknumber(L, 1);
|
||||
//DEBUG(std::to_string(d))
|
||||
//lua_pushnumber(L, sin(d));
|
||||
LuaNumber LN = LuaNumber();
|
||||
LN.Pull(L, 1);
|
||||
LuaNumber LN2 = LN.Value();
|
||||
//LN2.Push(L);
|
||||
DEBUG(std::to_string(LN.Value()));
|
||||
DEBUG(std::to_string(LN2.Value()));
|
||||
DEBUG(std::to_string(sin(LN.Value())));
|
||||
lua_pushnumber(L, LN2.Value());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char buff[256];
|
||||
int error;
|
||||
//lua_State *L = luaL_newstate();
|
||||
//luaL_openlibs(L);
|
||||
LuaEngine LE = LuaEngine();
|
||||
|
||||
|
||||
lua_pushcfunction(LE.L, l_sin);
|
||||
lua_setglobal(LE.L, "mysin");
|
||||
|
||||
while(fgets(buff, sizeof(buff), stdin) != NULL) {
|
||||
error = LE.LoadString(buff) || lua_pcall(LE.L, 0, 0, 0);
|
||||
if (error) {
|
||||
fprintf(stderr, "%s\n", lua_tostring(LE.L, -1));
|
||||
lua_pop(LE.L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
lua_close(LE.L);
|
||||
return 0;
|
||||
}
|
1
src/ReScript/luaengine.cpp
Normal file
1
src/ReScript/luaengine.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "ReScript/luaengine.hpp"
|
Reference in New Issue
Block a user