/* ** Call a function (C or Lua). The parameters must be on the stack, ** between [stack+base,top). The function to be called is at stack+base-1. ** When returns, the results are on the stack, between [stack+base-1,top). ** The number of results is nResults, unless nResults=MULT_RET. */ static void do_call (StkId base, int nResults) { StkId firstResult; Object *func = stack+base-1; int i; if (tag(func) == LUA_T_CFUNCTION) { tag(func) = LUA_T_CMARK; firstResult = callC(fvalue(func), base); } else if (tag(func) == LUA_T_FUNCTION) { tag(func) = LUA_T_MARK; firstResult = lua_execute(func->value.tf->code, base); } else { /* func is not a function */ /* Call the fallback for invalid functions */ open_stack((top-stack)-(base-1)); stack[base-1] = luaI_fallBacks[FB_FUNCTION].function; do_call(base, nResults); return; } /* adjust the number of results */ if (nResults != MULT_RET && top - (stack+firstResult) != nResults) adjust_top(firstResult+nResults); /* move results to base-1 (to erase parameters and function) */ base--; nResults = top - (stack+firstResult); /* actual number of results */ for (i=0; i<nResults; i++) *(stack+base+i) = *(stack+firstResult+i); top -= firstResult-base; }
/* ** Call a function (C or Lua). The parameters must be on the stack, ** between [stack+base,top). The function to be called is at stack+base-1. ** When returns, the results are on the stack, between [stack+base-1,top). ** The number of results is nResults, unless nResults=MULT_RET. */ static void do_call (StkId base, int nResults) { StkId firstResult; TObject *func = stack+base-1; int i; if (ttype(func) == LUA_T_CFUNCTION) { ttype(func) = LUA_T_CMARK; firstResult = callC(fvalue(func), base); } else if (ttype(func) == LUA_T_FUNCTION) { ttype(func) = LUA_T_MARK; firstResult = lua_execute(func->value.tf->code, base); } else { /* func is not a function */ /* Check the tag method for invalid functions */ TObject *im = luaI_getimbyObj(func, IM_FUNCTION); if (ttype(im) == LUA_T_NIL) lua_error("call expression not a function"); open_stack((top-stack)-(base-1)); stack[base-1] = *im; do_call(base, nResults); return; } /* adjust the number of results */ if (nResults != MULT_RET) adjust_top(firstResult+nResults); /* move results to base-1 (to erase parameters and function) */ base--; nResults = top - (stack+firstResult); /* actual number of results */ for (i=0; i<nResults; i++) *(stack+base+i) = *(stack+firstResult+i); top -= firstResult-base; }
/* ** Parse LUA code and execute global statement. ** Return 0 on success or 1 on error. */ int lua_parse (void) { Byte *initcode = maincode; err = 0; if (yyparse () || (err==1)) return 1; *maincode++ = HALT; if (lua_execute (initcode)) return 1; maincode = initcode; return 0; }
int main (int argc, char **argv) { L = luaL_newstate(); /* create state */ lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ luaL_openlibs(L); /* open libraries */ lua_gc(L, LUA_GCRESTART, 0); #if EMSCRIPTEN emscripten_exit_with_live_runtime(); #else lua_execute("print('hello world')"); #endif return 0; }
/* ** Parse LUA code and execute global statement. ** Return 0 on success or 1 on error. */ int lua_parse (void) { Byte *init = initcode = (Byte *) calloc(GAPCODE, sizeof(Byte)); maincode = 0; maxmain = GAPCODE; if (init == NULL) { lua_error("not enough memory"); return 1; } err = 0; if (yyparse () || (err==1)) return 1; initcode[maincode++] = HALT; init = initcode; #if LISTING PrintCode(init,init+maincode); #endif if (lua_execute (init)) return 1; free(init); return 0; }