- out_java_enum.write(struct_line + "\n")
- out_c.write("\t\tcase %d: return %s;\n" % (ord_v, struct_line.strip().strip(",")))
- ord_v = ord_v + 1
- out_c.write("\t}\n")
- out_c.write("\tabort();\n")
- out_c.write("}\n")
-
- ord_v = 0
- out_c.write("static jclass " + struct_name + "_class = NULL;\n")
- for idx, struct_line in enumerate(field_lines):
- if idx > 0 and idx < len(field_lines) - 3:
- variant = struct_line.strip().strip(",")
- out_c.write("static jfieldID " + struct_name + "_" + variant + " = NULL;\n")
- out_c.write("JNIEXPORT void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass clz) {\n")
- out_c.write("\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n")
- out_c.write("\tCHECK(" + struct_name + "_class != NULL);\n")
- for idx, struct_line in enumerate(field_lines):
- if idx > 0 and idx < len(field_lines) - 3:
- variant = struct_line.strip().strip(",")
- out_c.write("\t" + struct_name + "_" + variant + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
- out_c.write("\tCHECK(" + struct_name + "_" + variant + " != NULL);\n")
- out_c.write("}\n")
- out_c.write("static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n")
- out_c.write("\tswitch (val) {\n")
- for idx, struct_line in enumerate(field_lines):
- if idx > 0 and idx < len(field_lines) - 3:
- variant = struct_line.strip().strip(",")
- out_c.write("\t\tcase " + variant + ":\n")
- out_c.write("\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + variant + ");\n")
- ord_v = ord_v + 1
- out_c.write("\t\tdefault: abort();\n")
- out_c.write("\t}\n")
- out_c.write("}\n\n")
-
- def map_complex_enum(struct_name, union_enum_items):
- java_hu_type = struct_name.replace("LDK", "")
- complex_enums.add(struct_name)
- with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
- out_java_enum.write(hu_struct_file_prefix)
- out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
- out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
- out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
- out_java_enum.write("\tprotected void finalize() throws Throwable {\n")
- out_java_enum.write("\t\tsuper.finalize();\n")
- out_java_enum.write("\t\tbindings." + java_hu_type + "_free(ptr);\n")
- out_java_enum.write("\t}\n")
- out_java_enum.write("\tlong conv_to_c() { assert false; return 0; /* Should only be called on subclasses */ }\n")
- out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
- out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
- java_hu_subclasses = ""
-
- tag_field_lines = union_enum_items["field_lines"]
- init_meth_jty_strs = {}
- for idx, struct_line in enumerate(tag_field_lines):
- if idx == 0:
- out_java.write("\tpublic static class " + struct_name + " {\n")
- out_java.write("\t\tprivate " + struct_name + "() {}\n")
- elif idx == len(tag_field_lines) - 3:
- assert(struct_line.endswith("_Sentinel,"))
- elif idx == len(tag_field_lines) - 2:
- out_java.write("\t\tstatic native void init();\n")
- out_java.write("\t}\n")
- elif idx == len(tag_field_lines) - 1:
- assert(struct_line == "")
- else:
- var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
- out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
- java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
- out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
- out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n")
- out_c.write("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
- out_c.write("static jmethodID " + struct_name + "_" + var_name + "_meth = NULL;\n")
- init_meth_jty_str = ""
- init_meth_params = ""
- init_meth_body = ""
- hu_conv_body = ""
- if "LDK" + var_name in union_enum_items:
- enum_var_lines = union_enum_items["LDK" + var_name]
- for idx, field in enumerate(enum_var_lines):
- if idx != 0 and idx < len(enum_var_lines) - 2:
- field_ty = map_type(field.strip(' ;'), False, None, False, True)
- out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
- java_hu_subclasses = java_hu_subclasses + "\t\tpublic " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
- if field_ty.to_hu_conv is not None:
- hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
- hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
- hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
- else:
- hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
- init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
- if idx > 1:
- init_meth_params = init_meth_params + ", "
- init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
- init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
- out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
- out_java.write(init_meth_body)
- out_java.write("}\n")
- out_java.write("\t\t}\n")
- out_java_enum.write("\t\t}\n")
- java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
- java_hu_subclasses = java_hu_subclasses + hu_conv_body
- java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t\t@Override long conv_to_c() { return 0; /*XXX*/ }\n"
- java_hu_subclasses = java_hu_subclasses + "\t}\n"
- init_meth_jty_strs[var_name] = init_meth_jty_str
- out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
- out_java_enum.write(java_hu_subclasses)
- out_java.write("\tstatic { " + struct_name + ".init(); }\n")
- out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
-
- out_c.write("JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass _a) {\n")
- for idx, struct_line in enumerate(tag_field_lines):
- if idx != 0 and idx < len(tag_field_lines) - 3:
- var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
- out_c.write("\t" + struct_name + "_" + var_name + "_class =\n")
- out_c.write("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
- out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
- out_c.write("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
- out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
- out_c.write("}\n")
- out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
- out_c.write("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
- out_c.write("\tswitch(obj->tag) {\n")
- for idx, struct_line in enumerate(tag_field_lines):
- if idx != 0 and idx < len(tag_field_lines) - 3:
- var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
- out_c.write("\t\tcase " + struct_name + "_" + var_name + ": {\n")
- c_params_text = ""
- if "LDK" + var_name in union_enum_items:
- enum_var_lines = union_enum_items["LDK" + var_name]
- for idx, field in enumerate(enum_var_lines):
- if idx != 0 and idx < len(enum_var_lines) - 2:
- field_map = map_type(field.strip(' ;'), False, None, False, True)
- if field_map.ret_conv is not None:
- out_c.write("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
- out_c.write("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
- out_c.write(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
- c_params_text = c_params_text + ", " + field_map.ret_conv_name
- else:
- c_params_text = c_params_text + ", obj->" + camel_to_snake(var_name) + "." + field_map.arg_name
- out_c.write("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
- out_c.write("\t\t}\n")
- out_c.write("\t\tdefault: abort();\n")
- out_c.write("\t}\n}\n")
- out_java_enum.write("}\n")
-
- def map_trait(struct_name, field_var_lines, trait_fn_lines):
- with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_trait:
- out_c.write("typedef struct " + struct_name + "_JCalls {\n")
- out_c.write("\tatomic_size_t refcnt;\n")
- out_c.write("\tJavaVM *vm;\n")
- out_c.write("\tjweak o;\n")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_c.write("\t" + var_line.group(1) + "_JCalls* " + var_line.group(2) + ";\n")
- for fn_line in trait_fn_lines:
- if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
- out_c.write("\tjmethodID " + fn_line.group(2) + "_meth;\n")
- out_c.write("} " + struct_name + "_JCalls;\n")
-
- out_java_trait.write(hu_struct_file_prefix)
- out_java_trait.write("public class " + struct_name.replace("LDK","") + " extends CommonBase {\n")
- out_java_trait.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); }\n")
- out_java_trait.write("\tpublic " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg") # XXX: Should be priv ( but is currently used in tests
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_java_trait.write(", bindings." + var_line.group(1) + " " + var_line.group(2))
- out_java_trait.write(") {\n")
- out_java_trait.write("\t\tsuper(bindings." + struct_name + "_new(arg")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_java_trait.write(", " + var_line.group(2))
- out_java_trait.write("));\n")
- out_java_trait.write("\t\tthis.ptrs_to.add(arg);\n")
- out_java_trait.write("\t}\n")
- out_java_trait.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
- out_java_trait.write("\tprotected void finalize() throws Throwable {\n")
- out_java_trait.write("\t\tbindings." + struct_name.replace("LDK","") + "_free(ptr); super.finalize();\n")
- out_java_trait.write("\t}\n\n")
-
- java_trait_constr = "\tpublic " + struct_name.replace("LDK", "") + "(" + struct_name.replace("LDK", "") + "Interface arg) {\n"
- java_trait_constr = java_trait_constr + "\t\tthis(new bindings." + struct_name + "() {\n"
- #out_java_trait.write("\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n")
- out_java.write("\tpublic interface " + struct_name + " {\n")
- java_meths = []
- for fn_line in trait_fn_lines:
- java_meth_descr = "("
- if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
- ret_ty_info = map_type(fn_line.group(1), True, None, False, False)
-
- out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(2) + "(")
- java_trait_constr = java_trait_constr + "\t\t\t@Override public " + ret_ty_info.java_ty + " " + fn_line.group(2) + "("
- #out_java_trait.write("\t\t" + ret_ty_info.java_hu_ty + " " + fn_line.group(2) + "(")
- is_const = fn_line.group(3) is not None
- out_c.write(fn_line.group(1) + fn_line.group(2) + "_jcall(")
- if is_const:
- out_c.write("const void* this_arg")
- else:
- out_c.write("void* this_arg")
-
- arg_names = []
- for idx, arg in enumerate(fn_line.group(4).split(',')):
- if arg == "":
- continue
- if idx >= 2:
- out_java.write(", ")
- java_trait_constr = java_trait_constr + ", "
- #out_java_trait.write(", ")
- out_c.write(", ")
- arg_conv_info = map_type(arg, True, None, False, False)
- out_c.write(arg.strip())
- out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
- #out_java_trait.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
- java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
- arg_names.append(arg_conv_info)
- java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
- java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
- java_meths.append(java_meth_descr)
-
- out_java.write(");\n")
- #out_java_trait.write(");\n")
- java_trait_constr = java_trait_constr + ") {\n"
- out_c.write(") {\n")
- out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
- out_c.write("\tJNIEnv *_env;\n")
- out_c.write("\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&_env, JNI_VERSION_1_8) == JNI_OK);\n")
-
- for arg_info in arg_names:
- if arg_info.ret_conv is not None:
- out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t'));
- out_c.write(arg_info.arg_name)
- out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t') + "\n")
-
- out_c.write("\tjobject obj = (*_env)->NewLocalRef(_env, j_calls->o);\n\tCHECK(obj != NULL);\n")
- if ret_ty_info.c_ty.endswith("Array"):
- out_c.write("\t" + ret_ty_info.c_ty + " ret = (*_env)->CallObjectMethod(_env, obj, j_calls->" + fn_line.group(2) + "_meth")
- elif not ret_ty_info.passed_as_ptr:
- out_c.write("\treturn (*_env)->Call" + ret_ty_info.java_ty.title() + "Method(_env, obj, j_calls->" + fn_line.group(2) + "_meth")
- else:
- out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*_env)->CallLongMethod(_env, obj, j_calls->" + fn_line.group(2) + "_meth");
- if ret_ty_info.java_ty != "void":
- java_trait_constr = java_trait_constr + "\t\t\t\treturn arg." + fn_line.group(2) + "("
- else:
- java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(2) + "("