- var_is_arr = var_is_arr_regex.match(fn_arg)
- if var_is_arr is not None or ret_arr_len is not None:
- assert(not take_by_ptr)
- assert(not is_ptr)
- java_ty = java_ty + "[]"
- c_ty = c_ty + "Array"
- if var_is_arr is not None:
- if var_is_arr.group(1) == "":
- return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
- passed_as_ptr=False, is_ptr=False, var_name="arg", arr_len=var_is_arr.group(2), arr_access=arr_access)
- return TypeInfo(rust_obj=rust_obj, 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), arr_access=arr_access)
- 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, arr_access=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, arg_conv_cleanup = 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")
- ret_conv = ("jbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", ", "")
- arg_conv_cleanup = None
- if not arr_len.isdigit():
- arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
- arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = (*_env)->GetByteArrayElements (_env, " + arr_name + ", NULL);\n"
- arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");"
- arg_conv_cleanup = "(*_env)->ReleaseByteArrayElements(_env, " + arr_name + ", (int8_t*)" + arr_name + "_ref." + ty_info.arr_access + ", 0);"
- arr_access = "." + ty_info.arr_access
- ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
- ret_conv = (ret_conv[0], ";\njbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_name + "_var." + arr_len + ");\n")
- ret_conv = (ret_conv[0], ret_conv[1] + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_name + "_var." + arr_len + ", " + arr_name + "_var." + ty_info.arr_access + ");")
- elif ty_info.rust_obj is not None:
- arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
- arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
- arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_ref." + ty_info.arr_access + ");"
- ret_conv = (ret_conv[0], "." + ty_info.arr_access + ");")
- else:
- arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n"
- arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
- arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" + "unsigned char (*" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;"
- ret_conv = (ret_conv[0] + "*", ");")
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = arg_conv, arg_conv_name = arr_name + "_ref", arg_conv_cleanup = arg_conv_cleanup,
- ret_conv = ret_conv, 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)
- opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
- opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = (void*)(" + ty_info.var_name + " & (~1));\n"
- opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = (" + ty_info.var_name + " & 1) || (" + ty_info.var_name + " == 0);"
- if not ty_info.is_ptr and not is_free:
- if (ty_info.rust_obj.replace("LDK", "") + "_clone") in clone_fns:
- # TODO: This is a bit too naive, even with the checks above, we really need to know if rust wants a ref or not, not just if its pass as a ptr.
- opaque_arg_conv = opaque_arg_conv + "\nif (" + ty_info.var_name + "_conv.inner != NULL)\n"
- opaque_arg_conv = opaque_arg_conv + "\t" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
- elif ty_info.passed_as_ptr:
- opaque_arg_conv = opaque_arg_conv + "\n// Warning: we may need a move here but can't clone!"
- 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",
- arg_conv_cleanup = None,
- ret_conv = ("jclass " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_to_java(_env, ", ");"),
- ret_conv_name = ty_info.var_name + "_conv")
- if ty_info.rust_obj in opaque_structs:
- ret_conv_suf = ";\nCHECK((((long)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
- ret_conv_suf = ret_conv_suf + "CHECK((((long)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
- ret_conv_suf = ret_conv_suf + "long " + ty_info.var_name + "_ref;\n"
- ret_conv_suf = ret_conv_suf + "if (" + ty_info.var_name + "_var.is_owned) {\n"
- ret_conv_suf = ret_conv_suf + "\t" + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner | 1;\n"
- ret_conv_suf = ret_conv_suf + "} else {\n"
- ret_conv_suf = ret_conv_suf + "\t" + ty_info.var_name + "_ref = (long)&" + ty_info.var_name + "_var;\n"
- ret_conv_suf = ret_conv_suf + "}"
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
- ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", ret_conv_suf),
- ret_conv_name = ty_info.var_name + "_ref")
- 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", arg_conv_cleanup = None,
- 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 - Rust doesn't auto-free the
- # underlying unlike Vecs, and it gives Java more freedom.
- base_conv = base_conv + "\nFREE((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", arg_conv_cleanup = None,
- ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref")
- else:
- assert(not is_free)
- if ty_info.rust_obj in opaque_structs:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
- ret_conv = None, ret_conv_name = None) # its a pointer, no conv needed
- 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", arg_conv_cleanup = None,
- 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, arg_conv_cleanup = None, 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, arg_conv_cleanup = None,
- ret_conv = ("jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv")
- else:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = ty_info.var_name, arg_conv_cleanup = None, ret_conv = None, ret_conv_name = None)
- elif not print_void:
- # We don't have a parameter name, and want one, just call it arg
- if ty_info.rust_obj is not None:
- assert(not is_free or ty_info.rust_obj not in opaque_structs);
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = ty_info.rust_obj + " arg_conv = *(" + ty_info.rust_obj + "*)arg;\nFREE((void*)arg);",
- arg_conv_name = "arg_conv", arg_conv_cleanup = None,
- ret_conv = None, ret_conv_name = None)
- else:
- assert(not is_free)
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = "arg", arg_conv_cleanup = None, ret_conv = None, ret_conv_name = None)
- else:
- # We don't have a parameter name, and don't want one (cause we're returning)
- if ty_info.rust_obj is not None:
- 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 + " ret = " + ty_info.rust_obj + "_from_java(_env, " + ty_info.var_name + ");",
- arg_conv_name = "ret", arg_conv_cleanup = None,
- ret_conv = ("jclass ret = " + ty_info.rust_obj + "_to_java(_env, ", ");"), ret_conv_name = "ret")
- if ty_info.rust_obj in opaque_structs:
- # If we're returning a newly-allocated struct, we don't want Rust to ever
- # free, instead relying on the Java GC to lose the ref. We undo this in
- # any _free function.
- # To avoid any issues, we first assert that the incoming object is non-ref.
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- ret_conv = (ty_info.rust_obj + " ret = ", ";"),
- ret_conv_name = "((long)ret.inner) | (ret.is_owned ? 1 : 0)",
- arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None)
- else:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"),
- ret_conv_name = "(long)ret",
- arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None)
- else:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- ret_conv = ("long ret = (long)", ";"), ret_conv_name = "ret",
- arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None)
- else:
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None, ret_conv = None, ret_conv_name = None)