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
This commit is contained in:
William S Fulton 2019-08-20 18:53:02 +01:00
commit 6925ec796b
11 changed files with 189 additions and 1 deletions

View file

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

View file

@ -3,6 +3,7 @@ constant
enum
exception
functor
native
nspace
operator
overload

View file

@ -0,0 +1,3 @@
SRCS =
include $(SRCDIR)../example.mk

View file

@ -0,0 +1,9 @@
{
"targets": [
{
"target_name": "example",
"sources": [ "example_wrap.cxx" ],
"include_dirs": ["$srcdir"]
}
]
}

View file

@ -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();

View file

@ -0,0 +1 @@
module.exports = require("build/Release/example");

View file

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

View file

@ -0,0 +1,3 @@
var example = require("example");
console.log("My magic number is: ", example.magicNumber());

View file

@ -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";
})();

View file

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

View file

@ -197,6 +197,11 @@ public:
*/
virtual int emitWrapperFunction(Node *n);
/**
* Invoked by nativeWrapper callback
*/
virtual int emitNativeFunction(Node *n);
/**
* Invoked from constantWrapper after call to Language::constantWrapper.
**/
@ -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);
@ -441,6 +447,18 @@ int JAVASCRIPT::constantWrapper(Node *n) {
return SWIG_OK;
}
/* ---------------------------------------------------------------------
* nativeWrapper()
*
* Function wrapper for generating placeholders for native functions
* --------------------------------------------------------------------- */
int JAVASCRIPT::nativeWrapper(Node *n) {
emitter->emitNativeFunction(n);
return SWIG_OK;
}
/* ---------------------------------------------------------------------
* classHandler()
*
@ -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"));