- c_ty = c_ty + "Array"
- if var_is_arr is not None:
- return TypeInfo(rust_obj=None, java_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
- passed_as_ptr=False, is_ptr=False, var_name=var_is_arr.group(1), arr_len=var_is_arr.group(2))
- return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
- is_ptr=is_ptr, var_name=fn_arg, arr_len=None)
-
- def map_type(fn_arg, print_void, ret_arr_len, is_free):
- ty_info = java_c_types(fn_arg, ret_arr_len)
-
- if ty_info.c_ty == "void":
- if not print_void:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = None, ret_conv = None, ret_conv_name = None)
-
- if ty_info.c_ty.endswith("Array"):
- arr_len = ty_info.arr_len
- if arr_len is not None:
- arr_name = ty_info.var_name
- else:
- arr_name = "ret"
- arr_len = ret_arr_len
- assert(ty_info.c_ty == "jbyteArray")
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n" +
- "(*_env)->GetByteArrayRegion (_env, """ + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" +
- "unsigned char (*""" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;",
- arg_conv_name = arr_name + "_ref",
- ret_conv = ("jbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" +
- "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", *",
- ");"),
- ret_conv_name = arr_name + "_arr")
- elif ty_info.var_name != "":
- # If we have a parameter name, print it (noting that it may indicate its a pointer)
- if ty_info.rust_obj is not None:
- assert(ty_info.passed_as_ptr)
- if not ty_info.is_ptr:
- if ty_info.rust_obj in unitary_enums:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_from_java(_env, " + ty_info.var_name + ");",
- arg_conv_name = ty_info.var_name + "_conv",
- ret_conv = ("jclass " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_to_java(_env, ", ");"),
- ret_conv_name = ty_info.var_name + "_conv")
- base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";";
- if ty_info.rust_obj in trait_structs:
- if not is_free:
- base_conv = base_conv + "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
- base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
- base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}"
- else:
- base_conv = base_conv + "\n" + "FREE((void*)" + ty_info.var_name + ");"
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = base_conv,
- arg_conv_name = ty_info.var_name + "_conv",
- ret_conv = ("CANT PASS TRAIT TO Java?", ""), ret_conv_name = "NO CONV POSSIBLE")
- if ty_info.rust_obj != "LDKu8slice":
- # Don't bother free'ing slices passed in - we often pass them Rust -> Rust
- base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");";
- if ty_info.rust_obj in opaque_structs:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = base_conv + "\n" + ty_info.var_name + "_conv.is_owned = true;",
- arg_conv_name = ty_info.var_name + "_conv",
- ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref")
-
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv",
- ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref")
- else:
- assert(not is_free)
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
- arg_conv_name = ty_info.var_name + "_conv",
- ret_conv = None, ret_conv_name = None) # its a pointer, no conv needed
- elif ty_info.is_ptr:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = ty_info.var_name, ret_conv = None, ret_conv_name = None)
- elif ty_info.java_ty == "String":
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = None,
- ret_conv = ("jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv")
+ java_hu_ty = java_ty
+ c_ty = c_ty + "Array"
+
+ subty = java_c_types(arr_ty, None)
+ if subty is None:
+ assert java_c_types_none_allowed
+ return None
+ if is_ptr:
+ subty.pass_by_ref = True
+
+ if var_is_arr is not None:
+ if var_is_arr.group(1) == "":
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty, is_const=is_const,
+ passed_as_ptr=False, is_ptr=False, nonnull_ptr=nonnull_ptr, var_name="arg", subty=subty,
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False, contains_trait=contains_trait)
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty, is_const=is_const,
+ passed_as_ptr=False, is_ptr=False, nonnull_ptr=nonnull_ptr, var_name=var_is_arr.group(1), subty=subty,
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False, contains_trait=contains_trait)
+
+ if java_hu_ty is None:
+ java_hu_ty = java_ty
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
+ is_const=is_const, is_ptr=is_ptr, nonnull_ptr=nonnull_ptr, var_name=fn_arg, arr_len=arr_len, arr_access=arr_access, is_native_primitive=is_primitive,
+ contains_trait=contains_trait, subty=subty)
+
+fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
+fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
+reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
+clone_fns = set()
+constructor_fns = {}
+
+from gen_type_mapping import TypeMappingGenerator
+type_mapping_generator = TypeMappingGenerator(java_c_types, consts, opaque_structs, clone_fns, unitary_enums, trait_structs, complex_enums, result_types, tuple_types)
+
+with open(sys.argv[1]) as in_h:
+ for line in in_h:
+ reg_fn = reg_fn_regex.match(line)
+ if reg_fn is not None:
+ if reg_fn.group(2).endswith("_clone"):
+ clone_fns.add(reg_fn.group(2))