Skip to content

Instantly share code, notes, and snippets.

@jddurand
Last active June 21, 2017 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jddurand/09954a5984596ceaf113 to your computer and use it in GitHub Desktop.
Save jddurand/09954a5984596ceaf113 to your computer and use it in GitHub Desktop.
Marpa/LUA bindings proof of concept
#!env sh -v
#
# Marpa in Lua proof of concept - Assumption: Debian/Linux
#
# Prologue: apt-get -t unstable install lua5.1 liblua5.1-dev swig
#
# Please go to Marpa-R2-2.096000/engine/read_only and execute this file
# ---------------------------------------------------------------------
#
[ -r ./Makefile ] && make distclean
./configure --enable-shared
make
swig -lua -o ./luaMarpa.c ./marpa.i
cc -I/usr/include/lua5.1 -o ./luaMarpa.o -c ./luaMarpa.c -g
cc -I/usr/include/lua5.1 -o ./luaMarpa.so -shared ./luaMarpa.o -L`pwd`/.libs -lmarpa
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/.libs lua5.1 <<EOF
require("luaMarpa")
v = luaMarpa.new_intArray(3);
r=luaMarpa.marpa_version(v)
print("marpa_version return code: ", r)
print("Marpa major version :", luaMarpa.intArray_getitem(v, 0))
print("Marpa minor version :", luaMarpa.intArray_getitem(v, 1))
print("Marpa micro version :", luaMarpa.intArray_getitem(v, 2))
luaMarpa.delete_intArray(v)
EOF
%module luaMarpa
%{
#include "marpa.h"
%}
/*
* Helper functions for lua to play with int arrays within the luaMarpa namespace
*/
%include <carrays.i>
%array_functions(int, intArray)
%include "marpa.h"
@jddurand
Copy link
Author

With "luaMarpa.delete_intArray(v)" valgrind output is perfect:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/.libs valgrind lua5.1 <<EOF
require("luaMarpa")
v = luaMarpa.new_intArray(3);
r=luaMarpa.marpa_version(v)
print("marpa_version return code: ", r)
print("Marpa major version      :", luaMarpa.intArray_getitem(v, 0))
print("Marpa minor version      :", luaMarpa.intArray_getitem(v, 1))
print("Marpa micro version      :", luaMarpa.intArray_getitem(v, 2))
luaMarpa.delete_intArray(v)
EOF
==9218== Memcheck, a memory error detector
==9218== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9218== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==9218== Command: lua5.1
==9218== 
marpa_version return code:      0
Marpa major version      :      7
Marpa minor version      :      0
Marpa micro version      :      3
==9218== 
==9218== HEAP SUMMARY:
==9218==     in use at exit: 0 bytes in 0 blocks
==9218==   total heap usage: 1,328 allocs, 1,328 frees, 99,122 bytes allocated
==9218== 
==9218== All heap blocks were freed -- no leaks are possible
==9218== 
==9218== For counts of detected and suppressed errors, rerun with: -v
==9218== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment