Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: base/android/jni_generator/jni_generator.py

Issue 938373002: android: Refactor function name template in JNI generator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added docstring for GetStubName Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Extracts native methods from a Java file and generates the JNI bindings. 6 """Extracts native methods from a Java file and generates the JNI bindings.
7 If you change this, please run and update the tests.""" 7 If you change this, please run and update the tests."""
8 8
9 import collections 9 import collections
10 import errno 10 import errno
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 [JavaDataTypeToC(param.datatype) + ' ' + 1028 [JavaDataTypeToC(param.datatype) + ' ' +
1029 param.name 1029 param.name
1030 for param in native.params]) 1030 for param in native.params])
1031 1031
1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native): 1032 def GetCalledByNativeParamsInDeclaration(self, called_by_native):
1033 return ',\n '.join([ 1033 return ',\n '.join([
1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' + 1034 JavaDataTypeToCForCalledByNativeParam(param.datatype) + ' ' +
1035 param.name 1035 param.name
1036 for param in called_by_native.params]) 1036 for param in called_by_native.params])
1037 1037
1038 def GetStubName(self, native):
1039 """Return the name of the stub function for this native method.
1040
1041 Args:
1042 native: the native dictionary describing the method.
1043
1044 Returns:
1045 A string with the stub function name. For native exports mode this is the
1046 Java_* symbol name required by the JVM; otherwise it is just the name of
1047 the native method itself.
1048 """
1049 if self.options.native_exports:
1050 template = Template("Java_${JAVA_NAME}_native${NAME}")
1051
1052 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1053 java_name = java_name.replace('_', '_1').replace('/', '_')
1054 if native.java_class_name:
1055 java_name += '_00024' + native.java_class_name
1056
1057 values = {'NAME': native.name,
1058 'JAVA_NAME': java_name}
1059 return template.substitute(values)
1060 else:
1061 return native.name
1062
1038 def GetForwardDeclaration(self, native): 1063 def GetForwardDeclaration(self, native):
1039 template_str = """ 1064 template_str = """
1040 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); 1065 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS});
1041 """ 1066 """
1042 if self.options.native_exports: 1067 if self.options.native_exports:
1043 template_str += """ 1068 template_str += """
1044 __attribute__((visibility("default"), alias("${NAME}"))) 1069 __attribute__((visibility("default"), alias("${NAME}")))
1045 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, ${PARAMS}); 1070 ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS});
1046 """ 1071 """
1047 template = Template(template_str) 1072 template = Template(template_str)
1048 params_in_call = [] 1073 params_in_call = []
1049 if not self.options.pure_native_methods: 1074 if not self.options.pure_native_methods:
1050 params_in_call = ['env', 'jcaller'] 1075 params_in_call = ['env', 'jcaller']
1051 params_in_call = ', '.join(params_in_call + [p.name for p in native.params]) 1076 params_in_call = ', '.join(params_in_call + [p.name for p in native.params])
1052 1077
1053 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1054 java_name = java_name.replace('_', '_1').replace('/', '_')
1055 if native.java_class_name:
1056 java_name += '_00024' + native.java_class_name
1057
1058 values = {'RETURN': JavaDataTypeToC(native.return_type), 1078 values = {'RETURN': JavaDataTypeToC(native.return_type),
1059 'NAME': native.name, 1079 'NAME': native.name,
1060 'JAVA_NAME': java_name,
1061 'PARAMS': self.GetParamsInDeclaration(native), 1080 'PARAMS': self.GetParamsInDeclaration(native),
1062 'PARAMS_IN_CALL': params_in_call} 1081 'PARAMS_IN_CALL': params_in_call,
1082 'STUB_NAME': self.GetStubName(native)}
1063 return template.substitute(values) 1083 return template.substitute(values)
1064 1084
1065 def GetNativeMethodStubString(self, native): 1085 def GetNativeMethodStubString(self, native):
1066 """Returns stubs for native methods.""" 1086 """Returns stubs for native methods."""
1067 if self.options.native_exports: 1087 if self.options.native_exports:
1068 template_str = """\ 1088 template_str = """\
1069 __attribute__((visibility("default"))) 1089 __attribute__((visibility("default")))
1070 ${RETURN} Java_${JAVA_NAME}_native${NAME}(JNIEnv* env, 1090 ${RETURN} ${STUB_NAME}(JNIEnv* env,
1071 ${PARAMS_IN_DECLARATION}) {""" 1091 ${PARAMS_IN_DECLARATION}) {"""
1072 else: 1092 else:
1073 template_str = """\ 1093 template_str = """\
1074 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {""" 1094 static ${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) {"""
1075 template_str += """ 1095 template_str += """
1076 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); 1096 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME});
1077 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); 1097 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN});
1078 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; 1098 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL};
1079 } 1099 }
1080 """ 1100 """
1081 1101
1082 template = Template(template_str) 1102 template = Template(template_str)
1083 params = [] 1103 params = []
1084 if not self.options.pure_native_methods: 1104 if not self.options.pure_native_methods:
1085 params = ['env', 'jcaller'] 1105 params = ['env', 'jcaller']
1086 params_in_call = ', '.join(params + [p.name for p in native.params[1:]]) 1106 params_in_call = ', '.join(params + [p.name for p in native.params[1:]])
1087 1107
1088 return_type = JavaDataTypeToC(native.return_type) 1108 return_type = JavaDataTypeToC(native.return_type)
1089 optional_error_return = JavaReturnValueToC(native.return_type) 1109 optional_error_return = JavaReturnValueToC(native.return_type)
1090 if optional_error_return: 1110 if optional_error_return:
1091 optional_error_return = ', ' + optional_error_return 1111 optional_error_return = ', ' + optional_error_return
1092 post_call = '' 1112 post_call = ''
1093 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): 1113 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type):
1094 post_call = '.Release()' 1114 post_call = '.Release()'
1095 1115
1096 if self.options.native_exports:
1097 java_name = JniParams.RemapClassName(self.fully_qualified_class)
1098 java_name = java_name.replace('_', '_1').replace('/', '_')
1099 if native.java_class_name:
1100 java_name += '_00024' + native.java_class_name
1101 else:
1102 java_name = ''
1103
1104 values = { 1116 values = {
1105 'RETURN': return_type, 1117 'RETURN': return_type,
1106 'OPTIONAL_ERROR_RETURN': optional_error_return, 1118 'OPTIONAL_ERROR_RETURN': optional_error_return,
1107 'JAVA_NAME': java_name,
1108 'NAME': native.name, 1119 'NAME': native.name,
1109 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), 1120 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native),
1110 'PARAM0_NAME': native.params[0].name, 1121 'PARAM0_NAME': native.params[0].name,
1111 'P0_TYPE': native.p0_type, 1122 'P0_TYPE': native.p0_type,
1112 'PARAMS_IN_CALL': params_in_call, 1123 'PARAMS_IN_CALL': params_in_call,
1113 'POST_CALL': post_call 1124 'POST_CALL': post_call,
1125 'STUB_NAME': self.GetStubName(native),
1114 } 1126 }
1115 return template.substitute(values) 1127 return template.substitute(values)
1116 1128
1117 def GetArgument(self, param): 1129 def GetArgument(self, param):
1118 return ('as_jint(' + param.name + ')' 1130 return ('as_jint(' + param.name + ')'
1119 if param.datatype == 'int' else param.name) 1131 if param.datatype == 'int' else param.name)
1120 1132
1121 def GetArgumentsInCall(self, params): 1133 def GetArgumentsInCall(self, params):
1122 """Return a string of arguments to call from native into Java""" 1134 """Return a string of arguments to call from native into Java"""
1123 return [self.GetArgument(p) for p in params] 1135 return [self.GetArgument(p) for p in params]
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 values['FUNCTION_SIGNATURE'] = ( 1230 values['FUNCTION_SIGNATURE'] = (
1219 function_signature_template.substitute(values)) 1231 function_signature_template.substitute(values))
1220 if called_by_native.system_class: 1232 if called_by_native.system_class:
1221 values['FUNCTION_HEADER'] = ( 1233 values['FUNCTION_HEADER'] = (
1222 function_header_with_unused_template.substitute(values)) 1234 function_header_with_unused_template.substitute(values))
1223 else: 1235 else:
1224 values['FUNCTION_HEADER'] = function_header_template.substitute(values) 1236 values['FUNCTION_HEADER'] = function_header_template.substitute(values)
1225 return template.substitute(values) 1237 return template.substitute(values)
1226 1238
1227 def GetKMethodArrayEntry(self, native): 1239 def GetKMethodArrayEntry(self, native):
1228 template = Template("""\ 1240 template = Template(' { "native${NAME}", ${JNI_SIGNATURE}, ' +
1229 { "native${NAME}", ${JNI_SIGNATURE}, reinterpret_cast<void*>(${NAME}) },""") 1241 'reinterpret_cast<void*>(${STUB_NAME}) },')
1230 values = {'NAME': native.name, 1242 values = {'NAME': native.name,
1231 'JNI_SIGNATURE': JniParams.Signature(native.params, 1243 'JNI_SIGNATURE': JniParams.Signature(native.params,
1232 native.return_type, 1244 native.return_type,
1233 True)} 1245 True),
1246 'STUB_NAME': self.GetStubName(native)}
1234 return template.substitute(values) 1247 return template.substitute(values)
1235 1248
1236 def GetUniqueClasses(self, origin): 1249 def GetUniqueClasses(self, origin):
1237 ret = {self.class_name: self.fully_qualified_class} 1250 ret = {self.class_name: self.fully_qualified_class}
1238 for entry in origin: 1251 for entry in origin:
1239 class_name = self.class_name 1252 class_name = self.class_name
1240 jni_class_path = self.fully_qualified_class 1253 jni_class_path = self.fully_qualified_class
1241 if entry.java_class_name: 1254 if entry.java_class_name:
1242 class_name = entry.java_class_name 1255 class_name = entry.java_class_name
1243 jni_class_path = self.fully_qualified_class + '$' + class_name 1256 jni_class_path = self.fully_qualified_class + '$' + class_name
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
1520 GenerateJNIHeader(input_file, output_file, options) 1533 GenerateJNIHeader(input_file, output_file, options)
1521 1534
1522 if options.depfile: 1535 if options.depfile:
1523 build_utils.WriteDepfile( 1536 build_utils.WriteDepfile(
1524 options.depfile, 1537 options.depfile,
1525 build_utils.GetPythonDependencies()) 1538 build_utils.GetPythonDependencies())
1526 1539
1527 1540
1528 if __name__ == '__main__': 1541 if __name__ == '__main__':
1529 sys.exit(main(sys.argv)) 1542 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698