-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
249 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using Hosihikari.Minecraft.Extension; | ||
using Hosihikari.VanillaScript.QuickJS.Extensions; | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
using Hosihikari.VanillaScript.QuickJS.Wrapper; | ||
|
||
namespace Hosihikari.VanillaScript.QuickJS.Helper; | ||
|
||
public static class JsPromiseHelper | ||
{ | ||
internal static JsValue BuildErrorJsValue(this JsContextWrapper ctx, Exception exception) | ||
{ | ||
unsafe | ||
{ | ||
var errorObj = JsValueCreateHelper.NewError(ctx.Context).Steal(); | ||
/* | ||
JS_DefinePropertyValue(ctx, obj, JS_ATOM_message, | ||
JS_NewString(ctx, buf), | ||
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); | ||
*/ | ||
errorObj.DefineProperty( | ||
ctx.Context, | ||
JsAtomConst.Message, | ||
JsValueCreateHelper.NewString(ctx.Context, exception.Message).Steal(), | ||
JsPropertyFlags.Writable | JsPropertyFlags.Configurable | ||
); | ||
errorObj.DefineProperty( | ||
ctx.Context, | ||
JsAtomConst.Stack, | ||
JsValueCreateHelper.NewString(ctx.Context, exception.StackTrace ?? "").Steal(), | ||
JsPropertyFlags.Writable | JsPropertyFlags.Configurable | ||
); | ||
//todo JsAtomConst.ToStringFunc define .toString() function for errorObj | ||
return errorObj; | ||
} | ||
} | ||
|
||
public static void AwaitTask( | ||
nint ctxPtr, | ||
JsValue thisObj, | ||
(SafeJsValue resolve, SafeJsValue reject) promise, | ||
Task tasks | ||
) | ||
{ | ||
var safeThis = new SafeJsValue(thisObj); | ||
Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await tasks.ConfigureAwait(false); | ||
LevelTick.PostTick(() => | ||
{ | ||
unsafe | ||
{ | ||
if (JsContextWrapper.TryGet(ctxPtr, out var ctx)) | ||
{ | ||
Native.JS_Call( | ||
ctx.Context, | ||
promise.resolve.Instance, | ||
safeThis.Instance, | ||
0, | ||
null | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
LevelTick.PostTick(() => | ||
{ | ||
var reason = ex.ToString(); | ||
unsafe | ||
{ | ||
if (JsContextWrapper.TryGet(ctxPtr, out var ctx)) | ||
{ | ||
var reasonObj = ctx.BuildErrorJsValue(ex); | ||
Native.JS_Call( | ||
ctx.Context, | ||
promise.reject.Instance, | ||
safeThis.Instance, | ||
1, | ||
&reasonObj | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// wrap Task to Promise | ||
/// </summary> | ||
/// <typeparam name="T"> result type </typeparam> | ||
/// <param name="ctxPtr"></param> | ||
/// <param name="thisObj"></param> | ||
/// <param name="promise"></param> | ||
/// <param name="tasks"></param> | ||
/// <param name="fetchResult"></param> | ||
public static void AwaitTask<T>( | ||
nint ctxPtr, | ||
JsValue thisObj, | ||
(SafeJsValue resolve, SafeJsValue reject) promise, | ||
Task<T> tasks, | ||
Func<T, JsValue> fetchResult | ||
) | ||
{ | ||
var safeThis = new SafeJsValue(thisObj); | ||
Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
var result = await tasks.ConfigureAwait(false); | ||
LevelTick.PostTick(() => | ||
{ | ||
var resultObj = fetchResult(result); | ||
unsafe | ||
{ | ||
if (JsContextWrapper.TryGet(ctxPtr, out var ctx)) | ||
{ | ||
Native.JS_Call( | ||
ctx.Context, | ||
promise.resolve.Instance, | ||
safeThis.Instance, | ||
1, | ||
&resultObj | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
LevelTick.PostTick(() => | ||
{ | ||
unsafe | ||
{ | ||
if (JsContextWrapper.TryGet(ctxPtr, out var ctx)) | ||
{ | ||
var reasonObj = ctx.BuildErrorJsValue(ex); | ||
Native.JS_Call( | ||
ctx.Context, | ||
promise.reject.Instance, | ||
safeThis.Instance, | ||
1, | ||
&reasonObj | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.