Browse Source

Adding Repl project

Sebastien Ros 12 years ago
parent
commit
179589b6b4

+ 6 - 0
Jint.Repl/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+    </startup>
+</configuration>

+ 64 - 0
Jint.Repl/Jint.Repl.csproj

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Jint.Repl</RootNamespace>
+    <AssemblyName>Jint.Repl</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\Jint\Jint.csproj">
+      <Project>{678738DA-F723-4920-B9E5-CAD667104BDA}</Project>
+      <Name>Jint</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 31 - 0
Jint.Repl/Program.cs

@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Jint.Repl
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var engine = new Engine();
+ 
+            while (true)
+            {
+                Console.ForegroundColor = ConsoleColor.Green;
+                Console.Write(" > ");
+                var input = Console.ReadLine();
+                if (input == "exit")
+                {
+                    return;
+                }
+
+                var result = engine.GetValue(engine.Execute(input));
+                Console.ForegroundColor = ConsoleColor.Magenta;
+                Console.WriteLine("=> {0}", result);
+            }
+        }
+    }
+}

+ 36 - 0
Jint.Repl/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Jint.Repl")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Jint.Repl")]
+[assembly: AssemblyCopyright("Copyright ©  2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("52865b1f-b3dc-40ff-afaa-f7c17f515b16")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

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

@@ -375,6 +375,14 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void StringFunctionCreatesString()
+        {
+            RunTest(@"
+                assert(Strint(NaN) === 'NaN');
+            ");
+        }
+
         [Fact]
         public void ScopeChainInWithStatement()
         {
@@ -421,6 +429,23 @@ namespace Jint.Tests.Runtime
                 assert(z == 1);
             ");
         }
+
+        [Theory]
+        [InlineData(double.NaN, "parseInt(NaN)")]
+        [InlineData(double.NaN, "parseInt(null)")]
+        [InlineData(double.NaN, "parseInt(undefined)")]
+        [InlineData(double.NaN, "parseInt(new Boolean(true))")]
+        [InlineData(double.NaN, "parseInt(Infinity)")]
+        [InlineData(-1, "parseInt(-1)")]
+        [InlineData(-1, "parseInt('-1')")]
+        public void ShouldEvaluateParseInt(object expected, string source)
+        {
+            var engine = new Engine();
+            var result = engine.GetValue(engine.Execute(source));
+
+            Assert.Equal(expected, result);
+        }
+
         /*
                         [Fact]
                         public void ()

+ 6 - 0
Jint.sln

@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint.Tests.Ecma", "Jint.Tes
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint", "Jint\Jint.csproj", "{678738DA-F723-4920-B9E5-CAD667104BDA}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jint.Repl", "Jint.Repl\Jint.Repl.csproj", "{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -37,6 +39,10 @@ Global
 		{678738DA-F723-4920-B9E5-CAD667104BDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{678738DA-F723-4920-B9E5-CAD667104BDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{678738DA-F723-4920-B9E5-CAD667104BDA}.Release|Any CPU.Build.0 = Release|Any CPU
+		{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A69FD3C5-F2B0-4055-A518-F4173EBA5E4D}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 167 - 2
Jint/Native/Global/GlobalObject.cs

@@ -1,4 +1,6 @@
-using Jint.Native.Object;
+using System;
+using System.Globalization;
+using Jint.Native.Object;
 using Jint.Runtime;
 using Jint.Runtime.Descriptors;
 using Jint.Runtime.Descriptors.Specialized;
@@ -25,16 +27,179 @@ namespace Jint.Native.Global
             global.DefineOwnProperty("undefined", new DataDescriptor(Undefined.Instance), false);
 
             // Global object functions
+            global.DefineOwnProperty("parseInt", new ClrDataDescriptor<object, object>(engine, ParseInt) { }, false);
+            global.DefineOwnProperty("parseFloat", new ClrDataDescriptor<object, object>(engine, ParseFloat), false);
             global.DefineOwnProperty("isNaN", new ClrDataDescriptor<object, bool>(engine, IsNaN), false);
+            global.DefineOwnProperty("isFinite", new ClrDataDescriptor<object, bool>(engine, IsFinite), false);
+            global.DefineOwnProperty("decodeURI", new ClrDataDescriptor<object, string>(engine, DecodeUri), false);
+            global.DefineOwnProperty("decodeURIComponent", new ClrDataDescriptor<object, string>(engine, DecodeUriComponent), false);
+            global.DefineOwnProperty("encodeURI", new ClrDataDescriptor<object, string>(engine, EncodeUri), false);
+            global.DefineOwnProperty("encodeURIComponent", new ClrDataDescriptor<object, string>(engine, EncodeUriComponent), false);
 
             return global;
         }
 
-        private static bool IsNaN(object thisObject, object[] arguments)
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
+        /// </summary>
+        public static object ParseInt(object thisObject, object[] arguments)
+        {
+            int radix = arguments.Length > 1 ? TypeConverter.ToInt32(arguments[1]) : 10;
+            object v = arguments[0];
+
+            if (radix == 0)
+            {
+                radix = 10;
+            }
+
+            if (radix < 2 || radix > 36)
+            {
+                return double.NaN;
+            }
+
+            try
+            {
+                var s = TypeConverter.ToString(v);
+                return Convert.ToInt32(s.TrimStart(), radix);
+            }
+            catch
+            {
+                return double.NaN;
+            }
+
+        }
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.3
+        /// </summary>
+        public static object ParseFloat(object thisObject, object[] arguments)
+        {
+            object v = arguments[0];
+
+            var s = TypeConverter.ToString(v);
+            double n;
+            if (double.TryParse(s, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out n))
+            {
+                return n;
+            }
+
+            return double.NaN;
+        }
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.4
+        /// </summary>
+        public static bool IsNaN(object thisObject, object[] arguments)
         {
             var x = TypeConverter.ToNumber(arguments[0]);
             return double.IsNaN(x);
         }
 
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.5
+        /// </summary>
+        public static bool IsFinite(object thisObject, object[] arguments)
+        {
+            if (arguments.Length != 1)
+            {
+                return false;
+            }
+
+            var n = TypeConverter.ToNumber(arguments[0]);
+            if (n == double.NaN || n == double.PositiveInfinity || n == double.NegativeInfinity)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.1
+        /// </summary>
+        /// <param name="thisObject"></param>
+        /// <param name="arguments"></param>
+        /// <returns></returns>
+        public static string DecodeUri(object thisObject, object[] arguments)
+        {
+            if (arguments.Length < 1 || arguments[0] == Undefined.Instance)
+            {
+                return "";
+            }
+
+            return Uri.UnescapeDataString(arguments[0].ToString().Replace("+", " "));
+        }
+
+        private static readonly char[] ReservedEncoded = new [] { ';', ',', '/', '?', ':', '@', '&', '=', '+', '$', '#' };
+        private static readonly char[] ReservedEncodedComponent = new [] { '-', '_', '.', '!', '~', '*', '\'', '(', ')', '[', ']' };
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.2
+        /// </summary>
+        /// <param name="thisObject"></param>
+        /// <param name="arguments"></param>
+        /// <returns></returns>
+        public static string EncodeUri(object thisObject, object[] arguments)
+        {
+            if (arguments.Length < 1 || arguments[0] == Undefined.Instance)
+            {
+                return "";
+            }
+
+            string encoded = Uri.EscapeDataString(arguments[0].ToString());
+
+            foreach (char c in ReservedEncoded)
+            {
+                encoded = encoded.Replace(Uri.EscapeDataString(c.ToString()), c.ToString());
+            }
+
+            foreach (char c in ReservedEncodedComponent)
+            {
+                encoded = encoded.Replace(Uri.EscapeDataString(c.ToString()), c.ToString());
+            }
+
+            return encoded.ToUpper();
+        }
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.3
+        /// </summary>
+        /// <param name="thisObject"></param>
+        /// <param name="arguments"></param>
+        /// <returns></returns>
+        public static string DecodeUriComponent(object thisObject, object[] arguments)
+        {
+            if (arguments.Length < 1 || arguments[0] == Undefined.Instance)
+            {
+                return "";
+            }
+
+            return Uri.UnescapeDataString(arguments[0].ToString().Replace("+", " "));
+        }
+
+        /// <summary>
+        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
+        /// </summary>
+        /// <param name="thisObject"></param>
+        /// <param name="arguments"></param>
+        /// <returns></returns>
+        public static string EncodeUriComponent(object thisObject, object[] arguments)
+        {
+            if (arguments.Length < 1 || arguments[0] == Undefined.Instance)
+            {
+                return "";
+            }
+
+            string encoded = Uri.EscapeDataString(arguments[0].ToString());
+
+            foreach (char c in ReservedEncodedComponent)
+            {
+                encoded = encoded.Replace(Uri.EscapeDataString(c.ToString()), c.ToString().ToUpper());
+            }
+
+            return encoded;
+        }
+
+
     }
 }

+ 9 - 0
Jint/Native/Math/MathInstance.cs

@@ -45,6 +45,15 @@ namespace Jint.Native.Math
             math.DefineOwnProperty("sqrt", new ClrDataDescriptor<MathInstance, double>(engine, Sqrt), false);
             math.DefineOwnProperty("tan", new ClrDataDescriptor<MathInstance, double>(engine, Tan), false);
 
+            math.FastAddProperty("E", System.Math.E, false, false, false);
+            math.FastAddProperty("LN10", System.Math.Log(10), false, false, false);
+            math.FastAddProperty("LN2", System.Math.Log(2), false, false, false);
+            math.FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
+            math.FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
+            math.FastAddProperty("PI", System.Math.PI, false, false, false);
+            math.FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
+            math.FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
+
             return math;
         }