Lua fonctionel
This commit is contained in:
parent
96e1c59930
commit
3b39ff4301
1 changed files with 5 additions and 32 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
@ -22,7 +23,7 @@ static int lua_add_machine(lua_State *L){
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_mtprint(lua_State *L){
|
static int lua_mtprint(lua_State *L){
|
||||||
printf("(lua) %s\n", lua_tostring(L, 0));
|
printf("(lua) %s\n", luaL_checkstring(L, 1));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -36,50 +37,22 @@ struct LuaFn exported_funcs[] = {
|
||||||
{"mtprint", lua_mtprint}
|
{"mtprint", lua_mtprint}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Taken from the Lua manual
|
|
||||||
static void *l_alloc (void *ud, void *ptr, size_t osize,size_t nsize){
|
|
||||||
(void)ud; (void)osize; /* not used */
|
|
||||||
if (nsize == 0) {
|
|
||||||
free(ptr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return realloc(ptr, nsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct load_dat {
|
struct load_dat {
|
||||||
char *str;
|
char *str;
|
||||||
int pos;
|
int pos;
|
||||||
int len;
|
int len;
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *file_reader(lua_State *L, void *data, size_t *size){
|
|
||||||
(void)L;
|
|
||||||
struct load_dat *dat = data;
|
|
||||||
if(dat->pos >= dat->len){
|
|
||||||
*size = 0;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
*size = dat->len;
|
|
||||||
dat->pos += *size;
|
|
||||||
return dat->str;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_State *gl_L;
|
lua_State *gl_L;
|
||||||
|
|
||||||
void script_init(){
|
void script_init(){
|
||||||
gl_L = lua_newstate(l_alloc, NULL);
|
gl_L = luaL_newstate();
|
||||||
|
|
||||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
||||||
lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn);
|
lua_register(gl_L, exported_funcs[i].name, exported_funcs[i].fn);
|
||||||
|
|
||||||
struct load_dat dat = {"mtprint(\"hello\")", 0};
|
luaL_loadstring(gl_L, "mtprint(\"hello world\")\n");
|
||||||
dat.len = strlen(dat.str);
|
lua_pcall(gl_L, 0, 0, 0);
|
||||||
lua_load(gl_L, file_reader, &dat, "test", "t");
|
|
||||||
int results = 0;
|
|
||||||
lua_resume(gl_L, NULL, 0, &results);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void script_clean(){
|
void script_clean(){
|
||||||
|
|
Loading…
Add table
Reference in a new issue