免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
和我一起寫lua - C和lua的參數(shù)傳遞與返回值

分類: Python/Ruby

lua通過一個運(yùn)行時棧來維護(hù)參數(shù)傳遞及返回,使用lua_to*等函數(shù)獲取lua傳遞到C函數(shù)的參數(shù),使用lua_push*從C函數(shù)返回值到lua腳本。此外也可以使用lua_getglobal從C函數(shù)獲取lua腳本定義的全局變量。具體參看例子(test_lua.c):

點(diǎn)擊(此處)折疊或打開

  1. #include <lua.h>
  2. #include <lauxlib.h>

  3. #include <stdlib.h> /* For function exit() */
  4. #include <stdio.h> /* For input/output */

  5. void bail(lua_State *L, char *msg){
  6.     fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
  7.         msg, lua_tostring(L, -1));
  8.     exit(1);
  9. }
  10. int lua_func_from_c(lua_State *L)
  11. {
  12.     printf("This is C\n");
  13.     int argc = lua_gettop(L);    /* number of arguments */
  14.     const char * str = lua_tostring(L,1);    /* the first argument: string */
  15.     int num = lua_tonumber(L,2); /* the second argument: number */
  16.     
  17.     printf("The first argument: %s\n", str);
  18.     printf("The second argument: %d\n", num);

  19.     lua_getglobal(L,"global_var");
  20.     const char * global_str = lua_tostring(L,-1);
  21.     printf("global_var is %s\n", global_str);

  22.     int the_second_ret = 2*num;
  23.     lua_pushstring(L, "the first return");
  24.     lua_pushnumber(L, the_second_ret);
  25.     return 2;            /* tell lua how many variables are returned */
  26. }
  27. int main(int argc, const char *argv[])
  28. {
  29.     if(argc != 2)
  30.     {
  31.         return 1;
  32.     }
  33.     lua_State *L = luaL_newstate(); /* Create new lua state variable */

  34.     /* Load Lua libraries, otherwise, the lua function in *.lua will be nil */
  35.     luaL_openlibs(L);
  36.     
  37.     /* register new lua function in C */
  38.     lua_register(L, "lua_func_from_c", lua_func_from_c);

  39.     if( luaL_loadfile(L,argv[1]) ) /* Only load the lua script file */
  40.         bail(L, "luaL_loadfile() failed");

  41.     if( lua_pcall(L,0,0,0) ) /* Run the loaded lua file */
  42.         bail(L, "lua_pcall() failed");
  43.     lua_close(L);                 /* Close the lua state variable */    

  44.     return 0;
  45. }
lua腳本(my.lua)如下所示(在lua腳本中,如果變量不用local明確聲明為局部變量,則默認(rèn)為全局變量):

點(diǎn)擊(此處)折疊或打開

  1. print("Hello world")
  2. global_var = "this is a global string"
  3. first, second = lua_func_from_c("the first one", 2)
  4. print("the first returned", first)
  5. print("the second returned", second)
執(zhí)行結(jié)果:
$ ./a.out my.lua 
Hello world
This is C
The first argument: the first one
The second argument: 2
global_var is this is a global string
the first returned the first return
the second returned 4
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
實(shí)現(xiàn)整數(shù)轉(zhuǎn)化為字符串函數(shù)itoa()函數(shù)
c語言例題
在c語言中sprintf使用的方法與printf的區(qū)別
C語言scanf()函數(shù):格式化輸入函數(shù)(2014-11-11)
lua和c/c 互相調(diào)用實(shí)例分析 - lxyfirst - C 博客
c/C++ 文件讀寫 收集備用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服