本文整理汇总了C#中JsValue.AsBoolean方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.AsBoolean方法的具体用法?C# JsValue.AsBoolean怎么用?C# JsValue.AsBoolean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsValue
的用法示例。
在下文中一共展示了JsValue.AsBoolean方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBoolean
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static bool ToBoolean(JsValue o)
{
if (o.IsObject())
{
return true;
}
if (o == Undefined.Instance || o == Null.Instance)
{
return false;
}
if (o.IsBoolean())
{
return o.AsBoolean();
}
if (o.IsNumber())
{
var n = o.AsNumber();
if (n.Equals(0) || double.IsNaN(n))
{
return false;
}
else
{
return true;
}
}
if (o.IsString())
{
var s = o.AsString();
if (String.IsNullOrEmpty(s))
{
return false;
}
else
{
return true;
}
}
return true;
}
示例2: Equal
public static bool Equal(JsValue x, JsValue y)
{
var typex = x.Type;
var typey = y.Type;
if (typex == typey)
{
if (typex == Types.Undefined || typex == Types.Null)
{
return true;
}
if (typex == Types.Number)
{
var nx = TypeConverter.ToNumber(x);
var ny = TypeConverter.ToNumber(y);
if (double.IsNaN(nx) || double.IsNaN(ny))
{
return false;
}
if (nx.Equals(ny))
{
return true;
}
return false;
}
if (typex == Types.String)
{
return TypeConverter.ToString(x) == TypeConverter.ToString(y);
}
if (typex == Types.Boolean)
{
return x.AsBoolean() == y.AsBoolean();
}
return x == y;
}
if (x == Null.Instance && y == Undefined.Instance)
{
return true;
}
if (x == Undefined.Instance && y == Null.Instance)
{
return true;
}
if (typex == Types.Number && typey == Types.String)
{
return Equal(x, TypeConverter.ToNumber(y));
}
if (typex == Types.String && typey == Types.Number)
{
return Equal(TypeConverter.ToNumber(x), y);
}
if (typex == Types.Boolean)
{
return Equal(TypeConverter.ToNumber(x), y);
}
if (typey == Types.Boolean)
{
return Equal(x, TypeConverter.ToNumber(y));
}
if (typey == Types.Object && (typex == Types.String || typex == Types.Number))
{
return Equal(x, TypeConverter.ToPrimitive(y));
}
if (typex == Types.Object && (typey == Types.String || typey == Types.Number))
{
return Equal(TypeConverter.ToPrimitive(x), y);
}
return false;
}
示例3: ToObject
public static ObjectInstance ToObject(Engine engine, JsValue value)
{
if (value.IsObject())
{
return value.AsObject();
}
if (value == Undefined.Instance)
{
throw new JavaScriptException(engine.TypeError);
}
if (value == Null.Instance)
{
throw new JavaScriptException(engine.TypeError);
}
if (value.IsBoolean())
{
return engine.Boolean.Construct(value.AsBoolean());
}
if (value.IsNumber())
{
return engine.Number.Construct(value.AsNumber());
}
if (value.IsString())
{
return engine.String.Construct(value.AsString());
}
throw new JavaScriptException(engine.TypeError);
}
示例4: ToString
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string ToString(JsValue o)
{
if (o.IsObject())
{
var p = o.AsObject() as IPrimitiveInstance;
if (p != null)
{
o = p.PrimitiveValue;
}
}
if (o.IsString())
{
return o.AsString();
}
if (o == Undefined.Instance)
{
return Undefined.Text;
}
if (o == Null.Instance)
{
return Null.Text;
}
if (o.IsBoolean())
{
return o.AsBoolean() ? "true" : "false";
}
if (o.IsNumber())
{
return NumberPrototype.ToNumberString(o.AsNumber());
}
return ToString(ToPrimitive(o, Types.String));
}
示例5: ToNumber
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static double ToNumber(JsValue o)
{
// check number first as this is what is usually expected
if (o.IsNumber())
{
return o.AsNumber();
}
if (o.IsObject())
{
var p = o.AsObject() as IPrimitiveInstance;
if (p != null)
{
o = p.PrimitiveValue;
}
}
if (o == Undefined.Instance)
{
return double.NaN;
}
if (o == Null.Instance)
{
return 0;
}
if (o.IsBoolean())
{
return o.AsBoolean() ? 1 : 0;
}
if (o.IsString())
{
var s = o.AsString().Trim();
if (String.IsNullOrEmpty(s))
{
return 0;
}
if ("+Infinity".Equals(s) || "Infinity".Equals(s))
{
return double.PositiveInfinity;
}
if ("-Infinity".Equals(s))
{
return double.NegativeInfinity;
}
// todo: use a common implementation with JavascriptParser
try
{
if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
var start = s[0];
if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start))
{
return double.NaN;
}
double n = Double.Parse(s,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign |
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
if (s.StartsWith("-") && n.Equals(0))
{
return -0.0;
}
return n;
}
int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
return i;
}
catch (OverflowException)
{
return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity;
}
catch
{
return double.NaN;
}
}
return ToNumber(ToPrimitive(o, Types.Number));
}
示例6: Put
/// Implementation from ObjectInstance official specs as the one
/// in ObjectInstance is optimized for the general case and wouldn't work
/// for arrays
public override void Put(string propertyName, JsValue value, bool throwOnError)
{
if (!CanPut(propertyName))
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError);
}
return;
}
if (InternalArray != null)
{
int ind;
if (int.TryParse(propertyName, out ind))
{
object val;
switch (value.Type)
{
case Types.Undefined:
val = null;
break;
case Types.Null:
val = null;
break;
case Types.Boolean:
val = value.AsBoolean();
break;
case Types.String:
val = value.AsString();
break;
case Types.Number:
val = value.AsNumber();
break;
case Types.Object:
val = value;
var ow = ((JsValue)val).AsObject() as ObjectWrapper;
if (ow != null)
{
val = ow.Target;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
if (ind >= InternalArray.Count)
{
for (int i = InternalArray.Count; i <= ind; i++)
{
if (val != null)
InternalArray.Add(GetDefault(val.GetType()));
else
InternalArray.Add(null);
}
}
InternalArray[ind] = val;
}
}
var ownDesc = GetOwnProperty(propertyName);
if (ownDesc.IsDataDescriptor())
{
var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
DefineOwnProperty(propertyName, valueDesc, throwOnError);
return;
}
// property is an accessor or inherited
var desc = GetProperty(propertyName);
if (desc.IsAccessorDescriptor())
{
var setter = desc.Set.Value.TryCast<ICallable>();
setter.Call(new JsValue(this), new[] { value });
}
else
{
var newDesc = new PropertyDescriptor(value, true, true, true);
DefineOwnProperty(propertyName, newDesc, throwOnError);
}
}