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,46 @@
/* File : example.cxx */
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
# define _CRT_SECURE_NO_DEPRECATE
#endif
#include "example.h"
#include <stdio.h>
#include <stdlib.h>
Vector operator+(const Vector &a, const Vector &b) {
Vector r;
r.x = a.x + b.x;
r.y = a.y + b.y;
r.z = a.z + b.z;
return r;
}
char *Vector::print() {
static char temp[512];
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
return temp;
}
VectorArray::VectorArray(int size) {
items = new Vector[size];
maxsize = size;
}
VectorArray::~VectorArray() {
delete [] items;
}
Vector &VectorArray::operator[](int index) {
if ((index < 0) || (index >= maxsize)) {
printf("Panic! Array index out of bounds.\n");
exit(1);
}
return items[index];
}
int VectorArray::size() {
return maxsize;
}

View file

@ -0,0 +1,26 @@
/* File : example.h */
class Vector {
private:
double x,y,z;
public:
Vector() : x(0), y(0), z(0) { };
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
friend Vector operator+(const Vector &a, const Vector &b);
char *print();
};
class VectorArray {
private:
Vector *items;
int maxsize;
public:
VectorArray(int maxsize);
~VectorArray();
Vector &operator[](int);
int size();
};

View file

@ -0,0 +1,42 @@
/* File : example.i */
/* This file has a few "typical" uses of C++ references. */
%module example
%{
#include "example.h"
%}
class Vector {
public:
Vector(double x, double y, double z);
~Vector();
char *print();
};
/* This helper function calls an overloaded operator */
%inline %{
Vector addv(Vector &a, Vector &b) {
return a+b;
}
%}
/* Wrapper around an array of vectors class */
class VectorArray {
public:
VectorArray(int maxsize);
~VectorArray();
int size();
/* This wrapper provides an alternative to the [] operator */
%extend {
Vector &get(int index) {
return (*$self)[index];
}
void set(int index, Vector &a) {
(*$self)[index] = a;
}
}
};

View file

@ -0,0 +1,67 @@
// This file illustrates the manipulation of C++ references in Javascript.
var example = require("./build/Release/example");
// ----- Object creation -----
console.log("Creating some objects:\n");
a = new example.Vector(3,4,5);
b = new example.Vector(10,11,12);
console.log(" created" + a.print());
console.log(" created" + b.print());
// ----- Call an overloaded operator -----
// This calls the wrapper we placed around operator+(const Vector &a, const Vector &)
// It returns a new allocated object.
console.log("Adding a+b\n");
c = example.addv(a, b);
console.log("a+b = " + c.print());
// TODO: Note: Unless we free the result, a memory leak will occur
//delete_Vector(c);
// ----- Create a vector array -----
// Note: Using the high-level interface here
console.log("Creating an array of vectors\n");
va = new example.VectorArray(10);
console.log("va = " + va + "\n");
// ----- Set some values in the array -----
// These operators copy the value of a and b to the vector array
va.set(0,a);
va.set(1,b);
// This will work, but it will cause a memory leak!
va.set(2,example.addv(a,b));
// The non-leaky way to do it
//c = addv(a,b);
//va.set(3,c);
//delete_Vector(c);
// Get some values from the array
console.log("Getting some array values\n");
for (i = 0; i < 5; i++) {
temp = va.get(i);
console.log(i,temp.print());
}
// Watch under resource meter to check on this
console.log("Making sure we don't leak memory.\n");
for (i = 0; i < 1000000; i++) {
c = va.get(i % 10);
}
//---------TODO---------
//----- Clean up -----
//console.log("Cleaning up\n");
//example.delete_VectorArray(va);
//example.delete_Vector(a);
//example.delete_Vector(b);

View file

@ -0,0 +1,22 @@
Loaded swig printers
SwigStringPrinter: Could not convert const char* to string
SwigListIterator: Construction failed.
Cannot access memory at address 0x7d894828ec834853.
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not convert const char* to string
SwigListIterator: Construction failed.
Cannot access memory at address 0x7d894828ec834853.
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not dereference struct String*
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not dereference struct String*
SwigStringPrinter: Could not dereference struct String*
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not dereference struct String*
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not convert const char* to string
SwigStringPrinter: Could not dereference struct String*
SwigStringPrinter: Could not dereference struct String*