Skip to content

Instantly share code, notes, and snippets.

Created May 10, 2013 16:34
Show Gist options
  • Save anonymous/5555597 to your computer and use it in GitHub Desktop.
Save anonymous/5555597 to your computer and use it in GitHub Desktop.
Java function wrapper for kroll-v8
var FunctionCall = require('com.obscure.fncall');
FunctionCall.example(function(doc, emit) {
// emit is a function that calls back to Java
emit(doc.bar, ["bar", 1, false]);
});
private V8Function emit;
private V8Function gnee;
public FunctioncallModule() {
super();
System.loadLibrary("functioncall-utils");
// create callback functions
this.emit = new V8Function(createJSCallback("do_emit", "(Ljava/lang/Object;Ljava/lang/Object;)V"));
this.gnee = new V8Function(createJSCallback("do_gnee", "()V"));
}
private void do_emit(Object key, Object value) {
Log.i(TAG, "emit("+key+", "+value+")");
}
private void do_gnee() {
Log.i(TAG, "Gnee gnee!");
}
@Kroll.method
public void example(KrollFunction map) {
Map doc = new HashMap();
doc.put("foo", 10);
doc.put("bar", "baz");
// passing the "emit" V8Function object as an argument to the "map" function
map.call(this.getKrollObject(), new Object[] { doc, emit });
}
#include "FunctioncallUtils.h"
#include <android/log.h>
#include <v8.h>
#include "V8Util.h"
#include "JNIUtil.h"
#include "V8Runtime.h"
#include "NativeObject.h"
#include "TypeConverter.h"
#define LOG_TAG "FunctionCallNative"
using namespace v8;
using namespace titanium;
class WrappedFunction : public NativeObject
{
public:
static WrappedFunction* New(jobject target, const char * methodname, const char * methodsignature)
{
JNIEnv * env = JNIUtil::getJNIEnv();
jclass clazz = env->GetObjectClass(target);
jmethodID methodid = env->GetMethodID(clazz, methodname, methodsignature);
if (methodid == 0)
{
LOGW(LOG_TAG, "unable to find method %s with signature %s", methodname, methodsignature);
return NULL;
}
WrappedFunction* self = new WrappedFunction(target, methodid);
Local<FunctionTemplate> t = FunctionTemplate::New(&WrappedFunction::JSWrapper, External::Wrap(self));
self->Wrap(t->GetFunction());
return self;
}
static Handle<Value> JSWrapper( const Arguments &args )
{
JNIEnv * env = JNIUtil::getJNIEnv();
WrappedFunction * self = (WrappedFunction*)External::Unwrap(args.Data());
jvalue jargs[args.Length()];
for (int i=0; i < args.Length(); i++) {
jargs[i].l = TypeConverter::jsValueToJavaObject(args[i]);
}
env->CallVoidMethodA(self->target_, self->methodID_, jargs);
return Undefined();
}
WrappedFunction(jobject target, jmethodID methodID);
private:
jobject target_;
jmethodID methodID_;
};
WrappedFunction::WrappedFunction(jobject target, jmethodID methodID)
: NativeObject()
, target_(target)
, methodID_(methodID)
{
}
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jlong JNICALL Java_com_obscure_fncall_FunctioncallModule_createJSCallback
(JNIEnv * env, jobject obj, jstring name, jstring signature)
{
const char * cname = env->GetStringUTFChars(name, 0);
const char * csignature = env->GetStringUTFChars(signature, 0);
WrappedFunction* wfn = WrappedFunction::New(obj, cname, csignature);
env->ReleaseStringUTFChars(name, cname);
env->ReleaseStringUTFChars(signature, csignature);
return (jlong) *(wfn->handle_);
}
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
return JNI_VERSION_1_4;
}
#ifdef __cplusplus
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment