Engine.Advanced.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Jint.Native.Promise;
  2. using Jint.Runtime.Environments;
  3. using Environment = Jint.Runtime.Environments.Environment;
  4. namespace Jint;
  5. public partial class Engine
  6. {
  7. public AdvancedOperations Advanced { get; }
  8. }
  9. public class AdvancedOperations
  10. {
  11. private readonly Engine _engine;
  12. public AdvancedOperations(Engine engine)
  13. {
  14. _engine = engine;
  15. }
  16. /// <summary>
  17. /// Gets current stack trace that is active in engine.
  18. /// </summary>
  19. public string StackTrace
  20. {
  21. get
  22. {
  23. var lastSyntaxElement = _engine._lastSyntaxElement;
  24. if (lastSyntaxElement is null)
  25. {
  26. return string.Empty;
  27. }
  28. return _engine.CallStack.BuildCallStackString(lastSyntaxElement.Location);
  29. }
  30. }
  31. /// <summary>
  32. /// Initializes list of references of called functions
  33. /// </summary>
  34. public void ResetCallStack()
  35. {
  36. _engine.ResetCallStack();
  37. }
  38. /// <summary>
  39. /// Forcefully processes the current task queues (micro and regular), this API may break and change behavior!
  40. /// </summary>
  41. public void ProcessTasks()
  42. {
  43. _engine.RunAvailableContinuations();
  44. }
  45. /// <summary>
  46. /// Creates a new declarative environment that has current lexical environment as outer scope.
  47. /// </summary>
  48. public Environment CreateDeclarativeEnvironment()
  49. {
  50. return JintEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment);
  51. }
  52. /// <summary>
  53. /// EXPERIMENTAL! Subject to change.
  54. ///
  55. /// Registers a promise within the currently running EventLoop (has to be called within "ExecuteWithEventLoop" call).
  56. /// Note that ExecuteWithEventLoop will not trigger "onFinished" callback until ALL manual promises are settled.
  57. ///
  58. /// NOTE: that resolve and reject need to be called withing the same thread as "ExecuteWithEventLoop".
  59. /// The API assumes that the Engine is called from a single thread.
  60. /// </summary>
  61. /// <returns>a Promise instance and functions to either resolve or reject it</returns>
  62. public ManualPromise RegisterPromise()
  63. {
  64. return _engine.RegisterPromise();
  65. }
  66. }