나는 동적으로 Lua 명령을 생성하고 소켓으로 클라이언트에 보내는 C 서버에서 작업하고 있습니다. 현재 서버는 일반 텍스트를 사용하고 있지만 클라이언트에게 보내기 전에 서버에서 스크립트를 미리 컴파일하고 싶습니다.Lua 스크립트를 서명되지 않은 char 버퍼로 컴파일
나는 luac.c을 확인하지만, 이런 식으로 뭔가를 할 수있을하는 방법을 찾을 수 없습니다 :
char lua_commands[ 1024 ] = { "a = 123; b = 456; c = a + b;" };
int socket
unsigned int send_buffer_size
unsigned char *send_buffer
/* Compile lua_commands and store the binary script into send_buffer without
having to write first the .out on disk then read it again in order store the content
into send_buffer */
send(socket, send_buffer, send_buffer_size, 0);
누구는 이것을 달성하는 데 도움이 수 있습니까?
[업데이트]
좋아, 나는 그것을 알아낼 생각 :
이#include "lua.h"
#include "lauxlib.h"
#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstring.h"
#include "lundump.h"
#define toproto(L,i) (clvalue(L->top+(i))->l.p)
static int writer(lua_State *L, const void *p, size_t size, void *u){
unsigned int i = 0;
unsigned char *d = (unsigned char *)p;
// Print all the bytes on the console.
while(i != size) {
printf("%d ", d[ i ]);
++i;
}
return 0;
}
void compile(lua_State *L, char *command){
const Proto* f;
if (luaL_loadstring(L, command) !=0) {
printf("%s\n", lua_tostring(L, -1));
}
f = toproto(L,-1);
lua_lock(L);
luaU_dump(L, f, writer, NULL, 1);
lua_unlock(L);
}
int main (int argc, const char * argv[]) {
lua_State *L = lua_open();
compile(L, "a = 123; b = 456; c = a + b; print(c);");
lua_close(L);
return 0;
}
그러나 다른 질문에 날 리드하는 내가 루아 상태를 닫고 (lua_close, lua_open) 다시해야합니까 매번 다른 Lua 명령어로 컴파일() 함수를 호출하거나 출력은 최신 luaL_loadstring의 결과 일뿐입니다?
Im 확실하지 않지만 toproto 매크로 정의에서 최상위 스택이 반환 될 것이라고 생각합니까?
가능한 복제본 [컴파일 루아 코드, 저장 바이트 코드,로드 및 실행] (http://stackoverflow.com/questions/8936369/compile-lua-code-store-bytecode-then-load-and-execute- it) –
바이트 코드보다는 루아 소스 텍스트에 메시지를 남기는 것이 여러 가지 이점을 가질 수 있음에 유의하십시오. 내 머리 꼭대기에서 ... 우선, 바이트 코드는 종종 작지 않습니다. 둘째, 바이트 코드는 플랫폼에 따라 다릅니다. 셋째, 바이트 코드는 안전하다고 확인하기가 매우 어렵습니다. 크기가 중요한 경우 짧은 식별자, 최소 공백 및 주석없이 항상 메시지를 생성 할 수 있습니다. 또한 큰 메시지에 도움이되는 유선에서 압축을 사용할 수도 있습니다. – RBerteig