From: Matt Corallo Date: Thu, 22 Apr 2021 16:29:50 +0000 (+0000) Subject: Handle calls which take strings as an argument in Java X-Git-Tag: v0.0.98~1^2~26 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=753d902bc977acbf08b3166f054673236c39c01b;p=ldk-java Handle calls which take strings as an argument in Java --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 8863462f..3e0d9aee 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -171,18 +171,24 @@ class TypeMappingGenerator: arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = arg_conv_cleanup, ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = to_hu_conv, to_hu_conv_name = to_hu_conv_name, from_hu_conv = from_hu_conv) elif ty_info.java_ty == "String": + if not is_free: + arg_conv = "LDKStr " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_c_call(ty_info.var_name) + ";" + arg_conv_name = ty_info.var_name + "_conv" + else: + arg_conv = "LDKStr dummy = { .chars = NULL, .len = 0, .chars_is_owned = false };" + arg_conv_name = "dummy" if ty_info.arr_access is None: return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, - arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None, + arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None, ret_conv = ("const char* " + ty_info.var_name + "_str = ", - ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_c_call(ty_info.var_name + "_str", "strlen(" + ty_info.var_name + "_str)") + ";"), + ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str", "strlen(" + ty_info.var_name + "_str)") + ";"), ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = 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, + arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None, ret_conv = ("LDKStr " + ty_info.var_name + "_str = ", - ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_c_call(ty_info.var_name + "_str." + ty_info.arr_access, ty_info.var_name + "_str." + ty_info.arr_len) + ";"), + ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str." + ty_info.arr_access, ty_info.var_name + "_str." + ty_info.arr_len) + ";"), ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None) elif ty_info.var_name == "" and not print_void: # We don't have a parameter name, and want one, just call it arg diff --git a/java_strings.py b/java_strings.py index af7672b0..96d69096 100644 --- a/java_strings.py +++ b/java_strings.py @@ -315,6 +315,20 @@ static inline jstring str_ref_to_java(JNIEnv *env, const char* chars, size_t len FREE(err_buf); return err_conv; } +static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) { + uint64_t str_len = (*env)->GetStringUTFLength(env, str); + char* newchars = MALLOC(str_len + 1, "String chars"); + const char* jchars = (*env)->GetStringUTFChars(env, str, NULL); + memcpy(newchars, jchars, str_len); + newchars[str_len] = 0; + (*env)->ReleaseStringUTFChars(env, str, jchars); + LDKStr res = { + .chars = newchars, + .len = str_len, + .chars_is_owned = true + }; + return res; +} """ self.hu_struct_file_prefix = """package org.ldk.structs; @@ -381,8 +395,10 @@ import java.util.Arrays; else: return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)" - def str_ref_to_c_call(self, var_name, str_len): + def str_ref_to_native_call(self, var_name, str_len): return "str_ref_to_java(env, " + var_name + ", " + str_len + ")" + def str_ref_to_c_call(self, var_name): + return "java_to_owned_str(env, " + var_name + ")" def c_fn_name_define_pfx(self, fn_name, has_args): if has_args: diff --git a/typescript_strings.py b/typescript_strings.py index 20f639fb..4300f1f8 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -233,12 +233,24 @@ static inline uint32_t init_arr(size_t arr_len, size_t elem_size, const char *ty return (uint32_t)elems; } -jstring str_ref_to_ts(const char* chars, size_t len) { +static inline jstring str_ref_to_ts(const char* chars, size_t len) { char* err_buf = MALLOC(len + 4, "str conv buf"); *((uint32_t*)err_buf) = len; memcpy(err_buf + 4, chars, len); return (uint32_t) err_buf; } +static inline LDKStr str_ref_to_owned_c(jstring str) { + uint32_t *str_len = (uint32_t*)str; + char* newchars = MALLOC(*str_len + 1, "String chars"); + memcpy(newchars, (const char*)(str + 4), *str_len); + newchars[*str_len] = 0; + LDKStr res= { + .chars = newchars, + .len = *str_len, + .chars_is_owned = true + }; + return res; +} typedef bool jboolean; @@ -305,8 +317,10 @@ import * as bindings from '../bindings' // TODO: figure out location else: return None - def str_ref_to_c_call(self, var_name, str_len): + def str_ref_to_native_call(self, var_name, str_len): return "str_ref_to_ts(" + var_name + ", " + str_len + ")" + def str_ref_to_c_call(self, var_name): + return "str_ref_to_owned_c(" + var_name + ")" def c_fn_name_define_pfx(self, fn_name, have_args): return " __attribute__((visibility(\"default\"))) TS_" + fn_name + "("