public override void EmitPublicPostCallStatements(CodeBuilder b, string var) { b.BeginIf("{0}_str != {0}_pinned.Obj.PinnedPtr", var); b.BeginIf("{0}_length > 0", var); b.AppendLine("{0} = System.Runtime.InteropServices.Marshal.PtrToStringUni({0}_str, {0}_length);", var); b.AppendLine("// free the native string?", var); b.BeginElse(); b.AppendLine("{0} = null;", var); b.EndBlock(); b.EndBlock(); b.AppendLine("{0}_pinned.Obj.Free();", var); }
public override void EmitRemoteEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("m_{0}_managed == null", var); b.AppendLine("var {0} = new RemotePtr[(ulong)call.{1}];", var, CountArg.VarName); b.AppendLine("m_{0}_managed = new {1}[{0}.Length];", var, Struct.RemoteClassName); b.BeginIf("{0}.Length > 0", var); b.AppendLine("CfrRuntime.Marshal.Copy(new RemotePtr(connection, call.{0}), {0}, 0, {0}.Length);", var); b.BeginBlock("for(int i = 0; i < {0}.Length; ++i)", var); b.AppendLine("m_{0}_managed[i] = {1}.Wrap({0}[i]);", var, Struct.RemoteClassName); b.EndBlock(); b.EndBlock(); b.EndBlock(); b.AppendLine("return m_{0}_managed;", var); }
public override void EmitPostNativeCallbackStatements(CodeBuilder b, string var) { b.BeginIf("{0}_tmp_length > 0", var); b.AppendLine("cef_string_set({0}_tmp_str, {0}_tmp_length, {0}, 1);", var); b.AppendLine("cfx_gc_handle_switch(&{0}_gc_handle, GC_HANDLE_FREE);", var); b.EndBlock(); }
public override void EmitPublicEventCtorStatements(CodeBuilder b, string var) { b.AppendLine("m_{0} = new IntPtr[{1}];", var, CountArg.VarName); b.BeginIf("{0} > 0", CountArg.VarName); b.AppendLine("System.Runtime.InteropServices.Marshal.Copy({0}, m_{0}, 0, {1});", var, CountArg.VarName); b.EndBlock(); }
public override void EmitNativeCallbackReturnStatements(CodeBuilder b, string var) { b.BeginIf("__retval"); b.AppendLine("((cef_base_t*)__retval)->add_ref((cef_base_t*)__retval);"); b.EndBlock(); base.EmitNativeCallbackReturnStatements(b, var); }
public override void EmitPublicEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("!m_{0}_changed && m_{0}_wrapped == null", var); b.AppendLine("m_{0}_wrapped = StringFunctions.PtrToStringUni(m_{0}_str, m_{0}_length);", var); b.EndBlock(); b.AppendLine("return m_{0}_wrapped;", var); }
public override void EmitPublicEventFieldInitializers(CodeBuilder b, string var) { b.AppendLine("e.m_{0} = new IntPtr[(ulong){1}];", var, CountArg.VarName); b.BeginIf("e.m_{0}.Length > 0", var); b.AppendLine("System.Runtime.InteropServices.Marshal.Copy({0}, e.m_{0}, 0, (int){1});", var, CountArg.VarName); b.EndBlock(); }
private void EmitPublicEvent(CodeBuilder b, int cbIndex, CefCallbackFunction cb) { var isSimpleGetterEvent = cb.Signature.ManagedParameters.Length == 1 && cb.Signature.ReturnType.IsCefStructPtrType; b.AppendSummaryAndRemarks(cb.Comments, false, true); b.BeginBlock("public event {0} {1}", cb.EventHandlerName, CSharp.Escape(cb.PublicName)); b.BeginBlock("add"); b.BeginBlock("lock(eventLock)"); if (isSimpleGetterEvent) { b.BeginBlock("if(m_{0} != null)", cb.PublicName); b.AppendLine("throw new CfxException(\"Can't add more than one event handler to this type of event.\");"); b.EndBlock(); } else { b.BeginBlock("if(m_{0} == null)", cb.PublicName); } b.AppendLine("CfxApi.{3}.{0}_set_callback(NativePtr, {1}, {2}_native_ptr);", CefStruct.CfxName, cbIndex, cb.Name, CefStruct.ClassName.Substring(3)); if (!isSimpleGetterEvent) { b.EndBlock(); } b.AppendLine("m_{0} += value;", cb.PublicName); b.EndBlock(); b.EndBlock(); b.BeginBlock("remove"); b.BeginBlock("lock(eventLock)"); b.AppendLine("m_{0} -= value;", cb.PublicName); b.BeginBlock("if(m_{0} == null)", cb.PublicName); b.AppendLine("CfxApi.{2}.{0}_set_callback(NativePtr, {1}, IntPtr.Zero);", CefStruct.CfxName, cbIndex, CefStruct.ClassName.Substring(3)); b.EndBlock(); b.EndBlock(); b.EndBlock(); b.EndBlock(); b.AppendLine(); if (isSimpleGetterEvent) { b.AppendLine("/// <summary>"); b.AppendLine("/// Retrieves the {0} provided by the event handler attached to the {1} event, if any.", cb.Signature.ReturnType.PublicSymbol, CSharp.Escape(cb.PublicName)); b.AppendLine("/// Returns null if no event handler is attached."); b.AppendLine("/// </summary>"); b.BeginBlock("public {0} Retrieve{1}()", cb.Signature.ReturnType.PublicSymbol, cb.Signature.ReturnType.PublicSymbol.Substring(3)); b.AppendLine("var h = m_{0};", cb.PublicName); b.BeginIf("h != null"); b.AppendLine("var e = new {0}();", cb.PublicEventArgsClassName); b.AppendLine("h(this, e);"); b.AppendLine("return e.m_returnValue;"); b.BeginElse(); b.AppendLine("return null;"); b.EndBlock(); b.EndBlock(); b.AppendLine(); } b.AppendLine("private {0} m_{1};", cb.EventHandlerName, cb.PublicName); }
public override void EmitPostPublicRaiseEventStatements(CodeBuilder b, string var) { b.BeginIf("e.m_{0}_changed", var); b.AppendLine("var {0}_pinned = new PinnedString(e.m_{0}_wrapped);", var); b.AppendLine("{0}_str = {0}_pinned.Obj.PinnedPtr;", var); b.AppendLine("{0}_length = {0}_pinned.Length;", var); b.EndBlock(); }
public override void EmitPreNativeCallStatements(CodeBuilder b, string var) { b.BeginIf("{0}", CountArg.VarName); b.BeginBlock("for(size_t i = 0; i < {0}; ++i)", CountArg.VarName); b.AppendLine("if({0}[i]) ((cef_base_t*){0}[i])->add_ref((cef_base_t*){0}[i]);", StructArg.VarName); b.EndBlock(); b.EndBlock(); }
public override void EmitNativeCallbackReturnStatements(CodeBuilder b) { Debug.Assert(Struct.IsRefCounted); b.BeginIf("__retval"); b.AppendLine("((cef_base_ref_counted_t*)__retval)->add_ref((cef_base_ref_counted_t*)__retval);"); b.EndBlock(); base.EmitNativeCallbackReturnStatements(b); }
public override void EmitRemoteEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("!m_{0}_changed && m_{0}_wrapped == null", var); b.AppendLine("m_{0} = call.{0}_str == IntPtr.Zero ? null : (call.{0}_length == 0 ? String.Empty : CfrRuntime.Marshal.PtrToStringUni(new RemotePtr(connection, call.{0}_str), call.{0}_length));", var); b.AppendLine("m_{0}_changed = true;", var); b.EndBlock(); b.AppendLine("return m_{0}_wrapped;", var); }
public override void EmitPostRemoteRaiseEventStatements(CodeBuilder b, string var) { b.BeginIf("e.m_{0}_managed != null", var); b.BeginFor("e.m_{0}_managed.Length", var); b.AppendLine("e.m_{0}_managed[i].Dispose();", var); b.EndBlock(); b.EndBlock(); }
public override void EmitPostPublicRaiseEventStatements(CodeBuilder b, string var) { b.BeginIf("e.m_{0}_managed == null", var); b.BeginBlock("for(int i = 0; i < {1}; ++i)", var, CountArg.VarName); b.AppendLine("CfxApi.cfx_release(e.m_{0}[i]);", var); b.EndBlock(); b.EndBlock(); }
public override void EmitPreRemoteCallStatements(CodeBuilder b, string var) { b.BeginIf("{0} != null", var); b.AppendLine("call.{0} = new IntPtr[{0}.Length];", var); b.BeginBlock("for(int i = 0; i < {0}.Length; ++i)", var); b.AppendLine("call.{0}[i] = {1};", var, StructPtr.RemoteUnwrapExpression(var + "[i]")); b.EndBlock(); b.EndBlock(); }
public override void EmitPublicEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("m_{0}_managed == null", var); b.AppendLine("m_{0}_managed = new {1}[m_{2}];", var, Struct.ClassName, CountArg.VarName); b.BeginFor("m_" + CountArg.VarName); b.AppendLine("m_{0}_managed[i] = {1}.Wrap(m_{0} + (i * m_{0}_structsize));", var, Struct.ClassName); b.EndBlock(); b.EndBlock(); b.AppendLine("return m_{0}_managed;", var); }
public override void EmitPublicEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("m_{0}_managed == null", var); b.AppendLine("m_{0}_managed = new {1}[m_{0}.Length];", var, Struct.ClassName); b.BeginBlock("for(int i = 0; i < m_{0}.Length; ++i)", var); b.AppendLine("m_{0}_managed[i] = {1}.Wrap(m_{0}[i]);", var, Struct.ClassName); b.EndBlock(); b.EndBlock(); b.AppendLine("return m_{0}_managed;", var); }
public override void EmitRemotePreCallStatements(CodeBuilder b, string var) { b.BeginIf("{0} != null", var); b.AppendLine("call.{0} = new IntPtr[{0}.Length];", var); b.BeginBlock("for(int i = 0; i < {0}.Length; ++i)", var); b.AppendLine("if(!CheckConnection({0}[i], connection)) throw new ArgumentException(\"Render process connection mismatch.\", \"{1}[\" + i + \"]\");", CSharp.Escape(var), var); b.AppendLine("call.{0}[i] = {1};", var, StructPtr.RemoteUnwrapExpression(var + "[i]")); b.EndBlock(); b.EndBlock(); }
public override void EmitRemoteClass(CodeBuilder b) { b.AppendLine(); b.AppendSummaryAndRemarks(Comments, true, Category == StructCategory.Client); b.BeginClass(RemoteClassName + " : CfrStructure", GeneratorConfig.ClassModifiers(RemoteClassName, "public sealed")); b.AppendLine(); if (NeedsWrapFunction) { b.BeginFunction("Wrap", RemoteClassName, "RemotePtr remotePtr", "internal static"); b.AppendLine("if(remotePtr == RemotePtr.Zero) return null;"); b.AppendLine("var weakCache = CfxRemoteCallContext.CurrentContext.connection.weakCache;"); b.AppendLine("return ({0})weakCache.GetOrAdd(remotePtr.ptr, () => new {0}(remotePtr));", RemoteClassName); b.EndBlock(); b.AppendLine(); b.AppendLine(); } b.AppendLine("private {0}(RemotePtr remotePtr) : base(remotePtr) {{}}", RemoteClassName); b.AppendLine("public {0}() : base(new {1}CtorRemoteCall(), new {1}DtorRemoteCall()) {{}}", RemoteClassName, ClassName); foreach (var sm in StructMembers) { b.AppendLine(); b.AppendLine("{0} m_{1};", sm.MemberType.RemoteSymbol, sm.PublicName); b.AppendLine("bool m_{0}_fetched;", sm.PublicName); b.AppendLine(); b.AppendSummaryAndRemarks(sm.Comments, true); b.BeginBlock("public {1} {0}", CSharp.Escape(sm.PublicName), sm.MemberType.RemoteSymbol); b.BeginBlock("get"); b.BeginIf("!m_{0}_fetched", sm.PublicName); b.AppendLine("var call = new {0}Get{1}RemoteCall();", ClassName, sm.PublicName); b.AppendLine("call.sender = RemotePtr.ptr;"); b.AppendLine("call.RequestExecution(RemotePtr.connection);"); b.AppendLine("m_{0} = {1};", sm.PublicName, sm.MemberType.RemoteWrapExpression("call.value")); b.AppendLine("m_{0}_fetched = true;", sm.PublicName); b.EndBlock(); b.AppendLine("return m_{0};", sm.PublicName); b.EndBlock(); b.BeginBlock("set"); b.AppendLine("var call = new {0}Set{1}RemoteCall();", ClassName, sm.PublicName); b.AppendLine("call.sender = RemotePtr.ptr;"); b.AppendLine("call.value = {0};", sm.MemberType.RemoteUnwrapExpression("value")); b.AppendLine("call.RequestExecution(RemotePtr.connection);"); b.AppendLine("m_{0} = value;", sm.PublicName); b.AppendLine("m_{0}_fetched = true;", sm.PublicName); b.EndBlock(); b.EndBlock(); } b.EndBlock(); }
protected override void EmitExecuteInTargetProcess(CodeBuilder b) { Debug.Assert(Arguments[2].ArgumentType.PublicSymbol == "CfxPostDataElement"); b.AppendLine("var elements = ((CfxPostData)RemoteProxy.Unwrap(self)).Elements;"); b.BeginIf("elements != null"); b.AppendLine("__retval = new IntPtr[elements.Length];"); b.BeginFor("elements.Length"); b.AppendLine("__retval[i] = RemoteProxy.Wrap(elements[i]);"); b.EndBlock(); b.EndBlock(); }
public override void EmitPreProxyCallStatements(CodeBuilder b, string var) { b.AppendLine("{0}[] {1}_unwrapped;", Struct.ClassName, var); b.BeginIf("{0} != null", var); b.AppendLine("{0}_unwrapped = new {1}[{0}.Length];", var, Struct.ClassName); b.BeginBlock("for(int i = 0; i < {0}.Length; ++i)", var); b.AppendLine("{0}_unwrapped[i] = {1};", var, StructPtr.ProxyUnwrapExpression(var + "[i]")); b.EndBlock(); b.BeginElse(); b.AppendLine("{0}_unwrapped = null;", var); b.EndBlock(); }
public override void EmitRemoteEventArgGetterStatements(CodeBuilder b, string var) { b.BeginIf("m_{0}_managed == null", var); b.AppendLine("m_{0}_managed = new {1}[({2})m_{3}];", var, Struct.RemoteClassName, CountArg.ArgumentType.PublicSymbol, CountArg.VarName); b.AppendLine("var currentPtr = m_{0};", var); b.BeginBlock("for({0} i = 0; i < ({0})m_{1}; ++i)", CountArg.ArgumentType.PublicSymbol, CountArg.VarName); b.AppendLine("m_{0}_managed[i] = {1}.Wrap(currentPtr);", var, Struct.ClassName); b.AppendLine("currentPtr += m_{0}_structsize;", var); b.EndBlock(); b.EndBlock(); b.AppendLine("return m_{0}_managed;", var); }
public override void EmitPostPublicRaiseEventStatements(CodeBuilder b, string var) { b.BeginIf("e.m_{0}_wrapped != null && e.m_{0}_wrapped.Length > 0", var); b.AppendLine("var {0}_pinned = new PinnedString(e.m_{0}_wrapped);", var); b.AppendLine("{0}_str = {0}_pinned.Obj.PinnedPtr;", var); b.AppendLine("{0}_length = {0}_pinned.Length;", var); b.AppendLine("{0}_gc_handle = {0}_pinned.Obj.GCHandlePtr();", var); b.BeginElse(); b.AppendLine("{0}_str = IntPtr.Zero;", var); b.AppendLine("{0}_length = 0;", var); b.AppendLine("{0}_gc_handle = IntPtr.Zero;", var); b.EndBlock(); }
public override void EmitPostRemoteCallbackStatements(CodeBuilder b, string var) { b.BeginIf("call.{0} != null && call.{0}.Length > 0", CSharp.Escape(var)); b.AppendLine("var {0}_pinned = new PinnedString(call.{1});", var, CSharp.Escape(var)); b.AppendLine("{0}_str = {0}_pinned.Obj.PinnedPtr;", var); b.AppendLine("{0}_length = {0}_pinned.Length;", var); b.AppendLine("{0}_gc_handle = {0}_pinned.Obj.GCHandlePtr();", var); b.BeginElse(); b.AppendLine("{0}_str = IntPtr.Zero;", var); b.AppendLine("{0}_length = 0;", var); b.AppendLine("{0}_gc_handle = IntPtr.Zero;", var); b.EndBlock(); }
public override void EmitNativeCall(CodeBuilder b, string functionName) { b.AppendLine("cef_range_t *ranges_tmp = (cef_range_t*)malloc(*rangesCount * sizeof(cef_range_t));"); b.BeginIf("ranges_tmp"); b.AppendLine("*ranges_nomem = 0;"); b.AppendLine("self->get_page_ranges(self, rangesCount, ranges_tmp);"); b.BeginBlock("for(size_t i = 0; i < *rangesCount; ++i)"); b.AppendLine("ranges[i] = (cef_range_t*)malloc(sizeof(cef_range_t));"); b.AppendLine("*ranges[i] = ranges_tmp[i];"); b.EndBlock(); b.AppendLine("free(ranges_tmp);"); b.BeginElse(); b.AppendLine("*ranges_nomem = 1;"); b.EndBlock(); }
public override void EmitPublicPreCallStatements(CodeBuilder b, string var) { b.AppendLine("UIntPtr {0}_length;", var); b.AppendLine("IntPtr[] {0}_ptrs;", var); b.BeginIf("{0} != null", var); b.AppendLine("{0}_length = (UIntPtr){0}.Length;", var); b.AppendLine("{0}_ptrs = new IntPtr[{0}.Length];", var); b.BeginBlock("for(int i = 0; i < {0}.Length; ++i)", var); b.AppendLine("{0}_ptrs[i] = {1}.Unwrap({0}[i]);", var, Struct.ClassName); b.EndBlock(); b.BeginElse(); b.AppendLine("{0}_length = UIntPtr.Zero;", var); b.AppendLine("{0}_ptrs = null;", var); b.EndBlock(); b.AppendLine("PinnedObject {0}_pinned = new PinnedObject({0}_ptrs);", var); }
public override void EmitRemoteClass(CodeBuilder b) { b.AppendLine(); b.AppendSummaryAndRemarks(Comments, true, Category == StructCategory.ApiCallbacks); b.BeginClass(RemoteClassName + " : CfrStructure", GeneratorConfig.ClassModifiers(RemoteClassName, "public sealed")); b.AppendLine(); EmitRemoteClassWrapperFunction(b); b.AppendLine("private {0}(RemotePtr remotePtr) : base(remotePtr) {{}}", RemoteClassName); b.AppendLine("public {0}() : base(new {1}CtorRemoteCall(), new {1}DtorRemoteCall()) {{}}", RemoteClassName, ClassName); foreach (var sm in StructMembers) { b.AppendLine(); b.AppendLine("{0} m_{1};", sm.MemberType.RemoteSymbol, sm.PublicName); b.AppendLine("bool m_{0}_fetched;", sm.PublicName); b.AppendLine(); b.AppendSummaryAndRemarks(sm.Comments, true); b.BeginBlock("public {1} {0}", CSharp.Escape(sm.PublicName), sm.MemberType.RemoteSymbol); b.BeginBlock("get"); b.BeginIf("!m_{0}_fetched", sm.PublicName); b.AppendLine("var call = new {0}Get{1}RemoteCall();", ClassName, sm.PublicName); b.AppendLine("call.sender = RemotePtr.ptr;"); b.AppendLine("call.RequestExecution(RemotePtr.connection);"); b.AppendLine("m_{0} = {1};", sm.PublicName, sm.MemberType.RemoteWrapExpression("call.value")); b.AppendLine("m_{0}_fetched = true;", sm.PublicName); b.EndBlock(); b.AppendLine("return m_{0};", sm.PublicName); b.EndBlock(); b.BeginBlock("set"); b.AppendLine("var call = new {0}Set{1}RemoteCall();", ClassName, sm.PublicName); b.AppendLine("call.sender = RemotePtr.ptr;"); b.AppendLine("call.value = {0};", sm.MemberType.RemoteUnwrapExpression("value")); b.AppendLine("call.RequestExecution(RemotePtr.connection);"); b.AppendLine("m_{0} = value;", sm.PublicName); b.AppendLine("m_{0}_fetched = true;", sm.PublicName); b.EndBlock(); b.EndBlock(); } b.EndBlock(); }
public void EmitRemoteEventArgsAndHandler(CodeBuilder b, CefCallbackFunction cb) { if (cb.IsBasicEvent) { return; } if (!ShouldEmitEventHandler(emittedRemoteHandlers, cb)) { return; } b.AppendSummaryAndRemarks(cb.Comments, true, true); b.AppendLine("public delegate void {0}(object sender, {1} e);", cb.RemoteEventHandlerName, cb.RemoteEventArgsClassName); b.AppendLine(); b.AppendSummaryAndRemarks(cb.Comments, true, true); b.BeginClass(cb.RemoteEventArgsClassName + " : CfrEventArgs", GeneratorConfig.ClassModifiers(cb.RemoteEventArgsClassName)); b.AppendLine(); b.AppendLine("private {0}RemoteEventCall call;", cb.RemoteCallName); b.AppendLine(); for (var i = 1; i <= cb.Signature.ManagedParameters.Count() - 1; i++) { cb.Signature.ManagedParameters[i].EmitRemoteEventArgFields(b); } b.AppendLine(); if (!cb.Signature.PublicReturnType.IsVoid) { b.AppendLine("internal {0} m_returnValue;", cb.Signature.PublicReturnType.RemoteSymbol); b.AppendLine("private bool returnValueSet;"); b.AppendLine(); } b.AppendLine("internal {0}({1}RemoteEventCall call) {{ this.call = call; }}", cb.RemoteEventArgsClassName, cb.RemoteCallName); b.AppendLine(); for (var i = 1; i <= cb.Signature.ManagedParameters.Count() - 1; i++) { var arg = cb.Signature.ManagedParameters[i]; var cd = new CommentNode(); if (arg.ParameterType.IsIn && arg.ParameterType.IsOut) { cd.Lines = new string[] { string.Format("Get or set the {0} parameter for the <see cref=\"{1}.{2}\"/> render process callback.", arg.PublicPropertyName, CefStruct.RemoteSymbol, cb.PublicFunctionName) }; } else if (arg.ParameterType.IsIn) { cd.Lines = new string[] { string.Format("Get the {0} parameter for the <see cref=\"{1}.{2}\"/> render process callback.", arg.PublicPropertyName, CefStruct.RemoteSymbol, cb.PublicFunctionName) }; } else { cd.Lines = new string[] { string.Format("Set the {0} out parameter for the <see cref=\"{1}.{2}\"/> render process callback.", arg.PublicPropertyName, CefStruct.RemoteSymbol, cb.PublicFunctionName) }; } if (arg.ParameterType is CefStructArrayType && arg.ParameterType.IsIn) { cd.Lines = cd.Lines.Concat(new string[] { "Do not keep a reference to the elements of this array outside of this function." }).ToArray(); } b.AppendSummary(cd); b.BeginBlock("public {0} {1}", arg.ParameterType.RemoteSymbol, arg.PublicPropertyName); if (arg.ParameterType.IsIn) { b.BeginBlock("get"); b.AppendLine("CheckAccess();"); arg.EmitRemoteEventArgGetterStatements(b); b.EndBlock(); } if (arg.ParameterType.IsOut) { b.BeginBlock("set"); b.AppendLine("CheckAccess();"); arg.EmitRemoteEventArgSetterStatements(b); b.EndBlock(); } b.EndBlock(); } if (!cb.Signature.PublicReturnType.IsVoid) { var cd = new CommentNode(); cd.Lines = new string[] { string.Format("Set the return value for the <see cref=\"{0}.{1}\"/> render process callback.", CefStruct.RemoteClassName, cb.PublicFunctionName), "Calling SetReturnValue() more then once per callback or from different event handlers will cause an exception to be thrown." }; b.AppendSummary(cd); b.BeginBlock("public void SetReturnValue({0} returnValue)", cb.Signature.PublicReturnType.RemoteSymbol); b.BeginIf("returnValueSet"); b.AppendLine("throw new CfxException(\"The return value has already been set\");"); b.EndBlock(); b.AppendLine("m_returnValue = returnValue;"); b.AppendLine("returnValueSet = true;"); b.EndBlock(); } if (cb.Signature.ManagedParameters.Count() > 1) { b.AppendLine(); EmitEventToString(b, cb); } b.EndBlock(); }
public override void EmitRemoteCalls(CodeBuilder b, List <string> callIds) { b.AppendLine("using Event;"); b.AppendLine(); b.BeginRemoteCallClass(ClassName, callIds, "CtorWithGCHandleRemoteCall"); b.AppendLine(); b.BeginBlock("protected override void RemoteProcedure()"); b.AppendLine("__retval = CfxApi.{0}.{1}_ctor(gcHandlePtr, 1);", ApiClassName, CfxName); b.EndBlock(); b.EndBlock(); b.AppendLine(); if (NeedsWrapFunction) { b.BeginRemoteCallClass(ClassName, callIds, "GetGcHandleRemoteCall"); b.AppendLine(); b.BeginBlock("protected override void RemoteProcedure()"); b.AppendLine("gc_handle = CfxApi.{0}.{1}_get_gc_handle(self);", ApiClassName, CfxName); b.EndBlock(); b.EndBlock(); b.AppendLine(); } b.BeginRemoteCallClass(ClassName, callIds, "SetCallbackRemoteCall"); b.AppendLine(); b.BeginBlock("protected override void RemoteProcedure()"); b.AppendLine("{0}RemoteClient.SetCallback(self, index, active);", ClassName); b.EndBlock(); b.EndBlock(); b.AppendLine(); foreach (var cb in RemoteCallbackFunctions) { var sig = cb.Signature; b.BeginRemoteCallClass(cb.RemoteCallName, callIds, "RemoteEventCall"); b.AppendLine(); var inArgumentList = new List <string>(); var outArgumentList = new List <string>(); foreach (var arg in sig.Parameters) { if (!arg.IsThisArgument) { foreach (var pm in arg.ParameterType.RemoteCallbackParameterList(arg.VarName)) { if (pm.StartsWith("out ")) { b.AppendLine("internal {0};", pm.Substring(4)); outArgumentList.Add(pm.Substring(pm.LastIndexOf(' ') + 1)); } else { b.AppendLine("internal {0};", pm); inArgumentList.Add(pm.Substring(pm.LastIndexOf(' ') + 1)); } } } } b.AppendLine(); if (!sig.ReturnType.IsVoid) { b.AppendLine("internal {0} __retval;", sig.ReturnType.PInvokeSymbol); b.AppendLine(); } b.BeginBlock("protected override void WriteArgs(StreamHandler h)"); b.AppendLine("h.Write(gcHandlePtr);"); foreach (var pm in inArgumentList) { b.AppendLine("h.Write({0});", pm); } b.EndBlock(); b.AppendLine(); b.BeginBlock("protected override void ReadArgs(StreamHandler h)"); b.AppendLine("h.Read(out gcHandlePtr);"); foreach (var pm in inArgumentList) { b.AppendLine("h.Read(out {0});", pm); } b.EndBlock(); b.AppendLine(); b.BeginBlock("protected override void WriteReturn(StreamHandler h)"); foreach (var pm in outArgumentList) { b.AppendLine("h.Write({0});", pm); } if (!sig.ReturnType.IsVoid) { b.AppendLine("h.Write(__retval);"); } b.EndBlock(); b.AppendLine(); b.BeginBlock("protected override void ReadReturn(StreamHandler h)"); foreach (var pm in outArgumentList) { b.AppendLine("h.Read(out {0});", pm); } if (!sig.ReturnType.IsVoid) { b.AppendLine("h.Read(out __retval);"); } b.EndBlock(); b.AppendLine(); b.BeginBlock("protected override void RemoteProcedure()"); b.AppendLine("var self = ({0})System.Runtime.InteropServices.GCHandle.FromIntPtr(gcHandlePtr).Target;", RemoteClassName); b.BeginIf("self == null || self.CallbacksDisabled"); b.AppendLine("return;"); b.EndBlock(); if (cb.IsBasicEvent) { b.AppendLine("var e = new CfrEventArgs();"); } else { b.AppendLine("var e = new {0}(this);", cb.RemoteEventArgsClassName); } b.AppendLine("e.connection = CfxRemoteCallContext.CurrentContext.connection;"); b.AppendLine("self.m_{0}?.Invoke(self, e);", cb.PublicName); b.AppendLine("e.connection = null;"); for (var i = 1; i <= sig.ManagedParameters.Count() - 1; i++) { sig.ManagedParameters[i].EmitPostRemoteRaiseEventStatements(b); } sig.EmitPostRemoteEventHandlerReturnValueStatements(b); b.EndBlock(); b.EndBlock(); b.AppendLine(); } }
public override void EmitNativeWrapper(CodeBuilder b) { b.AppendComment(CefStruct.Name); b.AppendLine(); b.BeginBlock("typedef struct _{0}", CfxNativeSymbol); b.AppendLine("{0} {1};", OriginalSymbol, CefStruct.Name); b.AppendLine("unsigned int ref_count;"); b.AppendLine("gc_handle_t gc_handle;"); b.AppendLine("int wrapper_kind;"); b.AppendLine("// managed callbacks"); foreach (var cb in CallbackFunctions) { b.AppendLine("void (CEF_CALLBACK *{0})({1});", cb.Name, cb.Signature.NativeParameterList); } b.EndBlock("{0};", CfxNativeSymbol); b.AppendLine(); b.BeginBlock("void CEF_CALLBACK _{0}_add_ref(struct _cef_base_ref_counted_t* base)", CfxName); if (GeneratorConfig.UseStrongHandleFor(CefStruct.Name)) { b.AppendLine("int count = InterlockedIncrement(&(({0}*)base)->ref_count);", CfxNativeSymbol); b.BeginIf("count == 2"); b.BeginIf("(({0}*)base)->wrapper_kind == 0", CfxNativeSymbol); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_UPGRADE);", CfxNativeSymbol); b.BeginElse(); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_UPGRADE | GC_HANDLE_REMOTE);", CfxNativeSymbol); b.EndBlock(); b.EndBlock(); } else { b.AppendLine("InterlockedIncrement(&(({0}*)base)->ref_count);", CfxNativeSymbol); } b.EndBlock(); b.BeginBlock("int CEF_CALLBACK _{0}_release(struct _cef_base_ref_counted_t* base)", CfxName); b.AppendLine("int count = InterlockedDecrement(&(({0}*)base)->ref_count);", CfxNativeSymbol); b.BeginIf("count == 0"); b.BeginIf("(({0}*)base)->wrapper_kind == 0", CfxNativeSymbol); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_FREE);", CfxNativeSymbol); b.BeginElse(); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_FREE | GC_HANDLE_REMOTE);", CfxNativeSymbol); b.EndBlock(); b.AppendLine("free(base);"); b.AppendLine("return 1;"); b.EndBlock(); if (GeneratorConfig.UseStrongHandleFor(CefStruct.Name)) { b.BeginIf("count == 1"); b.BeginIf("(({0}*)base)->wrapper_kind == 0", CfxNativeSymbol); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_DOWNGRADE);", CfxNativeSymbol); b.BeginElse(); b.AppendLine("cfx_gc_handle_switch(&(({0}*)base)->gc_handle, GC_HANDLE_DOWNGRADE | GC_HANDLE_REMOTE);", CfxNativeSymbol); b.EndBlock(); b.EndBlock(); } b.AppendLine("return 0;"); b.EndBlock(); b.BeginBlock("int CEF_CALLBACK _{0}_has_one_ref(struct _cef_base_ref_counted_t* base)", CfxName); b.AppendLine("return (({0}*)base)->ref_count == 1 ? 1 : 0;", CfxNativeSymbol); b.EndBlock(); b.AppendLine(); b.BeginBlock("static {0}* {1}_ctor(gc_handle_t gc_handle, int wrapper_kind)", CfxNativeSymbol, CfxName); b.AppendLine("{0}* ptr = ({0}*)calloc(1, sizeof({0}));", CfxNativeSymbol); b.AppendLine("if(!ptr) return 0;"); b.AppendLine("ptr->{0}.base.size = sizeof({1});", CefStruct.Name, OriginalSymbol); b.AppendLine("ptr->{0}.base.add_ref = _{1}_add_ref;", CefStruct.Name, CfxName); b.AppendLine("ptr->{0}.base.release = _{1}_release;", CefStruct.Name, CfxName); b.AppendLine("ptr->{0}.base.has_one_ref = _{1}_has_one_ref;", CefStruct.Name, CfxName); b.AppendLine("ptr->ref_count = 1;"); b.AppendLine("ptr->gc_handle = gc_handle;"); b.AppendLine("ptr->wrapper_kind = wrapper_kind;"); b.AppendLine("return ptr;"); b.EndBlock(); b.AppendLine(); if (NeedsWrapFunction) { b.BeginBlock("static gc_handle_t {0}_get_gc_handle({1}* self)", CfxName, CfxNativeSymbol); b.AppendLine("return self->gc_handle;"); b.EndBlock(); b.AppendLine(); } foreach (var cb in CallbackFunctions) { b.AppendLine("// {0}", cb); b.AppendLine(); b.BeginBlock("{0} CEF_CALLBACK {1}({2})", cb.NativeReturnType.OriginalSymbol, cb.NativeCallbackName, cb.Signature.OriginalParameterList); if (!cb.NativeReturnType.IsVoid) { cb.NativeReturnType.EmitNativeCallbackReturnValueFields(b); } foreach (var arg in cb.Signature.Parameters) { arg.EmitPreNativeCallbackStatements(b); } b.AppendLine("(({0}_t*)self)->{1}({2});", CfxName, cb.Name, cb.Signature.NativeArgumentList); foreach (var arg in cb.Signature.Parameters) { arg.EmitPostNativeCallbackStatements(b); } cb.NativeReturnType.EmitNativeCallbackReturnStatements(b); b.EndBlock(); b.AppendLine(); } b.BeginBlock("static void {0}_set_callback({1}* self, int index, void* callback)", CfxName, OriginalSymbol); b.BeginBlock("switch(index)"); var index = 0; foreach (var cb in CallbackFunctions) { b.DecreaseIndent(); b.AppendLine("case {0}:", index); b.IncreaseIndent(); b.AppendLine("(({0}_t*)self)->{1} = (void (CEF_CALLBACK *)({2}))callback;", CfxName, cb.Name, cb.Signature.NativeParameterList); b.AppendLine("self->{0} = callback ? {1} : 0;", cb.Name, cb.NativeCallbackName); b.AppendLine("break;"); cb.ClientCallbackIndex = index; index += 1; } b.EndBlock(); b.EndBlock(); b.AppendLine(); }