ArgumentsInstancePool.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Jint.Native;
  2. using Jint.Native.Argument;
  3. using Jint.Native.Function;
  4. using Jint.Runtime.Environments;
  5. using Jint.Runtime.References;
  6. namespace Jint.Pooling
  7. {
  8. /// <summary>
  9. /// Cache reusable <see cref="Reference" /> instances as we allocate them a lot.
  10. /// </summary>
  11. internal sealed class ArgumentsInstancePool
  12. {
  13. private const int PoolSize = 10;
  14. private readonly Engine _engine;
  15. private readonly ObjectPool<ArgumentsInstance> _pool;
  16. public ArgumentsInstancePool(Engine engine)
  17. {
  18. _engine = engine;
  19. _pool = new ObjectPool<ArgumentsInstance>(Factory, PoolSize);
  20. }
  21. private ArgumentsInstance Factory()
  22. {
  23. return new ArgumentsInstance(_engine)
  24. {
  25. _prototype = _engine.Realm.Intrinsics.Object.PrototypeObject
  26. };
  27. }
  28. public ArgumentsInstance Rent(JsValue[] argumentsList) => Rent(null, null, argumentsList, null, false);
  29. public ArgumentsInstance Rent(
  30. FunctionInstance? func,
  31. Key[]? formals,
  32. JsValue[] argumentsList,
  33. DeclarativeEnvironmentRecord? env,
  34. bool hasRestParameter)
  35. {
  36. var obj = _pool.Allocate();
  37. obj.Prepare(func!, formals!, argumentsList, env!, hasRestParameter);
  38. return obj;
  39. }
  40. public void Return(ArgumentsInstance instance)
  41. {
  42. if (ReferenceEquals(instance, null))
  43. {
  44. return;
  45. }
  46. _pool.Free(instance);;
  47. }
  48. }
  49. }