diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2019-08-20 18:53:02 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2019-08-20 18:53:02 +0100 |
commit | 6925ec796b9d10412b836917b69edbc29d9dc1d5 (patch) | |
tree | 041aad4a3ec40dd2555e6e111a06266899da0ea3 | |
parent | 5c2e0b2c5e07a91a79daeee7424ba3a601b42da0 (diff) | |
parent | 025269f121832f8fe46d299faf426ff37d289699 (diff) | |
download | swig-6925ec796b9d10412b836917b69edbc29d9dc1d5.tar.gz |
Merge branch 'contrib/TekuConcept'
* contrib/TekuConcept:
Dev Checkpoint 201908200213
Dev Checkpoint 201906261312
Dev Checkpoint 201906252227
Dev Checkpoint 201906252221
Dev Checkpoint 201906252210
Dev Checkpoint 201906252113
Add JS Native Directive Testcase
JS Example Campatibility Update
Add Native Directive Example
Update JavaScript Documentation
Add JS Native Wrapper API
-rw-r--r-- | Doc/Manual/Javascript.html | 1 | ||||
-rw-r--r-- | Examples/javascript/check.list | 1 | ||||
-rw-r--r-- | Examples/javascript/native/Makefile | 3 | ||||
-rw-r--r-- | Examples/javascript/native/binding.gyp.in | 9 | ||||
-rw-r--r-- | Examples/javascript/native/example.i | 47 | ||||
-rw-r--r-- | Examples/javascript/native/example.js | 1 | ||||
-rw-r--r-- | Examples/javascript/native/index.html | 31 | ||||
-rw-r--r-- | Examples/javascript/native/runme.js | 3 | ||||
-rw-r--r-- | Examples/test-suite/javascript/native_directive_runme.js | 9 | ||||
-rw-r--r-- | Examples/test-suite/native_directive.i | 59 | ||||
-rw-r--r-- | Source/Modules/javascript.cxx | 26 |
11 files changed, 189 insertions, 1 deletions
diff --git a/Doc/Manual/Javascript.html b/Doc/Manual/Javascript.html index 417ee4585..0b301377c 100644 --- a/Doc/Manual/Javascript.html +++ b/Doc/Manual/Javascript.html @@ -163,7 +163,6 @@ $ make check-javascript-examples V8_VERSION=0x032530 ENGINE=v8</pre> <li><p>Multiple output arguments do not work for JSC</p></li> <li><p>C89 incompatibility: the JSC generator might still generate C89 violating code</p></li> <li><p><code>long long</code> is not supported</p></li> - <li><p><code>%native</code> is not supported</p></li> <li><p>Javascript callbacks are not supported</p></li> <li><p><code>instanceOf</code> does not work under JSC</p></li> </ul> 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/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/test-suite/javascript/native_directive_runme.js b/Examples/test-suite/javascript/native_directive_runme.js new file mode 100644 index 000000000..5c1d69cf1 --- /dev/null +++ b/Examples/test-suite/javascript/native_directive_runme.js @@ -0,0 +1,9 @@ +var native_directive = require("native_directive"); + +(function main() { + var s = "abc.DEF-123"; + if (native_directive.CountAlphas(s) !== 6) + throw "CountAlphas failed"; + if (native_directive.CountAlphaCharacters(s) !== 6) + throw "CountAlphaCharacters failed"; +})(); diff --git a/Examples/test-suite/native_directive.i b/Examples/test-suite/native_directive.i index d08c9a954..9ae76e0b7 100644 --- a/Examples/test-suite/native_directive.i +++ b/Examples/test-suite/native_directive.i @@ -41,3 +41,62 @@ extern "C" JNIEXPORT jint JNICALL Java_native_1directive_native_1directiveJNI_Co %} #endif + +// TODO: C# +// TODO: Python + + +#ifdef SWIGJAVASCRIPT +%native(CountAlphaCharacters) void JavaScript_alpha_count(); +%{ +#ifdef SWIG_V8_VERSION /* engine = node || v8 */ + +static SwigV8ReturnValue JavaScript_alpha_count(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + v8::Handle<v8::Value> jsresult; + char *arg1 = (char *)0; + int res1; + char *buf1 = 0; + int alloc1 = 0; + int result; + if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_alpha_count."); + res1 = SWIG_AsCharPtrAndSize(args[0], &buf1, NULL, &alloc1); + if (!SWIG_IsOK(res1)) + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "alpha_count" "', argument " "1"" of type '" "char const *""'"); + arg1 = reinterpret_cast< char * >(buf1); + result = (int)alpha_count((char const *)arg1); + jsresult = SWIG_From_int(static_cast< int >(result)); + if (alloc1 == SWIG_NEWOBJ) delete[] buf1; + SWIGV8_RETURN(jsresult); +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + +#else /* engine = jsc */ + +static JSValueRef JavaScript_alpha_count(JSContextRef context, JSObjectRef function, + JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) +{ + char *arg1 = (char *)0; + int res1; + char *buf1 = 0; + int alloc1 = 0; + int result; + JSValueRef jsresult; + if (argc != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments."); + res1 = SWIG_JSC_AsCharPtrAndSize(context, argv[0], &buf1, NULL, &alloc1); + if (!SWIG_IsOK(res1)) + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "alpha_count" "', argument " "1"" of type '" "char const *""'"); + arg1 = reinterpret_cast< char * >(buf1); + result = (int)alpha_count((char const *)arg1); + jsresult = SWIG_From_int SWIG_JSC_FROM_CALL_ARGS(static_cast< int >(result)); + if (alloc1 == SWIG_NEWOBJ) delete[] buf1; + return jsresult; +fail: + return JSValueMakeUndefined(context); +} + +#endif /* engine */ +%} +#endif /* SWIGJAVASCRIPT */ + diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index 8c87330b7..d2b33b1b4 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -198,6 +198,11 @@ public: virtual int emitWrapperFunction(Node *n); /** + * Invoked by nativeWrapper callback + */ + virtual int emitNativeFunction(Node *n); + + /** * Invoked from constantWrapper after call to Language::constantWrapper. **/ virtual int emitConstant(Node *n); @@ -311,6 +316,7 @@ public: virtual int classHandler(Node *n); virtual int functionWrapper(Node *n); virtual int constantWrapper(Node *n); + virtual int nativeWrapper(Node *n); virtual void main(int argc, char *argv[]); virtual int top(Node *n); @@ -442,6 +448,18 @@ int JAVASCRIPT::constantWrapper(Node *n) { } /* --------------------------------------------------------------------- + * nativeWrapper() + * + * Function wrapper for generating placeholders for native functions + * --------------------------------------------------------------------- */ + +int JAVASCRIPT::nativeWrapper(Node *n) { + emitter->emitNativeFunction(n); + + return SWIG_OK; +} + +/* --------------------------------------------------------------------- * classHandler() * * Function handler for generating wrappers for class @@ -768,6 +786,14 @@ int JSEmitter::emitWrapperFunction(Node *n) { return ret; } +int JSEmitter::emitNativeFunction(Node *n) { + String *wrapname = Getattr(n, "wrap:name"); + enterFunction(n); + state.function(WRAPPER_NAME, wrapname); + exitFunction(n); + return SWIG_OK; +} + int JSEmitter::enterClass(Node *n) { state.clazz(RESET); state.clazz(NAME, Getattr(n, "sym:name")); |