Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement lua-vec - vectors as native Lua datatype #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Pirulax wants to merge 7 commits into multitheftauto:master
base: master
Choose a base branch
Loading
from Pirulax:feature/native-vectors
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Client/mods/deathmatch/logic/lua/CLuaMain.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ void CLuaMain::InitVM()
// Set the instruction count hook
lua_sethook(m_luaVM, InstructionCountHook, LUA_MASKCOUNT, HOOK_INSTRUCTION_COUNT);

// Load LUA libraries
// Load Lua libraries
luaopen_base(m_luaVM);
luaopen_math(m_luaVM);
luaopen_string(m_luaVM);
luaopen_table(m_luaVM);
luaopen_debug(m_luaVM);
luaopen_utf8(m_luaVM);
luaopen_os(m_luaVM);
luaopen_vec(m_luaVM);

// Initialize security restrictions. Very important to prevent lua trojans and viruses!
InitSecurity();
Expand Down
3 changes: 2 additions & 1 deletion Server/mods/deathmatch/logic/lua/CLuaMain.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,15 @@ void CLuaMain::Initialize()
// Set the instruction count hook
lua_sethook(m_luaVM, InstructionCountHook, LUA_MASKCOUNT, HOOK_INSTRUCTION_COUNT);

// Load LUA libraries
// Load Lua libraries
luaopen_base(m_luaVM);
luaopen_math(m_luaVM);
luaopen_string(m_luaVM);
luaopen_table(m_luaVM);
luaopen_debug(m_luaVM);
luaopen_utf8(m_luaVM);
luaopen_os(m_luaVM);
luaopen_vec(m_luaVM);

// Initialize security restrictions. Very important to prevent lua trojans and viruses!
InitSecurity();
Expand Down
2 changes: 1 addition & 1 deletion vendor/lua/src/Makefile.std
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
lundump.o lvm.o lzio.o
LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
lstrlib.o loadlib.o linit.o
lstrlib.o loadlib.o linit.o lvector.o

LUA_T= lua$(SFX)
LUA_O= lua.o
Expand Down
20 changes: 16 additions & 4 deletions vendor/lua/src/lapi.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"

#include "lvector.h" /*LUA-VEC*/


const char lua_ident[] =
Expand Down Expand Up @@ -110,7 +110,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) {
LUA_API int lua_getstackgap (lua_State *L) {
int res, gap1, gap2;
lua_lock(L);
gap1 = ( (char *)L->stack_last - (char *)L->top ) / (int)sizeof(TValue);
gap1 = ( (char *)L->stack_last - (char *)L->top ) / sizeof(TValue);
gap2 = L->ci->top - L->top;
res = gap1 < gap2 ? gap1 : gap2;
lua_unlock(L);
Expand Down Expand Up @@ -437,7 +437,11 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
}
}


/* LUA-VEC */
LUA_API const float* lua_tovec(lua_State* L, int idx) {
StkId o = index2adr(L, idx);
return (!ttisvec(o)) ? NULL : vvalue(o)->vec;
}

/*
** push functions (C -> stack)
Expand Down Expand Up @@ -550,6 +554,14 @@ LUA_API int lua_pushthread (lua_State *L) {
return (G(L)->mainthread == L);
}

/* LUA-VEC */
LUA_API void lua_pushvec(lua_State* L, float x, float y, float z, float w) {
lua_lock(L);
luaC_checkGC(L);
setvvalue(L, L->top, luaVec_new(L, x, y, z, w));
api_incr_top(L);
lua_unlock(L);
}

/*
** get functions (Lua -> stack)
Expand Down Expand Up @@ -796,7 +808,7 @@ LUA_API int lua_setfenv (lua_State *L, int idx) {

#define checkresults(L,na,nr) \
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))

LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
StkId func;
Expand Down
12 changes: 12 additions & 0 deletions vendor/lua/src/lauxlib.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
return luaL_opt(L, luaL_checknumber, narg, def);
}

/* LUA-VEC */
LUALIB_API const float* luaL_checkvec(lua_State* L, int narg) {
const float* v = lua_tovec(L, narg);
if (v == 0 && !lua_isvec(L, narg)) /* avoid extra test when d is not 0 */
tag_error(L, narg, LUA_TVEC);
return v;
}

/* LUA-VEC */
LUALIB_API const float* luaL_optvec(lua_State* L, int narg, const float* def) {
return luaL_opt(L, luaL_checkvec, narg, def);
}

LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
lua_Integer d = lua_tointeger(L, narg);
Expand Down
8 changes: 5 additions & 3 deletions vendor/lua/src/lauxlib.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);

LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
lua_Integer def);
LUALIB_API const float *(luaL_checkvec)(lua_State* L, int numArg); /* LUA-VEC */
LUALIB_API const float *(luaL_optvec)(lua_State* L, int nArg, const float* def); /* LUA-VEC */

LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);

LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
Expand Down
6 changes: 6 additions & 0 deletions vendor/lua/src/lbaselib.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ static int luaB_tostring (lua_State *L) {
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
case LUA_TVEC: /* LUA-VEC */
{
const float* v = lua_tovec(L, 1);
lua_pushfstring(L, "vec(%f, %f, %f, %f)", v[0], v[1], v[2], v[3]);
}
break;
default:
lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
break;
Expand Down
9 changes: 9 additions & 0 deletions vendor/lua/src/lgc.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvector.h" /*LUA-VEC*/


#define GCSTEPSIZE 1024u
Expand Down Expand Up @@ -107,6 +108,10 @@ static void reallymarkobject (global_State *g, GCObject *o) {
g->gray = o;
break;
}
case LUA_TVEC: { /* LUA-VEC */
gray2black(o);
break;
}
default: lua_assert(0);
}
}
Expand Down Expand Up @@ -395,6 +400,10 @@ static void freeobj (lua_State *L, GCObject *o) {
luaM_freemem(L, o, sizeudata(gco2u(o)));
break;
}
case LUA_TVEC: { /* LUA-VEC */
luaVec_free(L, o);
break;
}
default: lua_assert(0);
}
}
Expand Down
3 changes: 1 addition & 2 deletions vendor/lua/src/linit.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ static const luaL_Reg lualibs[] = {
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_VECLIBNAME, luaopen_vec}, /* LUA-VEC */
{LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL}
};


