Пример #1
0
JNIEXPORT jobject JNICALL Java_com_furture_react_DuktapeEngine_nativeCallJs
  (JNIEnv *env, jobject thisObject, jlong ptr, jstring jsTarget, jstring jsMethod, jobjectArray args){
	duk_context *ctx  = convert_to_context(ptr);
	jboolean iscopy = JNI_FALSE;
    const char* targetName = ((*env)->GetStringUTFChars(env, jsTarget, &iscopy));
    DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJs call on %s", targetName);
	duk_push_global_object(ctx);
    if(duk_get_prop_string(ctx, -1, targetName)){
    	    const char* methodName = ((*env)->GetStringUTFChars(env, jsMethod, &iscopy));
	    duk_push_string(ctx, methodName);
	    jsize length = duk_push_java_object_array(ctx, env, args);
	    DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJs call  Function %d", length);
	    if(duk_pcall_prop(ctx, -2 - length, length) != DUK_EXEC_SUCCESS){
	     	LOGE("ScriptEngine","ScriptEngine CallJS %s.%s() method %s", targetName, methodName, duk_js_error_to_string(ctx, -1));
	        duk_pop(ctx);
	        duk_push_null(ctx);
	    }
 	    (*env)->ReleaseStringUTFChars(env, jsTarget, targetName);
	    (*env)->ReleaseStringUTFChars(env, jsMethod, methodName);
	    jobject  value =  duk_to_java_object(ctx, env, -1);
	    duk_pop_2(ctx);
	    return value;
    }
    (*env)->ReleaseStringUTFChars(env, jsTarget, targetName);
    duk_pop_2(ctx);
    return NULL;

}
Пример #2
0
JNIEXPORT void JNICALL Java_com_furture_react_DuktapeEngine_nativeDestory
  (JNIEnv * env, jobject thisObject, jlong ptr){
	duk_context *ctx  = convert_to_context(ptr);
	if(ctx){
        jobject enginePtr  = get_engine_from_context(ctx);
        int length = duk_js_ref_size(ctx);
		for(int i=1; i <= length; i++){
			duk_push_js_ref(ctx, i);
			if(duk_is_object(ctx, -1)){
				if(duk_get_prop_string(ctx, -1, JS_REF_MARK)){
					  jweak  weakRef = duk_to_pointer(ctx, -1);
					  if(weakRef != NULL){
					      (*env)->DeleteWeakGlobalRef(env, weakRef);
					  }
					  DEBUG_LOG("ScriptEngine", "Free jweak Ref onJSRef %d", i);
			     }
				 duk_pop(ctx);
			}
			duk_pop(ctx);
		}
		duk_destroy_heap(ctx);
		if(enginePtr != NULL){
		    (*env)->DeleteGlobalRef(env, enginePtr);
		}
	}
}
Пример #3
0
JNIEXPORT void JNICALL Java_com_furture_react_DuktapeEngine_nativeFinalizeJSRef
  (JNIEnv *env, jobject thisObject, jlong ptr, jint ref){
	 duk_context *ctx  = convert_to_context(ptr);
	 duk_push_js_ref(ctx, ref);
	 if(duk_get_prop_string(ctx, -1, JS_REF_MARK)){
	     jweak  weakRef = duk_to_pointer(ctx, -1);
	     (*env)->DeleteWeakGlobalRef(env, weakRef);
	 }
	 duk_pop_2(ctx);
	 duk_js_unref(ctx, ref);
}
Пример #4
0
JNIEXPORT void JNICALL Java_com_furture_react_DuktapeEngine_nativeRegister
  (JNIEnv *env, jobject thisObject, jlong ptr, jstring key, jobject value){
	duk_context *ctx  = convert_to_context(ptr);
	jboolean iscopy = JNI_FALSE;
	const char* src =  ((*env)->GetStringUTFChars(env, key, &iscopy));
	duk_push_global_object(ctx);
	duk_push_java_object(ctx, env, value);
 	duk_put_prop_string(ctx, -2, src);
	duk_pop(ctx);
	(*env)->ReleaseStringUTFChars(env, key, src);
}
Пример #5
0
JNIEXPORT jobject JNICALL Java_com_furture_react_DuktapeEngine_nativeExeclute
  (JNIEnv *env, jobject thisObject, jlong  ptr, jstring script){
	jboolean iscopy = JNI_FALSE;
	const char* src = ((*env)->GetStringUTFChars(env, script, &iscopy));
	duk_context *ctx  = convert_to_context(ptr);
	if(duk_peval_string(ctx, src) != DUK_EXEC_SUCCESS){
		 LOGE("ScriptEngine", "ScriptEngine eval_string %s\n", duk_js_error_to_string(ctx, -1));
	     duk_push_null(ctx);
	}
	if(src != NULL){
	    (*env)->ReleaseStringUTFChars(env, script, src);
	}
	return duk_to_java_object(ctx, env, -1);
}
Пример #6
0
static FILE *convert_format (FILE *f, char format)
{
	switch (format) {
	default:
		break;
	case 'c':
		f = convert_to_context (f, "rb", 0);
		break;
	case 'u':
		f = convert_to_unified (f, "rb", 0);
		break;
	}

	return f;
}
Пример #7
0
JNIEXPORT jobject JNICALL Java_com_furture_react_DuktapeEngine_nativeCallJSRef
  (JNIEnv *env, jobject thisObject, jlong ptr, jint jsRef, jstring proxyMethod, jobjectArray args){
   duk_context *ctx  = convert_to_context(ptr);
   DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef start %d", duk_get_top(ctx));
   duk_push_js_ref(ctx, jsRef);
   if(duk_is_function(ctx, -1)){
	      DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef is Function %d", jsRef);
	   	  jsize length = duk_push_java_object_array(ctx, env, args);
	   	   DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef call  Function %d", length);
		  if(duk_pcall(ctx, length) != DUK_EXEC_SUCCESS){
			  jboolean iscopy = JNI_FALSE;
			  const char* method = ((*env)->GetStringUTFChars(env, proxyMethod, &iscopy));
			  LOGE("ScriptEngine","ScriptEngine CallJSRef   %s method exception %s", method, duk_js_error_to_string(ctx, -1));
			  duk_pop(ctx);
			  duk_push_null(ctx);
			  if(method != NULL){
			     (*env)->ReleaseStringUTFChars(env, proxyMethod, method);
			  }
		  }
		  DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef call  Function %d", length);
		  jobject  value =  duk_to_java_object(ctx, env, -1);
		  duk_pop(ctx);
		  return value;
   }
   jboolean iscopy = JNI_FALSE;
   const char* method = ((*env)->GetStringUTFChars(env, proxyMethod, &iscopy));
   duk_push_string(ctx, method);
   jsize length = duk_push_java_object_array(ctx, env, args);
   DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef call Object  Function %d", length);
   if(duk_pcall_prop(ctx, -2 - length, length) != DUK_EXEC_SUCCESS){
	   LOGE("ScriptEngine","ScriptEngine CallJSRef  %d proxy method %s exception %s", jsRef, method, duk_js_error_to_string(ctx, -1));
	   duk_pop(ctx);
	   duk_push_null(ctx);
   }
   (*env)->ReleaseStringUTFChars(env, proxyMethod, method);
   DEBUG_LOG("ScriptEngine","Java_com_furture_react_DuktapeEngine_nativeCallJSRef call Object success %d", duk_get_type(ctx, -1));
   jobject  value =  duk_to_java_object(ctx, env, -1);
   duk_pop_2(ctx);
   return value;
}