-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_runstring.c
More file actions
32 lines (27 loc) · 972 Bytes
/
lua_runstring.c
File metadata and controls
32 lines (27 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <string.h>
#include "emscripten.h"
#include "lua_src/lua.h"
#include "lua_src/lualib.h"
#include "lua_src/lauxlib.h"
EMSCRIPTEN_KEEPALIVE
void runstring(const char *js_string) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
// Load the Lua code as a string
if (luaL_loadstring(L, js_string) == LUA_OK) {
// Execute the loaded code
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
// Handle error if the code fails to execute
const char *error = lua_tostring(L, -1);
printf("Error running Lua code: %s\n", error);
lua_pop(L, 1); // Remove error message from the stack
}
} else {
// Handle error if the code fails to load
const char *error = lua_tostring(L, -1);
printf("Error loading Lua code: %s\n", error);
lua_pop(L, 1); // Remove error message from the stack
}
lua_close(L); // Close the Lua state
}