import { CodeGenerator } from "./generation" import { RayLibEnum, RayLibFunction, RayLibStruct } from "./interfaces" import { QuickJsGenerator, QuickJsHeader } from "./quickjs" import { TypeScriptDeclaration } from "./typescript" export class RayLibHeader extends QuickJsHeader { typings = new TypeScriptDeclaration() constructor(name: string){ super(name) this.includes.include("raylib.h") //this.includes.line("#define RAYMATH_IMPLEMENTATION") } addApiFunction(api: RayLibFunction){ const options = api.binding || {} if(options.ignore) return const jName = options.jsName || api.name.charAt(0).toLowerCase() + api.name.slice(1) console.log("Binding function "+api.name) const fun = this.functions.jsBindingFunction(jName) if(options.body) { options.body(fun) } else { if(options.before) options.before(fun) // read parameters api.params = api.params || [] for (let i = 0; i < api.params.length; i++) { if(api.params[i]?.binding?.ignore) continue; const para = api.params[i] if(para.binding?.customConverter) para.binding.customConverter(fun) else fun.jsToC(para.type,para.name,"argv["+i+"]", this.structLookup, false, para.binding?.typeAlias) } // call c function if(options.customizeCall) fun.line(options.customizeCall) else fun.call(api.name, api.params.map(x => x.name), api.returnType === "void" ? null : {type: api.returnType, name: "returnVal"}) // clean up parameters for (const param of api.params) { if(param.binding?.customCleanup) param.binding.customCleanup(fun) fun.jsCleanUpParameter(param.type, param.name) } // return result if(api.returnType === "void"){ if(options.after) options.after(fun) fun.statement("return JS_UNDEFINED") } else { fun.jsToJs(api.returnType, "ret", "returnVal", this.structLookup) if(options.after) options.after(fun) fun.returnExp("ret") } } // add binding to function declaration this.moduleFunctionList.jsFuncDef(jName, (api.params || []).filter(x => !x.binding?.ignore).length, fun.getTag("_name")) this.typings.addFunction(jName,api) } addEnum(renum: RayLibEnum){ console.log("Binding enum "+ renum.name) renum.values.forEach(x => this.exportGlobalConstant(x.name, x.description)) } addApiStruct(struct: RayLibStruct){ const options = struct.binding || {} console.log("Binding struct "+ struct.name) const classId = this.definitions.jsClassId(`js_${struct.name}_class_id`) this.registerStruct(struct.name, classId) options.aliases?.forEach(x => this.registerStruct(x, classId)) const finalizer = this.structs.jsStructFinalizer(classId, struct.name, (gen,ptr) => options.destructor && gen.call(options.destructor.name, ["*"+ptr])) const propDeclarations = this.structs.createGenerator() if(options && options.properties){ for (const field of Object.keys(options.properties)) { const type = struct.fields.find(x => x.name === field)?.type if(!type) throw new Error(`Struct ${struct.name} does not contain field ${field}`) const el = options.properties[field] let _get: CodeGenerator | undefined = undefined let _set: CodeGenerator | undefined = undefined if(el.get) _get = this.structs.jsStructGetter(struct.name, classId, field, type, /*Be carefull when allocating memory in a getter*/this.structLookup) if(el.set) _set = this.structs.jsStructSetter(struct.name, classId, field, type, this.structLookup) propDeclarations.jsGetSetDef(field, _get?.getTag("_name"), _set?.getTag("_name")) } } const classFuncList = this.structs.jsFunctionList(`js_${struct.name}_proto_funcs`) classFuncList.child(propDeclarations) classFuncList.jsPropStringDef("[Symbol.toStringTag]", struct.name) const classDecl = this.structs.jsClassDeclaration(struct.name, classId, finalizer.getTag("_name"), classFuncList.getTag("_name")) this.moduleInit.call(classDecl.getTag("_name"), ["ctx", "m"]) if(options?.createConstructor || options?.createEmptyConstructor){ const body = this.functions.jsStructConstructor(struct.name, options?.createEmptyConstructor ? [] : struct.fields, classId, this.structLookup) this.moduleInit.statement(`JSValue ${struct.name}_constr = JS_NewCFunction2(ctx, ${body.getTag("_name")},"${struct.name})", ${struct.fields.length}, JS_CFUNC_constructor_or_func, 0)`) this.moduleInit.call("JS_SetModuleExport", ["ctx","m", `"${struct.name}"`, struct.name+"_constr"]) this.moduleEntry.call("JS_AddModuleExport", ["ctx","m",'"'+struct.name+'"']) } this.typings.addStruct(struct) } exportGlobalStruct(structName: string, exportName: string, values: string[], description: string){ this.moduleInit.declareStruct(structName,exportName+"_struct", values) const classId = this.structLookup[structName] if(!classId) throw new Error("Struct "+structName+" not found in register") this.moduleInit.jsStructToOpq(structName, exportName+"_js", exportName+"_struct", classId) this.moduleInit.call("JS_SetModuleExport", ["ctx","m",`"${exportName}"`, exportName+"_js"]) this.moduleEntry.call("JS_AddModuleExport", ["ctx","m",`"${exportName}"`]) this.typings.constants.tsDeclareConstant(exportName, structName, description) } exportGlobalConstant(name: string, description: string){ this.moduleInit.statement(`JS_SetModuleExport(ctx, m, "${name}", JS_NewInt32(ctx, ${name}))`) this.moduleEntry.statement(`JS_AddModuleExport(ctx, m, "${name}")`) this.typings.constants.tsDeclareConstant(name, "number", description) } }