Skip to content

Commit

Permalink
fix(clrcore-v2): exports/reffunc fixes
Browse files Browse the repository at this point in the history
* Fix (a)sync exports calls
* Properly handle latest exception fixes for Exports and RefFuncs
* Shrink serialized data to interface properly with other runtimes
  • Loading branch information
thorium-cfx committed Jun 10, 2023
1 parent 1e32597 commit b657622
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions code/client/clrcore-v2/Coroutine/Coroutine.cs
Expand Up @@ -202,6 +202,8 @@ public object GetResult()
return GetResultInternal();
}

internal object GetResultNonThrowing() => GetResultInternal();

protected virtual object GetResultInternal() => null;

internal void Complete() => CompleteInternal(State.Completed);
Expand Down
11 changes: 7 additions & 4 deletions code/client/clrcore-v2/Interop/LocalFunction.cs
Expand Up @@ -53,12 +53,15 @@ internal unsafe Coroutine<object> Invoke(object[] args)
&& requestAsyncCallback is Callback callbackRequested)
{
var c = new Coroutine<object>();
callbackRequested(new Action<object, object>((a, e) =>
callbackRequested(new Action<object, object>((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;
Expand Down
4 changes: 2 additions & 2 deletions code/client/clrcore-v2/Interop/MsgPackSerializer.cs
Expand Up @@ -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();
}
}

Expand Down
34 changes: 22 additions & 12 deletions code/client/clrcore-v2/Interop/ReferenceFunctionManager.cs
Expand Up @@ -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<string, object>(1);
returnDictionary.Add("__cfx_async_retval", new Action<Callback>(asyncResult =>
{
coroutine.ContinueWith(new Action(() => asyncResult(coroutine.GetResult(), coroutine.Exception)));
})
);

return MsgPackSerializer.Serialize(new[] { returnDictionary });
var returnDictionary = new Dictionary<string, object>()
{
{ "__cfx_async_retval", new Action<Callback>(asyncResult =>
coroutine.ContinueWith(() =>
{
if (coroutine.Exception != null)
{
Debug.Write(coroutine.Exception);
}
asyncResult(coroutine.GetResultNonThrowing(), coroutine.Exception?.ToString());
}))
}
};

return MsgPackSerializer.Serialize(new object[] { returnDictionary });
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/client/clrcore-v2/Native/Native.cs
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b657622

Please sign in to comment.