Javascript examples.

This commit is contained in:
Oliver Buchtala 2013-09-27 02:46:11 +02:00
commit 48af60d829
76 changed files with 1685 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "example",
"sources": [ "example_wrap.cxx" ]
}
]
}

View file

@ -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; }
};

View file

@ -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);
}
};

View file

@ -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);