Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Created September 4, 2017 01:58
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tylerneylon/ca8dc2c17c249ef7c437b270787e04a9 to your computer and use it in GitHub Desktop.
An example Lua module written in C.
// A Lua module written in C.
//
// This module enables terminal-based ASCII art
// of a loquacious bovine nature.
//
#include <stdio.h>
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
// This is a Lua-callable function.
// It prints out a cow uttering the user's string.
static int say(lua_State *L) {
char *str;
asprintf(&str, "cowsay %s", lua_tostring(L, -1));
system(str);
free(str);
return 0;
}
// This is the module's initialization function.
// It's called implicitly by Lua when the module loads.
int luaopen_cow(lua_State *L) {
static const struct luaL_Reg cow_lib[] = {
{"say", say},
{NULL, NULL}
};
luaL_newlib(L, cow_lib);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment