_Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
+// Int types across Windows/Linux are different, so make sure we're using the right headers.
+_Static_assert(sizeof(void*) == sizeof(uintptr_t), "stdints must be correct");
+_Static_assert(sizeof(void*) == sizeof(intptr_t), "stdints must be correct");
+_Static_assert(sizeof(uint64_t) == 8, "stdints must be correct");
+_Static_assert(sizeof(int64_t) == 8, "stdints must be correct");
+_Static_assert(sizeof(uint32_t) == 4, "stdints must be correct");
+_Static_assert(sizeof(int32_t) == 4, "stdints must be correct");
+_Static_assert(sizeof(uint16_t) == 2, "stdints must be correct");
+_Static_assert(sizeof(int16_t) == 2, "stdints must be correct");
+_Static_assert(sizeof(uint8_t) == 1, "stdints must be correct");
+_Static_assert(sizeof(int8_t) == 1, "stdints must be correct");
+
#define DECL_ARR_TYPE(ty, name) \\
struct name##array { \\
uint64_t arr_len; /* uint32_t would suffice but we want to align uint64_ts as well */ \\
typedef bool jboolean;
int64_t CS_LDK_allocate_buffer(int64_t len) {
- return MALLOC(len, "C#-requested buffer");
+ return (int64_t)MALLOC(len, "C#-requested buffer");
}
void CS_LDK_free_buffer(int64_t buf) {
- FREE(buf);
+ FREE((void*)buf);
}
jstring CS_LDK_get_ldk_c_bindings_version() {
for arg_info in fn_line.args_ty:
fn_suffix += ty_to_c(arg_info.java_ty, arg_info)
fn_java_callback_args += ", " + arg_info.java_ty + " " + chr(ord("a") + idx)
- fn_c_callback_args += ", " + arg_info.c_ty + " " + chr(ord("a") + idx)
+ if arg_info.c_ty.endswith("Array") or arg_info.c_ty == "jstring":
+ fn_c_callback_args += ", int64_t " + chr(ord("a") + idx)
+ else:
+ fn_c_callback_args += ", " + arg_info.c_ty + " " + chr(ord("a") + idx)
if idx != 0:
fn_callback_call_args += ", "
fn_callback_call_args += chr(ord("a") + idx)
out_c += "\tuint64_t ret = js_invoke_function_" + fn_suffix + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
if fn_suffix not in self.function_ptrs:
- self.function_ptrs[fn_suffix] = {"args": [fn_java_callback_args, fn_c_callback_args], "ret": [fn_line.ret_ty_info.java_ty, fn_line.ret_ty_info.c_ty]}
+ caller_ret_c_ty = fn_line.ret_ty_info.c_ty
+ if fn_line.ret_ty_info.c_ty.endswith("Array") or fn_line.ret_ty_info.c_ty == "jstring":
+ caller_ret_c_ty = "int64_t"
+ self.function_ptrs[fn_suffix] = {"args": [fn_java_callback_args, fn_c_callback_args], "ret": [fn_line.ret_ty_info.java_ty, caller_ret_c_ty]}
self.function_ptrs[fn_suffix][self.function_ptr_counter] = (struct_name, fn_line.fn_name, fn_callback_call_args)
self.function_ptr_counter += 1
for idx, arg_info in enumerate(fn_line.args_ty):
if arg_info.ret_conv is not None:
- if arg_info.c_ty.endswith("Array"):
+ if arg_info.c_ty.endswith("Array") or arg_info.c_ty == "jstring":
out_c += ", (int64_t)" + arg_info.ret_conv_name
else:
out_c += ", " + arg_info.ret_conv_name
fn_name = f"{struct_name}_{var.var_name}_get_{field_map.arg_name}"
out_c += self.c_fn_ty_pfx + field_map.c_ty + self.c_fn_name_define_pfx(fn_name, True) + self.ptr_c_ty + " ptr) {\n"
out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
- out_c += f"\tassert(obj->tag == {struct_name}_{var.var_name});\n"
+ out_c += f"\tCHECK(obj->tag == {struct_name}_{var.var_name});\n"
if field_map.ret_conv is not None:
out_c += ("\t" + field_map.ret_conv[0].replace("\n", "\n\t"))
if var.tuple_variant:
}}
}}
public delegate {jret} {fn_suffix}_callback(int obj_ptr, int fn_id{jargs});
+ static {fn_suffix}_callback {fn_suffix}_callback_inst = c_callback_{fn_suffix};
""")
bindings.write(self.native_meth_decl(f"register_{fn_suffix}_invoker", "int") + f"({fn_suffix}_callback callee);\n")
# Easiest way to get a static run is just define a variable, even if we dont care
- bindings.write(f"\tstatic int _run_{fn_suffix}_registration = register_{fn_suffix}_invoker(c_callback_{fn_suffix});")
+ bindings.write(f"\tstatic int _run_{fn_suffix}_registration = register_{fn_suffix}_invoker({fn_suffix}_callback_inst);")
bindings.write("""
}