JavascriptAsyncGenerator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class JavascriptAsyncGenerator : public JavascriptGenerator
  9. {
  10. struct AsyncGeneratorRequest
  11. {
  12. Field(Var) data;
  13. Field(ResumeYieldKind) kind;
  14. Field(JavascriptPromise*) promise;
  15. AsyncGeneratorRequest(Var data, ResumeYieldKind kind, JavascriptPromise* promise) :
  16. data(data),
  17. kind(kind),
  18. promise(promise) {}
  19. };
  20. using RequestQueue = DList<AsyncGeneratorRequest*, Recycler>;
  21. enum class PendingState
  22. {
  23. None,
  24. Await,
  25. AwaitReturn,
  26. Yield
  27. };
  28. private:
  29. DEFINE_VTABLE_CTOR(JavascriptAsyncGenerator, JavascriptGenerator);
  30. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptAsyncGenerator);
  31. Field(RequestQueue*) requestQueue;
  32. Field(PendingState) pendingState = PendingState::None;
  33. Field(RuntimeFunction*) onFulfilled;
  34. Field(RuntimeFunction*) onRejected;
  35. JavascriptAsyncGenerator(
  36. DynamicType* type,
  37. Arguments& args,
  38. ScriptFunction* scriptFunction,
  39. RequestQueue* requestQueue) :
  40. JavascriptGenerator(type, args, scriptFunction),
  41. requestQueue(requestQueue) {}
  42. public:
  43. static JavascriptAsyncGenerator* New(
  44. Recycler* recycler,
  45. DynamicType* generatorType,
  46. Arguments& args,
  47. ScriptFunction* scriptFunction);
  48. void PushRequest(AsyncGeneratorRequest* request)
  49. {
  50. requestQueue->Append(request);
  51. }
  52. AsyncGeneratorRequest* ShiftRequest()
  53. {
  54. Assert(!requestQueue->Empty());
  55. AsyncGeneratorRequest* request = requestQueue->Head();
  56. requestQueue->RemoveHead();
  57. return request;
  58. }
  59. AsyncGeneratorRequest* PeekRequest()
  60. {
  61. Assert(!requestQueue->Empty());
  62. return requestQueue->Head();
  63. }
  64. bool HasRequest()
  65. {
  66. return !requestQueue->Empty();
  67. }
  68. void ResumeNext();
  69. void ResumeCoroutine(Var value, ResumeYieldKind resumeKind);
  70. void ResolveNext(Var value);
  71. void RejectNext(Var reason);
  72. void UnwrapValue(Var value, PendingState pendingState);
  73. class EntryInfo
  74. {
  75. public:
  76. static FunctionInfo Next;
  77. static FunctionInfo Return;
  78. static FunctionInfo Throw;
  79. };
  80. static Var EntryNext(RecyclableObject* function, CallInfo callInfo, ...);
  81. static Var EntryReturn(RecyclableObject* function, CallInfo callInfo, ...);
  82. static Var EntryThrow(RecyclableObject* function, CallInfo callInfo, ...);
  83. static Var EntryAwaitFulfilledCallback(RecyclableObject* function, CallInfo callInfo, ...);
  84. static Var EntryAwaitRejectedCallback(RecyclableObject* function, CallInfo callInfo, ...);
  85. static Var EnqueueRequest(
  86. Var thisValue,
  87. ScriptContext* scriptContext,
  88. Var input,
  89. ResumeYieldKind resumeKind,
  90. const char16* apiNameForErrorMessage);
  91. };
  92. template<>
  93. bool VarIsImpl<JavascriptAsyncGenerator>(RecyclableObject* obj);
  94. class AsyncGeneratorCallbackFunction : public RuntimeFunction
  95. {
  96. protected:
  97. DEFINE_VTABLE_CTOR(AsyncGeneratorCallbackFunction, RuntimeFunction);
  98. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(AsyncGeneratorCallbackFunction);
  99. public:
  100. AsyncGeneratorCallbackFunction(
  101. DynamicType* type,
  102. FunctionInfo* functionInfo,
  103. JavascriptAsyncGenerator* generator) :
  104. RuntimeFunction(type, functionInfo),
  105. generator(generator) {}
  106. Field(JavascriptAsyncGenerator*) generator;
  107. };
  108. template<>
  109. bool VarIsImpl<AsyncGeneratorCallbackFunction>(RecyclableObject* obj);
  110. }