Javascript examples.
This commit is contained in:
parent
ecf9f96079
commit
48af60d829
76 changed files with 1685 additions and 0 deletions
21
Examples/javascript/exception/Makefile
Executable file
21
Examples/javascript/exception/Makefile
Executable 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
|
||||
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 @@
|
|||
|
||||
53
Examples/javascript/exception/example.h
Normal file
53
Examples/javascript/exception/example.h
Normal 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
|
||||
|
||||
12
Examples/javascript/exception/example.i
Normal file
12
Examples/javascript/exception/example.i
Normal 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"
|
||||
|
||||
64
Examples/javascript/exception/runme.js
Normal file
64
Examples/javascript/exception/runme.js
Normal 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().");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue