Guest User

Untitled

a guest
Dec 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2. // This is pseudo code, but it gives you an idea of how v8<->JNI might work
  3.  
  4. static JavaVM *vm;
  5.  
  6. // Called when the shared library is loaded
  7. extern "C" JNIEXPORT jint JNICALL
  8. JNI_OnLoad(JavaVM *jvm, void *reserved)
  9. {
  10. JNIEnv *env;
  11. vm = jvm; /* cache the JavaVM pointer */
  12.  
  13. if (jvm->GetEnv((void **) &env, JNI_VERSION_1_4)) {
  14. return JNI_ERR; /* JNI version not supported */
  15. }
  16. return JNI_VERSION_1_4;
  17. }
  18.  
  19. jclass objClass;
  20. jmethodID javaMethodId;
  21.  
  22. // V8Main.main(src, name);
  23. extern "C" JNIEXPORT void JNICALL
  24. Java_org_appcelerator_kroll_v8_V8Main_main(JNIEnv *env, jobject thiz, jstring source, jstring name)
  25. {
  26. HandleScope scope;
  27. Handle<ObjectTemplate> globalTemplate = ObjectTemplate::New();
  28. Persistent<Context> context = Context::New(NULL, globalTemplate);
  29. Context::Scope contextScope(context);
  30.  
  31. // Object templates are the way v8 exposes prototypes natively
  32. // Any object that is created w/ the template has the APIs on the template itself
  33. Handle<ObjectTemplate> objTemplate = ObjectTemplate::New();
  34.  
  35. // To bind a java method, we would first bind a native function pointer
  36. objTemplate->Set(String::New("javaMethod"), Obj_javaMethod);
  37.  
  38. Handle<Object> obj = objTemplate->NewInstance();
  39. // We wrap the "thiz" pointer into an internal field so it can be fetched
  40. obj->SetInternalField(0, External::Wrap(reinterpret_cast<void*>(thiz)));
  41.  
  42. // Cache the class, and "javaMethod" methodID for later
  43. objClass = env->FindClass("com/my/Class");
  44.  
  45. // javaMethod() is a void method
  46. javaMethodId = env->GetMethodID(objClass, "javaMethod", "()V");
  47.  
  48. // Finally bind "obj" as "helloWorld" in JS land
  49. context->Global()->Set(String::New("helloWorld"), obj);
  50.  
  51. const char *srcData = const_cast<const char *>(env->GetStringUTFChars(source, NULL));
  52. const char *cName = const_cast<const char *>(env->GetStringUTFChars(name, NULL));
  53.  
  54. Local<Script> script = Script::Compile(String::New(srcData), String::New(cName));
  55. script->Run();
  56.  
  57. env->ReleaseStringUTFChars(source, srcData);
  58. env->ReleaseStringUTFChars(name, cName);
  59. context.Dispose();
  60. }
  61.  
  62. static Handle<Value> Obj_javaMethod(const Arguments& args)
  63. {
  64. // get the java pointer
  65. jobject obj = reinterpret_cast<jobject>(External::Unwrap(args.This()->GetInternalField(0)));
  66.  
  67. // get the JNIEnv
  68. JNIEnv *env;
  69. vm->GetEnv((void **) &env, JNI_VERSION_1_4);
  70.  
  71. env->CallVoidMethod(obj, javaMethodId);
  72. return Undefined();
  73. }
  74.  
  75. ## test.js
  76.  
  77. helloWorld.javaMethod();
Add Comment
Please, Sign In to add comment