summaryrefslogtreecommitdiff
path: root/Examples/javascript/operator
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/operator
parentecf9f96079067386a5f8bc83fadd4ac9e03f551c (diff)
downloadswig-48af60d82904f1eef37b9beac03f8412947e883e.tar.gz
Javascript examples.
Diffstat (limited to 'Examples/javascript/operator')
-rwxr-xr-xExamples/javascript/operator/Makefile21
-rw-r--r--Examples/javascript/operator/binding.gyp8
-rw-r--r--Examples/javascript/operator/example.h36
-rw-r--r--Examples/javascript/operator/example.i34
-rw-r--r--Examples/javascript/operator/runme.js25
5 files changed, 124 insertions, 0 deletions
diff --git a/Examples/javascript/operator/Makefile b/Examples/javascript/operator/Makefile
new file mode 100755
index 000000000..99a9e9e86
--- /dev/null
+++ b/Examples/javascript/operator/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/operator/binding.gyp b/Examples/javascript/operator/binding.gyp
new file mode 100644
index 000000000..69af46b22
--- /dev/null
+++ b/Examples/javascript/operator/binding.gyp
@@ -0,0 +1,8 @@
+{
+ "targets": [
+ {
+ "target_name": "example",
+ "sources": [ "example_wrap.cxx" ]
+ }
+ ]
+}
diff --git a/Examples/javascript/operator/example.h b/Examples/javascript/operator/example.h
new file mode 100644
index 000000000..4da6a2307
--- /dev/null
+++ b/Examples/javascript/operator/example.h
@@ -0,0 +1,36 @@
+/* File : example.h */
+#include <math.h>
+
+class Complex {
+private:
+ double rpart, ipart;
+public:
+ Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
+ Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { }
+ Complex &operator=(const Complex &c) {
+ rpart = c.rpart;
+ ipart = c.ipart;
+ return *this;
+ }
+ Complex operator+(const Complex &c) const {
+ return Complex(rpart+c.rpart, ipart+c.ipart);
+ }
+ Complex operator-(const Complex &c) const {
+ return Complex(rpart-c.rpart, ipart-c.ipart);
+ }
+ Complex operator*(const Complex &c) const {
+ return Complex(rpart*c.rpart - ipart*c.ipart,
+ rpart*c.ipart + c.rpart*ipart);
+ }
+ Complex operator-() const {
+ return Complex(-rpart, -ipart);
+ }
+
+ double re() const { return rpart; }
+ double im() const { return ipart; }
+};
+
+
+
+
+
diff --git a/Examples/javascript/operator/example.i b/Examples/javascript/operator/example.i
new file mode 100644
index 000000000..7a1bd45e1
--- /dev/null
+++ b/Examples/javascript/operator/example.i
@@ -0,0 +1,34 @@
+/* File : example.i */
+%module example
+#pragma SWIG nowarn=SWIGWARN_IGNORE_OPERATOR_EQ
+%{
+#include "example.h"
+%}
+
+/* This header file is a little tough to handle because it has overloaded
+ operators and constructors. We're going to try and deal with that here */
+
+/* This turns the copy constructor in a function ComplexCopy() that can
+ be called */
+
+%rename(assign) Complex::operator=;
+%rename(plus) Complex::operator+;
+%rename(minus) Complex::operator-(const Complex &) const;
+%rename(uminus) Complex::operator-() const;
+%rename(times) Complex::operator*;
+
+/* Now grab the original header file */
+%include "example.h"
+
+/* An output method that turns a complex into a short string */
+%extend Complex {
+ char *toString() {
+ static char temp[512];
+ sprintf(temp,"(%g,%g)", $self->re(), $self->im());
+ return temp;
+ }
+ static Complex* copy(const Complex& c) {
+ return new Complex(c);
+ }
+};
+
diff --git a/Examples/javascript/operator/runme.js b/Examples/javascript/operator/runme.js
new file mode 100644
index 000000000..9dba4b7f9
--- /dev/null
+++ b/Examples/javascript/operator/runme.js
@@ -0,0 +1,25 @@
+var example = require("./build/Release/example");
+
+a = new example.Complex(2,3);
+b = new example.Complex(-5,10);
+
+console.log ("a =" + a);
+console.log ("b =" + b);
+
+c = a.plus(b);
+
+console.log("c =" + c);
+console.log("a*b =" + a.times(b));
+console.log("a-c =" + a.minus(c));
+
+e = example.Complex.copy(a.minus(c));
+console.log("e =" + e);
+
+// Big expression
+f = a.plus(b).times(c.plus(b.times(e))).plus(a.uminus());
+console.log("f =" + f);
+
+
+
+
+