ln.templates/ln.templates/script/NewExpression.cs

56 lines
1.5 KiB
C#

// /**
// * File: NewExpression.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using Jint;
namespace ln.templates.script
{
public class NewExpression
{
public string ExpressionText { get; }
public NewExpression(string expression)
{
ExpressionText = expression;
}
public virtual object Resolve() => Resolve(new Engine());
public virtual object Resolve(Engine engine) =>
engine.Evaluate(ExpressionText).ToObject();
public virtual bool IsTrue() => IsTrue(new Engine());
public virtual bool IsTrue(Engine engine)
{
object resolved = Resolve(engine);
if (resolved == null)
return false;
if (resolved is bool b)
return b;
if (resolved is string s)
return s.Length > 0;
if (resolved is int i)
return i != 0;
if (resolved is long l)
return l != 0;
if (resolved is short sh)
return sh != 0;
if (resolved is byte by)
return by != 0;
if (resolved is float f)
return Math.Abs(f) > float.Epsilon;
if (resolved is double d)
return Math.Abs(d) > double.Epsilon;
return true;
}
}
}