+
+ with open(self.outdir + "/bindings.mts", "a") as bindings:
+ bindings.write("""
+
+js_invoke = function(obj_ptr: number, fn_id: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
+ const weak: WeakRef<object> = js_objs[obj_ptr];
+ if (weak == null || weak == undefined) {
+ console.error("Got function call on unknown/free'd JS object!");
+ throw new Error("Got function call on unknown/free'd JS object!");
+ }
+ const obj: object = weak.deref();
+ if (obj == null || obj == undefined) {
+ console.error("Got function call on GC'd JS object!");
+ throw new Error("Got function call on GC'd JS object!");
+ }
+ var fn;
+""")
+ bindings.write("\tswitch (fn_id) {\n")
+ for f in self.function_ptrs:
+ bindings.write(f"\t\tcase {str(f)}: fn = Object.getOwnPropertyDescriptor(obj, \"{self.function_ptrs[f][1]}\"); break;\n")
+
+ bindings.write("""\t\tdefault:
+ console.error("Got unknown function call from C!");
+ throw new Error("Got unknown function call from C!");
+ }
+ if (fn == null || fn == undefined) {
+ console.error("Got function call on incorrect JS object!");
+ throw new Error("Got function call on incorrect JS object!");
+ }
+ return fn.value.bind(obj)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
+}""")