-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJNodeCB.cc
More file actions
120 lines (87 loc) · 3.45 KB
/
Copy pathJNodeCB.cc
File metadata and controls
120 lines (87 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "node.h"
#include "node_object_wrap.h"
#include <jni.h>
#include <iostream>
// Made with *some* help from:
// - https://nodejs.org/api/addons.html
// - http://bespin.cz/~ondras/html/classv8_1_1Value.html
// - http://www.codeproject.com/Articles/993067/Calling-Java-from-Cplusplus-with-JNI
// FUTURE TBD: put this in a namespace??
/* exported by JNode.cc */
extern JNIEnv * jnode_jni_env;
class MethodCallObject : public node::ObjectWrap {
public:
static void init(v8::Isolate *);
static void newInstance(const v8::FunctionCallbackInfo<v8::Value> &);
private:
MethodCallObject(JNIEnv *e, jclass c, jmethodID m) : e(e), c(c), m(m) { }
~MethodCallObject() { }
static void _new(const v8::FunctionCallbackInfo<v8::Value> &);
static void call(const v8::FunctionCallbackInfo<v8::Value> &);
static v8::Persistent<v8::Function> ctor;
JNIEnv * e;
jclass c;
jmethodID m;
};
v8::Persistent<v8::Function> MethodCallObject::ctor;
void MethodCallObject::init(v8::Isolate * isolate) {
v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New(isolate, _new);
ft->SetClassName(v8::String::NewFromUtf8(isolate, "MethodCallObject"));
ft->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(ft, "call", call);
ctor.Reset(isolate, ft->GetFunction());
}
void MethodCallObject::_new(const v8::FunctionCallbackInfo<v8::Value> & fci) {
// not expected to be false:
if (fci.IsConstructCall()) {
// ref: http://stackoverflow.com/a/10255816/1283667
v8::String::Utf8Value v1(fci[0]->ToString());
v8::String::Utf8Value v2(fci[1]->ToString());
JNIEnv * e = jnode_jni_env;
jclass c = e->FindClass(*v1);
if (c == nullptr) {
std::cerr << "Could not find class: " << *v1 << std::endl;
return;
}
jmethodID m = e->GetStaticMethodID(c, *v2, "(J)V");
if (m == 0) {
std::cerr << "Could not find method: " << *v2 << std::endl;
return;
}
MethodCallObject * o = new MethodCallObject(e, c, m);
o->Wrap(fci.This());
fci.GetReturnValue().Set(fci.This());
}
}
void MethodCallObject::newInstance(const v8::FunctionCallbackInfo<v8::Value> & fci) {
v8::Isolate * isolate = fci.GetIsolate();
// XXX TODO: if class or static function does not really exist,
// should throw exception and stop!
v8::Handle<v8::Value> args[2] = { fci[0], fci[1] };
v8::Local<v8::Function> myctor = v8::Local<v8::Function>::New(isolate, ctor);
v8::Local<v8::Object> inst = myctor->NewInstance(2, args);
fci.GetReturnValue().Set(inst);
}
void MethodCallObject::call(const v8::FunctionCallbackInfo<v8::Value> & fci) {
MethodCallObject * o = ObjectWrap::Unwrap<MethodCallObject>(fci.Holder());
//std::cout << "call() called" << std::endl;
long long fciHandle = (long long)&fci;
o->e->CallStaticVoidMethod(o->c, o->m, fciHandle);
}
void getStaticMethodObject(const v8::FunctionCallbackInfo<v8::Value> & fci) {
if (fci.Length() < 2) {
std::cerr << "JNodeCB.getStaticMethodObject() called with missing parameter(s)" << std::endl;
// XXX TODO should throw here!
return;
}
if (!fci[0]->IsString() || !fci[1]->IsString()) {
std::cerr << "JNodeCB.getStaticMethodObject() called with incorrect parameter(s)" << std::endl;
return;
}
MethodCallObject::newInstance(fci);
}
void initJNodeCB(v8::Local<v8::Object> exports) {
MethodCallObject::init(exports->GetIsolate());
NODE_SET_METHOD(exports, "getStaticMethodObject", getStaticMethodObject);
}
NODE_MODULE(JNodeCB, initJNodeCB)