LUALIB_API void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib = lualibs;
for (; lib->func; lib++) {
Expand All @@ -35,4 +35,3 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
lua_call(L, 1, 0);
}
}

18 changes: 15 additions & 3 deletions vendor/lua/src/lobject.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


/* tags for values visible from Lua */
#define LAST_TAG LUA_TTHREAD
#define LAST_TAG LUA_TVEC /* LUA_TTHREAD -- LUA-VEC */

#define NUM_TAGS (LAST_TAG+1)

Expand Down Expand Up @@ -85,6 +85,7 @@ typedef struct lua_TValue {
#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
#define ttisvec(o) (ttype(o) == LUA_TVEC) /* LUA-VEC */

/* Macros to access values */
#define ttype(o) ((o)->tt)
Expand All @@ -99,6 +100,7 @@ typedef struct lua_TValue {
#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
#define vvalue(o) check_exp(ttisvec(o), &(o)->value.gc->v) /* LUA-VEC */

#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))

Expand Down Expand Up @@ -155,8 +157,11 @@ typedef struct lua_TValue {
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
checkliveness(G(L),i_o); }



/* LUA-VEC */
#define setvvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TVEC; \
checkliveness(G(L),i_o); }

#define setobj(L,obj1,obj2) \
{ const TValue *o2=(obj2); TValue *o1=(obj1); \
Expand Down Expand Up @@ -347,7 +352,14 @@ typedef struct Table {
int sizearray; /* size of `array' array */
} Table;

/*
** Vector (LUA-VEC)
*/

typedef struct Vector {
CommonHeader;
float vec[LUA_VEC_SIZE];
} Vector;

/*
** `module' operation for hashing (size is always a power of 2)
Expand Down
3 changes: 2 additions & 1 deletion vendor/lua/src/lstate.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ union GCObject {
struct Proto p;
struct UpVal uv;
struct lua_State th; /* thread */
struct Vector v; /* LUA-VEC */
};


/* macros to convert a GCObject into a specific value */
#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
#define gco2ts(o) (&rawgco2ts(o)->tsv)
Expand All @@ -162,6 +162,7 @@ union GCObject {
#define ngcotouv(o) \
check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
#define gco2v(o) check_exp((o)->gch.tt == LUA_TVEC, &((o)->v)) /* LUA-VEC */

/* macro to convert any Lua object into a GCObject */
#define obj2gco(v) (cast(GCObject *, (v)))
Expand Down
3 changes: 2 additions & 1 deletion vendor/lua/src/ltm.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@



const char *const luaT_typenames[] = {
const char* const luaT_typenames[] = {
"nil", "boolean", "userdata", "number",
"string", "table", "function", "userdata", "thread",
"vec", /* LUA-VEC */
"proto", "upval"
};

Expand Down
18 changes: 12 additions & 6 deletions vendor/lua/src/lua.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,19 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
*/
#define LUA_TNONE (-1)

#define LUA_TNIL 0
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8

#define LUA_TTHREAD 8
#define LUA_TVEC 9 /* LUA-VEC */

/* LUA_VEC - Number of components in a vec */
#define LUA_VEC_SIZE 4

/* minimum Lua stack available to a C function */
#define LUA_MINSTACK 50 // MTA change. Was 20
Expand Down Expand Up @@ -170,6 +172,7 @@ LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
LUA_API void *(lua_touserdata) (lua_State *L, int idx);
LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
LUA_API const void *(lua_topointer) (lua_State *L, int idx);
LUA_API const float *(lua_tovec)(lua_State* L, int idx); /* LUA-VEC */


/*
Expand All @@ -187,6 +190,7 @@ LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
LUA_API void (lua_pushboolean) (lua_State *L, int b);
LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
LUA_API int (lua_pushthread) (lua_State *L);
LUA_API void (lua_pushvec)(lua_State* L, float x, float y, float z, float w); /* LUA-VEC */

/*
** get functions (Lua -> stack)
Expand Down Expand Up @@ -293,6 +297,7 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
#define lua_isvec(L, n) (lua_type(L, (n)) == LUA_TVEC) /* LUA-VEC */

#define lua_pushliteral(L, s) \
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
Expand Down Expand Up @@ -388,6 +393,7 @@ struct lua_Debug {

/******************************************************************************
* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved.
* Lua-vec Extension Copyright (C) 2010 Petri H�kkinen and Henri H�kkinen.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down
3 changes: 3 additions & 0 deletions vendor/lua/src/lualib.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ LUALIB_API int (luaopen_debug) (lua_State *L);
#define LUA_LOADLIBNAME "package"
LUALIB_API int (luaopen_package) (lua_State *L);

/* LUA-VEC */
#define LUA_VECLIBNAME "vec"
LUALIB_API int (luaopen_vec)(lua_State* L);

/* open all previous libraries */
LUALIB_API void (luaL_openlibs) (lua_State *L);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /