Merge branch 'devel' of https://github.com/Neha03/gsoc2012-javascript into devel
Conflicts: .project COPYRIGHT Doc/Manual/style.css Examples/Makefile.in Examples/test-suite/common.mk Lib/typemaps/strings.swg Makefile.in Source/DOH/fio.c Source/Makefile.am Source/Modules/emit.cxx Source/Modules/javascript.cxx configure.in git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13764 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
360ef44e09
commit
050219d998
136 changed files with 7987 additions and 141 deletions
24
Examples/javascript/reference/Makefile
Executable file
24
Examples/javascript/reference/Makefile
Executable file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cpp
|
||||
JSCXXSRCS = $(TOP)/../Tools/javascript/javascript.cxx
|
||||
JAVASCRIPT_EXE = $(TOP)/../Tools/javascript/javascript
|
||||
JAVASCRIPT_MODULE = example
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -I$(TOP)/../Lib/javascript -I$(TOP)/../Lib/javascript/jsc
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile javascript_clean
|
||||
|
||||
check:: all
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' javascript_exe
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
JAVASCRIPT_EXE='$(JAVASCRIPT_EXE)' JAVASCRIPT_MODULE='$(JAVASCRIPT_MODULE)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
||||
46
Examples/javascript/reference/example.cpp
Executable file
46
Examples/javascript/reference/example.cpp
Executable 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;
|
||||
}
|
||||
|
||||
26
Examples/javascript/reference/example.h
Executable file
26
Examples/javascript/reference/example.h
Executable 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();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
42
Examples/javascript/reference/example.i
Executable file
42
Examples/javascript/reference/example.i
Executable 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
67
Examples/javascript/reference/runme.js
Executable file
67
Examples/javascript/reference/runme.js
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
// This file illustrates the manipulation of C++ references in Javascript.
|
||||
// TODO: deleteion of vector objects created here
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
print("Creating some objects:\n");
|
||||
a = new example.Vector(3,4,5);
|
||||
b = new example.Vector(10,11,12);
|
||||
|
||||
print(" created" + a.print());
|
||||
print(" 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.
|
||||
|
||||
print("Adding a+b\n");
|
||||
c = example.addv(a, b);
|
||||
print("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
|
||||
print("Creating an array of vectors\n");
|
||||
va = new example.VectorArray(10);
|
||||
print("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
|
||||
|
||||
print("Getting some array values\n");
|
||||
for (i = 0; i < 5; i++) {
|
||||
temp = va.get(i);
|
||||
print(i,temp.print());
|
||||
}
|
||||
|
||||
// Watch under resource meter to check on this
|
||||
print("Making sure we don't leak memory.\n");
|
||||
for (i = 0; i < 1000000; i++) {
|
||||
c = va.get(i % 10);
|
||||
}
|
||||
//---------TODO---------
|
||||
//----- Clean up -----
|
||||
//print("Cleaning up\n");
|
||||
|
||||
//example.delete_VectorArray(va);
|
||||
//example.delete_Vector(a);
|
||||
//example.delete_Vector(b);
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue