public EventProxyCodeGen(CodeGenerator cg, EventBindingInfo eventBindingInfo) { this.cg = cg; this.eventBindingInfo = eventBindingInfo; var eventInfo = this.eventBindingInfo.eventInfo; var declaringType = eventInfo.DeclaringType; var tsFieldVar = BindingManager.GetTSVariable(eventBindingInfo.regName); this.cg.cs.AppendLine("DuktapeDLL.duk_push_this(ctx);"); this.cg.cs.AppendLine($"duk_add_event_instanced(ctx, \"{tsFieldVar}\", {this.eventBindingInfo.adderName}, {this.eventBindingInfo.removerName}, -1);"); this.cg.cs.AppendLine("DuktapeDLL.duk_remove(ctx, -2);"); this.cg.cs.AppendLine("return 1;"); }
public EventRemoverCodeGen(CodeGenerator cg, EventBindingInfo bindingInfo) { this.cg = cg; this.bindingInfo = bindingInfo; var eventInfo = this.bindingInfo.eventInfo; var declaringType = eventInfo.DeclaringType; var caller = this.cg.AppendGetThisCS(bindingInfo); this.cg.cs.AppendLine("{0} value;", this.cg.bindingManager.GetCSTypeFullName(eventInfo.EventHandlerType)); this.cg.cs.AppendLine("{0}(ctx, 0, out value);", this.cg.bindingManager.GetDuktapeGetter(eventInfo.EventHandlerType)); this.cg.cs.AppendLine("{0}.{1} -= value;", caller, eventInfo.Name); if (declaringType.IsValueType && !eventInfo.GetAddMethod().IsStatic) { // 非静态结构体属性修改, 尝试替换实例 this.cg.cs.AppendLine($"duk_rebind_this(ctx, {caller});"); } this.cg.cs.AppendLine("return 0;"); }
public string AppendGetThisCS(EventBindingInfo bindingInfo) { var isStatic = bindingInfo.isStatic; var declaringType = bindingInfo.declaringType; var caller = ""; if (isStatic) { caller = this.bindingManager.GetCSTypeFullName(declaringType, false); } else { caller = "self"; this.cs.AppendLine($"{this.bindingManager.GetCSTypeFullName(declaringType)} {caller};"); this.cs.AppendLine($"DuktapeDLL.duk_push_this(ctx);"); this.cs.AppendLine($"{this.bindingManager.GetDuktapeGetter(declaringType)}(ctx, -1, out {caller});"); this.cs.AppendLine($"DuktapeDLL.duk_pop(ctx);"); } return(caller); }