123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using Jint.Native;
- using Jint.Native.Argument;
- using Jint.Native.Function;
- using Jint.Runtime.Environments;
- using Jint.Runtime.References;
- namespace Jint.Pooling
- {
- /// <summary>
- /// Cache reusable <see cref="Reference" /> instances as we allocate them a lot.
- /// </summary>
- internal sealed class ArgumentsInstancePool
- {
- private const int PoolSize = 10;
- private readonly Engine _engine;
- private readonly ObjectPool<ArgumentsInstance> _pool;
- public ArgumentsInstancePool(Engine engine)
- {
- _engine = engine;
- _pool = new ObjectPool<ArgumentsInstance>(Factory, PoolSize);
- }
- private ArgumentsInstance Factory()
- {
- return new ArgumentsInstance(_engine);
- }
- public ArgumentsInstance Rent(
- FunctionInstance func,
- string[] names,
- JsValue[] args,
- EnvironmentRecord env,
- bool strict)
- {
- var obj = _pool.Allocate();
- obj.Prepare(func, names, args, env, strict);
- // These properties are pre-initialized as their don't trigger
- // the EnsureInitialized() event and are cheap
- obj.Prototype = _engine.Object.PrototypeObject;
- obj.Extensible = true;
- obj.Strict = strict;
- return obj;
- }
- public void Return(ArgumentsInstance instance)
- {
- if (ReferenceEquals(instance, null))
- {
- return;
- }
- _pool.Free(instance);;
- }
- }
- }
|