Browse Source

Implementing primitive value assignment

Sebastien Ros 11 năm trước cách đây
mục cha
commit
d0d5c7cc6f

+ 3 - 3
Jint.Repl/Program.cs

@@ -8,9 +8,9 @@ namespace Jint.Repl
     {
         static void Main(string[] args)
         {
-            var engine = new Engine(cfg => cfg
-                .WithDelegate("log", new Action<object>(Console.WriteLine))
-            );
+            var engine = new Engine()
+                .WithMember("log", new Action<object>(Console.WriteLine))
+            ;
 
             while (true)
             {

+ 1 - 0
Jint.Tests/Jint.Tests.csproj

@@ -54,6 +54,7 @@
   <ItemGroup>
     <Compile Include="Parser\JavascriptParserTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Runtime\InteropTests.cs" />
     <Compile Include="Runtime\EngineTests.cs" />
     <Compile Include="Runtime\SunSpiderTests.cs" />
   </ItemGroup>

+ 4 - 4
Jint.Tests/Runtime/EngineTests.cs

@@ -11,10 +11,10 @@ namespace Jint.Tests.Runtime
     {
         private Engine RunTest(string source)
         {
-            var engine = new Engine(cfg => cfg
-                .WithDelegate("log", new Action<object>(Console.WriteLine))
-                .WithDelegate("assert", new Action<bool>(Assert.True))
-                );
+            var engine = new Engine()
+                .WithMember("log", new Action<object>(Console.WriteLine))
+                .WithMember("assert", new Action<bool>(Assert.True))
+                ;
 
             engine.Execute(source);
 

+ 51 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -0,0 +1,51 @@
+using System;
+using Xunit;
+
+namespace Jint.Tests.Runtime
+{
+    public class InteropTests : IDisposable
+    {
+        private readonly Engine _engine;
+
+        public InteropTests()
+        {
+            _engine = new Engine()
+                .WithMember("log", new Action<object>(Console.WriteLine))
+                .WithMember("assert", new Action<bool>(Assert.True))
+                ;
+        }
+
+        void IDisposable.Dispose()
+        {
+        }
+
+        private void RunTest(string source)
+        {
+            _engine.Execute(source);
+        }
+
+        [Fact]
+        public void PrimitiveTypesCanBeSet()
+        {
+            _engine.WithMember("x", 10);
+            _engine.WithMember("y", true);
+            _engine.WithMember("z", "foo");
+
+            RunTest(@"
+                assert(x === 10);
+                assert(y === true);
+                assert(z === 'foo');
+            ");
+        }
+
+        [Fact]
+        public void DelegatesCanBeSet()
+        {
+            _engine.WithMember("square", new Func<double, double>(x => x * x));
+
+            RunTest(@"
+                assert(square(10) === 100);
+            ");
+        }
+    }
+}

+ 4 - 4
Jint.Tests/Runtime/SunSpiderTests.cs

@@ -9,10 +9,10 @@ namespace Jint.Tests.Runtime
     {
         private Engine RunTest(string source)
         {
-            var engine = new Engine(cfg => cfg
-                .WithDelegate("log", new Action<object>(Console.WriteLine))
-                .WithDelegate("assert", new Action<bool>(Assert.True))
-            );
+            var engine = new Engine()
+                .WithMember("log", new Action<object>(Console.WriteLine))
+                .WithMember("assert", new Action<bool>(Assert.True))
+            ;
 
             engine.Execute(source);
 

+ 30 - 8
Jint/Engine.cs

@@ -109,14 +109,6 @@ namespace Jint
                 options(Options);
             }
 
-            if (options != null)
-            {
-                foreach (var entry in Options.GetDelegates())
-                {
-                    Global.FastAddProperty(entry.Key, new DelegateWrapper(this, entry.Value), true, false, true);
-                }
-            }
-
             Eval = new EvalFunctionInstance(this, new string[0], LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
             Global.FastAddProperty("eval", Eval, true, false, true);
 
@@ -164,6 +156,36 @@ namespace Jint
             return executionContext;
         }
 
+        public Engine WithMember(string name, Delegate value)
+        {
+            Global.FastAddProperty(name, new DelegateWrapper(this, value), true, false, true);
+            return this;
+        }
+
+        public Engine WithMember(string name, string value)
+        {
+            Global.FastAddProperty(name, new JsValue(value), true, false, true);
+            return this;
+        }
+
+        public Engine WithMember(string name, double value)
+        {
+            Global.FastAddProperty(name, new JsValue(value), true, false, true);
+            return this;
+        }
+
+        public Engine WithMember(string name, bool value)
+        {
+            Global.FastAddProperty(name, new JsValue(value), true, false, true);
+            return this;
+        }
+
+        public Engine WithMember(string name, DateTime value)
+        {
+            Global.FastAddProperty(name, Date.Construct(value), true, false, true);
+            return this;
+        }
+
         public void LeaveExecutionContext()
         {
             _executionContexts.Pop();

+ 1 - 18
Jint/Options.cs

@@ -1,15 +1,10 @@
-using System;
-using System.Collections.Generic;
-
-namespace Jint
+namespace Jint
 {
     public class Options
     {
         private bool _discardGlobal;
         private bool _strict;
 
-        private readonly Dictionary<string, Delegate> _delegates = new Dictionary<string, Delegate>();
-
         /// <summary>
         /// When called, doesn't initialize the global scope.
         /// Can be useful in lightweight scripts for performance reason.
@@ -26,23 +21,11 @@ namespace Jint
             return this;
         }
 
-        public Options WithDelegate(string name, Delegate del)
-        {
-            _delegates[name] = del;
-
-            return this;
-        }
-
         internal bool GetDiscardGlobal()
         {
             return _discardGlobal;
         }
 
-        internal IDictionary<string, Delegate> GetDelegates()
-        {
-            return _delegates;
-        }
-
         internal bool IsStrict()
         {
             return _strict;