Exemplo n.º 1
0
// native JavaScript method we'll be calling from JS land
void sampleMethod(JXValue *results, int argc) {
  assert(argc == 4 && "number of arguments supposed to be 4");

  assert(JX_IsFunction(&results[0]) && JX_IsFunction(&results[1]) &&
         "both parameters supposed to be a function");

  assert(JX_IsString(&results[2]) && JX_IsString(&results[3]) &&
         "both parameters supposed to be a function");

  bool JX_CallFunction(JXValue * fnc, JXValue * params, const int argc,
                       JXValue * out);

  JXValue out;
  assert(JX_CallFunction(&results[0], &results[2], 1, &out) &&
         "failed while calling console.log");
  assert(JX_IsUndefined(&out) &&
         "return value from console.log should be undefined");

  assert(JX_CallFunction(&results[0], &results[3], 1, &out) &&
         "failed while calling console.error");
  assert(JX_IsUndefined(&out) &&
         "return value from console.error should be undefined");

  counter++;
}
JNIEXPORT jlong JNICALL
Java_io_jxcore_node_jxcore_evalEngine(JNIEnv *env, jobject thiz,
                                      jstring contents) {
  const char *data = env->GetStringUTFChars(contents, 0);

  JXValue result;
  JX_Evaluate(data, 0, &result);

  env->ReleaseStringUTFChars(contents, data);

  if (!JX_IsNull(&result) && !JX_IsUndefined(&result))
    return JX_StoreValue(&result);
  else
    return -1;
}
JNIEXPORT jlong JNICALL
Java_io_jxcore_node_jxcore_callCBString(JNIEnv *env, jobject thiz,
                                        jstring ev_name, jstring param,
                                        jint json) {
  if (eventCB.type_ == 0) {
    error_console("event callback is not ready yet.");
    return 0;
  }

  const char *str_ev = env->GetStringUTFChars(ev_name, 0);
  const char *str_param = env->GetStringUTFChars(param, 0);

  long ret_val = 0;
  JXValue args[2];
  if (JX_CreateEmptyObject(&args[1])) {
    JXValue jx_str_param;
    JX_New(&args[0]);
    JX_New(&jx_str_param);

    JX_SetString(&args[0], str_ev, 0);
    JX_SetString(&jx_str_param, str_param, 0);

    if (json != 1)
      JX_SetNamedProperty(&args[1], "str", &jx_str_param);
    else
      JX_SetNamedProperty(&args[1], "json", &jx_str_param);

    JX_Free(&jx_str_param);

    JXValue out;
    JX_CallFunction(&eventCB, args, 2, &out);

    JX_Free(&args[0]);
    JX_Free(&args[1]);

    if (!JX_IsNull(&out) && !JX_IsUndefined(&out))
      ret_val = JX_StoreValue(&out);
    else
      ret_val = -1;
  } else {
    error_console("couldn't create JXValue Object");
  }

  env->ReleaseStringUTFChars(ev_name, str_ev);
  env->ReleaseStringUTFChars(param, str_param);

  return ret_val;
}
static void callback(JXValue *results, int argc) {
  if (argc != 3) {
    error_console(
        "wrong callback received. expected number of parameters was 3. "
        "received %d",
        argc);
    return;
  }

  if (!JX_IsString(results + 2)) {
    error_console(
        "JXcore-Cordova: Unexpected callback received. Third parameter must "
        "be a String");
    return;
  }

  cb_values = results;

  // results+1 -> err_val
  jxcore::Callback((JX_IsNull(results + 1) || JX_IsUndefined(results + 1)) ? 0
                                                                           : 1);

  cb_values = NULL;
}
JNIEXPORT jlong JNICALL
Java_io_jxcore_node_jxcore_callCBArray(JNIEnv *env, jobject thiz,
                                       jstring ev_name, jobjectArray params,
                                       jint size) {
  if (eventCB.type_ == 0) {
    error_console("event callback is not ready yet.");
    return 0;
  }

  const char *str_ev = env->GetStringUTFChars(ev_name, 0);

  long ret_val = 0;
  JXValue args[2];
  if (JX_CreateArrayObject(&args[1])) {
    JX_New(&args[0]);

    JX_SetString(&args[0], str_ev, 0);

    jclass boolClass = env->FindClass("java/lang/Boolean");
    jclass doubleClass = env->FindClass("java/lang/Double");
    jclass intClass = env->FindClass("java/lang/Integer");
    jclass strClass = env->FindClass("java/lang/String");
    jclass barrClass = env->FindClass("[B");

    for (int i = 0; i < (int)size; i++) {
      jobject elm = (jobject)env->GetObjectArrayElement(params, i);

      JXValue val;
      JX_New(&val);
      if (elm == NULL) {
        JX_SetNull(&val);
      } else if (env->IsInstanceOf(elm, boolClass) == JNI_TRUE) {
        jmethodID bvalID = env->GetMethodID(boolClass, "booleanValue", "()Z");
        bool nval = (bool)env->CallBooleanMethod(elm, bvalID);
        JX_SetBoolean(&val, nval);
      } else if (env->IsInstanceOf(elm, intClass) == JNI_TRUE) {
        jmethodID bvalID = env->GetMethodID(intClass, "intValue", "()I");
        int nval = (int)env->CallIntMethod(elm, bvalID);
        JX_SetInt32(&val, nval);
      } else if (env->IsInstanceOf(elm, doubleClass) == JNI_TRUE) {
        jmethodID bvalID = env->GetMethodID(doubleClass, "doubleValue", "()D");
        double nval = (double)env->CallDoubleMethod(elm, bvalID);
        JX_SetDouble(&val, nval);
      } else if (env->IsInstanceOf(elm, strClass) == JNI_TRUE) {
        jstring jstr = (jstring)elm;
        const char *str = env->GetStringUTFChars(jstr, 0);
        JX_SetString(&val, str, strlen(str));
        env->ReleaseStringUTFChars(jstr, str);
      } else if (env->IsInstanceOf(elm, barrClass) == JNI_TRUE) {
        jbyteArray jarr = (jbyteArray)elm;
        int len = env->GetArrayLength(jarr);
        jbyte *barr = env->GetByteArrayElements(jarr, 0);
        JX_SetBuffer(&val, (char *)barr, len);

        env->ReleaseByteArrayElements(jarr, barr, JNI_ABORT);
      }

      JX_SetIndexedProperty(&args[1], i, &val);
      JX_Free(&val);

      env->DeleteLocalRef(elm);
    }

    env->DeleteLocalRef(boolClass);
    env->DeleteLocalRef(doubleClass);
    env->DeleteLocalRef(intClass);
    env->DeleteLocalRef(strClass);
    env->DeleteLocalRef(barrClass);

    JXValue out;
    JX_CallFunction(&eventCB, args, 2, &out);

    JX_Free(&args[0]);
    JX_Free(&args[1]);

    if (!JX_IsNull(&out) && !JX_IsUndefined(&out))
      ret_val = JX_StoreValue(&out);
    else
      ret_val = -1;
  } else {
    error_console("couldn't create JXValue Object");
  }

  env->ReleaseStringUTFChars(ev_name, str_ev);

  return ret_val;
}