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.cxx", "example_wrap.cxx" ]
}
]
}

View file

@ -0,0 +1,36 @@
/* File : example.c */
#include <iostream>
#include "example.h"
#define M_PI 3.14159
/* A global variable */
namespace nspace
{
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
Circle::Circle(): radius(1.0) {}
Circle::Circle(double r): radius(r) {
std::cout << "created Circle with r=" << radius << std::endl;
}
double Circle::area() {
std::cout << "Circle::area called, r=" << radius << std::endl;
return M_PI*radius*radius;
}
}

View file

@ -0,0 +1,20 @@
namespace nspace {
extern int gcd(int x, int y);
extern double Foo;
class Circle
{
public:
Circle();
Circle(double r);
double area();
double radius;
};
}

View file

@ -0,0 +1,10 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
%feature("nspace", 1);
%include "example.h"

View file

@ -0,0 +1,10 @@
var example = require("./build/Release/example");
console.log("Global variable Foo=" + example.nspace.Foo);
example.nspace.Foo = 5;
console.log("Variable Foo changed to " + example.nspace.Foo);
console.log("GCD of number 6,18 is " + example.nspace.gcd(6,18));
console.log("Creating some objects:");
c = new example.nspace.Circle(10);
console.log("area = " + c.area());