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,28 @@
/* File : example.c */
#include <iostream>
#include "example.h"
#define M_PI 3.14159265358979323846
/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
x += dx;
y += dy;
}
int Shape::nshapes = 0;
double Circle::area(void) {
return M_PI*radius*radius;
}
double Circle::perimeter(void) {
return 2*M_PI*radius;
}
double Square::area(void) {
return width*width;
}
double Square::perimeter(void) {
return 4*width;
}

View file

@ -0,0 +1,34 @@
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area(void);
virtual double perimeter(void);
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area(void);
virtual double perimeter(void);
};

View file

@ -0,0 +1,10 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -0,0 +1,46 @@
var example = require("./build/Release/example");
// ----- Object creation -----
console.log("Creating some objects:");
c = new example.Circle(10);
console.log("Created circle " + c);
s = new example.Square(10);
console.log("Created square " + s);
// ----- Access a static member -----
console.log("\nA total of " + example.Shape.nshapes + " shapes were created"); // access static member as properties of the class object
// ----- Member data access -----
// Set the location of the object.
// Note: methods in the base class Shape are used since
// x and y are defined there.
c.x = 20;
c.y = 30;
s.x = -10;
s.y = 5;
console.log("\nHere is their new position:");
console.log("Circle = (" + c.x + "," + c.y + ")");
console.log("Square = (" + s.x + "," + s.y + ")");
// ----- Call some methods -----
console.log("\nHere are some properties of the shapes:");
console.log("Circle:");
console.log("area = " + c.area() + "");
console.log("perimeter = " + c.perimeter() + "");
console.log("\n");
console.log("Square:");
console.log("area = " + s.area() + "");
console.log("perimeter = " + s.perimeter() + "");
// ----- Delete everything -----
console.log("\nGuess I'll clean up now");
// Note: this invokes the virtual destructor
delete c;
delete s;
console.log(example.Shape.nshapes + " shapes remain");
console.log("Goodbye");