int Start(char* src) { if (!InitJerry()) { DLOG("InitJerry failed"); return 1; } JObject* process = InitModules(); // FIXME: this should be moved to seperate function { JObject argv; argv.SetProperty("1", JObject(src)); process->SetProperty("argv", argv); } if (!StartIoTjs(process)) { DLOG("StartIoTJs failed"); return 1; } CleanupModules(); ReleaseJerry(); return 0; }
JObject JObject::GetProperty(const char* name) { IOTJS_ASSERT(IsObject()); JRawValueType res; bool is_ok = jerry_api_get_object_field_value( _obj_val.v_object, reinterpret_cast<const jerry_api_char_t*>(name), &res); IOTJS_ASSERT(is_ok); return JObject(&res); }
QVariant JClass::getStaticValue(const char* name, const char* sig) const { JScope s; JNIEnv* env = *JVM::instance(); jfieldID fid = env->GetStaticFieldID(m_class, name, sig); if (!fid) throw RuntimeException(QObject::tr("Field %1 %2 not found").arg(name).arg(sig)); switch (sig[0]) { case '[': { QVariant var; jobject obj = env->GetStaticObjectField(m_class, fid); var.setValue<JArray>(JArray(obj)); return var; } case 'L': { jclass string_class = env->FindClass("java/lang/String"); jobject obj = env->GetStaticObjectField(m_class, fid); if (env->IsInstanceOf(obj, string_class)) return JString(jstring(obj)).str(); else { QVariant var; var.setValue<JObject>(JObject(obj)); return var; } } case 'Z': return (bool) env->GetStaticBooleanField(m_class, fid); case 'B': return env->GetStaticByteField(m_class, fid); case 'C': return env->GetStaticCharField(m_class, fid); case 'S': return env->GetStaticShortField(m_class, fid); case 'I': return env->GetStaticIntField(m_class, fid); case 'J': return (qlonglong) env->GetStaticLongField(m_class, fid); case 'F': return env->GetStaticFloatField(m_class, fid); case 'D': return env->GetStaticDoubleField(m_class, fid); default: throw RuntimeException(QObject::tr("Unknown Java data type: %1").arg(sig[0])); } }
JObject CreateError(const char* message, jerry_api_error_t error) { return JObject(jerry_api_create_error( error, reinterpret_cast<const jerry_api_char_t*>(message))); }
JObject JObject::Global() { return JObject(GetGlobal()); }
void JavaExtractor::changeActive(bool nowActive) { if (nowActive) { if (m_strUrl.startsWith("http://") || m_strUrl.startsWith("https://")) m_reply = m_network->get(QNetworkRequest(m_strUrl)); else { logMessage(tr("JavaExtractor: Not an HTTP(S) URI, passing the URI directly to the extension")); m_plugin->call("extractList", JSignature().addString().add("java.nio.ByteBuffer").add("java.util.Map"), m_strUrl, JObject(), JObject()); } } else { if (m_reply) m_reply->abort(); } }
JObject JClass::toClassObject() const { return JObject(m_class); }
QVariant JClass::callStatic(const char* name, const char* sig, QList<QVariant> args) { JScope s; JNIEnv* env = *JVM::instance(); jmethodID mid = env->GetStaticMethodID(m_class, name, sig); if (!mid) throw RuntimeException(QObject::tr("Method %1 %2 not found").arg(name).arg(sig)); JValue vals[args.size()]; jvalue jargs[args.size()]; for(int i=0;i<args.size();i++) { vals[i] = variantToValue(args[i]); jargs[i] = vals[i]; } const char* rvtype = strchr(sig, ')'); if (!rvtype) throw RuntimeException(QObject::tr("Invalid method return type").arg(name).arg(sig)); rvtype++; QVariant retval; switch (*rvtype) { case 'V': env->CallStaticVoidMethodA(m_class, mid, jargs); break; case '[': { jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); retval.setValue<JArray>(JArray(obj)); break; } case 'L': { jclass string_class = env->FindClass("java/lang/String"); jobject obj = env->CallStaticObjectMethodA(m_class, mid, jargs); if (obj && env->IsInstanceOf(obj, string_class) && !strcmp(rvtype+1, "java/lang/String;")) retval = JString(jstring(obj)).str(); else { QVariant var; var.setValue<JObject>(JObject(obj)); retval = var; } break; } case 'Z': retval = (bool) env->CallStaticBooleanMethodA(m_class, mid, jargs); break; case 'B': retval = env->CallStaticByteMethodA(m_class, mid, jargs); break; case 'C': retval = env->CallStaticCharMethodA(m_class, mid, jargs); break; case 'S': retval = env->CallStaticShortMethodA(m_class, mid, jargs); break; case 'I': retval = env->CallStaticIntMethodA(m_class, mid, jargs); break; case 'J': retval = (qlonglong) env->CallStaticLongMethodA(m_class, mid, jargs); break; case 'F': retval = env->CallStaticFloatMethodA(m_class, mid, jargs); break; case 'D': retval = env->CallStaticDoubleMethodA(m_class, mid, jargs); break; default: throw RuntimeException(QObject::tr("Unknown Java data type: %1").arg(*rvtype)); } JObject ex = env->ExceptionOccurred(); if (!ex.isNull()) { env->ExceptionClear(); ex.call("printStackTrace"); QString message = ex.call("getMessage", JSignature().retString()).toString(); QString className = ex.getClass().getClassName(); throw JException(message, className, ex); } return retval; }
ReturnPointer<JObject> JStaticField::GetObjectValue(JReference* object) { return autonew JObject(GetVM(), GetVM()->GetEnv()->GetStaticObjectField((jclass)object->GetReference(), GetField())); }
ReturnPointer<JObject> JStaticField::GetReflection() { return autonew JObject(GetVM(), GetVM()->GetEnv()->ToReflectedField(GetClass()->GetClass(), GetField(), true)); }
ReturnPointer<JObject> JField::GetObjectValue(JReference* object) { return autonew JObject(GetVM(), GetVM()->GetEnv()->GetObjectField(object->GetReference(), GetField())); }