public override DynValue Eval(ScriptExecutionContext context) { DynValue v1 = m_Exp1.Eval(context).ToScalar(); if (m_Operator == Operator.Or) { if (v1.CastToBool()) { return(v1); } else { return(m_Exp2.Eval(context).ToScalar()); } } if (m_Operator == Operator.And) { if (!v1.CastToBool()) { return(v1); } else { return(m_Exp2.Eval(context).ToScalar()); } } DynValue v2 = m_Exp2.Eval(context).ToScalar(); if ((m_Operator & COMPARES) != 0) { return(DynValue.NewBoolean(EvalComparison(v1, v2, m_Operator))); } else if (m_Operator == Operator.StrConcat) { string s1 = v1.CastToString(); string s2 = v2.CastToString(); if (s1 == null || s2 == null) { throw new DynamicExpressionException("Attempt to perform concatenation on non-strings."); } return(DynValue.NewString(s1 + s2)); } else { return(DynValue.NewNumber(EvalArithmetic(v1, v2))); } }
public override (bool done, bool result) Execute() { framesElapsed += 1; if (framesElapsed > delayFrames) { try { var func = script.Globals[function]; DynValue returnValue = script.Call(func); if (returnValue.IsNotNil()) { return(true, returnValue.CastToBool()); } return(true, true); } catch (ScriptRuntimeException ex) { Debug.LogError("[Execute] Could not execute function: " + ex.DecoratedMessage); } catch (SyntaxErrorException ex) { Debug.LogError("[Execute] Could not execute function: " + ex.DecoratedMessage); } return(true, false); } return(false, false); }
public static DynValue assert(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue v = args[0]; DynValue message = args[1]; if (!v.CastToBool()) { if (message.IsNil()) { throw new ScriptRuntimeException("assertion failed!") { DoNotDecorateMessage = true } } ; else { throw new ScriptRuntimeException(message.ToPrintString()) { DoNotDecorateMessage = true } }; } return(DynValue.NewTupleNested(args.GetArray())); }
public override DynValue Eval(ScriptExecutionContext context) { DynValue v = m_Exp.Eval(context).ToScalar(); switch (m_OpText) { case "not": return(DynValue.NewBoolean(!v.CastToBool())); case "#": return(v.GetLength()); case "-": { double d; if (v.TryCastToNumber(out d)) { return(DynValue.NewNumber(-d)); } throw new DynamicExpressionException("Attempt to perform arithmetic on non-numbers."); } default: throw new DynamicExpressionException("Unexpected unary operator '{0}'", m_OpText); } }
void Start() { Instance = this; UserData.RegisterAssembly(); script = new Script(); script.Options.DebugPrint += (string str) => { Debug.Log("LUA: " + str); }; FileInfo info = new FileInfo( Path.Combine(Application.streamingAssetsPath, Path.Combine(directory, filename) ) ); Load(info); Debug.Log("Test call to LUA!"); DynValue ret = Call("generic", "test"); Debug.Assert(ret.CastToBool() == true); }
private void ExecCNot(Instruction i) { DynValue v = m_ValueStack.Pop().ToScalar(); DynValue not = m_ValueStack.Pop().ToScalar(); if (not.Type != DataType.Boolean) { throw new InternalErrorException("CNOT had non-bool arg"); } if (not.CastToBool()) { m_ValueStack.Push(DynValue.NewBoolean(!(v.CastToBool()))); } else { m_ValueStack.Push(DynValue.NewBoolean(v.CastToBool())); } }
private int JumpBool(Instruction i, bool expectedValueForJump, int instructionPtr) { DynValue op = m_ValueStack.Pop().ToScalar(); if (op.CastToBool() == expectedValueForJump) { return(i.NumVal); } return(instructionPtr); }
public ScriptLoader() { UserData.RegisterAssembly(); UserData.RegisterType <Color>(); LoadScript("test", "test.lua"); DynValue ret = Call("test", "test"); Debug.Assert(ret.CastToBool() == true); }
public void TableMethod6() { string script = @" do local a = {x=0} function a:add (x) self.x, a.y = self.x+x, 20; return self end return (a:add(10):add(20):add(30).x == 60 and a.y == 20) end" ; DynValue res = Script.RunString(script); Assert.AreEqual(DataType.Boolean, res.Type); Assert.AreEqual(true, res.CastToBool()); }
private static int LuaComparerToClrComparer(DynValue dynValue1, DynValue dynValue2) { bool v1 = dynValue1.CastToBool(); bool v2 = dynValue2.CastToBool(); if (v1 && !v2) { return(-1); } if (v2 && !v1) { return(1); } return(0); }
private int ExecShortCircuitingOperator(Instruction i, int instructionPtr) { bool expectedValToShortCircuit = i.OpCode == OpCode.JtOrPop; DynValue op = m_ValueStack.Peek().ToScalar(); if (op.CastToBool() == expectedValToShortCircuit) { return(i.NumVal); } else { m_ValueStack.Pop(); return(instructionPtr); } }
private static int LuaComparerToClrComparer(DynValue dynValue1, DynValue dynValue2) { bool v1 = dynValue1.CastToBool(); bool v2 = dynValue2.CastToBool(); if (v1 && !v2) { return(-1); } if (v2 && !v1) { return(1); } if (v1 || v2) { throw new ScriptRuntimeException("invalid order function for sorting"); } return(0); }
public static bool AsBool(this DynValue value) { return((value.Type != DataType.String || value.String != @"0") && value.CastToBool()); }
public static Table AskForParametersNoClear() { // TODO add repeating with previous parameters // FIXME check for duplicate keys if (ScriptContext.context.ui_parameters.keys == null || ScriptContext.context.ui_parameters.keys.Count <= 0) { ScriptContext.context.Warn("AskForParameters called with no parameters set up."); return(null); } Dictionary <string, string> dict = ScriptMode.RequestAndWaitForParams(); if (dict == null) { // FIXME do this without an error message probably? throw new ScriptRuntimeException("User cancelled script execution."); } Table output = new Table(ScriptContext.context.script); foreach (KeyValuePair <string, string> pair in dict) { output.Set( pair.Key, DynValue.FromObject(ScriptContext.context.script, pair.Value) ); } int count = ScriptContext.context.ui_parameters.datatypes.Count; if (ScriptContext.context.ui_parameters.keys.Count != count) { Logger.WriteLogLine("Inconsistent parameter counts in Lua script!"); Logger.WriteLogLine("datatypes: "); foreach (DataType o in ScriptContext.context.ui_parameters.datatypes) { Logger.WriteLogLine(o.ToString()); } Logger.WriteLogLine("keys: "); foreach (string o in ScriptContext.context.ui_parameters.keys) { if (o == null) { Logger.WriteLogLine("null"); } else { Logger.WriteLogLine(o); } } Logger.WriteLogLine("labels: "); foreach (string o in ScriptContext.context.ui_parameters.labels) { if (o == null) { Logger.WriteLogLine("null"); } else { Logger.WriteLogLine(o); } } Logger.WriteLogLine("labels: "); foreach (string o in ScriptContext.context.ui_parameters.defaultvalues) { if (o == null) { Logger.WriteLogLine("null"); } else { Logger.WriteLogLine(o); } } throw new ScriptRuntimeException("Inconsistent parameter counts in Lua script, might be a DBXLua bug. :("); } for (int i = 0; i < count; i++) { DataType cur_type = ScriptContext.context.ui_parameters.datatypes[i]; DynValue cur_value = output.Get(ScriptContext.context.ui_parameters.keys[i]); switch (cur_type) { case DataType.Boolean: cur_value.Assign(DynValue.NewBoolean(cur_value.CastToBool())); //output.Set(ScriptContext.context.ui_parameters.keys[i], cur_value.CastToBool()); break; case DataType.Number: double d = cur_value.CastToNumber() ?? 0; cur_value.Assign(DynValue.NewNumber(d)); break; default: break; // nope } } return(output); }
private void ExecNot(Instruction i) { DynValue v = m_ValueStack.Pop().ToScalar(); m_ValueStack.Push(DynValue.NewBoolean(!(v.CastToBool()))); }
private static int LuaComparerToClrComparer(DynValue dynValue1, DynValue dynValue2) { var v1 = dynValue1.CastToBool(); var v2 = dynValue2.CastToBool(); if (v1 && !v2) return -1; if (v2 && !v1) return 1; if (v1 || v2) throw new ScriptRuntimeException("invalid order function for sorting"); return 0; }
// manipulate values at runtime internal bool GetResult() { return(_result.CastToBool()); }