summaryrefslogtreecommitdiff
path: root/Examples/lua/variables
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2009-08-18 20:56:02 +0000
committerLorry <lorry@roadtrain.codethink.co.uk>2012-09-25 16:59:08 +0000
commit9f8a09ed743cedd9547bf0661d518647966ab114 (patch)
tree9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/lua/variables
downloadswig-tarball-master.tar.gz
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/lua/variables')
-rw-r--r--Examples/lua/variables/Makefile18
-rw-r--r--Examples/lua/variables/example.c91
-rw-r--r--Examples/lua/variables/example.h6
-rw-r--r--Examples/lua/variables/example.i49
-rw-r--r--Examples/lua/variables/runme.lua81
5 files changed, 245 insertions, 0 deletions
diff --git a/Examples/lua/variables/Makefile b/Examples/lua/variables/Makefile
new file mode 100644
index 0000000..f181818
--- /dev/null
+++ b/Examples/lua/variables/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+
+all::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' lua
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='mylua' INTERFACE='$(INTERFACE)' lua_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile lua_clean
+
+check: all
diff --git a/Examples/lua/variables/example.c b/Examples/lua/variables/example.c
new file mode 100644
index 0000000..aa4ffe9
--- /dev/null
+++ b/Examples/lua/variables/example.c
@@ -0,0 +1,91 @@
+/* File : example.c */
+
+/* I'm a file containing some C global variables */
+
+/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
+#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
+# define _CRT_SECURE_NO_DEPRECATE
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "example.h"
+
+int ivar = 0;
+short svar = 0;
+long lvar = 0;
+unsigned int uivar = 0;
+unsigned short usvar = 0;
+unsigned long ulvar = 0;
+signed char scvar = 0;
+unsigned char ucvar = 0;
+char cvar = 0;
+float fvar = 0;
+double dvar = 0;
+char *strvar = 0;
+const char cstrvar[] = "Goodbye";
+int *iptrvar = 0;
+char name[256] = "Dave";
+char path[256] = "/home/beazley";
+
+
+/* Global variables involving a structure */
+Point *ptptr = 0;
+Point pt = { 10, 20 };
+
+/* A variable that we will make read-only in the interface */
+int status = 1;
+
+/* A debugging function to print out their values */
+
+void print_vars() {
+ printf("ivar = %d\n", ivar);
+ printf("svar = %d\n", svar);
+ printf("lvar = %ld\n", lvar);
+ printf("uivar = %u\n", uivar);
+ printf("usvar = %u\n", usvar);
+ printf("ulvar = %lu\n", ulvar);
+ printf("scvar = %d\n", scvar);
+ printf("ucvar = %u\n", ucvar);
+ printf("fvar = %g\n", fvar);
+ printf("dvar = %g\n", dvar);
+ printf("cvar = %c\n", cvar);
+ printf("strvar = %s\n", strvar ? strvar : "(null)");
+ printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
+ printf("iptrvar = %p\n", iptrvar);
+ printf("name = %s\n", name);
+ printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
+ printf("pt = (%d, %d)\n", pt.x, pt.y);
+ printf("status = %d\n", status);
+}
+
+/* A function to create an integer (to test iptrvar) */
+
+int *new_int(int value) {
+ int *ip = (int *) malloc(sizeof(int));
+ *ip = value;
+ return ip;
+}
+
+/* A function to create a point */
+
+Point *new_Point(int x, int y) {
+ Point *p = (Point *) malloc(sizeof(Point));
+ p->x = x;
+ p->y = y;
+ return p;
+}
+
+char * Point_print(Point *p) {
+ static char buffer[256];
+ if (p) {
+ sprintf(buffer,"(%d,%d)", p->x,p->y);
+ } else {
+ sprintf(buffer,"null");
+ }
+ return buffer;
+}
+
+void pt_print() {
+ printf("(%d, %d)\n", pt.x, pt.y);
+}
diff --git a/Examples/lua/variables/example.h b/Examples/lua/variables/example.h
new file mode 100644
index 0000000..0f7e895
--- /dev/null
+++ b/Examples/lua/variables/example.h
@@ -0,0 +1,6 @@
+/* File: example.h */
+
+typedef struct {
+ int x,y;
+} Point;
+
diff --git a/Examples/lua/variables/example.i b/Examples/lua/variables/example.i
new file mode 100644
index 0000000..591b871
--- /dev/null
+++ b/Examples/lua/variables/example.i
@@ -0,0 +1,49 @@
+/* File : example.i */
+%module example
+%{
+#include "example.h"
+%}
+
+/* Some global variable declarations */
+%inline %{
+extern int ivar;
+extern short svar;
+extern long lvar;
+extern unsigned int uivar;
+extern unsigned short usvar;
+extern unsigned long ulvar;
+extern signed char scvar;
+extern unsigned char ucvar;
+extern char cvar;
+extern float fvar;
+extern double dvar;
+extern char *strvar;
+extern const char cstrvar[];
+extern int *iptrvar;
+extern char name[256];
+
+extern Point *ptptr;
+extern Point pt;
+%}
+
+
+/* Some read-only variables */
+
+%immutable;
+
+%inline %{
+extern int status;
+extern char path[256];
+%}
+
+%mutable;
+
+/* Some helper functions to make it easier to test */
+%inline %{
+extern void print_vars();
+extern int *new_int(int value);
+extern Point *new_Point(int x, int y);
+extern char *Point_print(Point *p);
+extern void pt_print();
+%}
+
diff --git a/Examples/lua/variables/runme.lua b/Examples/lua/variables/runme.lua
new file mode 100644
index 0000000..05b2d3f
--- /dev/null
+++ b/Examples/lua/variables/runme.lua
@@ -0,0 +1,81 @@
+---- 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
+
+-- Try to set the values of some global variables
+
+example.ivar = 42
+example.svar = -31000
+example.lvar = 65537
+example.uivar = 123456
+example.usvar = 61000
+example.ulvar = 654321
+example.scvar = -13
+example.ucvar = 251
+example.cvar = "S"
+example.fvar = 3.14159
+example.dvar = 2.1828
+example.strvar = "Hello World"
+example.iptrvar= example.new_int(37)
+example.ptptr = example.new_Point(37,42)
+example.name = "Bill"
+
+-- Now print out the values of the variables
+
+print("Variables (values printed from Lua)")
+
+print("ivar =", example.ivar)
+print("svar =", example.svar)
+print("lvar =", example.lvar)
+print("uivar =", example.uivar)
+print("usvar =", example.usvar)
+print("ulvar =", example.ulvar)
+print("scvar =", example.scvar)
+print("ucvar =", example.ucvar)
+print("fvar =", example.fvar)
+print("dvar =", example.dvar)
+print("cvar =", example.cvar)
+print("strvar =", example.strvar)
+print("cstrvar =", example.cstrvar)
+print("iptrvar =", example.iptrvar)
+print("name =", example.name)
+print("ptptr =", example.ptptr, example.Point_print(example.ptptr))
+print("pt =", example.pt, example.Point_print(example.pt))
+
+print("\nVariables (values printed from C)")
+
+example.print_vars()
+
+print "\nNow I'm going to try and modify some read only variables";
+
+print " Tring to set 'path' to 'Whoa!'";
+if pcall(function() example.path = "Whoa!" end)==true then
+ print " Thats funny, it didn't give an error!"
+else
+ print " It gave an error, as it should"
+end
+print(" Just checking the value: path =", example.path)
+
+print " Trying to set 'status' to '0'";
+if pcall(function() example.status = 0 end)==true then
+ print " Thats funny, it didn't give an error!"
+else
+ print " It gave an error, as it should"
+end
+print(" Just checking the value: status =", example.status)
+
+
+print "\nI'm going to try and update a structure variable.\n"
+
+example.pt = example.ptptr
+
+print "The new value is"
+example.pt_print()
+print("You should see the value", example.Point_print(example.ptptr))
+