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