本文整理汇总了C#中IEnvironment.CreateDataDescriptor方法的典型用法代码示例。如果您正苦于以下问题:C# IEnvironment.CreateDataDescriptor方法的具体用法?C# IEnvironment.CreateDataDescriptor怎么用?C# IEnvironment.CreateDataDescriptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnvironment
的用法示例。
在下文中一共展示了IEnvironment.CreateDataDescriptor方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Construct
public override sealed IObject Construct(IEnvironment environment, IArgs args)
{
var patternArg = args[0];
var flagsArg = args[1];
var pattern = "";
var flags = "";
bool global, ignoreCase, multiline;
var patternObject = patternArg as IObject;
if (patternObject != null && patternObject.Class == "RegExp")
{
if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
{
throw environment.CreateTypeError("");
}
global = ((IBoolean)patternObject.Get("global")).BaseValue;
ignoreCase = ((IBoolean)patternObject.Get("ignoreCase")).BaseValue;
multiline = ((IBoolean)patternObject.Get("multiline")).BaseValue;
pattern = ((IString)patternObject.Get("source")).BaseValue;
if (global) flags += "g";
if (ignoreCase) flags += "i";
if (multiline) flags += "m";
}
else
{
if (patternArg.TypeCode != LanguageTypeCode.Undefined)
{
pattern = patternArg.ConvertToString().BaseValue;
}
if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
{
flags = flagsArg.ConvertToString().BaseValue;
}
global = flags.Contains("g");
ignoreCase = flags.Contains("i");
multiline = flags.Contains("m");
}
var regExpObj = new NRegExp(environment);
regExpObj.Class = "RegExp";
regExpObj.Extensible = true;
regExpObj.Prototype = environment.RegExpPrototype;
regExpObj.Body = pattern;
regExpObj.Flags = flags;
regExpObj.RegExpMatcher = Machete.Compiler.RegExpParser.Parse(Environment, pattern, flags);
regExpObj.DefineOwnProperty("source", environment.CreateDataDescriptor(environment.CreateString(pattern), false, false, false), false);
regExpObj.DefineOwnProperty("global", environment.CreateDataDescriptor(environment.CreateBoolean(global), false, false, false), false);
regExpObj.DefineOwnProperty("ignoreCase", environment.CreateDataDescriptor(environment.CreateBoolean(ignoreCase), false, false, false), false);
regExpObj.DefineOwnProperty("multiline", environment.CreateDataDescriptor(environment.CreateBoolean(multiline), false, false, false), false);
regExpObj.DefineOwnProperty("lastIndex", environment.CreateDataDescriptor(environment.CreateNumber(0.0), true, false, false), false);
return regExpObj;
}
示例2: Construct
public override sealed IObject Construct(IEnvironment environment, IArgs args)
{
var obj = new NString(environment);
{
var str = args.Count > 0 ? args[0].ConvertToString() : environment.CreateString("");
var len = environment.CreateNumber(str.BaseValue.Length);
var lenDesc = environment.CreateDataDescriptor(len, false, false, false);
obj.Class = "String";
obj.Extensible = true;
obj.Prototype = environment.StringPrototype;
obj.PrimitiveValue = str;
obj.DefineOwnProperty("length", lenDesc, false);
}
return obj;
}
示例3: Split
IDynamic Split(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding;
environment.CheckObjectCoercible(o);
var s = o.ConvertToString().BaseValue;
var separatorArg = args[0];
var limitArg = args[1];
var array = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
int limit = 0, lower = 0, upper = 0, count = 0;
IPropertyDescriptor desc;
IString value;
// Using int32 instead of uint32 due to limitations of .NET arrays.
limit = limitArg is IUndefined ? int.MaxValue : (int)limitArg.ConvertToUInt32().BaseValue;
if (limit == 0)
{
return array;
}
var separatorObj = separatorArg as NRegExp;
if (separatorObj != null)
{
do
{
var result = separatorObj.RegExpMatcher(s, upper);
if (!result.success)
{
upper++;
}
else
{
upper = result.matchState.endIndex;
value = environment.CreateString(s.Substring(lower, upper - lower - 1));
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty(count.ToString(), desc, true);
if (++count == limit) return array;
var captures = result.matchState.captures;
for (int i = 0; i < captures.Length; i++)
{
value = environment.CreateString(captures[i]);
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty(count.ToString(), desc, true);
if (++count == limit) return array;
}
lower = upper;
}
} while (upper < s.Length);
value = environment.CreateString(s.Substring(lower));
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty(count.ToString(), desc, true);
}
else
{
var separatorStr = ((IString)separatorArg).BaseValue;
var pieces = s.Split(new[] { separatorStr }, StringSplitOptions.None);
for (int i = 0; i < pieces.Length; i++)
{
value = environment.CreateString(pieces[i]);
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty(i.ToString(), desc, true);
if (++count == limit) return array;
}
}
return array;
}
示例4: Match
IDynamic Match(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding;
environment.CheckObjectCoercible(o);
var dynS = o.ConvertToString();
var s = dynS.BaseValue;
var regexpArg = args[0];
var regexpObj = regexpArg as NRegExp;
if (regexpObj == null)
{
var constructor = (IConstructable)environment.RegExpConstructor;
var pattern = regexpArg.ConvertToString();
regexpObj = (NRegExp)constructor.Construct(environment, environment.CreateArgs(new [] { pattern }));
}
var exec = ((ICallable)regexpObj.Get("exec"));
var execArgs = environment.CreateArgs(new [] { dynS });
if (!regexpObj.Get("global").ConvertToBoolean().BaseValue)
{
return exec.Call(environment, regexpObj, execArgs);
}
else
{
var constructor = (IConstructable)environment.ArrayConstructor;
var array = constructor.Construct(environment, environment.EmptyArgs);
var previousLastIndex = 0;
var lastMatch = true;
var n = 0;
while (lastMatch)
{
var result = exec.Call(environment, regexpObj, execArgs);
switch (result.TypeCode)
{
case LanguageTypeCode.Null:
lastMatch = false;
break;
default:
var thisIndex = (int)regexpObj.Get("lastIndex").ConvertToNumber().BaseValue;
if (thisIndex == previousLastIndex)
{
thisIndex += 1;
regexpObj.Put("lastIndex", environment.CreateNumber(thisIndex), false);
previousLastIndex = thisIndex;
}
else
{
previousLastIndex = thisIndex;
}
var matchStr = ((IObject)result).Get("0");
var desc = environment.CreateDataDescriptor(matchStr, true, true, true);
array.DefineOwnProperty(n.ToString(), desc, true);
n++;
break;
}
}
if (n == 0)
{
return environment.Null;
}
return array;
}
}
示例5: Exec
IDynamic Exec(IEnvironment environment, IArgs args)
{
var r = (NRegExp)environment.Context.ThisBinding;
var s = args[0].ConvertToString().BaseValue;
var length = s.Length;
var global = r.Get("global").ConvertToBoolean().BaseValue;
var i = !global ? 0 : (int)r.Get("lastIndex").ConvertToInteger().BaseValue;
int beginIndex = 0;
MatchResult result;
while (true)
{
beginIndex = i;
if (i < 0 || i > length)
{
r.Put("length", environment.CreateNumber(0.0), true);
return environment.Null;
}
result = r.RegExpMatcher(s, i);
if (result.success) break;
i++;
}
if (global)
{
r.Put("length", environment.CreateNumber(result.matchState.endIndex), true);
}
var captures = result.matchState.captures;
var n = captures.Length;
var array = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
array.DefineOwnProperty("index", environment.CreateDataDescriptor(environment.CreateNumber(beginIndex), true, true, true), true);
array.DefineOwnProperty("input", environment.CreateDataDescriptor(environment.CreateString(s), true, true, true), true);
array.DefineOwnProperty("length", environment.CreateDataDescriptor(environment.CreateNumber(n + 1), null, null, null), true);
IDynamic value = environment.CreateString(result.matchState.input.Substring(beginIndex, result.matchState.endIndex - beginIndex));
var desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty("0", desc, true);
for (int index = 0; index < n; index++)
{
var v = captures[index];
value = v == null ? (IDynamic)environment.Undefined : (IDynamic)environment.CreateString(v);
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty((index + 1).ToString(), desc, true);
}
return array;
}
示例6: Bind
private static IDynamic Bind(IEnvironment environment, IArgs args)
{
var callable = environment.Context.ThisBinding as ICallable;
if (callable == null)
{
throw environment.CreateTypeError("");
}
var target = (IObject)callable;
var thisArg = args[0];
var callArgs = environment.EmptyArgs;
var func = new NBoundFunction(environment);
if (args.Count > 1)
{
callArgs = environment.CreateArgs(args.Skip(1));
}
func.Class = "Function";
func.Extensible = true;
func.Prototype = environment.FunctionPrototype;
func.TargetFunction = target;
func.BoundThis = thisArg;
func.BoundArguments = callArgs;
var length = 0.0;
if (target.Class == "Function")
{
length = callArgs.Count - target.Get("length").ConvertToUInt32().BaseValue;
}
var lengthNum = environment.CreateNumber(length);
var thrower = environment.ThrowTypeErrorFunction;
var desc = environment.CreateDataDescriptor(lengthNum, false, false, false);
func.DefineOwnProperty("length", desc, false);
desc = environment.CreateAccessorDescriptor(thrower, thrower, false, false);
func.DefineOwnProperty("caller", desc, false);
func.DefineOwnProperty("arguments", desc, false);
return func;
}
示例7: Splice
private static IDynamic Splice(IEnvironment environment, IArgs args)
{
var obj = environment.Context.ThisBinding.ConvertToObject();
var a = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
var len = (uint)obj.Get("length").ConvertToUInt32().BaseValue;
var start = args[0].ConvertToInteger().BaseValue;
var deleteCount = args[1].ConvertToInteger().BaseValue;
var actualStart = start < 0 ? (int)Math.Max(len + start, 0) : (int)Math.Min(start, len);
var actualDeleteCount = (int)Math.Min(Math.Max(deleteCount, 0), len - actualStart);
for (var k = 0; k < actualDeleteCount; k++)
{
var from = (actualStart + k).ToString();
if (obj.HasProperty(from))
{
var fromValue = obj.Get(from);
var desc = environment.CreateDataDescriptor(fromValue, true, true, true);
var key = k.ToString();
a.DefineOwnProperty(key, desc, false);
}
}
var itemCount = 0;
if (args.Count > 2)
{
itemCount = args.Count;
}
if (itemCount < actualDeleteCount)
{
var limit = len - actualDeleteCount;
for (var k = 0; k < limit; k++)
{
var from = (k + actualDeleteCount).ToString();
var to = (k + itemCount).ToString();
if (!obj.HasProperty(from))
{
obj.Delete(to, true);
}
else
{
var fromValue = obj.Get(from);
obj.Put(to, fromValue, true);
}
}
var deleteLimit = len - actualDeleteCount + itemCount;
for (var k = len; k > deleteLimit; k--)
{
obj.Delete((k - 1).ToString(), true);
}
}
else if (itemCount > actualDeleteCount)
{
for (var k = len - actualDeleteCount; k > actualStart; k--)
{
var from = (k + actualDeleteCount - 1).ToString();
var to = (k + itemCount - 1).ToString();
if (!obj.HasProperty(from))
{
obj.Delete(to, true);
}
else
{
var fromValue = obj.Get(from);
obj.Put(to, fromValue, true);
}
}
}
for (int k = actualStart, i = 2; i < args.Count; k++, i++)
{
obj.Put(k.ToString(), args[i], true);
}
return a;
}
示例8: Slice
private static IDynamic Slice(IEnvironment environment, IArgs args)
{
var result = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
var obj = environment.Context.ThisBinding.ConvertToObject();
var length = (uint)obj.Get("length").ConvertToNumber().BaseValue;
var start = Math.Floor(args[0].ConvertToNumber().BaseValue);
var end = args[1] is IUndefined ? length : Math.Floor(args[1].ConvertToNumber().BaseValue);
var currentIndex = (int)(start < 0 ? Math.Max(length + start, 0) : Math.Min(start, length)) - 1;
var endIndex = (int)(end < 0 ? Math.Max(length + end, 0) : Math.Min(end, length));
var splicedIndex = 0;
while (++currentIndex < endIndex)
{
if (obj.HasProperty(currentIndex.ToString()))
{
var key = currentIndex.ToString();
var value = obj.Get(key);
var desc = environment.CreateDataDescriptor(value, true, true,true);
result.DefineOwnProperty((splicedIndex++).ToString(), desc, false);
}
}
return result;
}
示例9: Map
private static IDynamic Map(IEnvironment environment, IArgs args)
{
var result = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
var obj = environment.Context.ThisBinding.ConvertToObject();
var callArgs = new IDynamic[] { null, null, obj };
var length = (uint)obj.Get("length").ConvertToUInt32().BaseValue;
var callbackfn = args[0] as ICallable;
var thisArg = args[1];
if (callbackfn == null)
{
throw environment.CreateTypeError("The first parameter 'callbackfn' must be callable.");
}
for (uint i = 0; i < length; i++)
{
var key = i.ToString();
if (obj.HasProperty(key))
{
callArgs[0] = obj.Get(key);
callArgs[1] = environment.CreateNumber(i);
var value = callbackfn.Call(environment, thisArg, environment.CreateArgs(callArgs));
result.DefineOwnProperty(key, environment.CreateDataDescriptor(value, true, true, true), false);
}
}
return result;
}
示例10: Filter
private static IDynamic Filter(IEnvironment environment, IArgs args)
{
var result = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
var obj = environment.Context.ThisBinding.ConvertToObject();
var length = (uint)obj.Get("length").ConvertToUInt32().BaseValue;
var callbackfn = args[0] as ICallable;
var thisArg = args[1];
if (callbackfn == null)
throw environment.CreateTypeError("");
for (uint i = 0; i < length; i++)
{
var key = i.ToString();
if (obj.HasProperty(key))
{
var value = obj.Get(key);
var callArgs = environment.CreateArgs(new[] { value, environment.CreateNumber(i), obj });
var include = callbackfn.Call(environment, thisArg, callArgs);
if (include.ConvertToBoolean().BaseValue)
{
result.DefineOwnProperty(key, environment.CreateDataDescriptor(value, true, true, true), false);
}
}
}
return result;
}
示例11: Concat
private static IDynamic Concat(IEnvironment environment, IArgs args)
{
var obj = environment.Context.ThisBinding.ConvertToObject();
var array = environment.ArrayConstructor.Op_Construct(environment.EmptyArgs);
var current = default(IDynamic);
var count = 0;
for (int i = -1; i < args.Count; i++)
{
current = i < 0 ? obj : args[i];
if (current.TypeCode != LanguageTypeCode.Object)
{
var desc = environment.CreateDataDescriptor(current, true, true, true);
array.DefineOwnProperty((count++).ToString(), desc, false);
}
else
{
var inner = current as NArray;
if (inner != null)
{
var length = (uint)inner.Get("length").ConvertToUInt32().BaseValue;
for (int j = 0; j < length; j++)
{
var key = j.ToString();
if (inner.HasProperty(key))
{
var desc = environment.CreateDataDescriptor(inner.Get(key), true, true, true);
array.DefineOwnProperty((count++).ToString(), desc, false);
}
}
}
}
}
return array;
}
示例12: Keys
internal static IDynamic Keys(IEnvironment environment, IArgs args)
{
if (args[0].TypeCode != LanguageTypeCode.Object)
{
throw environment.CreateTypeError("");
}
var obj = args[0].ConvertToObject();
var array = environment.CreateArray();
var index = 0;
foreach (var name in obj)
{
var desc = obj.GetOwnProperty(name);
if (desc.Enumerable ?? false)
{
desc = environment.CreateDataDescriptor(environment.CreateString(name), true, true, true);
array.DefineOwnProperty((index++).ToString(), desc, false);
}
}
return array;
}