diff --git a/code/client/clrcore-v2/Coroutine/Coroutine.cs b/code/client/clrcore-v2/Coroutine/Coroutine.cs index b0e14c2eaf..bc5064e391 100644 --- a/code/client/clrcore-v2/Coroutine/Coroutine.cs +++ b/code/client/clrcore-v2/Coroutine/Coroutine.cs @@ -202,6 +202,8 @@ public object GetResult() return GetResultInternal(); } + internal object GetResultNonThrowing() => GetResultInternal(); + protected virtual object GetResultInternal() => null; internal void Complete() => CompleteInternal(State.Completed); diff --git a/code/client/clrcore-v2/Interop/LocalFunction.cs b/code/client/clrcore-v2/Interop/LocalFunction.cs index 0f4955978a..4e777ec552 100644 --- a/code/client/clrcore-v2/Interop/LocalFunction.cs +++ b/code/client/clrcore-v2/Interop/LocalFunction.cs @@ -53,12 +53,15 @@ internal unsafe Coroutine Invoke(object[] args) && requestAsyncCallback is Callback callbackRequested) { var c = new Coroutine(); - callbackRequested(new Action((a, e) => + callbackRequested(new Action((results, exception) => { - if (e != null) - c.SetException(new Exception(e.ToString())); + if (exception != null) + c.Fail(null, new Exception(exception.ToString())); + else if(results is object[] array && array.Length > 0) + c.Complete(array[0]); else - c.Complete(a); + c.Complete(null); + })); return c; diff --git a/code/client/clrcore-v2/Interop/MsgPackSerializer.cs b/code/client/clrcore-v2/Interop/MsgPackSerializer.cs index 51ebb513d3..1b3c5c65cf 100644 --- a/code/client/clrcore-v2/Interop/MsgPackSerializer.cs +++ b/code/client/clrcore-v2/Interop/MsgPackSerializer.cs @@ -35,8 +35,8 @@ internal static byte[] Serialize(object obj, bool remote = false) using (var packer = Packer.Create(stream, PackerCompatibilityOptions.None)) { Serialize(obj, packer); - //return stream.ToArray(); - return stream.GetBuffer(); + return stream.ToArray(); // other runtimes read the full length we give them, so shrink the buffer (copy) + //return stream.GetBuffer(); } } diff --git a/code/client/clrcore-v2/Interop/ReferenceFunctionManager.cs b/code/client/clrcore-v2/Interop/ReferenceFunctionManager.cs index 00dabbacdf..0ad406c287 100644 --- a/code/client/clrcore-v2/Interop/ReferenceFunctionManager.cs +++ b/code/client/clrcore-v2/Interop/ReferenceFunctionManager.cs @@ -182,21 +182,31 @@ internal unsafe static byte[] Invoke(int reference, byte* arguments, uint argsSi { if (coroutine.IsCompleted) { - if (coroutine.GetResult() is Callback callback) - coroutine.ContinueWith(() => callback(coroutine.GetResult(), coroutine.Exception)); - else - return MsgPackSerializer.Serialize(new[] { coroutine.GetResult() }); + if (coroutine.Exception != null) + { + Debug.Write(coroutine.Exception); + } + + return MsgPackSerializer.Serialize(new[] { coroutine.GetResultNonThrowing(), coroutine.Exception?.ToString() }); } else { - var returnDictionary = new Dictionary(1); - returnDictionary.Add("__cfx_async_retval", new Action(asyncResult => - { - coroutine.ContinueWith(new Action(() => asyncResult(coroutine.GetResult(), coroutine.Exception))); - }) - ); - - return MsgPackSerializer.Serialize(new[] { returnDictionary }); + var returnDictionary = new Dictionary() + { + { "__cfx_async_retval", new Action(asyncResult => + coroutine.ContinueWith(() => + { + if (coroutine.Exception != null) + { + Debug.Write(coroutine.Exception); + } + + asyncResult(coroutine.GetResultNonThrowing(), coroutine.Exception?.ToString()); + })) + } + }; + + return MsgPackSerializer.Serialize(new object[] { returnDictionary }); } } diff --git a/code/client/clrcore-v2/Native/Native.cs b/code/client/clrcore-v2/Native/Native.cs index 759743902f..c77ad878a5 100644 --- a/code/client/clrcore-v2/Native/Native.cs +++ b/code/client/clrcore-v2/Native/Native.cs @@ -93,7 +93,7 @@ internal static unsafe object[] InvokeFunctionReference(CString referenceIdentit ulong* __data = stackalloc ulong[] { (ulong)p_referenceIdentity, (ulong)p_argsSerialized, unchecked((ulong)argsSerialized.value?.LongLength), (ulong)&retLength }; ScriptContext.InvokeNative(ref s_0xe3551879, 0xe3551879, __data, 4); // INVOKE_FUNCTION_REFERENCE - return MsgPackDeserializer.DeserializeArray((byte*)__data, (long)retLength); + return MsgPackDeserializer.DeserializeArray(*(byte**)__data, (long)retLength); } }