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 @@

View file

@ -0,0 +1,53 @@
/* File : example.h */
#include <string.h>
#ifndef SWIG
struct A {
};
#endif
class Exc {
public:
Exc(int c, const char *m) {
code = c;
strncpy(msg,m,256);
}
int code;
char msg[256];
};
#if defined(_MSC_VER)
#pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
#endif
class Test {
public:
int simple() throw(int) {
throw(37);
return 1;
}
int message() throw(const char *) {
throw("I died.");
return 1;
}
int hosed() throw(Exc) {
throw(Exc(42,"Hosed"));
return 1;
}
int unknown() throw(A*) {
static A a;
throw &a;
return 1;
}
int multi(int x) throw(int, const char *, Exc) {
if (x == 1) throw(37);
if (x == 2) throw("Bleah!");
if (x == 3) throw(Exc(42,"No-go-diggy-die"));
return 1;
}
};
#if defined(_MSC_VER)
#pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
#endif

View file

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

View file

@ -0,0 +1,64 @@
var example = require("./build/Release/example");
console.log("Trying to catch some exceptions.");
t = new example.Test();
try{
t.unknown();
throw -1;
} catch(error)
{
if(error == -1) {
console.log("t.unknown() didn't throw");
} else {
console.log("successfully catched throw in Test::unknown().");
}
}
try{
t.simple();
throw -1;
}
catch(error){
if(error == -1) {
console.log("t.simple() did not throw");
} else {
console.log("successfully catched throw in Test::simple().");
}
}
try{
t.message();
throw -1;
} catch(error){
if(error == -1) {
console.log("t.message() did not throw");
} else {
console.log("successfully catched throw in Test::message().");
}
}
try{
t.hosed();
throw -1;
}
catch(error){
if(error == -1) {
console.log("t.hosed() did not throw");
} else {
console.log("successfully catched throw in Test::hosed().");
}
}
for (var i=1; i<4; i++) {
try{
t.multi(i);
throw -1;
}
catch(error){
if(error == -1) {
console.log("t.multi(" + i + ") did not throw");
} else {
console.log("successfully catched throw in Test::multi().");
}
}
}