本文整理汇总了C#中IEnvironment.CreateTypeError方法的典型用法代码示例。如果您正苦于以下问题:C# IEnvironment.CreateTypeError方法的具体用法?C# IEnvironment.CreateTypeError怎么用?C# IEnvironment.CreateTypeError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnvironment
的用法示例。
在下文中一共展示了IEnvironment.CreateTypeError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Filter
internal static IDynamic Filter(IEnvironment environment, IArgs args)
{
if (args.Count < 2)
throw environment.CreateTypeError("The arguments 'iterable' and 'predicate' are required.");
var iterable = args[1].ConvertToObject();
var predicate = args[0].ConvertToObject() as ICallable;
if (predicate == null)
throw environment.CreateTypeError("The argument 'predicate' must be a callable function.");
return new HFilterIterable(environment, iterable, predicate);
}
示例2: Iterator
public Iterator(IEnvironment environment, IDynamic iterable)
{
_environment = environment;
var o = iterable.ConvertToObject();
var createIterator = o.Get("createIterator") as ICallable;
if (createIterator == null)
throw environment.CreateTypeError("The object supplied does not contain a callable property named 'createIterator'.");
_iterator = createIterator.Call(environment, iterable, environment.EmptyArgs).ConvertToObject();
if (!_iterator.HasProperty("current"))
throw environment.CreateTypeError("The object returned from the iterable supplied does not have a property named 'current'.");
_next = _iterator.Get("next") as ICallable;
if (_next == null)
throw environment.CreateTypeError("The object returned from the iterable supplied does not have a callable property named 'next'.");
}
示例3: 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;
}
示例4: Iterate
internal static IDynamic Iterate(IEnvironment environment, IArgs args)
{
var iterator = new Iterator(environment, args[1]);
var callback = args[0] as ICallable;
if (callback == null)
throw environment.CreateTypeError("The argument 'callback' must be a callable function.");
while (iterator.Next())
{
var callArgs = environment.CreateArgs(new[] { iterator.Current });
callback.Call(environment, environment.Undefined, callArgs);
}
return environment.Undefined;
}
示例5: DefineProperties
internal static IDynamic DefineProperties(IEnvironment environment, IArgs args)
{
if (args[0].TypeCode != LanguageTypeCode.Object)
{
throw environment.CreateTypeError("");
}
var obj = args[0].ConvertToObject();
var props = args[1].ConvertToObject();
foreach (var name in props)
{
var desc = environment.ToPropertyDescriptor(props.Get(name).ConvertToObject());
obj.DefineOwnProperty(name, desc, true);
}
return obj;
}
示例6: ToString
private static IDynamic ToString(IEnvironment environment, IArgs args)
{
var v = environment.Context.ThisBinding;
switch (v.TypeCode)
{
case LanguageTypeCode.Boolean:
return environment.CreateString(((IBoolean)v).BaseValue ? "true" : "false");
case LanguageTypeCode.Object:
var o = (IObject)v;
if (o.Class == "Boolean")
{
return environment.CreateString(((IBoolean)((IPrimitiveWrapper)o).PrimitiveValue).BaseValue ? "true" : "false");
}
break;
}
throw environment.CreateTypeError("");
}
示例7: Create
internal static IDynamic Create(IEnvironment environment, IArgs args)
{
if (args[0].TypeCode != LanguageTypeCode.Object)
{
throw environment.CreateTypeError("");
}
var obj = args[0].ConvertToObject();
var newObj = environment.CreateObject();
newObj.Prototype = obj;
if (args.Count > 1)
{
var props = args[1];
if (props.TypeCode != LanguageTypeCode.Undefined)
{
return DefineProperties(environment, environment.CreateArgs(new [] { newObj, props }));
}
}
return newObj;
}
示例8: Apply
private static IDynamic Apply(IEnvironment environment, IArgs args)
{
var callable = environment.Context.ThisBinding as ICallable;
if (callable == null)
{
throw environment.CreateTypeError("");
}
var thisArg = args[0];
var argArray = args[1];
switch (argArray.TypeCode)
{
case LanguageTypeCode.Undefined:
case LanguageTypeCode.Null:
return callable.Call(environment, thisArg, environment.EmptyArgs);
case LanguageTypeCode.Object:
break;
default:
throw environment.CreateTypeError("");
}
var argList = new List<IDynamic>();
var argArrayObj = argArray.ConvertToObject();
var len = argArrayObj.Get("length");
switch (len.TypeCode)
{
case LanguageTypeCode.Undefined:
case LanguageTypeCode.Null:
throw environment.CreateTypeError("");
}
var n = (uint)len.ConvertToUInt32().BaseValue;
var index = (uint)0;
while (index < n)
{
argList.Add(argArrayObj.Get(index.ToString()));
index++;
}
var callArgs = environment.CreateArgs(argList);
return callable.Call(environment, thisArg, callArgs);
}
示例9: GetTimeValue
internal static double GetTimeValue(IEnvironment environment)
{
var date = environment.Context.ThisBinding.Value as NDate;
if (date == null)
{
throw environment.CreateTypeError("");
}
return date.PrimitiveValue.ConvertToNumber().BaseValue;
}
示例10: Sort
private static IDynamic Sort(IEnvironment environment, IArgs args)
{
var obj = environment.Context.ThisBinding.ConvertToObject();
var len = (uint)obj.Get("length").ConvertToUInt32().BaseValue;
var comparefn = args[0];
var sortCompare = new Func<uint, uint, IDynamic>((j, k) =>
{
var jString = j.ToString();
var kString = k.ToString();
var hasJ = obj.HasProperty(jString);
var hasK = obj.HasProperty(kString);
if (!hasJ || !hasK)
{
if (!hasJ && !hasK) return environment.CreateNumber(0);
else if (!hasJ) return environment.CreateNumber(1);
else return environment.CreateNumber(-1);
}
var x = obj.Get(jString);
var y = obj.Get(kString);
if (x is IUndefined || y is IUndefined)
{
if (x is IUndefined && y is IUndefined) return environment.CreateNumber(0);
else if (x is IUndefined) return environment.CreateNumber(1);
else return environment.CreateNumber(-1);
}
if (comparefn.TypeCode != LanguageTypeCode.Undefined)
{
var callable = comparefn as ICallable;
if (callable == null)
{
throw environment.CreateTypeError("");
}
return callable.Call(environment, environment.Undefined, environment.CreateArgs(new[] { x, y }));
}
var xString = x.ConvertToString();
var yString = y.ConvertToString();
var xLess = xString.Op_Lessthan(yString);
if (((IBoolean)xLess).BaseValue) return environment.CreateNumber(-1);
var yLess = yString.Op_Lessthan(xString);
if (((IBoolean)yLess).BaseValue) return environment.CreateNumber(1);
return environment.CreateNumber(0);
});
var items = new List<IDynamic>();
for (uint i = 0; i < len; i++)
{
var key = i.ToString();
if (obj.HasProperty(key))
{
var value = obj.Get(key);
items.Add(value);
obj.Delete(key, true);
}
}
items.Sort((x, y) =>
{
if (x is IUndefined || y is IUndefined)
{
if (x is IUndefined && y is IUndefined) return 0;
else if (x is IUndefined) return 1;
else return -1;
}
if (comparefn.TypeCode != LanguageTypeCode.Undefined)
{
var callable = comparefn as ICallable;
if (callable == null)
{
throw environment.CreateTypeError("");
}
var result = callable.Call(environment, environment.Undefined, environment.CreateArgs(new[] { x, y }));
var number = result.ConvertToInteger();
if (double.IsNaN(number.BaseValue) || double.IsInfinity(number.BaseValue))
{
return 0;
}
return (int)number.BaseValue;
}
var xString = x.ConvertToString();
var yString = y.ConvertToString();
var xLess = xString.Op_Lessthan(yString);
if (((IBoolean)xLess).BaseValue) return -1;
var yLess = yString.Op_Lessthan(xString);
if (((IBoolean)yLess).BaseValue) return 1;
return 0;
});
for (int i = 0; i < items.Count; i++)
{
var key = i.ToString();
obj.Put(key, items[i], true);
}
return obj;
}
示例11: ToLocaleString
private static IDynamic ToLocaleString(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding.ConvertToObject();
var arrayLen = o.Get("length");
var len = (uint)arrayLen.ConvertToUInt32().BaseValue;
var separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
if (len == 0)
{
return environment.CreateString(string.Empty);
}
var sb = new StringBuilder();
{
for (uint i = 0; i < len; i++)
{
if (i > 0)
{
sb.Append(separator);
}
var nextElement = o.Get(i.ToString());
switch (nextElement.TypeCode)
{
case LanguageTypeCode.Boolean:
case LanguageTypeCode.String:
case LanguageTypeCode.Number:
case LanguageTypeCode.Object:
var elementObj = nextElement.ConvertToObject();
var func = elementObj.Get("toLocaleString") as ICallable;
if (func == null)
{
throw environment.CreateTypeError("");
}
sb.Append(func.Call(environment, elementObj, environment.EmptyArgs).ConvertToString().BaseValue);
break;
}
}
}
return environment.CreateString(sb.ToString());
}
示例12: ReduceRight
private static IDynamic ReduceRight(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding.ConvertToObject();
var len = (uint)o.Get("length").ConvertToUInt32().BaseValue;
var callbackfn = args[0] as ICallable;
if (callbackfn == null)
throw environment.CreateTypeError("");
if (len == 0 && args.Count < 2)
throw environment.CreateTypeError("");
uint k = len - 1;
var accumulator = args[1];
if (args.Count < 2)
{
var found = false;
for (;; k--)
{
var key = k.ToString();
if (o.HasProperty(key))
{
found = true;
accumulator = o.Get(key);
k--;
break;
}
if (k == 0) break;
}
if (!found)
throw environment.CreateTypeError("");
}
for (;; k--)
{
var key = k.ToString();
if (o.HasProperty(key))
{
var callArgs = environment.CreateArgs(new[] { accumulator, o.Get(key), environment.CreateNumber(k), o });
accumulator = callbackfn.Call(environment, environment.Undefined, callArgs);
}
if (k == 0) break;
}
return accumulator;
}
示例13: Some
private static IDynamic Some(IEnvironment environment, IArgs args)
{
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);
if (callbackfn.Call(environment, thisArg, environment.CreateArgs(callArgs)).ConvertToBoolean().BaseValue)
{
return environment.True;
}
}
}
return environment.False;
}
示例14: Call
private static IDynamic Call(IEnvironment environment, IArgs args)
{
var callable = environment.Context.ThisBinding as ICallable;
if (callable == null)
{
throw environment.CreateTypeError("");
}
var thisArg = args[0];
var callArgs = environment.EmptyArgs;
if (args.Count > 1)
{
callArgs = environment.CreateArgs(args.Skip(1));
}
return callable.Call(environment, thisArg, callArgs);
}
示例15: PreventExtensions
internal static IDynamic PreventExtensions(IEnvironment environment, IArgs args)
{
if (args[0].TypeCode != LanguageTypeCode.Object)
{
throw environment.CreateTypeError("");
}
var obj = args[0].ConvertToObject();
obj.Extensible = false;
return obj;
}