Ejemplo n.º 1
0
bool JavaNPObject_GetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result) {
    VOID_TO_NPVARIANT(*result);
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (instance == 0)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (name == 0)
        return false;

    JavaField* field = instance->getClass()->fieldNamed(name);
    free(name);  // TODO: use NPN_MemFree

    if (field == 0) {
        return false;
    }

    jobject local_ref = instance->getLocalRef();
    jvalue value = getJNIField(local_ref,
                               field->getJNIType(),
                               field->name(),
                               field->type());
    getJNIEnv()->DeleteLocalRef(local_ref);

    convertJValueToNPVariant(value, field->getJNIType(), field->type(), result);

    return true;
}
Ejemplo n.º 2
0
static bool ewk_js_property_remove(NPObject* npObject, NPIdentifier name)
{
    Ewk_JS_Object* object = reinterpret_cast<Ewk_JS_Object*>(npObject);
    Ewk_JS_Property* prop;
    bool fail = false;

    EINA_SAFETY_ON_NULL_RETURN_VAL(npObject, false);
    EINA_MAGIC_CHECK_OR_RETURN(object, false);

    if (!_NPN_IdentifierIsString(name)) {
        ERR("int NPIdentifier is not supported.");
        return fail;
    }

    char* prop_name = _NPN_UTF8FromIdentifier(name);
    prop = static_cast<Ewk_JS_Property*>(eina_hash_find(object->cls->properties, prop_name));
    if (prop && prop->del)
        fail = prop->del(object, prop->name); // Class has property and property has getter.
    else if (object->cls->default_prop.del)
        fail = object->cls->default_prop.del(object, prop_name);
    else
        fail = eina_hash_del(object->properties, prop_name, 0);

    free(prop_name);
    return fail;
}
Ejemplo n.º 3
0
bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result)
{
    VOID_TO_NPVARIANT(*result);
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (!instance)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (!name)
        return false;

    instance->begin();
    JavaField* field = instance->getClass()->fieldNamed(name);
    free(name); // TODO: use NPN_MemFree
    if (!field) {
        instance->end();
        return false;
    }

#if PLATFORM(ANDROID)
    // JSC does not seem to support returning object properties so we emulate that
    // behaviour here.
    JavaValue value;
#else
    JavaValue value = instance->getField(field);
#endif // PLATFORM(ANDROID)
    instance->end();

    convertJavaValueToNPVariant(value, result);

    return true;
}
Ejemplo n.º 4
0
bool JavaNPObject_HasProperty(NPObject* obj, NPIdentifier identifier) {
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (instance == 0)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (name == 0)
        return false;
    bool result = instance->getClass()->fieldNamed(name) != 0;
    free(name);
    return result;
}
Ejemplo n.º 5
0
bool JavaNPObject_HasMethod(NPObject* obj, NPIdentifier identifier) {
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (instance == 0)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (name == 0)
        return false;

    bool result = (instance->getClass()->methodsNamed(name).size() > 0);

    // TODO: use NPN_MemFree
    free(name);

    return result;
}
Ejemplo n.º 6
0
bool JavaNPObject_Invoke(NPObject* obj, NPIdentifier identifier,
        const NPVariant* args, uint32_t argCount, NPVariant* result) {
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (instance == 0)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (name == 0)
        return false;

    bool r = instance->invokeMethod(name, args, argCount, result);

    // TODO: use NPN_MemFree
    free(name);
    return r;

}
Ejemplo n.º 7
0
bool JavaNPObjectInvoke(NPObject* obj, NPIdentifier identifier, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (!instance)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (!name)
        return false;
		
    instance->begin();

    MethodList methodList = instance->getClass()->methodsNamed(name);
    // TODO: use NPN_MemFree
    free(name);

    // Try to find a good match for the overloaded method. The
    // fundamental problem is that JavaScript doesn't have the
    // notion of method overloading and Java does. We could
    // get a bit more sophisticated and attempt to do some
    // type checking as well as checking the number of parameters.
    size_t numMethods = methodList.size();
    JavaMethod* aMethod;
    JavaMethod* jMethod = 0;
    for (size_t methodIndex = 0; methodIndex < numMethods; methodIndex++) {
        aMethod = methodList[methodIndex];
        if (aMethod->numParameters() == static_cast<int>(argCount)) {
            jMethod = aMethod;
            break;
        }
    }
    if (!jMethod) {
        instance->end();
        return false;
    }

    JavaValue* jArgs = new JavaValue[argCount];
    for (unsigned int i = 0; i < argCount; i++)
        jArgs[i] = convertNPVariantToJavaValue(args[i], jMethod->parameterAt(i));

    JavaValue jResult = instance->invokeMethod(jMethod, jArgs);
    instance->end();
    delete[] jArgs;

    VOID_TO_NPVARIANT(*result);
    convertJavaValueToNPVariant(jResult, result);
    return true;
}
Ejemplo n.º 8
0
// These methods are used by NPAI, thats the reason to use bool instead of Eina_Bool.
static bool ewk_js_property_has(NPObject* npObject, NPIdentifier name)
{
    Ewk_JS_Object* object = reinterpret_cast<Ewk_JS_Object*>(npObject);

    EINA_SAFETY_ON_NULL_RETURN_VAL(npObject, false);
    EINA_MAGIC_CHECK_OR_RETURN(object, false);

    if (!_NPN_IdentifierIsString(name)) {
        ERR("int NPIdentifier is not supported.");
        return false;
    }

    char* prop_name = _NPN_UTF8FromIdentifier(name);
    bool fail = eina_hash_find(object->properties, prop_name); // FIXME: should search methods too?
    free(prop_name);

    return fail;
}
Ejemplo n.º 9
0
static bool ewk_js_property_get(NPObject* npObject, NPIdentifier name, NPVariant* result)
{
    Ewk_JS_Object* object = reinterpret_cast<Ewk_JS_Object*>(npObject);
    Ewk_JS_Variant* value;
    Ewk_JS_Property* prop;
    bool fail = false;

    EINA_SAFETY_ON_NULL_RETURN_VAL(npObject, false);
    EINA_MAGIC_CHECK_OR_RETURN(object, false);

    if (!_NPN_IdentifierIsString(name)) {
        ERR("int NPIdentifier is not supported.");
        return false;
    }

    value = static_cast<Ewk_JS_Variant*>(malloc(sizeof(Ewk_JS_Variant)));
    if (!value) {
        ERR("Could not allocate memory for ewk_js_variant");
        return false;
    }

    prop = static_cast<Ewk_JS_Property*>(eina_hash_find(object->cls->properties, name));
    if (prop && prop->get) { // Class has property and property has getter.
        fail = prop->get(object, prop->name, value);
        if (!fail)
            fail = ewk_js_variant_to_npvariant(value, result);
    } else if (object->cls->default_prop.get) { // Default getter exists.
        fail = object->cls->default_prop.get(object, prop->name, value);
        if (!fail)
            fail = ewk_js_variant_to_npvariant(value, result);
    } else { // Fallback to objects hash map.
        char* prop_name = _NPN_UTF8FromIdentifier(name);
        free(value);
        value = static_cast<Ewk_JS_Variant*>(eina_hash_find(object->properties, prop_name));
        free(prop_name);
        if (value)
            return ewk_js_variant_to_npvariant(value, result);
    }

    free(value);
    return fail;
}
Ejemplo n.º 10
0
bool JavaNPObjectGetProperty(NPObject* obj, NPIdentifier identifier, NPVariant* result)
{
    VOID_TO_NPVARIANT(*result);
    JavaInstance* instance = ExtractJavaInstance(obj);
    if (!instance)
        return false;
    NPUTF8* name = _NPN_UTF8FromIdentifier(identifier);
    if (!name)
        return false;

    instance->begin();
    JavaField* field = instance->getClass()->fieldNamed(name);
    free(name); // TODO: use NPN_MemFree
    if (!field)
        return false;

    JavaValue value = instance->getField(field);
    instance->end();

    convertJavaValueToNPVariant(value, result);

    return true;
}
Ejemplo n.º 11
0
NPUTF8* WebBindings::utf8FromIdentifier(NPIdentifier identifier)
{
    return _NPN_UTF8FromIdentifier(identifier);
}
Ejemplo n.º 12
0
NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier) {
  return _NPN_UTF8FromIdentifier(identifier);
}
Ejemplo n.º 13
0
static Ewk_JS_Object* ewk_js_npobject_to_object(NPObject* npObject)
{
    NPIdentifier* values;
    uint32_t np_props_count;
    Ewk_JS_Class* cls;
    Ewk_JS_Object* object;
    Eina_Iterator* it;
    Ewk_JS_Property* prop;
    JavaScriptObject* jso;

    if (EINA_MAGIC_CHECK(reinterpret_cast<Ewk_JS_Object*>(npObject), EWK_JS_OBJECT_MAGIC))
        return reinterpret_cast<Ewk_JS_Object*>(npObject);

    if (!_NPN_Enumerate(0, npObject, &values, &np_props_count))
        return 0;

    cls = static_cast<Ewk_JS_Class*>(malloc(sizeof(Ewk_JS_Class)));
    if (!cls) {
        ERR("Could not allocate memory for ewk_js_class");
        return 0;
    }

    cls->meta = 0;
    Ewk_JS_Default def = {
        ewk_js_npobject_property_set,
        ewk_js_npobject_property_get,
        ewk_js_npobject_property_del
    };
    cls->default_prop = def;
    cls->methods = eina_hash_pointer_new(0);
    cls->properties = eina_hash_pointer_new(reinterpret_cast<Eina_Free_Cb>(ewk_js_property_free));
    for (uint32_t i = 0; i < np_props_count; i++) {
        if (_NPN_HasProperty(0, npObject, values[i])) {
            NPVariant var;
            Ewk_JS_Property* prop = static_cast<Ewk_JS_Property*>(calloc(sizeof(Ewk_JS_Property), 1));
            if (!prop) {
                ERR("Could not allocate memory for ewk_js_property");
                goto error;
            }

            _NPN_GetProperty(0, npObject, values[i], &var);
            ewk_js_npvariant_to_variant(&(prop->value), &var);
            prop->name = _NPN_UTF8FromIdentifier(values[i]);
            eina_hash_add(cls->properties, values[i], prop);
        }
    }

    // Can't use ewk_js_object_new(cls) because it expects cls->meta to exist.
    object = static_cast<Ewk_JS_Object*>(malloc(sizeof(Ewk_JS_Object)));
    if (!object) {
        ERR("Could not allocate memory for ewk_js_object");
        goto error;
    }

    free(values);
    EINA_MAGIC_SET(object, EWK_JS_OBJECT_MAGIC);
    object->name = 0;
    object->cls = cls;
    object->view = 0;

    jso = reinterpret_cast<JavaScriptObject*>(npObject);
    if (!strcmp("Array", jso->imp->methodTable()->className(jso->imp).ascii().data()))
        object->type = EWK_JS_OBJECT_ARRAY;
    else if (!strcmp("Function", jso->imp->methodTable()->className(jso->imp).ascii().data()))
        object->type = EWK_JS_OBJECT_FUNCTION;
    else
        object->type = EWK_JS_OBJECT_OBJECT;

    if (eina_hash_population(cls->properties) < 25)
        object->properties = eina_hash_string_small_new(0);
    else
        object->properties = eina_hash_string_superfast_new(0);

    it = eina_hash_iterator_data_new(cls->properties);
    EINA_ITERATOR_FOREACH(it, prop) {
        const char* key = prop->name;
        Ewk_JS_Variant* value = &prop->value;
        eina_hash_add(object->properties, key, value);
    }

    eina_iterator_free(it);
    object->base = *reinterpret_cast<JavaScriptObject*>(npObject);

    return object;

error:
    ewk_js_class_free(cls);
    free(values);
    return 0;
}