소스 검색

Adding constructor with existing options (#762)

Sébastien Ros 5 년 전
부모
커밋
88ac7e3662
3개의 변경된 파일46개의 추가작업 그리고 1개의 파일을 삭제
  1. 12 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 10 1
      Jint/Engine.cs
  3. 24 0
      Jint/Options.cs

+ 12 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -2847,6 +2847,18 @@ x.test = {
                 .SetValue("a", 1);
         }
 
+        [Fact]
+        public void ShouldReuseOptions()
+        {
+            var options = new Options().Configure(e => e.SetValue("x", 1));
+
+            var engine1 = new Engine(options);
+            var engine2 = new Engine(options);
+
+            Assert.Equal(1, Convert.ToInt32(engine1.GetValue("x").ToObject()));
+            Assert.Equal(1, Convert.ToInt32(engine2.GetValue("x").ToObject()));
+        }
+
         private class Wrapper
         {
             public Testificate Test { get; set; }

+ 10 - 1
Jint/Engine.cs

@@ -128,6 +128,13 @@ namespace Jint
         {
         }
 
+        /// <summary>
+        /// Constructs a new engine with a custom <see cref="Options"/> instance.
+        /// </summary>
+        public Engine(Options options) : this((e, o) => e.Options = options)
+        {
+        }
+
         /// <summary>
         /// Constructs a new engine instance and allows customizing options.
         /// </summary>
@@ -203,6 +210,8 @@ namespace Jint
             }
 
             ClrTypeConverter = new DefaultTypeConverter(this);
+
+            Options.Apply(this);
         }
     
 
@@ -244,7 +253,7 @@ namespace Jint
 
         internal long CurrentMemoryUsage { get; private set; }
 
-        internal Options Options { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
+        internal Options Options { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; private set; }
 
         #region Debugger
         public delegate StepMode DebugStepDelegate(object sender, DebugInformation e);

+ 24 - 0
Jint/Options.cs

@@ -29,6 +29,7 @@ namespace Jint
         private List<Assembly> _lookupAssemblies = new List<Assembly>();
         private Predicate<Exception> _clrExceptionsHandler;
         private IReferenceResolver _referenceResolver = DefaultReferenceResolver.Instance;
+        private readonly List<Action<Engine>> _configurations = new List<Action<Engine>>();
 
         /// <summary>
         /// Run the script in strict mode.
@@ -198,6 +199,29 @@ namespace Jint
             return this;
         }
 
+        /// <summary>
+        /// Registers some custom logic to apply on an <see cref="Engine"/> instance when the options
+        /// are loaded.
+        /// </summary>
+        /// <param name="configuration">The action to register.</param>
+        public Options Configure(Action<Engine> configuration)
+        {
+            _configurations.Add(configuration);
+            return this;
+        }
+
+        /// <summary>
+        /// Called by the <see cref="Engine"/> instance that loads this <see cref="Options" />
+        /// once it is loaded.
+        /// </summary>
+        internal void Apply(Engine engine)
+        {
+            foreach (var configuration in _configurations)
+            {
+                configuration?.Invoke(engine);
+            }
+        }
+
         internal bool IsStrict => _strict;
 
         internal bool _IsDebuggerStatementAllowed => _allowDebuggerStatement;