// /** // * 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; using Jint.Native; using Jint.Native.String; namespace ln.templates.script { public class NewExpression { public string ExpressionText { get; } public NewExpression(string expression) { ExpressionText = expression; } public virtual object Resolve(IScriptContext scriptContext) { Engine engine = new Engine(); foreach (string itemName in scriptContext.ScriptObjectNames) engine.SetValue(itemName, scriptContext.GetScriptObject(itemName)); JsValue jsv = engine.Eval.Call(null, new Jint.Native.JsValue[] { ExpressionText }); return jsv.ToObject(); } public virtual bool IsTrue(IScriptContext scriptContext) { object resolved = Resolve(scriptContext); 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; } } }