summaryrefslogtreecommitdiff
path: root/Examples/lua/constants
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/lua/constants')
-rw-r--r--Examples/lua/constants/Makefile19
-rw-r--r--Examples/lua/constants/example.i27
-rw-r--r--Examples/lua/constants/runme.lua35
3 files changed, 81 insertions, 0 deletions
diff --git a/Examples/lua/constants/Makefile b/Examples/lua/constants/Makefile
new file mode 100644
index 0000000..4204545
--- /dev/null
+++ b/Examples/lua/constants/Makefile
@@ -0,0 +1,19 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS =
+TARGET = example
+INTERFACE = example.i
+LIBS =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua
+
+static::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='mylua' INTERFACE='$(INTERFACE)' lua_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile lua_clean
+
+check: all
diff --git a/Examples/lua/constants/example.i b/Examples/lua/constants/example.i
new file mode 100644
index 0000000..4f7b1a4
--- /dev/null
+++ b/Examples/lua/constants/example.i
@@ -0,0 +1,27 @@
+/* File : example.i */
+%module example
+
+/* A few preprocessor macros */
+
+#define ICONST 42
+#define FCONST 2.1828
+#define CCONST 'x'
+#define CCONST2 '\n'
+#define SCONST "Hello World"
+#define SCONST2 "\"Hello World\""
+
+/* This should work just fine */
+#define EXPR ICONST + 3*(FCONST)
+
+/* This shouldn't do anything */
+#define EXTERN extern
+
+/* Neither should this (BAR isn't defined) */
+#define FOO (ICONST + BAR)
+
+/* The following directives also produce constants */
+
+%constant int iconst = 37;
+%constant double fconst = 3.14;
+
+
diff --git a/Examples/lua/constants/runme.lua b/Examples/lua/constants/runme.lua
new file mode 100644
index 0000000..751e7d6
--- /dev/null
+++ b/Examples/lua/constants/runme.lua
@@ -0,0 +1,35 @@
+-- file: example.lua
+
+---- importing ----
+if string.sub(_VERSION,1,7)=='Lua 5.0' then
+ -- lua5.0 doesnt have a nice way to do this
+ lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
+ assert(lib)()
+else
+ -- lua 5.1 does
+ require('example')
+end
+
+print("ICONST = "..example.ICONST.." (should be 42)")
+print("FCONST = "..example.FCONST.." (should be 2.1828)")
+print("CCONST = "..example.CCONST.." (should be 'x')")
+print("CCONST2 = "..example.CCONST2.." (this should be on a new line)")
+print("SCONST = "..example.SCONST.." (should be 'Hello World')")
+print("SCONST2 = "..example.SCONST2.." (should be '\"Hello World\"')")
+print("EXPR = "..example.EXPR.." (should be 48.5484)")
+print("iconst = "..example.iconst.." (should be 37)")
+print("fconst = "..example.fconst.." (should be 3.14)")
+
+-- helper to check that a fn failed
+function checkfail(fn)
+ if pcall(fn)==true then
+ print("that shouldn't happen, it worked")
+ else
+ print("function failed as expected")
+ end
+end
+
+-- these should fail
+-- example.EXTERN is a nil value, so concatentatin will make it fail
+checkfail(function() print("EXTERN = "..example.EXTERN) end)
+checkfail(function() print("FOO = "..example.FOO) end)