From 9254f751c55dd40afd17bc129cb72f6af3d35ee8 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 25 Jan 2021 17:22:21 -0500 Subject: [PATCH] Move native string conversion to per-lang files --- gen_type_mapping.py | 4 ++-- java_strings.py | 1 + typescript_strings.py | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 07d892bf..5ad3d27f 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -174,7 +174,7 @@ class TypeMappingGenerator: 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, - ret_conv = ("jstring " + ty_info.var_name + "_conv = (*env)->NewStringUTF(env, ", ");"), ret_conv_name = ty_info.var_name + "_conv", + ret_conv = ("jstring " + ty_info.var_name + "_conv = " + self.consts.owned_str_to_c_call[0], self.consts.owned_str_to_c_call[1] + ";"), 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, @@ -183,7 +183,7 @@ class TypeMappingGenerator: ";\nchar* " + ty_info.var_name + "_buf = MALLOC(" + ty_info.var_name + "_str." + ty_info.arr_len + " + 1, \"str conv buf\");\n" + "memcpy(" + ty_info.var_name + "_buf, " + ty_info.var_name + "_str." + ty_info.arr_access + ", " + ty_info.var_name + "_str." + ty_info.arr_len + ");\n" + ty_info.var_name + "_buf[" + ty_info.var_name + "_str." + ty_info.arr_len + "] = 0;\n" + - "jstring " + ty_info.var_name + "_conv = (*env)->NewStringUTF(env, " + ty_info.var_name + "_str." + ty_info.arr_access + ");\n" + + "jstring " + ty_info.var_name + "_conv = " + self.consts.owned_str_to_c_call[0] + ty_info.var_name + "_str." + ty_info.arr_access + self.consts.owned_str_to_c_call[1] + ";\n" + "FREE(" + ty_info.var_name + "_buf);"), 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: diff --git a/java_strings.py b/java_strings.py index 516c15e2..04855ce9 100644 --- a/java_strings.py +++ b/java_strings.py @@ -297,6 +297,7 @@ import java.util.Arrays; self.ptr_c_ty = "int64_t" self.ptr_native_ty = "long" self.result_c_ty = "jclass" + self.owned_str_to_c_call = ("(*env)->NewStringUTF(env, ", ")") self.ptr_arr = "jobjectArray" self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")") self.get_native_arr_ptr_call = ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)") diff --git a/typescript_strings.py b/typescript_strings.py index e2201c77..e631237c 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -199,6 +199,9 @@ _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits"); typedef struct int64_tArray {uint32_t len;int64_t *ptr;} int64_tArray; typedef struct uint32_tArray {uint32_t len;int32_t *ptr;} uint32_tArray; typedef struct int8_tArray {uint32_t len;int8_t *ptr;} int8_tArray; +typedef struct jstring {} jstring; + +jstring conv_owned_string(const char* _src) { jstring a; return a; } typedef bool jboolean; @@ -216,6 +219,7 @@ import * as bindings from '../bindings' // TODO: figure out location self.ptr_c_ty = "uint32_t" self.ptr_native_ty = "number" # "uint32_t" self.result_c_ty = "uint32_t" + self.owned_str_to_c_call = ("conv_owned_string(", ")") self.ptr_arr = "uint32_tArray" self.get_native_arr_len_call = ("", ".len") self.get_native_arr_ptr_call = ("", ".ptr") -- 2.39.5