Esempio n. 1
0
void Proxy::setIndexedProperty(uint32_t index, Local<Value> value, const PropertyCallbackInfo<Value>& info)
{
    Isolate* isolate = info.GetIsolate();
    JNIEnv* env = JNIScope::getEnv();
    if (!env) {
        LOG_JNIENV_GET_ERROR(TAG);
        // Returns undefined by default
        return;
    }

    Proxy* proxy = NativeObject::Unwrap<Proxy>(info.Holder());

    bool javaValueIsNew;
    jobject javaValue = TypeConverter::jsValueToJavaObject(isolate, env, value, &javaValueIsNew);
    jobject javaProxy = proxy->getJavaObject();
    env->CallVoidMethod(javaProxy,
                        JNIUtil::krollProxySetIndexedPropertyMethod,
                        index,
                        javaValue);

    if (!JavaObject::useGlobalRefs) {
        env->DeleteLocalRef(javaProxy);
    }
    if (javaValueIsNew) {
        env->DeleteLocalRef(javaValue);
    }

    info.GetReturnValue().Set(value);
}
Esempio n. 2
0
void Proxy::hasListenersForEventType(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    Isolate* isolate = args.GetIsolate();
    JNIEnv* env = JNIScope::getEnv();
    if (!env) {
        JSException::GetJNIEnvironmentError(isolate);
        return;
    }

    Local<Object> holder = args.Holder();
    // If holder isn't the JavaObject wrapper we expect, look up the prototype chain
    if (!JavaObject::isJavaObject(holder)) {
        holder = holder->FindInstanceInPrototypeChain(baseProxyTemplate.Get(isolate));
    }
    Proxy* proxy = NativeObject::Unwrap<Proxy>(holder);

    Local<String> eventType = args[0]->ToString(isolate);
    Local<Boolean> hasListeners = args[1]->ToBoolean(isolate);

    jobject javaProxy = proxy->getJavaObject();
    jobject krollObject = env->GetObjectField(javaProxy, JNIUtil::krollProxyKrollObjectField);
    jstring javaEventType = TypeConverter::jsStringToJavaString(env, eventType);

    if (!JavaObject::useGlobalRefs) {
        env->DeleteLocalRef(javaProxy);
    }

    env->CallVoidMethod(krollObject,
                        JNIUtil::krollObjectSetHasListenersForEventTypeMethod,
                        javaEventType,
                        TypeConverter::jsBooleanToJavaBoolean(hasListeners));

    env->DeleteLocalRef(krollObject);
    env->DeleteLocalRef(javaEventType);
}
Esempio n. 3
0
static void onPropertyChangedForProxy(Isolate* isolate, Local<String> property, Local<Value> value, Local<Object> proxyObject)
{
    Proxy* proxy = NativeObject::Unwrap<Proxy>(proxyObject);

    JNIEnv* env = JNIScope::getEnv();
    if (!env) {
        LOG_JNIENV_GET_ERROR(TAG);
        return;
    }

    jstring javaProperty = TypeConverter::jsStringToJavaString(env, property);
    bool javaValueIsNew;
    jobject javaValue = TypeConverter::jsValueToJavaObject(isolate, env, value, &javaValueIsNew);

    jobject javaProxy = proxy->getJavaObject();
    env->CallVoidMethod(javaProxy,
                        JNIUtil::krollProxyOnPropertyChangedMethod,
                        javaProperty,
                        javaValue);

    if (!JavaObject::useGlobalRefs) {
        env->DeleteLocalRef(javaProxy);
    }

    env->DeleteLocalRef(javaProperty);
    if (javaValueIsNew) {
        env->DeleteLocalRef(javaValue);
    }

    // Store new property value on JS internal map.
    setPropertyOnProxy(isolate, property, value, proxyObject);
}
Esempio n. 4
0
Handle<Value> Proxy::hasListenersForEventType(const Arguments& args)
{
	JNIEnv* env = JNIScope::getEnv();
	if (!env) {
		return JSException::GetJNIEnvironmentError();
	}

	Proxy* proxy = NativeObject::Unwrap<Proxy>(args.Holder());

	Local<String> eventType = args[0]->ToString();
	Local<Boolean> hasListeners = args[1]->ToBoolean();

	jobject javaProxy = proxy->getJavaObject();
	jobject krollObject = env->GetObjectField(javaProxy, JNIUtil::krollProxyKrollObjectField);
	jstring javaEventType = TypeConverter::jsStringToJavaString(eventType);

	if (!JavaObject::useGlobalRefs) {
		env->DeleteLocalRef(javaProxy);
	}

	env->CallVoidMethod(krollObject,
		JNIUtil::krollObjectSetHasListenersForEventTypeMethod,
		javaEventType,
		TypeConverter::jsBooleanToJavaBoolean(hasListeners));

	env->DeleteLocalRef(krollObject);
	env->DeleteLocalRef(javaEventType);

	return Undefined();
}
Esempio n. 5
0
Handle<Value> Proxy::setIndexedProperty(uint32_t index, Local<Value> value, const AccessorInfo& info)
{
	JNIEnv* env = JNIScope::getEnv();
	if (!env) {
		LOG_JNIENV_GET_ERROR(TAG);
		return Undefined();
	}

	Proxy* proxy = NativeObject::Unwrap<Proxy>(info.Holder());

	bool javaValueIsNew;
	jobject javaValue = TypeConverter::jsValueToJavaObject(value, &javaValueIsNew);
	jobject javaProxy = proxy->getJavaObject();
	env->CallVoidMethod(javaProxy,
		JNIUtil::krollProxySetIndexedPropertyMethod,
		index,
		javaValue);

	if (!JavaObject::useGlobalRefs) {
		env->DeleteLocalRef(javaProxy);
	}
	if (javaValueIsNew) {
		env->DeleteLocalRef(javaValue);
	}

	return value;
}
Esempio n. 6
0
Handle<Value> Proxy::getIndexedProperty(uint32_t index, const AccessorInfo& info)
{
	JNIEnv* env = JNIScope::getEnv();
	if (!env) {
		return JSException::GetJNIEnvironmentError();
	}

	Proxy* proxy = NativeObject::Unwrap<Proxy>(info.Holder());
	jobject javaProxy = proxy->getJavaObject();
	jobject value = env->CallObjectMethod(javaProxy,
		JNIUtil::krollProxyGetIndexedPropertyMethod,
		index);

	if (!JavaObject::useGlobalRefs) {
		env->DeleteLocalRef(javaProxy);
	}

	Handle<Value> result = TypeConverter::javaObjectToJsValue(value);
	env->DeleteLocalRef(value);

	return result;
}
Esempio n. 7
0
void Proxy::getIndexedProperty(uint32_t index, const PropertyCallbackInfo<Value>& info)
{
    Isolate* isolate = info.GetIsolate();
    JNIEnv* env = JNIScope::getEnv();
    if (!env) {
        JSException::GetJNIEnvironmentError(isolate);
        return;
    }

    Proxy* proxy = NativeObject::Unwrap<Proxy>(info.Holder());
    jobject javaProxy = proxy->getJavaObject();
    jobject value = env->CallObjectMethod(javaProxy,
                                          JNIUtil::krollProxyGetIndexedPropertyMethod,
                                          index);

    if (!JavaObject::useGlobalRefs) {
        env->DeleteLocalRef(javaProxy);
    }

    Local<Value> result = TypeConverter::javaObjectToJsValue(isolate, env, value);
    env->DeleteLocalRef(value);

    info.GetReturnValue().Set(result);
}
Esempio n. 8
0
void Proxy::proxyOnPropertiesChanged(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    Isolate* isolate = args.GetIsolate();
    HandleScope scope(isolate);
    Local<Object> jsProxy = args.Holder();

    if (args.Length() < 1 || !(args[0]->IsArray())) {
        JSException::Error(isolate, "Proxy.propertiesChanged requires a list of lists of property name, the old value, and the new value");
        return;
    }

    JNIEnv *env = JNIScope::getEnv();
    if (!env) {
        JSException::GetJNIEnvironmentError(isolate);
        return;
    }

    Proxy *proxy = unwrap(jsProxy);
    if (!proxy) {
        JSException::Error(isolate, "Failed to unwrap Proxy instance");
        return;
    }

    Local<Array> changes = args[0].As<Array>();
    uint32_t length = changes->Length();
    jobjectArray jChanges = env->NewObjectArray(length, JNIUtil::objectClass, NULL);

    for (uint32_t i = 0; i < length; ++i) {
        Local<Array> change = changes->Get(i).As<Array>();
        Local<String> name = change->Get(INDEX_NAME)->ToString(isolate);
        Local<Value> oldValue = change->Get(INDEX_OLD_VALUE);
        Local<Value> value = change->Get(INDEX_VALUE);

        jobjectArray jChange = env->NewObjectArray(3, JNIUtil::objectClass, NULL);

        jstring jName = TypeConverter::jsStringToJavaString(env, name);
        env->SetObjectArrayElement(jChange, INDEX_NAME, jName);
        env->DeleteLocalRef(jName);

        bool isNew;
        jobject jOldValue = TypeConverter::jsValueToJavaObject(isolate, env, oldValue, &isNew);
        env->SetObjectArrayElement(jChange, INDEX_OLD_VALUE, jOldValue);
        if (isNew) {
            env->DeleteLocalRef(jOldValue);
        }

        jobject jValue = TypeConverter::jsValueToJavaObject(isolate, env, value, &isNew);
        env->SetObjectArrayElement(jChange, INDEX_VALUE, jValue);
        if (isNew) {
            env->DeleteLocalRef(jValue);
        }

        env->SetObjectArrayElement(jChanges, i, jChange);
        env->DeleteLocalRef(jChange);
    }

    jobject javaProxy = proxy->getJavaObject();
    env->CallVoidMethod(javaProxy, JNIUtil::krollProxyOnPropertiesChangedMethod, jChanges);
    env->DeleteLocalRef(jChanges);

    if (!JavaObject::useGlobalRefs) {
        env->DeleteLocalRef(javaProxy);
    }

    return;
}