Chromium Code Reviews| Index: base/android/jni_generator/jni_generator.py |
| diff --git a/base/android/jni_generator/jni_generator.py b/base/android/jni_generator/jni_generator.py |
| index 33f83ecc6f4fc9161cb7b16bb81358ffae54a275..c71d5d2c0726e6841b32d7fa19a1232e724397bf 100755 |
| --- a/base/android/jni_generator/jni_generator.py |
| +++ b/base/android/jni_generator/jni_generator.py |
| @@ -135,6 +135,15 @@ def JavaDataTypeToC(java_type): |
| return 'jobject' |
| +def JavaDataTypeToCForDeclaration(java_type): |
| + """Returns a JavaRef-wrapped C datatype for the given java type.""" |
| + c_type = JavaDataTypeToC(java_type) |
| + if re.match(RE_SCOPED_JNI_TYPES, c_type): |
| + return 'const JavaParamRef<' + c_type + '>&' |
| + else: |
| + return c_type |
| + |
| + |
| def JavaDataTypeToCForCalledByNativeParam(java_type): |
| """Returns a C datatype to be when calling from native.""" |
| if java_type == 'int': |
| @@ -527,10 +536,9 @@ def MangleCalledByNatives(called_by_natives): |
| return called_by_natives |
| -# Regex to match the JNI return types that should be included in a |
| -# ScopedJavaLocalRef. |
| -RE_SCOPED_JNI_RETURN_TYPES = re.compile( |
| - 'jobject|jclass|jstring|jthrowable|.*Array') |
| +# Regex to match the JNI types that should be wrapped in a JavaRef. |
| +RE_SCOPED_JNI_TYPES = re.compile('jobject|jclass|jstring|jthrowable|.*Array') |
| + |
| # Regex to match a string like "@CalledByNative public void foo(int bar)". |
| RE_CALLED_BY_NATIVE = re.compile( |
| @@ -1005,7 +1013,18 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| return '\n'.join(all_namespaces) + '\n' |
| return '' |
| - def GetJNIFirstParam(self, native): |
| + def GetJNIFirstParamForDeclaration(self, native): |
|
rmcilroy
2015/08/28 12:16:04
I think you could avoid this extra method if you h
Torne
2015/08/28 12:36:17
Done.
|
| + ret = [] |
| + if native.type == 'method': |
| + ret = ['const JavaParamRef<jobject>& jcaller'] |
| + elif native.type == 'function': |
| + if native.static: |
| + ret = ['const JavaParamRef<jclass>& jcaller'] |
| + else: |
| + ret = ['const JavaParamRef<jobject>& jcaller'] |
| + return ret |
| + |
| + def GetJNIFirstParamForStub(self, native): |
| ret = [] |
| if native.type == 'method': |
| ret = ['jobject jcaller'] |
| @@ -1016,7 +1035,32 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| ret = ['jobject jcaller'] |
| return ret |
| + def GetJNIFirstParamForCall(self, native): |
|
rmcilroy
2015/08/28 12:16:04
nit - move this down to be above GetNativeStub whe
Torne
2015/08/28 12:36:17
Done.
|
| + ret = [] |
| + if native.type == 'method': |
| + ret = ['JavaParamRef<jobject>(env, jcaller)'] |
| + elif native.type == 'function': |
| + if native.static: |
| + ret = ['JavaParamRef<jclass>(env, jcaller)'] |
| + else: |
| + ret = ['JavaParamRef<jobject>(env, jcaller)'] |
| + return ret |
| + |
| def GetParamsInDeclaration(self, native): |
| + """Returns the params for the forward declaration. |
| + |
| + Args: |
| + native: the native dictionary describing the method. |
| + |
| + Returns: |
| + A string containing the params. |
| + """ |
| + return ',\n '.join(self.GetJNIFirstParamForDeclaration(native) + |
| + [JavaDataTypeToCForDeclaration(param.datatype) + ' ' + |
| + param.name |
| + for param in native.params]) |
| + |
| + def GetParamsInStub(self, native): |
| """Returns the params for the stub declaration. |
| Args: |
| @@ -1025,7 +1069,7 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| Returns: |
| A string containing the params. |
| """ |
| - return ',\n '.join(self.GetJNIFirstParam(native) + |
| + return ',\n '.join(self.GetJNIFirstParamForStub(native) + |
| [JavaDataTypeToC(param.datatype) + ' ' + |
| param.name |
| for param in native.params]) |
| @@ -1065,8 +1109,18 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| params = native.params |
| params_in_call = [] |
| if not self.options.pure_native_methods: |
| - params_in_call = ['env', 'jcaller'] |
| - params_in_call = ', '.join(params_in_call + [p.name for p in params]) |
| + params_in_call = ['env'] + self.GetJNIFirstParamForCall(native) |
| + param_ref_template = Template('JavaParamRef<${TYPE}>(env, ${NAME})') |
|
rmcilroy
2015/08/28 12:16:04
nit - could you create a helper for this (which co
Torne
2015/08/28 12:36:17
Done.
|
| + for p in params: |
| + c_type = JavaDataTypeToC(p.datatype) |
| + if re.match(RE_SCOPED_JNI_TYPES, c_type): |
| + params_in_call.append(param_ref_template.substitute({ |
| + 'TYPE': c_type, |
| + 'NAME': p.name, |
| + })) |
| + else: |
| + params_in_call.append(p.name) |
| + params_in_call = ', '.join(params_in_call) |
| if self.options.native_exports: |
| stub_visibility = 'extern "C" __attribute__((visibility("default")))\n' |
| @@ -1074,7 +1128,7 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| stub_visibility = 'static ' |
| return_type = return_declaration = JavaDataTypeToC(native.return_type) |
| post_call = '' |
| - if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| + if re.match(RE_SCOPED_JNI_TYPES, return_type): |
| post_call = '.Release()' |
| return_declaration = 'ScopedJavaLocalRef<' + return_type + '>' |
| values = { |
| @@ -1082,6 +1136,7 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| 'RETURN_DECLARATION': return_declaration, |
| 'NAME': native.name, |
| 'PARAMS': self.GetParamsInDeclaration(native), |
| + 'PARAMS_IN_STUB': self.GetParamsInStub(native), |
| 'PARAMS_IN_CALL': params_in_call, |
| 'POST_CALL': post_call, |
| 'STUB_NAME': self.GetStubName(native), |
| @@ -1099,7 +1154,7 @@ Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { |
| }) |
| template = Template("""\ |
| ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, |
| - ${PARAMS}) { |
| + ${PARAMS_IN_STUB}) { |
| ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
| CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); |
| return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
| @@ -1109,7 +1164,7 @@ ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, |
| template = Template(""" |
| static ${RETURN_DECLARATION} ${NAME}(JNIEnv* env, ${PARAMS}); |
| -${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS}) { |
| +${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_STUB}) { |
| return ${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
| } |
| """) |
| @@ -1157,7 +1212,7 @@ ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS}) { |
| if return_type != 'void': |
| pre_call = ' ' + pre_call |
| return_declaration = return_type + ' ret =' |
| - if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| + if re.match(RE_SCOPED_JNI_TYPES, return_type): |
| return_type = 'ScopedJavaLocalRef<' + return_type + '>' |
| return_clause = 'return ' + return_type + '(env, ret);' |
| else: |