Engine.Advanced.cs 2.2 KB

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