The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ Runtime

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.     using System;
  18.     using System.Threading;
  19.     using System.Reflection;
  20.     using System.Reflection.Emit;
  21.     using System.Security;
  22.     using System.Security.Permissions;
  23.     using Microsoft.JScript.Vsa;
  24.    
  25.     public static class Runtime
  26.     {
  27.         // *** VS58565 - JSEE: Remove all uses of IDebugObject::GetManagedObject from the code ***
  28.         // JSEE does a funceval to this method to compare if to value types are equal.
  29.         public static new bool Equals(object v1, object v2)
  30.         {
  31.             Equality e = new Equality((int)JSToken.Equal);
  32.             return e.EvaluateEquality(v1, v2);
  33.         }
  34.        
  35.        
  36.         //
  37.         public static Int64 DoubleToInt64(double val)
  38.         {
  39.             if (Double.IsNaN(val)) {
  40.                 // Take care of NaN case first as it isn't ordered.
  41.                 return 0;
  42.             }
  43.             if (Int64.MinValue <= val && val <= Int64.MaxValue) {
  44.                 return (Int64)val;
  45.             }
  46.            
  47.             // Remove cases where IEEERemainder will return NaN.
  48.             if (Double.IsInfinity(val))
  49.                 return 0;
  50.            
  51.             // Definition of IEEERemandier returns:
  52.             // A number equal to x-(y*Q), where Q is the quotient of x/y rounded to the nearest
  53.             // integer (if x/y falls halfway between two integers, the even integer is returned).
  54.             //
  55.             // We use y = 2^64 = UInt64.MaxValue+1. The resulting remainder may range from -2^63
  56.             // to 2^63. i.e. x = (2^64+2^63) gives Q = 2 and result -2^63, x = -(2^64+2^63) gives
  57.             // Q = -2 and result 2^63. To fit into Int64, we translate 2^63 by -2^64 to get -2^63
  58.             // which is Int64.MinValue.
  59.             double rem = Math.IEEERemainder(Math.Sign(val) * Math.Floor(Math.Abs(val)), (double)UInt64.MaxValue + 1.0);
  60.             if (rem == (double)Int64.MaxValue + 1.0) {
  61.                 return Int64.MinValue;
  62.             }
  63.             return (Int64)rem;
  64.         }
  65.        
  66.        
  67.         private const decimal DecimalTwoToThe64 = (decimal)UInt64.MaxValue + 1;
  68.        
  69.         public static Int64 UncheckedDecimalToInt64(decimal val)
  70.         {
  71.             // ToInteger
  72.             val = Decimal.Truncate(val);
  73.            
  74.             // Ensure value is in range.
  75.             if (val < Int64.MinValue || Int64.MaxValue < val) {
  76.                 val = Decimal.Remainder(val, DecimalTwoToThe64);
  77.                 if (val < Int64.MinValue)
  78.                     val += DecimalTwoToThe64;
  79.                 else if (val > Int64.MaxValue)
  80.                     val -= DecimalTwoToThe64;
  81.             }
  82.            
  83.             // Checked coercion is guaranteed to succeed
  84.             Debug.Assert(Int64.MinValue <= val && val <= Int64.MaxValue);
  85.             return (Int64)val;
  86.         }
  87.        
  88.        
  89.         // RUNTIME TYPEREFERENCES
  90.         private static TypeReferences _typeRefs;
  91.         static internal TypeReferences TypeRefs {
  92.             get {
  93.                 TypeReferences typeRefs = Runtime._typeRefs;
  94.                 if (typeRefs == null)
  95.                     typeRefs = Runtime._typeRefs = new TypeReferences(typeof(Runtime).Module);
  96.                 return typeRefs;
  97.             }
  98.         }
  99.        
  100.         // ASSEMBLY AND MODULE BUILDER FOR FIELDACCESSOR AND METHODINVOKER THUNKS
  101.         private static ModuleBuilder _thunkModuleBuilder;
  102.         static internal ModuleBuilder ThunkModuleBuilder {
  103.             get {
  104.                 ModuleBuilder moduleBuilder = Runtime._thunkModuleBuilder;
  105.                 if (null == moduleBuilder)
  106.                     moduleBuilder = Runtime._thunkModuleBuilder = CreateThunkModuleBuilder();
  107.                 return moduleBuilder;
  108.             }
  109.         }
  110.        
  111.         [ReflectionPermission(SecurityAction.Assert, ReflectionEmit = true), FileIOPermission(SecurityAction.Assert, Unrestricted = true)]
  112.         private static ModuleBuilder CreateThunkModuleBuilder()
  113.         {
  114.             AssemblyName name = new AssemblyName();
  115.             name.Name = "JScript Thunk Assembly";
  116.             AssemblyBuilder assembly = Thread.GetDomain().DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
  117.             ModuleBuilder module = assembly.DefineDynamicModule("JScript Thunk Module");
  118.             module.SetCustomAttribute(new CustomAttributeBuilder(typeof(SecurityTransparentAttribute).GetConstructor(new Type[0]), new object[0]));
  119.             return module;
  120.         }
  121.     }
  122. }

Developer Fusion