summaryrefslogtreecommitdiff
path: root/Examples/javascript/variables
diff options
context:
space:
mode:
authorOliver Buchtala <oliver.buchtala@googlemail.com>2013-09-27 02:46:11 +0200
committerOliver Buchtala <oliver.buchtala@googlemail.com>2013-09-27 03:25:28 +0200
commit48af60d82904f1eef37b9beac03f8412947e883e (patch)
treef971856d71e61616cabc45a0527374350b10e5ee /Examples/javascript/variables
parentecf9f96079067386a5f8bc83fadd4ac9e03f551c (diff)
downloadswig-48af60d82904f1eef37b9beac03f8412947e883e.tar.gz
Javascript examples.
Diffstat (limited to 'Examples/javascript/variables')
-rwxr-xr-xExamples/javascript/variables/Makefile21
-rw-r--r--Examples/javascript/variables/binding.gyp8
-rwxr-xr-xExamples/javascript/variables/example.cxx96
-rwxr-xr-xExamples/javascript/variables/example.h6
-rwxr-xr-xExamples/javascript/variables/example.i49
-rwxr-xr-xExamples/javascript/variables/runme.js68
-rw-r--r--Examples/javascript/variables/swig_gdb.log9
7 files changed, 257 insertions, 0 deletions
diff --git a/Examples/javascript/variables/Makefile b/Examples/javascript/variables/Makefile
new file mode 100755
index 000000000..99a9e9e86
--- /dev/null
+++ b/Examples/javascript/variables/Makefile
@@ -0,0 +1,21 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+JS_SCRIPT = runme.js
+TARGET = example
+INTERFACE = example.i
+
+wrapper::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_wrapper_cpp
+
+build:: wrapper
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_build
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile javascript_clean
+
+check:: build
+ $(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
+ TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
diff --git a/Examples/javascript/variables/binding.gyp b/Examples/javascript/variables/binding.gyp
new file mode 100644
index 000000000..54eebfaa0
--- /dev/null
+++ b/Examples/javascript/variables/binding.gyp
@@ -0,0 +1,8 @@
+{
+ "targets": [
+ {
+ "target_name": "example",
+ "sources": [ "example.cxx", "example_wrap.cxx" ]
+ }
+ ]
+}
diff --git a/Examples/javascript/variables/example.cxx b/Examples/javascript/variables/example.cxx
new file mode 100755
index 000000000..3ad4c2323
--- /dev/null
+++ b/Examples/javascript/variables/example.cxx
@@ -0,0 +1,96 @@
+/* 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;
+#ifdef __cplusplus // Note: for v8 this must be linkable with g++, without extern cstrvar is mangled
+extern const char cstrvar[] = "Goodbye";
+#else
+const char cstrvar[] = "Goodbye";
+#endif
+const
+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/javascript/variables/example.h b/Examples/javascript/variables/example.h
new file mode 100755
index 000000000..0f7e89594
--- /dev/null
+++ b/Examples/javascript/variables/example.h
@@ -0,0 +1,6 @@
+/* File: example.h */
+
+typedef struct {
+ int x,y;
+} Point;
+
diff --git a/Examples/javascript/variables/example.i b/Examples/javascript/variables/example.i
new file mode 100755
index 000000000..591b871ed
--- /dev/null
+++ b/Examples/javascript/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/javascript/variables/runme.js b/Examples/javascript/variables/runme.js
new file mode 100755
index 000000000..22d208480
--- /dev/null
+++ b/Examples/javascript/variables/runme.js
@@ -0,0 +1,68 @@
+var example = require("./build/Release/example");
+
+// 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 console.log out the values of the variables
+console.log("Variables (values console.loged from Python)" + "\n");
+console.log("ivar = " + example.ivar + "\n");
+console.log("svar = " + example.svar + "\n");
+console.log("lvar = " + example.lvar + "\n");
+console.log("uivar = " + example.uivar + "\n");
+console.log("usvar = " + example.usvar + "\n");
+console.log("ulvar = " + example.ulvar + "\n");
+console.log("scvar = " + example.scvar + "\n");
+console.log("ucvar = " + example.ucvar + "\n");
+console.log("fvar = " + example.fvar + "\n");
+console.log("dvar = " + example.dvar + "\n");
+console.log("cvar = " + example.cvar + "\n");
+console.log("strvar = " + example.strvar+ "\n");
+console.log("cstrvar = " + example.cstrvar+ "\n");
+console.log("iptrvar = " + example.iptrvar+ "\n");
+console.log("name = " + example.name + "\n");
+console.log("ptptr = " + example.ptptr + ": " + example.Point_print(example.ptptr) + "\n");
+console.log("pt = " + example.pt + ": " + example.Point_print(example.pt) + "\n");
+
+
+console.log("\nVariables (values console.loged from C)");
+
+example.print_vars();
+
+console.log("\nNow I'm going to try and modify some read only variables");
+
+console.log("Tring to set 'path'");
+try{
+ example.path = "Whoa!";
+ console.log("Hey, what's going on?!?! This shouldn't work");
+}
+catch(e){
+ console.log("Good.");
+}
+
+console.log("Trying to set 'status'");
+try{
+ example.status = 0;
+ console.log("Hey, what's going on?!?! This shouldn't work");
+} catch(e){
+ console.log("Good.");
+}
+
+console.log("\nI'm going to try and update a structure variable.\n");
+example.pt = example.ptptr;
+console.log("The new value is: ");
+example.pt_print();
+console.log("You should see the value: " + example.Point_print(example.ptptr));
diff --git a/Examples/javascript/variables/swig_gdb.log b/Examples/javascript/variables/swig_gdb.log
new file mode 100644
index 000000000..2fe437110
--- /dev/null
+++ b/Examples/javascript/variables/swig_gdb.log
@@ -0,0 +1,9 @@
+Loaded swig printers
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string
+SwigStringPrinter: Could not convert const char* to string