2016-08-30 5 views

답변

3

loaded 청크는 단지 일반적인 기능입니다. C에서 모듈을로드하면 다음과 같이 생각할 수있다 :

// load the chunk 
if (luaL_loadstring(L, script)) { 
    return luaL_error(L, "Error loading script: %s", lua_tostring(L, -1)); 
} 

// call the chunk (function will be on top of the stack) 
if (lua_pcall(L, 0, 1, 0)) { 
    return luaL_error(L, "Error running chunk: %s", lua_tostring(L, -1)); 
} 

// call the function 
lua_pushinteger(L, 42); // function arg (i) 
if (lua_pcall(L, 1, 1, 0)) { 
    return luaL_error(L, "Error calling function: %s", lua_tostring(L, -1)); 
} 
+0

아이, 감사, 그건 :

return (function() -- this is the chunk compiled by load -- foo.lua return function (i) return i end end)() -- executed with call/pcall 

는 반환 값이 함수는 청크를로드하고 호출하기 만하면됩니다입니다 '덩어리'의 의미있는 의미 론적 의미. 사실, 지금까지 시도한 버전 중 하나는 비슷했는데, 첫 번째 호출은 결과를 무시한'lua_pcall (L, 0, 0)'이었습니다. 그래서 왜 돌아 오는 가치가 없는지 궁금합니다. –