| OLD | NEW |
| 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 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1060 params = native.params | 1060 params = native.params |
| 1061 params_in_call = [] | 1061 params_in_call = [] |
| 1062 if not self.options.pure_native_methods: | 1062 if not self.options.pure_native_methods: |
| 1063 params_in_call = ['env', 'jcaller'] | 1063 params_in_call = ['env', 'jcaller'] |
| 1064 params_in_call = ', '.join(params_in_call + [p.name for p in params]) | 1064 params_in_call = ', '.join(params_in_call + [p.name for p in params]) |
| 1065 | 1065 |
| 1066 if self.options.native_exports: | 1066 if self.options.native_exports: |
| 1067 stub_visibility = 'extern "C" __attribute__((visibility("default")))\n' | 1067 stub_visibility = 'extern "C" __attribute__((visibility("default")))\n' |
| 1068 else: | 1068 else: |
| 1069 stub_visibility = 'static ' | 1069 stub_visibility = 'static ' |
| 1070 return_type = return_declaration = JavaDataTypeToC(native.return_type) | 1070 return_type = JavaDataTypeToC(native.return_type) |
| 1071 post_call = '' | |
| 1072 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): | |
| 1073 post_call = '.Release()' | |
| 1074 return_declaration = 'ScopedJavaLocalRef<' + return_type + '>' | |
| 1075 values = { | 1071 values = { |
| 1076 'RETURN': return_type, | 1072 'RETURN': return_type, |
| 1077 'RETURN_DECLARATION': return_declaration, | |
| 1078 'NAME': native.name, | 1073 'NAME': native.name, |
| 1079 'PARAMS': self.GetParamsInDeclaration(native), | 1074 'PARAMS': self.GetParamsInDeclaration(native), |
| 1080 'PARAMS_IN_CALL': params_in_call, | 1075 'PARAMS_IN_CALL': params_in_call, |
| 1081 'POST_CALL': post_call, | |
| 1082 'STUB_NAME': self.GetStubName(native), | 1076 'STUB_NAME': self.GetStubName(native), |
| 1083 'STUB_VISIBILITY': stub_visibility, | 1077 'STUB_VISIBILITY': stub_visibility, |
| 1084 } | 1078 } |
| 1085 | 1079 |
| 1086 if is_method: | 1080 if is_method: |
| 1087 optional_error_return = JavaReturnValueToC(native.return_type) | 1081 optional_error_return = JavaReturnValueToC(native.return_type) |
| 1088 if optional_error_return: | 1082 if optional_error_return: |
| 1089 optional_error_return = ', ' + optional_error_return | 1083 optional_error_return = ', ' + optional_error_return |
| 1084 post_call = '' |
| 1085 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| 1086 post_call = '.Release()' |
| 1090 values.update({ | 1087 values.update({ |
| 1091 'OPTIONAL_ERROR_RETURN': optional_error_return, | 1088 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 1092 'PARAM0_NAME': native.params[0].name, | 1089 'PARAM0_NAME': native.params[0].name, |
| 1093 'P0_TYPE': native.p0_type, | 1090 'P0_TYPE': native.p0_type, |
| 1091 'POST_CALL': post_call, |
| 1094 }) | 1092 }) |
| 1095 template = Template("""\ | 1093 template = Template("""\ |
| 1096 ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, | 1094 ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, |
| 1097 ${PARAMS}) { | 1095 ${PARAMS}) { |
| 1098 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); | 1096 ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); |
| 1099 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); | 1097 CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); |
| 1100 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; | 1098 return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; |
| 1101 } | 1099 } |
| 1102 """) | 1100 """) |
| 1103 else: | 1101 else: |
| 1104 template = Template(""" | 1102 template = Template(""" |
| 1105 static ${RETURN_DECLARATION} ${NAME}(JNIEnv* env, ${PARAMS}); | 1103 static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); |
| 1106 | 1104 |
| 1107 ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS}) { | 1105 ${STUB_VISIBILITY}${RETURN} ${STUB_NAME}(JNIEnv* env, ${PARAMS}) { |
| 1108 return ${NAME}(${PARAMS_IN_CALL})${POST_CALL}; | 1106 return ${NAME}(${PARAMS_IN_CALL}); |
| 1109 } | 1107 } |
| 1110 """) | 1108 """) |
| 1111 | 1109 |
| 1112 return template.substitute(values) | 1110 return template.substitute(values) |
| 1113 | 1111 |
| 1114 def GetArgument(self, param): | 1112 def GetArgument(self, param): |
| 1115 return ('as_jint(' + param.name + ')' | 1113 return ('as_jint(' + param.name + ')' |
| 1116 if param.datatype == 'int' else param.name) | 1114 if param.datatype == 'int' else param.name) |
| 1117 | 1115 |
| 1118 def GetArgumentsInCall(self, params): | 1116 def GetArgumentsInCall(self, params): |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1146 return_type = JavaDataTypeToC(called_by_native.return_type) | 1144 return_type = JavaDataTypeToC(called_by_native.return_type) |
| 1147 optional_error_return = JavaReturnValueToC(called_by_native.return_type) | 1145 optional_error_return = JavaReturnValueToC(called_by_native.return_type) |
| 1148 if optional_error_return: | 1146 if optional_error_return: |
| 1149 optional_error_return = ', ' + optional_error_return | 1147 optional_error_return = ', ' + optional_error_return |
| 1150 return_declaration = '' | 1148 return_declaration = '' |
| 1151 return_clause = '' | 1149 return_clause = '' |
| 1152 if return_type != 'void': | 1150 if return_type != 'void': |
| 1153 pre_call = ' ' + pre_call | 1151 pre_call = ' ' + pre_call |
| 1154 return_declaration = return_type + ' ret =' | 1152 return_declaration = return_type + ' ret =' |
| 1155 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): | 1153 if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): |
| 1156 return_type = 'ScopedJavaLocalRef<' + return_type + '>' | 1154 return_type = 'base::android::ScopedJavaLocalRef<' + return_type + '>' |
| 1157 return_clause = 'return ' + return_type + '(env, ret);' | 1155 return_clause = 'return ' + return_type + '(env, ret);' |
| 1158 else: | 1156 else: |
| 1159 return_clause = 'return ret;' | 1157 return_clause = 'return ret;' |
| 1160 return { | 1158 return { |
| 1161 'JAVA_CLASS': java_class, | 1159 'JAVA_CLASS': java_class, |
| 1162 'RETURN_TYPE': return_type, | 1160 'RETURN_TYPE': return_type, |
| 1163 'OPTIONAL_ERROR_RETURN': optional_error_return, | 1161 'OPTIONAL_ERROR_RETURN': optional_error_return, |
| 1164 'RETURN_DECLARATION': return_declaration, | 1162 'RETURN_DECLARATION': return_declaration, |
| 1165 'RETURN_CLAUSE': return_clause, | 1163 'RETURN_CLAUSE': return_clause, |
| 1166 'FIRST_PARAM_IN_DECLARATION': first_param_in_declaration, | 1164 'FIRST_PARAM_IN_DECLARATION': first_param_in_declaration, |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1523 GenerateJNIHeader(input_file, output_file, options) | 1521 GenerateJNIHeader(input_file, output_file, options) |
| 1524 | 1522 |
| 1525 if options.depfile: | 1523 if options.depfile: |
| 1526 build_utils.WriteDepfile( | 1524 build_utils.WriteDepfile( |
| 1527 options.depfile, | 1525 options.depfile, |
| 1528 build_utils.GetPythonDependencies()) | 1526 build_utils.GetPythonDependencies()) |
| 1529 | 1527 |
| 1530 | 1528 |
| 1531 if __name__ == '__main__': | 1529 if __name__ == '__main__': |
| 1532 sys.exit(main(sys.argv)) | 1530 sys.exit(main(sys.argv)) |
| OLD | NEW |