JintAwaitExpression.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. namespace Jint.Runtime.Interpreter.Expressions;
  4. internal sealed class JintAwaitExpression : JintExpression
  5. {
  6. private JintExpression _awaitExpression = null!;
  7. private bool _initialized;
  8. public JintAwaitExpression(AwaitExpression expression) : base(expression)
  9. {
  10. _initialized = false;
  11. }
  12. protected override object EvaluateInternal(EvaluationContext context)
  13. {
  14. if (!_initialized)
  15. {
  16. _awaitExpression = Build(((AwaitExpression) _expression).Argument);
  17. _initialized = true;
  18. }
  19. var engine = context.Engine;
  20. var asyncContext = engine.ExecutionContext;
  21. try
  22. {
  23. var value = _awaitExpression.GetValue(context);
  24. if (value is not JsPromise)
  25. {
  26. var promiseInstance = new JsPromise(engine);
  27. promiseInstance.Resolve(value);
  28. value = promiseInstance;
  29. }
  30. engine.RunAvailableContinuations();
  31. return value.UnwrapIfPromise();
  32. }
  33. catch (PromiseRejectedException e)
  34. {
  35. ExceptionHelper.ThrowJavaScriptException(engine, e.RejectedValue, _expression.Location);
  36. return null;
  37. }
  38. }
  39. }