Dev Checkpoint 201906252113
This commit is contained in:
parent
2a8cb127ec
commit
e7993dca97
3 changed files with 79 additions and 107 deletions
|
|
@ -4,100 +4,37 @@
|
||||||
|
|
||||||
%wrapper
|
%wrapper
|
||||||
%{
|
%{
|
||||||
#include <cstring>
|
#ifdef SWIG_V8_VERSION /* Engine: Node || V8 */
|
||||||
#include <string>
|
|
||||||
|
static SwigV8ReturnValue JavaScript_do_work(const SwigV8Arguments &args) {
|
||||||
|
SWIGV8_HANDLESCOPE();
|
||||||
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903)
|
const int MY_MAGIC_NUMBER = 5;
|
||||||
typedef v8::Handle<v8::Value> V8ReturnValue;
|
v8::Handle<v8::Value> jsresult =
|
||||||
typedef v8::Arguments V8Arguments;
|
SWIG_From_int(static_cast< int >(MY_MAGIC_NUMBER));
|
||||||
# define V8_RETURN(val) return scope.Close(val)
|
if (args.Length() != 0)
|
||||||
#else
|
SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments.");
|
||||||
typedef void V8ReturnValue;
|
SWIGV8_RETURN(jsresult);
|
||||||
typedef v8::FunctionCallbackInfo<v8::Value> V8Arguments;
|
fail:
|
||||||
# define V8_RETURN(val) args.GetReturnValue().Set(val); return
|
SWIGV8_RETURN(SWIGV8_UNDEFINED());
|
||||||
#endif
|
|
||||||
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x032318)
|
|
||||||
# define V8_UNDEFINED() v8::Undefined()
|
|
||||||
#else
|
|
||||||
# define V8_UNDEFINED() v8::Undefined(v8::Isolate::GetCurrent())
|
|
||||||
#endif
|
|
||||||
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x032117)
|
|
||||||
# define V8_HANDLESCOPE() v8::HandleScope scope
|
|
||||||
#elif (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x032224)
|
|
||||||
# define V8_HANDLESCOPE() v8::HandleScope scope(v8::Isolate::GetCurrent());
|
|
||||||
#else
|
|
||||||
# define V8_HANDLESCOPE() v8::HandleScope scope(v8::Isolate::GetCurrent());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// 'unused' attribute only necessary for GNUC < v3.4
|
|
||||||
static /* __attribute__ ((__unused__)) */ V8ErrorHandler V8_ErrorHandler;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct worker_packet {
|
|
||||||
// -- basic --
|
|
||||||
uv_work_t request;
|
|
||||||
v8::Persistent<v8::Function> callback;
|
|
||||||
// -- persistent variables --
|
|
||||||
std::string result;
|
|
||||||
// -- async operation --
|
|
||||||
void (*execute)(std::string&);
|
|
||||||
} worker_packet;
|
|
||||||
|
|
||||||
|
|
||||||
// async process - parallel with node thread
|
|
||||||
static void work_async(uv_work_t* request) {
|
|
||||||
worker_packet* packet = static_cast<worker_packet*>(request->data);
|
|
||||||
packet->execute(packet->result);
|
|
||||||
// add a delay for dramatic effect - not necessary
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// send async result back to node's thread
|
|
||||||
static void work_complete(uv_work_t* request, int status) {
|
|
||||||
V8_HANDLESCOPE();
|
|
||||||
v8::Isolate* iso = v8::Isolate::GetCurrent();
|
|
||||||
worker_packet* packet = static_cast<worker_packet*>(request->data);
|
|
||||||
const int argc = 1;
|
|
||||||
v8::Handle<v8::Value> argv[] = {
|
|
||||||
v8::String::NewFromUtf8(iso, packet->result.c_str())
|
|
||||||
};
|
|
||||||
v8::Local<v8::Function>::New(iso, packet->callback)->Call
|
|
||||||
(iso->GetCurrentContext()->Global(), argc, argv);
|
|
||||||
packet->callback.Reset();
|
|
||||||
delete work;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void entry(const V8Arguments& args) {
|
|
||||||
v8::Isolate* iso = v8::Isolate::GetCurrent();
|
|
||||||
v8::Local<v8::Value> value = args[0];
|
|
||||||
if (!value->IsFunction()) {
|
|
||||||
V8_ErrorHandler.error((-1), "Invalid parameter type.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
worker_packet* packet = new worker_packet();
|
|
||||||
packet->request.data = packet;
|
|
||||||
packet->execute = [](std::string& res) { res = "My delayed message."; };
|
|
||||||
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(value);
|
|
||||||
packet->callback.Reset(iso, callback);
|
|
||||||
uv_queue_work(uv_default_loop(), &packet->request,
|
|
||||||
work_async, work_complete);
|
|
||||||
args.GetReturnValue().Set(Undefined(iso));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#else /* Engine: JavaScriptCore */
|
||||||
|
|
||||||
V8ReturnValue JavaScript_exampleV8_callback_function(const V8Arguments& args) {
|
static JSValueRef JavaScript_do_work(JSContextRef context,
|
||||||
V8_HANDLESCOPE();
|
JSObjectRef function, JSObjectRef thisObject, size_t argc,
|
||||||
if (args.Length() != 1) {
|
const JSValueRef argv[], JSValueRef* exception) {
|
||||||
V8_ErrorHandler.error((-1), "Illegal number of arguments.");
|
const int MY_MAGIC_NUMBER = 5;
|
||||||
V8_RETURN(V8_UNDEFINED());
|
JSValueRef jsresult =
|
||||||
}
|
SWIG_From_int SWIG_JSC_FROM_CALL_ARGS(
|
||||||
entry(args);
|
static_cast< int >(MY_MAGIC_NUMBER));
|
||||||
v8::Handle<v8::Value> jsresult = V8_UNDEFINED();
|
if (argc != 0)
|
||||||
V8_RETURN(jsresult);
|
SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments.");
|
||||||
}
|
return jsresult;
|
||||||
|
fail:
|
||||||
|
return JSValueMakeUndefined(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
9
Examples/test-suite/javascript/native_directive_runme.js
Normal file
9
Examples/test-suite/javascript/native_directive_runme.js
Normal 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";
|
||||||
|
})();
|
||||||
|
|
@ -42,32 +42,58 @@ extern "C" JNIEXPORT jint JNICALL Java_native_1directive_native_1directiveJNI_Co
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(SWIG_V8_VERSION)
|
|
||||||
|
#ifdef SWIGJAVASCRIPT
|
||||||
|
%native(CountAlphaCharacters) void JavaScript_alpha_count();
|
||||||
%{
|
%{
|
||||||
static SwigV8ReturnValue wrap_alpha_count(const SwigV8Arguments &args) {
|
#ifdef SWIG_V8_VERSION /* engine = node || v8 */
|
||||||
|
|
||||||
|
static SwigV8ReturnValue JavaScript_alpha_count(const SwigV8Arguments &args) {
|
||||||
SWIGV8_HANDLESCOPE();
|
SWIGV8_HANDLESCOPE();
|
||||||
v8::Handle<v8::Value> jsresult;
|
v8::Handle<v8::Value> jsresult;
|
||||||
char *arg1 = (char *) 0 ;
|
char *arg1 = (char *)0;
|
||||||
int res1 ;
|
int res1;
|
||||||
char *buf1 = 0 ;
|
char *buf1 = 0;
|
||||||
int alloc1 = 0 ;
|
int alloc1 = 0;
|
||||||
int result;
|
int result;
|
||||||
if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR,
|
if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_alpha_count.");
|
||||||
"Illegal number of arguments for _wrap_count_characters.");
|
|
||||||
res1 = SWIG_AsCharPtrAndSize(args[0], &buf1, NULL, &alloc1);
|
res1 = SWIG_AsCharPtrAndSize(args[0], &buf1, NULL, &alloc1);
|
||||||
if (!SWIG_IsOK(res1)) {
|
if (!SWIG_IsOK(res1))
|
||||||
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "count_characters"
|
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "alpha_count" "', argument " "1"" of type '" "char const *""'");
|
||||||
"', argument " "1"" of type '" "char const *""'");
|
|
||||||
}
|
|
||||||
arg1 = reinterpret_cast< char * >(buf1);
|
arg1 = reinterpret_cast< char * >(buf1);
|
||||||
result = (int)count_characters((char const *)arg1);
|
result = (int)alpha_count((char const *)arg1);
|
||||||
jsresult = SWIG_From_int(static_cast< int >(result));
|
jsresult = SWIG_From_int(static_cast< int >(result));
|
||||||
if (alloc1 == SWIG_NEWOBJ) delete[] buf1;
|
if (alloc1 == SWIG_NEWOBJ) delete[] buf1;
|
||||||
SWIGV8_RETURN(jsresult);
|
SWIGV8_RETURN(jsresult);
|
||||||
fail:
|
fail:
|
||||||
SWIGV8_RETURN(SWIGV8_UNDEFINED());
|
SWIGV8_RETURN(SWIGV8_UNDEFINED());
|
||||||
}
|
}
|
||||||
%}
|
|
||||||
%native(CountAlphaCharacters) void wrap_alpha_count();
|
#else /* engine = jsc */
|
||||||
#endif
|
|
||||||
|
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 */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue