1/*
2 * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright 2010, The Android Open Source Project
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "JavaInstanceJobjectV8.h"
29
30#if ENABLE(JAVA_BRIDGE)
31
32#include "JNIUtilityPrivate.h"
33#include "JavaClassJobjectV8.h"
34#include "JavaFieldV8.h"
35#include "JavaMethod.h"
36
37#include <wtf/OwnArrayPtr.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/text/CString.h>
40#include <wtf/text/StringBuilder.h>
41
42using namespace JSC::Bindings;
43
44JavaInstanceJobject::JavaInstanceJobject(jobject instance)
45    : m_instance(new JobjectWrapper(instance))
46{
47}
48
49#define NUM_LOCAL_REFS 64
50
51void JavaInstanceJobject::begin()
52{
53    getJNIEnv()->PushLocalFrame(NUM_LOCAL_REFS);
54}
55
56void JavaInstanceJobject::end()
57{
58    getJNIEnv()->PopLocalFrame(0);
59}
60
61JavaClass* JavaInstanceJobject::getClass() const
62{
63    if (!m_class)
64        m_class = adoptPtr(new JavaClassJobject(javaInstance()));
65    return m_class.get();
66}
67
68JavaValue JavaInstanceJobject::invokeMethod(const JavaMethod* method, JavaValue* args)
69{
70    ASSERT(getClass()->methodsNamed(method->name().utf8().data()).find(method) != notFound);
71    unsigned int numParams = method->numParameters();
72    OwnArrayPtr<jvalue> jvalueArgs = adoptArrayPtr(new jvalue[numParams]);
73    for (unsigned int i = 0; i < numParams; ++i)
74        jvalueArgs[i] = javaValueToJvalue(args[i]);
75    jvalue result = callJNIMethod(javaInstance(), method->returnType(), method->name().utf8().data(), method->signature(), jvalueArgs.get());
76    return jvalueToJavaValue(result, method->returnType());
77}
78
79static void appendClassName(StringBuilder& builder, const char* className)
80{
81    char* c = fastStrDup(className);
82    char* result = c;
83    while (*c) {
84        if (*c == '.')
85            *c = '/';
86        c++;
87    }
88    builder.append(result);
89    fastFree(result);
90}
91
92JavaValue JavaInstanceJobject::getField(const JavaField* field)
93{
94    ASSERT(getClass()->fieldNamed(field->name().utf8().data()) == field);
95
96    StringBuilder signature;
97    signature.append(signatureFromJavaType(field->type()));
98    if (field->type() == JavaTypeObject || field->type() == JavaTypeString) {
99        appendClassName(signature, field->typeClassName());
100        signature.append(';');
101    }
102    return jvalueToJavaValue(getJNIField(javaInstance(), field->type(), field->name().utf8().data(), signature.toString().utf8().data()), field->type());
103}
104
105#endif // ENABLE(JAVA_BRIDGE)
106