public static byte[] Invoke(int reference, byte[] arguments) { if (!ms_references.TryGetValue(reference, out var funcRef)) { Debug.WriteLine("No such reference for {0}.", reference); // return nil return(new byte[] { 0xC0 }); } var method = funcRef.m_method; // deserialize the passed arguments var argList = (List <object>)MsgPackDeserializer.Deserialize(arguments); var argArray = CallUtilities.GetPassArguments(method.Method, argList.ToArray(), string.Empty); // the Lua runtime expects this to be an array, so it be an array. return(MsgPackSerializer.Serialize(new[] { method.DynamicInvoke(argArray) })); }
public static byte[] Invoke(int reference, byte[] arguments) { if (!ms_references.TryGetValue(reference, out var funcRef)) { Debug.WriteLine("No such reference for {0}.", reference); // return nil return(new byte[] { 0xC0 }); } var method = funcRef.m_method; // deserialize the passed arguments var argList = (List <object>)MsgPackDeserializer.Deserialize(arguments); var argArray = CallUtilities.GetPassArguments(method.Method, argList.ToArray(), string.Empty); // the Lua runtime expects this to be an array, so it be an array. var rv = method.DynamicInvoke(argArray); // is this actually an asynchronous method? if (rv is Task) { dynamic rt = rv; rv = new { __cfx_async_retval = new Action <dynamic>(rvcb => { rt.ContinueWith(new Action <Task>(t => { rvcb(new object[] { rt.Result }, false); })); }) }; } return(MsgPackSerializer.Serialize(new[] { rv })); }
internal async Task Invoke(string sourceString, params object[] args) { var callbacks = m_callbacks.ToArray(); foreach (var callback in callbacks) { try { var passArgs = CallUtilities.GetPassArguments(callback.Method, args, sourceString); var rv = callback.DynamicInvoke(passArgs); if (rv != null && rv is Task task) { await task; } } catch (Exception e) { Debug.WriteLine("Error invoking callback for event {0}: {1}", m_eventName, e.ToString()); m_callbacks.Remove(callback); } } }