- is_ptr = False
- take_by_ptr = False
- rust_obj = None
- arr_access = None
- if fn_arg.startswith("LDKThirtyTwoBytes"):
- fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]"
- assert var_is_arr_regex.match(fn_arg[8:])
- rust_obj = "LDKThirtyTwoBytes"
- arr_access = "data"
- if fn_arg.startswith("LDKPublicKey"):
- fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]"
- assert var_is_arr_regex.match(fn_arg[8:])
- rust_obj = "LDKPublicKey"
- arr_access = "compressed_form"
- if fn_arg.startswith("LDKSecretKey"):
- fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]"
- assert var_is_arr_regex.match(fn_arg[8:])
- rust_obj = "LDKSecretKey"
- arr_access = "bytes"
- if fn_arg.startswith("LDKSignature"):
- fn_arg = "uint8_t (*" + fn_arg[13:] + ")[64]"
- assert var_is_arr_regex.match(fn_arg[8:])
- rust_obj = "LDKSignature"
- arr_access = "compact_form"
- if fn_arg.startswith("LDKThreeBytes"):
- fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]"
- assert var_is_arr_regex.match(fn_arg[8:])
- rust_obj = "LDKThreeBytes"
- arr_access = "data"
-
- if fn_arg.startswith("void"):
- java_ty = "void"
- c_ty = "void"
- fn_ty_arg = "V"
- fn_arg = fn_arg[4:].strip()
- elif fn_arg.startswith("bool"):
- java_ty = "boolean"
- c_ty = "jboolean"
- fn_ty_arg = "Z"
- fn_arg = fn_arg[4:].strip()
- elif fn_arg.startswith("uint8_t"):
- java_ty = "byte"
- c_ty = "jbyte"
- fn_ty_arg = "B"
- fn_arg = fn_arg[7:].strip()
- elif fn_arg.startswith("uint16_t"):
- java_ty = "short"
- c_ty = "jshort"
- fn_ty_arg = "S"
- fn_arg = fn_arg[8:].strip()
- elif fn_arg.startswith("uint32_t"):
- java_ty = "int"
- c_ty = "jint"
- fn_ty_arg = "I"
- fn_arg = fn_arg[8:].strip()
- elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"):
- java_ty = "long"
- c_ty = "jlong"
- fn_ty_arg = "J"
- if fn_arg.startswith("uint64_t"):
- fn_arg = fn_arg[8:].strip()
- else:
- fn_arg = fn_arg[9:].strip()
- elif is_const and fn_arg.startswith("char *"):
- java_ty = "String"
- c_ty = "const char*"
- fn_ty_arg = "Ljava/lang/String;"
- fn_arg = fn_arg[6:].strip()
- else:
- ma = var_ty_regex.match(fn_arg)
- if ma.group(1).strip() in unitary_enums:
- java_ty = ma.group(1).strip()
- c_ty = "jclass"
- fn_ty_arg = "Lorg/ldk/enums/" + ma.group(1).strip() + ";"
- fn_arg = ma.group(2).strip()
- rust_obj = ma.group(1).strip()
- take_by_ptr = True
- else:
- java_ty = "long"
- c_ty = "jlong"
- fn_ty_arg = "J"
- fn_arg = ma.group(2).strip()
- rust_obj = ma.group(1).strip()
- take_by_ptr = True
-
- if fn_arg.startswith(" *") or fn_arg.startswith("*"):
- fn_arg = fn_arg.replace("*", "").strip()
- is_ptr = True
- c_ty = "jlong"
- java_ty = "long"
- fn_ty_arg = "J"
-
- 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, 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")
- if 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 + ");"
- arr_access = ("", "." + 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;"
- arr_access = ("*", "")
- return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
- arg_conv = arg_conv,
- 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 + ", " + arr_access[0],
- arr_access[1] + ");"),
- 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",
- 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",
- 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",
- 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",
- 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",
- 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",
- 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")
- else:
- 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 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",
- 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", 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",
- 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)
- 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)
- 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)
- else:
- 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)