当前位置: 首页>>代码示例>>C#>>正文


C# mdr.SetField方法代码示例

本文整理汇总了C#中mdr.SetField方法的典型用法代码示例。如果您正苦于以下问题:C# mdr.SetField方法的具体用法?C# mdr.SetField怎么用?C# mdr.SetField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mdr的用法示例。


在下文中一共展示了mdr.SetField方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateArgumentsObject

    public static mdr.DArray CreateArgumentsObject(ref mdr.CallFrame callFrame, mdr.DObject context)
    {
      var metadata = (JSFunctionMetadata)callFrame.Function.Metadata;
      Debug.Assert(metadata.Scope.HasArgumentsSymbol, "Invalid situation, created arguments for the wrong scope!");
      mdr.DArray arguments = null;
      if (metadata.Scope.IsEvalFunction)
      {
        //Read from context
        var tmp = new mdr.DValue();
        context.GetField(JSFunctionArguments.Name, ref tmp);
        arguments = tmp.AsDArray();
      }
      else
      {
        arguments = CreateArgumentsObject(ref callFrame);
        var parameters = metadata.FunctionIR.Parameters;
        Debug.Assert(arguments.Length >= parameters.Count, "arguments array is not large enough to hold all arguments.");
        for (var i = parameters.Count - 1; i >= 0; --i)
        {
          var symbol = parameters[i].Symbol;
          var paramIndex = symbol.ParameterIndex;
          Debug.Assert(paramIndex == i, "Invalid situation!, Parameter indexes don't match!");

          if (symbol.SymbolType == JSSymbol.SymbolTypes.ClosedOnLocal)
          {
            var pd = context.AddOwnPropertyDescriptorByFieldId(symbol.FieldId, mdr.PropertyDescriptor.Attributes.Accessor | mdr.PropertyDescriptor.Attributes.NotConfigurable);
            context.Fields[pd.Index].Set(new ArgumentAccessor(arguments, paramIndex));
          }
        }
        if (metadata.Scope.HasEval)
          context.SetField(JSFunctionArguments.Name, arguments);
      }
      return arguments;
    }
开发者ID:reshadi2,项目名称:mcjs,代码行数:34,代码来源:JSFunctionArguments.cs

