JavascriptGenerator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Helper struct used to communicate to a yield point whether it was resumed via next(), return(), or throw()
  9. // and provide the data necessary for the corresponding action taken (see OP_ResumeYield)
  10. struct ResumeYieldData
  11. {
  12. Var data;
  13. JavascriptExceptionObject* exceptionObj;
  14. ResumeYieldData(Var data, JavascriptExceptionObject* exceptionObj) : data(data), exceptionObj(exceptionObj) { }
  15. };
  16. class JavascriptGenerator : public DynamicObject
  17. {
  18. public:
  19. enum class GeneratorState {
  20. Suspended,
  21. Executing,
  22. Completed
  23. };
  24. static uint32 GetFrameOffset() { return offsetof(JavascriptGenerator, frame); }
  25. static uint32 GetCallInfoOffset() { return offsetof(JavascriptGenerator, args) + Arguments::GetCallInfoOffset(); }
  26. static uint32 GetArgsPtrOffset() { return offsetof(JavascriptGenerator, args) + Arguments::GetValuesOffset(); }
  27. private:
  28. InterpreterStackFrame* frame;
  29. GeneratorState state;
  30. Arguments args;
  31. ScriptFunction* scriptFunction;
  32. DEFINE_VTABLE_CTOR_MEMBER_INIT(JavascriptGenerator, DynamicObject, args);
  33. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptGenerator);
  34. void SetState(GeneratorState state)
  35. {
  36. this->state = state;
  37. if (state == GeneratorState::Completed)
  38. {
  39. frame = nullptr;
  40. args.Values = nullptr;
  41. scriptFunction = nullptr;
  42. }
  43. }
  44. Var CallGenerator(ResumeYieldData* yieldData, const wchar_t* apiNameForErrorMessage);
  45. public:
  46. JavascriptGenerator(DynamicType* type, Arguments& args, ScriptFunction* scriptFunction);
  47. bool IsExecuting() const { return state == GeneratorState::Executing; }
  48. bool IsSuspended() const { return state == GeneratorState::Suspended; }
  49. bool IsCompleted() const { return state == GeneratorState::Completed; }
  50. bool IsSuspendedStart() const { return state == GeneratorState::Suspended && this->frame == nullptr; }
  51. void SetFrame(InterpreterStackFrame* frame) { Assert(this->frame == nullptr); this->frame = frame; }
  52. InterpreterStackFrame* GetFrame() const { return frame; }
  53. const Arguments& GetArguments() const { return args; }
  54. static bool Is(Var var);
  55. static JavascriptGenerator* FromVar(Var var);
  56. class EntryInfo
  57. {
  58. public:
  59. static FunctionInfo Next;
  60. static FunctionInfo Return;
  61. static FunctionInfo Throw;
  62. };
  63. static Var EntryNext(RecyclableObject* function, CallInfo callInfo, ...);
  64. static Var EntryReturn(RecyclableObject* function, CallInfo callInfo, ...);
  65. static Var EntryThrow(RecyclableObject* function, CallInfo callInfo, ...);
  66. };
  67. }