diff options
| author | Oliver Buchtala <oliver.buchtala@googlemail.com> | 2013-09-27 02:46:11 +0200 |
|---|---|---|
| committer | Oliver Buchtala <oliver.buchtala@googlemail.com> | 2013-09-27 03:25:28 +0200 |
| commit | 48af60d82904f1eef37b9beac03f8412947e883e (patch) | |
| tree | f971856d71e61616cabc45a0527374350b10e5ee /Examples/javascript/template | |
| parent | ecf9f96079067386a5f8bc83fadd4ac9e03f551c (diff) | |
| download | swig-48af60d82904f1eef37b9beac03f8412947e883e.tar.gz | |
Javascript examples.
Diffstat (limited to 'Examples/javascript/template')
| -rwxr-xr-x | Examples/javascript/template/Makefile | 21 | ||||
| -rw-r--r-- | Examples/javascript/template/binding.gyp | 8 | ||||
| -rw-r--r-- | Examples/javascript/template/example.h | 32 | ||||
| -rw-r--r-- | Examples/javascript/template/example.i | 17 | ||||
| -rw-r--r-- | Examples/javascript/template/runme.js | 30 |
5 files changed, 108 insertions, 0 deletions
diff --git a/Examples/javascript/template/Makefile b/Examples/javascript/template/Makefile new file mode 100755 index 000000000..99a9e9e86 --- /dev/null +++ b/Examples/javascript/template/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/template/binding.gyp b/Examples/javascript/template/binding.gyp new file mode 100644 index 000000000..69af46b22 --- /dev/null +++ b/Examples/javascript/template/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "example", + "sources": [ "example_wrap.cxx" ] + } + ] +} diff --git a/Examples/javascript/template/example.h b/Examples/javascript/template/example.h new file mode 100644 index 000000000..7401df650 --- /dev/null +++ b/Examples/javascript/template/example.h @@ -0,0 +1,32 @@ +/* File : example.h */ + +// Some template definitions + +template<class T> T max(T a, T b) { return a>b ? a : b; } + +template<class T> class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif +}; + diff --git a/Examples/javascript/template/example.i b/Examples/javascript/template/example.i new file mode 100644 index 000000000..8f94c4da1 --- /dev/null +++ b/Examples/javascript/template/example.i @@ -0,0 +1,17 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Now instantiate some specific template declarations */ + +%template(maxint) max<int>; +%template(maxdouble) max<double>; +%template(vecint) vector<int>; +%template(vecdouble) vector<double>; + diff --git a/Examples/javascript/template/runme.js b/Examples/javascript/template/runme.js new file mode 100644 index 000000000..551475c72 --- /dev/null +++ b/Examples/javascript/template/runme.js @@ -0,0 +1,30 @@ +var example = require("./build/Release/example"); + +//Call some templated functions +console.log(example.maxint(3,7)); +console.log(example.maxdouble(3.14,2.18)); + +// Create some class + +iv = new example.vecint(100); +dv = new example.vecdouble(1000); + +for(i=0;i<=100;i++) + iv.setitem(i,2*i); + +for(i=0;i<=1000;i++) + dv.setitem(i, 1.0/(i+1)); + +sum = 0; +for(i=0;i<=100;i++) + sum = sum + iv.getitem(i); + +console.log(sum); + +sum = 0.0; +for(i=0;i<=1000;i++) + sum = sum + dv.getitem(i); +console.log(sum); + +delete iv; +delete dv; |