示例2: AddExtendedMethods

        private static void AddExtendedMethods(mdr.DObject obj)
        {
            #region Mozilla intrinsics
            obj.SetField("assertTrue", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                int argsLen = callFrame.PassedArgsCount;
                if (argsLen > 0)
                {
                    var b = Operations.Convert.ToBoolean.Run(ref callFrame.Arg0);
                    assert(b, argsLen > 1 ? callFrame.Arg1.AsString() : null);
                }
                else
                    Trace.Fail("Not enough arguments");
            }));

            obj.SetField("assertFalse", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                int argsLen = callFrame.PassedArgsCount;
                if (argsLen > 0)
                {
                    var b = Operations.Convert.ToBoolean.Run(ref callFrame.Arg0);
                    assert(!b, argsLen > 1 ? callFrame.Arg1.AsString() : null);
                }
                else
                    Trace.Fail("Not enough arguments");
            }));

            obj.SetField("assertEquals", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                int argsLen = callFrame.PassedArgsCount;
                if (argsLen > 1)
                {
                    bool b = Operations.Binary.Equal.Run(ref callFrame.Arg0, ref callFrame.Arg1);
                    assert(b, argsLen > 2 ? callFrame.Arg2.AsString() : null);
                }
                else
                    Trace.Fail("Not enough arguments");
            }));

            obj.SetField("assertArrayEquals", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                int argsLen = callFrame.PassedArgsCount;
                if (argsLen > 1)
                {
                    var arrayA = callFrame.Arg0.AsDArray();
                    var arrayB = callFrame.Arg1.AsDArray();
                    bool areEqual = true;
                    if (arrayA != null && arrayB != null && arrayA.Length == arrayB.Length)
                    {
                        for (int i = 0; i < arrayA.Length; i++)
                            if (Operations.Binary.Equal.Run(ref arrayA.Elements[i], ref arrayB.Elements[i]))
                            {
                                areEqual = false;
                                break;
                            }
                    }
                    else if (arrayA != arrayB)
                        areEqual = false;
                    assert(areEqual, argsLen > 2 ? callFrame.Arg2.AsString() : null);
                }
                else
                    Trace.Fail("Not enough arguments");
            }));
            #endregion

            // FIXME: The below causes an infinite recursion in CodeSourceGenerator. Commenting for now. - SF
            //SetField("global", this); //to enable access to global scope directly!
            obj.SetField("print", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                var l = callFrame.PassedArgsCount;
                for (var i = 0; i < l; ++i)
                {
                    var arg = callFrame.Arg(i);
                    string s = ToString(ref arg);
                    Console.Write("{0}{1}", s, (i < l - 1) ? " " : "");

                }
                Console.WriteLine();
            }));

            obj.SetField("load", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                var l = callFrame.PassedArgsCount;
                if (l < 1)
                    throw new Exception("load must have an argument");
                var filename = callFrame.Arg0.AsString();
                JSRuntime.Instance.RunScriptFile(filename);
            }));

            #region __mcjs__ object
            {
                var mcjs = new mdr.DObject(mdr.Runtime.Instance.EmptyPropertyMapMetadata.Root);
                obj.SetField("__mcjs__", mcjs);
                mcjs.SetField("SetSwitch", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
                {
                    var switchName = callFrame.Arg0.AsString();
                    var switchValue = Operations.Convert.ToBoolean.Run(ref callFrame.Arg1);
                    var prop = typeof(JSRuntimeConfiguration).GetProperty(switchName, CodeGen.Types.ClrSys.Boolean);
                    if (prop != null)
                        prop.GetSetMethod().Invoke(JSRuntime.Instance.Configuration, new object[] { switchValue });
//.........这里部分代码省略.........
开发者ID:reshadi2,项目名称:mcjs,代码行数:101,代码来源:JSGlobalObject.cs

示例3: Init

        internal static void Init(mdr.DObject obj)
        {

            obj.SetField("global", obj);
            //obj.SetField("null", mdr.Runtime.Instance.DefaultDNull);
            obj.DefineOwnProperty("undefined", mdr.Runtime.Instance.DefaultDUndefined, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable);
            obj.DefineOwnProperty("NaN", double.NaN, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable);
            obj.DefineOwnProperty("Infinity", double.PositiveInfinity, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable);

            obj.SetField("Object", new JSObject());
            obj.SetField("Function", new JSFunction());
            obj.SetField("Array", new JSArray());
            obj.SetField("ArrayBuffer", new JSArrayBuffer());
            obj.SetField("Int8Array", new JSInt8Array());
            obj.SetField("Uint8Array", new JSUint8Array());
            obj.SetField("Int16Array", new JSInt16Array());
            obj.SetField("Uint16Array", new JSUint16Array());
            obj.SetField("Int32Array", new JSInt32Array());
            obj.SetField("Uint32Array", new JSUint32Array());
            obj.SetField("Float32Array", new JSFloat32Array());
            obj.SetField("Float64Array", new JSFloat64Array());

            obj.SetField("Math", new JSMath());
            obj.SetField("String", new JSString());
            obj.SetField("Number", new JSNumber());
            obj.SetField("Date", new JSDate());
            obj.SetField("Boolean", new JSBoolean());
            obj.SetField("Error", new JSError());
            obj.SetField("RegExp", new JSRegExp());

            obj.SetField("eval", BuiltinEval);

            AddStandardMethods(obj);
            AddExtendedMethods(obj);
        }
开发者ID:reshadi2,项目名称:mcjs,代码行数:35,代码来源:JSGlobalObject.cs

示例4: AddStandardMethods

        // TODO: These are currently unused; can we remove them?
        /*private static char[] reservedEncoded = new char[] { ';', ',', '/', '?', ':', '@', '&', '=', '+', '$', '#' };
        private static char[] reservedEncodedComponent = new char[] { '-', '_', '.', '!', '~', '*', '\'', '(', ')', '[', ']' };*/

        private static void AddStandardMethods(mdr.DObject obj)
        {
            obj.SetField("ToNumber", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                if (callFrame.PassedArgsCount > 0)
                    callFrame.Return.Set(Operations.Convert.ToDouble.Run(ref callFrame.Arg0));
                else
                    callFrame.Return.Set(double.NaN);
            }));

            obj.SetField("isNaN", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                if (callFrame.PassedArgsCount > 0)
                    callFrame.Return.Set(double.IsNaN(Operations.Convert.ToDouble.Run(ref callFrame.Arg0)));
                else
                    callFrame.Return.Set(false);
            }));

            obj.SetField("isFinite", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                if (callFrame.PassedArgsCount > 0)
                {
                    double arg = Operations.Convert.ToDouble.Run(ref callFrame.Arg0);
                    callFrame.Return.Set(double.IsNaN(arg) || !double.IsInfinity(arg));
                }
                else
                    callFrame.Return.Set(false);
            }));

            obj.SetField("assert", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                for (int i = 0; i < callFrame.PassedArgsCount; ++i)
                {
                  var b = Operations.Convert.ToBoolean.Run(ref callFrame.Arg0);
                    if (!b)
                        throw new Exception("Error in script");
                }
            }));

            //ECMA-262: 15.1.2.2
            obj.SetField("parseInt", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
              if (callFrame.PassedArgsCount == 1 && mdr.ValueTypesHelper.IsNumber(callFrame.Arg0.ValueType))
                callFrame.Return.Set(Operations.Convert.ToInt32.Run(ref callFrame.Arg0));
              else
              {
                string stringArg = Operations.Convert.ToString.Run(ref callFrame.Arg0).TrimStart();
                int sign = 1;
                if (stringArg[0] == '-' || stringArg[0] == '+')
                {
                  if (stringArg[0] == '-')
                    sign = -1;
                  stringArg = stringArg.Remove(0, 1);
                }

                bool stripPrefix = true;
                int radix = 10;
                if (callFrame.PassedArgsCount > 1)
                {
                  radix = Operations.Convert.ToInt32.Run(ref callFrame.Arg1);
                  if (radix != 0)
                  {
                    if (radix < 2 || radix > 36)
                    {
                      callFrame.Return.Set(double.NaN);
                      return;
                    }
                    if (radix != 16)
                      stripPrefix = false;
                  }
                  else
                    radix = 10;
                }

                if (stripPrefix && (stringArg.StartsWith("0x") || stringArg.StartsWith("0X")))
                {
                  stringArg = stringArg.Remove(0, 2);
                  radix = 16;
                }

                callFrame.Return.Set(MathInt(stringArg, radix) * sign);
              }
            }));

            //ECMA-262: 15.1.2.3
            obj.SetField("parseFloat", new mdr.DFunction((ref mdr.CallFrame callFrame) =>
            {
                string stringArg = (Operations.Convert.ToString.Run(ref callFrame.Arg0)).TrimStart();
                char[] charArg = stringArg.TrimEnd().ToCharArray();
                int len = charArg.Length;

                if (len == 0)
                {
                    callFrame.Return.Set(double.NaN);
                    return;
                }
//.........这里部分代码省略.........
开发者ID:reshadi2,项目名称:mcjs,代码行数:101,代码来源:JSGlobalObject.cs


注:本文中的mdr.SetField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。