Skip to content

Instantly share code, notes, and snippets.

@xxMrPHDxx
Last active June 6, 2020 16:23
Show Gist options
  • Save xxMrPHDxx/33e8c12cde400c6c0ee376a6ac25bd05 to your computer and use it in GitHub Desktop.
Save xxMrPHDxx/33e8c12cde400c6c0ee376a6ac25bd05 to your computer and use it in GitHub Desktop.
Lua test 1
local v1 = Vector:new(3, 7)
local v2 = Vector:new(4, 9)
print("Vector 1: x = " .. v1.x .. ", y = " .. v1.y);
print("Vector 2: x = " .. v2.x .. ", y = " .. v2.y);
print(v1.get_x);
print(v1.get_x());
--print("Method Vector 1 get_x: " .. Vector:get_x(v1));
--print("Method Vector 2 get_x: " .. Vector:get_x(v2));
--print("Method Vector 1 get_y: " .. Vector:get_y(v1));
--print("Method Vector 2 get_y: " .. Vector:get_y(v2));
#include <lua.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
struct Vector {
float x, y;
Vector(float _x, float _y) : x(_x), y(_y) {}
static int register_class(lua_State*);
static int create_vector(lua_State*);
static int get_x(lua_State*);
static int get_y(lua_State*);
};
int Vector::register_class(lua_State* L) {
luaL_Reg methods[] = {
{ "new" , create_vector }
};
const size_t N = sizeof(methods)/sizeof(luaL_Reg);
// Create empty table with __index = "Vector"
lua_newtable(L);
lua_pushstring(L, "Vector");
lua_setfield(L, -2, "__index");
lua_setglobal(L, "Vector");
// Assign methods
lua_getglobal(L, "Vector");
for (luaL_Reg* reg = &methods[0]; reg < &methods[N]; reg++) {
lua_pushcfunction(L, reg->func);
lua_setfield(L, -2, reg->name);
}
lua_pop(L, 1);
return 1;
}
int Vector::create_vector(lua_State* L) {
bool err = true, narg = 0;
if (lua_checkstack(L, 2)) {
if (lua_isnumber(L, -2) && lua_isnumber(L, -2)) {
float x = lua_tonumber(L, -2); // Get first argument
float y = lua_tonumber(L, -1); // Get second argument
lua_pop(L, 2); // Pop the two arguments
lua_newtable(L); // Create local this table metatable, it's on the top on the stack now
lua_pushnumber(L, x); // Push the x
lua_setfield(L, -2, "x"); // Set field "x" to x
lua_pushnumber(L, y); // Push the y
lua_setfield(L, -2, "y"); // Set field "y" to y
// Inserting function prototypes
luaL_Reg proto[] = {
{ "get_x", get_x },
{ "get_y", get_y }
};
const int N = sizeof(proto) / sizeof(luaL_Reg);
for (luaL_Reg* reg = &proto[0]; reg < &proto[N]; reg++) {
lua_pushcfunction(L, reg->func);
lua_setfield(L, -2, reg->name);
}
err = false;
}
else {
printf("Expecting Vector:new([number], [number]). Got (%s, %s)\n", lua_typename(L, -2), lua_typename(L, -1));
narg = 2;
}
}
else printf("Vector:new() requires 2 arguments. Got %i\n", lua_gettop(L));
if (err) {
lua_pop(L, narg); // Pop the two arguments
lua_pushnil(L); // Return nil instead
}
return 1;
}
int Vector::get_x(lua_State* L) {
if (lua_checkstack(L, 1) && lua_istable(L, -1)) {
lua_getfield(L, -1, "x");
}
else printf("Usage Vector:get_x([Vector]). %i\n", lua_gettop(L));
printf("Type: %s\n", lua_typename(L, -1));
// How can I get self here?
return 1;
}
int Vector::get_y(lua_State* L) {
if (lua_checkstack(L, 1) && lua_istable(L, -1)) {
lua_getfield(L, -1, "y");
}
else printf("Usage Vector:get_y([Vector])\n", lua_gettop(L));
return 1;
}
int main() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
Vector::register_class(L);
if (luaL_dofile(L, "test.lua")) {
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment