{"id":15685457,"url":"https://github.com/hparker/embedding-lua-in-c-by-example","last_synced_at":"2025-03-30T11:14:54.986Z","repository":{"id":75150618,"uuid":"298431663","full_name":"HParker/embedding-lua-in-c-by-example","owner":"HParker","description":"Here is a series of short C snippets to learn how to embed Lua in your C program","archived":false,"fork":false,"pushed_at":"2020-09-26T08:23:48.000Z","size":2,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T12:50:43.419Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://words.hparker.io/embedding-lua-in-c-by-example","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HParker.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-25T01:01:04.000Z","updated_at":"2022-04-08T12:19:58.000Z","dependencies_parsed_at":"2023-06-05T14:00:47.299Z","dependency_job_id":null,"html_url":"https://github.com/HParker/embedding-lua-in-c-by-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HParker%2Fembedding-lua-in-c-by-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HParker%2Fembedding-lua-in-c-by-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HParker%2Fembedding-lua-in-c-by-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HParker%2Fembedding-lua-in-c-by-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HParker","download_url":"https://codeload.github.com/HParker/embedding-lua-in-c-by-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246308051,"owners_count":20756482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T17:25:18.416Z","updated_at":"2025-03-30T11:14:54.963Z","avatar_url":"https://github.com/HParker.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Here is a series of short C snippets to learn how to embed Lua in your C program.\n\nYou can find all the scripts together in one file in `embeddingLua.c` and build the project with\n\nYou can build and run the project by running:\n- `git clone git@github.com:HParker/embedding-lua-in-c-by-example.git`\n- `make`\n- `./a.out`\n\n# Run a string of Lua code\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  // Run a lua string\n  luaL_dostring(L, \"print(\\\"hello from lua\\\")\");\n}\n```\n\n# Run a file of Lua code\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  // run a lua file\n  luaL_dofile(L, \"hello.lua\");\n}\n```\n\n# Get a number from Lua\n```c\nluaL_dostring(L, \"x = 10\");\nlua_getglobal(L, \"x\");\nint x = (int)lua_tonumber(L, -1);\nprintf(\"x = %i\\n\", x);\n```\n\n# Get a string from Lua\n```c\nluaL_dostring(L, \"string = 'hi there'\");\nlua_getglobal(L, \"string\");\nconst char * string = lua_tolstring(L, -1, NULL);\nprintf(\"string = '%s'\\n\", string);\n```\n\n# Get a table field from Lua\n```c\nluaL_dostring(L, \"table = { key = 123 }\");\nlua_getglobal(L, \"table\");\nlua_getfield(L, -1, \"key\");\nint key = (int)lua_tonumber(L, -1);\nprintf(\"key = %i\\n\", key);\n```\n\n# Call Lua function from C\n```c\nluaL_dostring(L, \"function foo() print(\\\"Hello from foo method\\\") end\");\nlua_getglobal(L, \"foo\");\nif (lua_isfunction(L, -1)) {\n  //                        V number of arguments\n  //                        |  V number of results\n  int status = lua_pcall(L, 0, 0, 0);\n  // status can be LUA_OK, LUA_ERRRUN, LUA_ERRMEM, or LUA_ERRERR\n  if (status != LUA_OK) {\n    printf(\"Something went wrong when calling the lua function\\n\");\n  } else {\n    int result = (int)lua_tonumber(L, -1);\n    printf(\"result = %i\\n\", result);\n  }\n} else {\n  printf(\"function `foo` was not found or was not a function\\n\");\n}\n```\n\n# Calll lua function with a return value from C\n```c\nluaL_dostring(L, \"function foo() return 456 end\");\nlua_getglobal(L, \"foo\");\nif (lua_isfunction(L, -1)) {\n  //                        V number of arguments\n  //                        |  V number of results\n  int status = lua_pcall(L, 0, 1, 0);\n  // status can be LUA_OK, LUA_ERRRUN, LUA_ERRMEM, or LUA_ERRERR\n  if (status != LUA_OK) {\n    printf(\"Something went wrong when calling the lua function\\n\");\n  } else {\n    int result = (int)lua_tonumber(L, -1);\n    printf(\"result = %i\\n\", result);\n  }\n} else {\n  printf(\"function `foo` was not found or was not a function\\n\");\n}\n```\n\n# Call a lua function with arguements from C\n```c\nluaL_dostring(L, \"function square(x) return x * x end\");\nlua_getglobal(L, \"square\");\nif (lua_isfunction(L, -1)) {\n  //                        V number of arguments\n  //                        |  V number of results\n  lua_pushnumber(L, (lua_Number)5);\n  int status = lua_pcall(L, 1, 1, 0);\n  // status can be LUA_OK, LUA_ERRRUN, LUA_ERRMEM, or LUA_ERRERR\n  if (status != LUA_OK) {\n    printf(\"Something went wrong when calling the lua function\\n\");\n    printf(\"Error: %s \\n\", lua_tostring(L, -1));\n    lua_pop(L, 1);\n  } else {\n    int doubled = (int)lua_tonumber(L, -1);\n    printf(\"doubled = %i\\n\", doubled);\n  }\n} else {\n  printf(\"function `square` was not found or was not a function\\n\");\n}\n```\n\n# Expose a C function to Lua\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint basicCFunc(lua_State * L) {\n  printf(\"This is my c function called from lua\\n\");\n  return 0; // number of results\n}\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  lua_register(L, \"basicCFunc\", basicCFunc);\n  luaL_dostring(L, \"basicCFunc()\");\n}\n```\n\n# Expose a C function to Lua that returns a value\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint returningCFunc(lua_State * L) {\n  lua_pushnumber(L, 678); // result\n  return 1; // number of results\n}\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  // expose a function to lua that returns a value\n  lua_register(L, \"returningCFunc\", returningCFunc);\n  luaL_dostring(L, \"returnedFromC = returningCFunc()\");\n  lua_getglobal(L, \"returnedFromC\");\n  int returnedFromC = (int)lua_tonumber(L, -1);\n  printf(\"returned from c = %i\\n\", returnedFromC);\n}\n```\n\n# Expose a C function to Lua that takes one arguments\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint argumentCFunc(lua_State * L) {\n  lua_pushnumber(L, lua_tonumber(L, 1) + 1); // result\n  return 1; // number of results\n}\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  lua_register(L, \"argumentCFunc\", argumentCFunc);\n  luaL_dostring(L, \"returnedFromC = argumentCFunc(1)\");\n  lua_getglobal(L, \"returnedFromC\");\n  returnedFromC = (int)lua_tonumber(L, -1);\n  printf(\"returned from c = %i\\n\", returnedFromC);\n}\n```\n\n# Expose a C function to Lua that takes any number of arguments\n```c\n#include \u003cstdio.h\u003e\n#include \u003clauxlib.h\u003e\n#include \u003clua.h\u003e\n#include \u003clualib.h\u003e\n\nint variableArgumentCFunc(lua_State * L) {\n  int n = lua_gettop(L); // number of arguments\n  float sum = 0.0;\n  int i;\n  for (i = 1; i \u003c= n; i++) {\n    if (!lua_isnumber(L, i)) {\n      lua_pushliteral(L, \"incorrect argument\");\n      lua_error(L);\n    }\n    sum += lua_tonumber(L, i);\n  }\n  lua_pushnumber(L, sum); // result\n  return 1; // number of results\n}\n\nint main() {\n  // setup lua\n  lua_State *L = luaL_newstate();\n  luaL_openlibs(L);\n\n  lua_register(L, \"variableArgumentCFunc\", variableArgumentCFunc);\n  luaL_dostring(L, \"returnedFromC = variableArgumentCFunc(1, 2, 3)\");\n  lua_getglobal(L, \"returnedFromC\");\n  returnedFromC = (int)lua_tonumber(L, -1);\n  printf(\"returned from c = %i\\n\", returnedFromC);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhparker%2Fembedding-lua-in-c-by-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhparker%2Fembedding-lua-in-c-by-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhparker%2Fembedding-lua-in-c-by-example/lists"}