Convert javascript examples to nodejs.
This commit is contained in:
parent
dbf2b1fe94
commit
5da54ca435
48 changed files with 312 additions and 223 deletions
|
|
@ -1,4 +1,3 @@
|
|||
// file: runme.js
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
// ----- Object creation -----
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS =
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript
|
||||
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
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/constant/binding.gyp
Normal file
8
Examples/javascript/constant/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
// file: runme.js
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
print("ICONST = " + example.ICONST + " (should be 42)\n");
|
||||
print("FCONST = " + example.FCONST + " (should be 2.1828)\n");
|
||||
print("CCONST = " + example.CCONST + " (should be 'x')\n");
|
||||
print("CCONST2 = " + example.CCONST2 + " (this should be on a new line)\n");
|
||||
print("SCONST = " + example.SCONST + " (should be 'Hello World')\n");
|
||||
print("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')\n");
|
||||
print("EXPR = " + example.EXPR + " (should be 48.5484)\n");
|
||||
print("iconst = " + example.iconst + " (should be 37)\n");
|
||||
print("fconst = " + example.fconst + " (should be 3.14)\n");
|
||||
console.log("ICONST = " + example.ICONST + " (should be 42)\n");
|
||||
console.log("FCONST = " + example.FCONST + " (should be 2.1828)\n");
|
||||
console.log("CCONST = " + example.CCONST + " (should be 'x')\n");
|
||||
console.log("CCONST2 = " + example.CCONST2 + " (this should be on a new line)\n");
|
||||
console.log("SCONST = " + example.SCONST + " (should be 'Hello World')\n");
|
||||
console.log("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')\n");
|
||||
console.log("EXPR = " + example.EXPR + " (should be 48.5484)\n");
|
||||
console.log("iconst = " + example.iconst + " (should be 37)\n");
|
||||
console.log("fconst = " + example.fconst + " (should be 3.14)\n");
|
||||
|
||||
print("EXTERN = " + example.EXTERN + " (should be undefined)\n");
|
||||
print("FOO = " + example.FOO + " (should be undefined)\n");
|
||||
console.log("EXTERN = " + example.EXTERN + " (should be undefined)\n");
|
||||
console.log("FOO = " + example.FOO + " (should be undefined)\n");
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cpp
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
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
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/enum/binding.gyp
Normal file
8
Examples/javascript/enum/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
37
Examples/javascript/enum/example.cxx
Executable file
37
Examples/javascript/enum/example.cxx
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void Foo::enum_test(speed s) {
|
||||
if (s == IMPULSE) {
|
||||
printf("IMPULSE speed\n");
|
||||
} else if (s == WARP) {
|
||||
printf("WARP speed\n");
|
||||
} else if (s == LUDICROUS) {
|
||||
printf("LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("Unknown speed\n");
|
||||
}
|
||||
}
|
||||
|
||||
void enum_test(color c, Foo::speed s) {
|
||||
if (c == RED) {
|
||||
printf("color = RED, ");
|
||||
} else if (c == BLUE) {
|
||||
printf("color = BLUE, ");
|
||||
} else if (c == GREEN) {
|
||||
printf("color = GREEN, ");
|
||||
} else {
|
||||
printf("color = Unknown color!, ");
|
||||
}
|
||||
if (s == Foo::IMPULSE) {
|
||||
printf("speed = IMPULSE speed\n");
|
||||
} else if (s == Foo::WARP) {
|
||||
printf("speed = WARP speed\n");
|
||||
} else if (s == Foo::LUDICROUS) {
|
||||
printf("speed = LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("speed = Unknown speed!\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +1,26 @@
|
|||
// file: runme.py
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
// Print out the value of some enums
|
||||
print("*** color ***");
|
||||
print(" RED =" + example.RED);
|
||||
print(" BLUE =" + example.BLUE);
|
||||
print(" GREEN =" + example.GREEN);
|
||||
console.log("*** color ***");
|
||||
console.log(" RED =" + example.RED);
|
||||
console.log(" BLUE =" + example.BLUE);
|
||||
console.log(" GREEN =" + example.GREEN);
|
||||
|
||||
print("\n*** Foo::speed ***");
|
||||
print(" Foo_IMPULSE =" + example.Foo.IMPULSE);
|
||||
print(" Foo_WARP =" + example.Foo.WARP);
|
||||
print(" Foo_LUDICROUS =" + example.Foo.LUDICROUS);
|
||||
console.log("\n*** Foo::speed ***");
|
||||
console.log(" Foo_IMPULSE =" + example.Foo.IMPULSE);
|
||||
console.log(" Foo_WARP =" + example.Foo.WARP);
|
||||
console.log(" Foo_LUDICROUS =" + example.Foo.LUDICROUS);
|
||||
|
||||
print("\nTesting use of enums with functions\n");
|
||||
console.log("\nTesting use of enums with functions\n");
|
||||
|
||||
example.enum_test(example.RED, example.Foo.IMPULSE);
|
||||
example.enum_test(example.BLUE, example.Foo.WARP);
|
||||
example.enum_test(example.GREEN, example.Foo.LUDICROUS);
|
||||
example.enum_test(1234,5678);
|
||||
|
||||
print("\nTesting use of enum with class method");
|
||||
console.log("\nTesting use of enum with class method");
|
||||
f = new example.Foo();
|
||||
|
||||
f.enum_test(example.Foo.IMPULSE);
|
||||
|
|
@ -28,7 +28,7 @@ f.enum_test(example.Foo.WARP);
|
|||
f.enum_test(example.Foo.LUDICROUS);
|
||||
|
||||
// enum value BLUE of enum color is accessed as property of cconst
|
||||
print("example.BLUE= " + example.BLUE);
|
||||
console.log("example.BLUE= " + example.BLUE);
|
||||
|
||||
// enum value LUDICROUS of enum Foo::speed is accessed as as property of cconst
|
||||
print("example.speed.LUDICROUS= " + example.Foo.LUDICROUS);
|
||||
console.log("example.speed.LUDICROUS= " + example.Foo.LUDICROUS);
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cpp
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
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
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/exception/binding.gyp
Normal file
8
Examples/javascript/exception/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
1
Examples/javascript/exception/example.cxx
Normal file
1
Examples/javascript/exception/example.cxx
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
//file: runme.js
|
||||
// Throw a lot of exceptions
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
print("Trying to catch some exceptions.");
|
||||
console.log("Trying to catch some exceptions.");
|
||||
t = new example.Test();
|
||||
try{
|
||||
t.unknown();
|
||||
|
|
@ -9,9 +8,9 @@ try{
|
|||
} catch(error)
|
||||
{
|
||||
if(error == -1) {
|
||||
print("t.unknown() didn't throw");
|
||||
console.log("t.unknown() didn't throw");
|
||||
} else {
|
||||
print("successfully catched throw in Test::unknown().");
|
||||
console.log("successfully catched throw in Test::unknown().");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -21,9 +20,9 @@ try{
|
|||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.simple() did not throw");
|
||||
console.log("t.simple() did not throw");
|
||||
} else {
|
||||
print("successfully catched throw in Test::simple().");
|
||||
console.log("successfully catched throw in Test::simple().");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -32,9 +31,9 @@ try{
|
|||
throw -1;
|
||||
} catch(error){
|
||||
if(error == -1) {
|
||||
print("t.message() did not throw");
|
||||
console.log("t.message() did not throw");
|
||||
} else {
|
||||
print("successfully catched throw in Test::message().");
|
||||
console.log("successfully catched throw in Test::message().");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,9 +43,9 @@ try{
|
|||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.hosed() did not throw");
|
||||
console.log("t.hosed() did not throw");
|
||||
} else {
|
||||
print("successfully catched throw in Test::hosed().");
|
||||
console.log("successfully catched throw in Test::hosed().");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,9 +56,9 @@ for (var i=1; i<4; i++) {
|
|||
}
|
||||
catch(error){
|
||||
if(error == -1) {
|
||||
print("t.multi(" + i + ") did not throw");
|
||||
console.log("t.multi(" + i + ") did not throw");
|
||||
} else {
|
||||
print("successfully catched throw in Test::multi().");
|
||||
console.log("successfully catched throw in Test::multi().");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cpp
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
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
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/functor/binding.gyp
Normal file
8
Examples/javascript/functor/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
0
Examples/javascript/functor/example.cxx
Normal file
0
Examples/javascript/functor/example.cxx
Normal file
|
|
@ -1,5 +1,4 @@
|
|||
// Operator overloading example
|
||||
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
a = new example.intSum(0);
|
||||
b = new example.doubleSum(100.0);
|
||||
|
|
@ -11,6 +10,6 @@ for (i=1;i<=100;i++)
|
|||
a.call(i); // Note: function call
|
||||
b.call(Math.sqrt(i)); // Note: function call
|
||||
|
||||
print(a.result());
|
||||
print(b.result());
|
||||
console.log(a.result());
|
||||
console.log(b.result());
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cpp
|
||||
CXXSRCS = example.cxx
|
||||
JS_SCRIPT = runme.js
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
wrapper::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' javascript_cpp
|
||||
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
|
||||
|
||||
javascript_exe::
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' javascript_exe
|
||||
|
||||
check:: all
|
||||
check:: build
|
||||
$(MAKE) -f $(TOP)/Makefile JSCXXSRCS='$(JSCXXSRCS)' TARGET='$(TARGET)' \
|
||||
TOP='$(TOP)' JS_SCRIPT='$(JS_SCRIPT)' javascript_run
|
||||
|
|
|
|||
8
Examples/javascript/namespace/binding.gyp
Normal file
8
Examples/javascript/namespace/binding.gyp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "example",
|
||||
"sources": [ "example.cxx", "example_wrap.cxx" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
36
Examples/javascript/namespace/example.cxx
Normal file
36
Examples/javascript/namespace/example.cxx
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
print("Global variable Foo=" + example.nspace.Foo);
|
||||
example.nspace.Foo = 5;
|
||||
print("Variable Foo changed to " + example.nspace.Foo);
|
||||
print("GCD of number 6,18 is " + example.nspace.gcd(6,18));
|
||||
var example = require("./build/Release/example");
|
||||
|
||||
print("Creating some objects:");
|
||||
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);
|
||||
print("area = " + c.area());
|
||||
|
||||
|
||||
|
||||
console.log("area = " + c.area());
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue