aboutsummaryrefslogtreecommitdiff
path: root/Examples/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/javascript')
-rw-r--r--Examples/javascript/check.list1
-rw-r--r--Examples/javascript/enum/runme.js2
-rw-r--r--Examples/javascript/exception/example.h18
-rw-r--r--Examples/javascript/exception/example.i6
-rw-r--r--Examples/javascript/exception/runme.js10
-rw-r--r--Examples/javascript/native/Makefile3
-rw-r--r--Examples/javascript/native/binding.gyp.in9
-rw-r--r--Examples/javascript/native/example.i47
-rw-r--r--Examples/javascript/native/example.js1
-rw-r--r--Examples/javascript/native/index.html31
-rw-r--r--Examples/javascript/native/runme.js3
-rw-r--r--Examples/javascript/variables/runme.js2
12 files changed, 113 insertions, 20 deletions
diff --git a/Examples/javascript/check.list b/Examples/javascript/check.list
index 9707e77d4..977835755 100644
--- a/Examples/javascript/check.list
+++ b/Examples/javascript/check.list
@@ -3,6 +3,7 @@ constant
enum
exception
functor
+native
nspace
operator
overload
diff --git a/Examples/javascript/enum/runme.js b/Examples/javascript/enum/runme.js
index 851d43c4b..f3264889c 100644
--- a/Examples/javascript/enum/runme.js
+++ b/Examples/javascript/enum/runme.js
@@ -30,5 +30,5 @@ f.enum_test(example.Foo.LUDICROUS);
// enum value BLUE of enum color is accessed as property of cconst
console.log("example.BLUE= " + example.BLUE);
-// enum value LUDICROUS of enum Foo::speed is accessed as as property of cconst
+// enum value LUDICROUS of enum Foo::speed is accessed as property of cconst
console.log("example.speed.LUDICROUS= " + example.Foo.LUDICROUS);
diff --git a/Examples/javascript/exception/example.h b/Examples/javascript/exception/example.h
index 7cf917d01..bc744cda7 100644
--- a/Examples/javascript/exception/example.h
+++ b/Examples/javascript/exception/example.h
@@ -16,30 +16,26 @@ public:
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) {
+ int simple() {
throw(37);
return 1;
}
- int message() throw(const char *) {
+ int message() {
throw("I died.");
return 1;
}
- int hosed() throw(Exc) {
+ int hosed() {
throw(Exc(42,"Hosed"));
return 1;
}
- int unknown() throw(A*) {
+ int unknown() {
static A a;
throw &a;
return 1;
}
- int multi(int x) throw(int, const char *, Exc) {
+ int multi(int x) {
if (x == 1) throw(37);
if (x == 2) throw("Bleah!");
if (x == 3) throw(Exc(42,"No-go-diggy-die"));
@@ -47,7 +43,3 @@ public:
}
};
-#if defined(_MSC_VER)
- #pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
-#endif
-
diff --git a/Examples/javascript/exception/example.i b/Examples/javascript/exception/example.i
index 08672c3a8..ca1084452 100644
--- a/Examples/javascript/exception/example.i
+++ b/Examples/javascript/exception/example.i
@@ -7,6 +7,12 @@
%include "std_string.i"
+%catches(int) Test::simple();
+%catches(const char *) Test::message();
+%catches(Exc) Test::hosed();
+%catches(A*) Test::unknown();
+%catches(int, const char *, Exc) Test::multi(int x);
+
/* Let's just grab the original header file here */
%include "example.h"
diff --git a/Examples/javascript/exception/runme.js b/Examples/javascript/exception/runme.js
index 43ce66d6d..1001a7111 100644
--- a/Examples/javascript/exception/runme.js
+++ b/Examples/javascript/exception/runme.js
@@ -10,7 +10,7 @@ try{
if(error == -1) {
console.log("t.unknown() didn't throw");
} else {
- console.log("successfully catched throw in Test::unknown().");
+ console.log("successfully caught throw in Test::unknown().");
}
}
@@ -22,7 +22,7 @@ catch(error){
if(error == -1) {
console.log("t.simple() did not throw");
} else {
- console.log("successfully catched throw in Test::simple().");
+ console.log("successfully caught throw in Test::simple().");
}
}
@@ -33,7 +33,7 @@ try{
if(error == -1) {
console.log("t.message() did not throw");
} else {
- console.log("successfully catched throw in Test::message().");
+ console.log("successfully caught throw in Test::message().");
}
}
@@ -45,7 +45,7 @@ catch(error){
if(error == -1) {
console.log("t.hosed() did not throw");
} else {
- console.log("successfully catched throw in Test::hosed().");
+ console.log("successfully caught throw in Test::hosed().");
}
}
@@ -58,7 +58,7 @@ for (var i=1; i<4; i++) {
if(error == -1) {
console.log("t.multi(" + i + ") did not throw");
} else {
- console.log("successfully catched throw in Test::multi().");
+ console.log("successfully caught throw in Test::multi().");
}
}
}
diff --git a/Examples/javascript/native/Makefile b/Examples/javascript/native/Makefile
new file mode 100644
index 000000000..0402f8d09
--- /dev/null
+++ b/Examples/javascript/native/Makefile
@@ -0,0 +1,3 @@
+SRCS =
+
+include $(SRCDIR)../example.mk
diff --git a/Examples/javascript/native/binding.gyp.in b/Examples/javascript/native/binding.gyp.in
new file mode 100644
index 000000000..59779aef4
--- /dev/null
+++ b/Examples/javascript/native/binding.gyp.in
@@ -0,0 +1,9 @@
+{
+ "targets": [
+ {
+ "target_name": "example",
+ "sources": [ "example_wrap.cxx" ],
+ "include_dirs": ["$srcdir"]
+ }
+ ]
+}
diff --git a/Examples/javascript/native/example.i b/Examples/javascript/native/example.i
new file mode 100644
index 000000000..8c6160060
--- /dev/null
+++ b/Examples/javascript/native/example.i
@@ -0,0 +1,47 @@
+/* File : example.i */
+%module example
+
+// placeholder() used to help SWIG generate "SWIG_From_int" call
+%{
+ int placeholder();
+%}
+int placeholder() { return 0; }
+
+// actual demo code
+%wrapper
+%{
+#ifdef SWIG_V8_VERSION /* Engine: Node || V8 */
+
+ static SwigV8ReturnValue JavaScript_do_work(const SwigV8Arguments &args) {
+ SWIGV8_HANDLESCOPE();
+ const int MY_MAGIC_NUMBER = 5;
+ v8::Handle<v8::Value> jsresult =
+ SWIG_From_int(static_cast< int >(MY_MAGIC_NUMBER));
+ if (args.Length() != 0)
+ SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments.");
+ SWIGV8_RETURN(jsresult);
+ fail:
+ SWIGV8_RETURN(SWIGV8_UNDEFINED());
+ }
+
+#else /* Engine: JavaScriptCore */
+
+ static JSValueRef JavaScript_do_work(JSContextRef context,
+ JSObjectRef function, JSObjectRef thisObject, size_t argc,
+ const JSValueRef argv[], JSValueRef* exception) {
+ const int MY_MAGIC_NUMBER = 5;
+ JSValueRef jsresult =
+ SWIG_From_int SWIG_JSC_FROM_CALL_ARGS(
+ static_cast< int >(MY_MAGIC_NUMBER));
+ if (argc != 0)
+ SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments.");
+ return jsresult;
+ fail:
+ return JSValueMakeUndefined(context);
+ }
+
+#endif
+%}
+
+
+%native(magicNumber) void JavaScript_do_work();
diff --git a/Examples/javascript/native/example.js b/Examples/javascript/native/example.js
new file mode 100644
index 000000000..2e7f83a06
--- /dev/null
+++ b/Examples/javascript/native/example.js
@@ -0,0 +1 @@
+module.exports = require("build/Release/example");
diff --git a/Examples/javascript/native/index.html b/Examples/javascript/native/index.html
new file mode 100644
index 000000000..7c7d6b071
--- /dev/null
+++ b/Examples/javascript/native/index.html
@@ -0,0 +1,31 @@
+<html>
+<head>
+<title>SWIG:Examples:javascript:native</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/javascript/native/</tt>
+<hr>
+
+<H2>Manually wrapped callback function in JavaScript</H2>
+
+<p>
+This example demonstrates how to manually add callback feature support to a SWIG module.
+</p>
+
+<ul>
+<li><a href="example.i">example.i</a>. Interface file containing the API function and async behind-the-scenes functions.
+<li><a href="runme.java">runme.js</a>. Sample JavaScript program showing the API function being called with a callback function parameter.
+</ul>
+
+<h2>Notes</h2>
+
+The V8 code queues the callback request for processing using the UV interface. An async function callback is invoked when the system is ready to process the next request. When the async function finishes, a completion function callback is invoked to finalize the request. Here the callback function parameter is invoked.
+<br/><br/>
+UV request queueing is only necessary for operations that would take a really long or otherwise unpredictable amount of time (async operations). A callback parameter could also be invoked immediately within the API function.
+
+<hr>
+</body>
+</html>
diff --git a/Examples/javascript/native/runme.js b/Examples/javascript/native/runme.js
new file mode 100644
index 000000000..b5e14d037
--- /dev/null
+++ b/Examples/javascript/native/runme.js
@@ -0,0 +1,3 @@
+var example = require("example");
+
+console.log("My magic number is: ", example.magicNumber());
diff --git a/Examples/javascript/variables/runme.js b/Examples/javascript/variables/runme.js
index a2b5f791c..36ddc5fe9 100644
--- a/Examples/javascript/variables/runme.js
+++ b/Examples/javascript/variables/runme.js
@@ -44,7 +44,7 @@ example.print_vars();
console.log("\nNow I'm going to try and modify some read only variables");
-console.log("Tring to set 'path'");
+console.log("Trying to set 'path'");
try{
example.path = "Whoa!";
console.log("Hey, what's going on?!?! This shouldn't work");