ArgumentsInstancePool.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. public ArgumentsInstance Rent(
  26. FunctionInstance func,
  27. string[] names,
  28. JsValue[] args,
  29. EnvironmentRecord env,
  30. bool strict)
  31. {
  32. var obj = _pool.Allocate();
  33. obj.Prepare(func, names, args, env, strict);
  34. // These properties are pre-initialized as their don't trigger
  35. // the EnsureInitialized() event and are cheap
  36. obj.Prototype = _engine.Object.PrototypeObject;
  37. obj.Extensible = true;
  38. obj.Strict = strict;
  39. return obj;
  40. }
  41. public void Return(ArgumentsInstance instance)
  42. {
  43. if (ReferenceEquals(instance, null))
  44. {
  45. return;
  46. }
  47. _pool.Free(instance);;
  48. }
  49. }
  50. }