public void InvalidStatesFormat(string function, string input, string context) { var f = IntrinsicFunction.Parse(function); Assert.Throws <InvalidIntrinsicFunctionException>(() => _registry.CallFunction(f, JToken.Parse(input), JObject.Parse(context))); }
public void OneParameterIntrinsicFunction() { var f = IntrinsicFunction.Parse("Test('hello')"); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <StringIntrinsicParam>(f.Parameters[0]); Assert.Equal("hello", ((StringIntrinsicParam)f.Parameters[0]).Value); f = IntrinsicFunction.Parse("Test($.hello)"); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <PathIntrinsicParam>(f.Parameters[0]); Assert.Equal("$.hello", ((PathIntrinsicParam)f.Parameters[0]).Path); f = IntrinsicFunction.Parse("Test(33.45)"); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <NumberIntrinsicParam>(f.Parameters[0]); Assert.Equal(33.45m, ((NumberIntrinsicParam)f.Parameters[0]).Number); f = IntrinsicFunction.Parse("Test(null)"); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <NullIntrinsicParam>(f.Parameters[0]); }
public void TestStringToJsonError(string function, string input) { var f = IntrinsicFunction.Parse(function); Assert.Throws <InvalidIntrinsicFunctionException>(() => _registry.CallFunction(f, JToken.Parse(input), new JObject())); }
public void TestJsonPathObject() { var f = IntrinsicFunction.Parse("States.JsonToString($.a)"); var res = _registry.CallFunction(f, JObject.Parse("{'a':{'b':1,'c':'test'}}"), new JObject()); Assert.Equal("{\"b\":1,\"c\":\"test\"}", res.Value <string>()); }
public void TestIntrinsicFunction() { var f = IntrinsicFunction.Parse("States.StringToJson(States.JsonToString($.a))"); var res = _registry.CallFunction(f, JObject.Parse("{'a':[1,2,3,4]}"), new JObject()); Assert.Equal(JArray.Parse("[1,2,3,4]"), res); }
public void TestRawJson() { var f = IntrinsicFunction.Parse("States.StringToJson('{\"number\": 20}')"); var res = _registry.CallFunction(f, "'a'", new JObject()); Assert.Equal(JObject.Parse("{'number': 20}"), res); }
private object EvalFunction(ICallable callee, object[] args) { if (callee is IntrinsicFunction) { IntrinsicFunction ifun = (IntrinsicFunction)callee; MethodBase mmodel = ifun.MethodModel; if (mmodel != null) { if (mmodel.IsStatic) { return(mmodel.ConvertArgumentsAndInvoke(null, args)); } else { object[] eargs = new object[args.Length - 1]; Array.Copy(args, 1, eargs, 0, eargs.Length); return(mmodel.ConvertArgumentsAndInvoke(args[0], eargs)); } } else { throw new NotImplementedException(); } } else if (callee is MSILFunctionRef) { MSILFunctionRef mfr = (MSILFunctionRef)callee; return(InterpreteInternal(mfr.Method, args)); } else { throw new NotImplementedException(); } }
public void TestWithoutArgumentFromPath() { var f = IntrinsicFunction.Parse("States.Format($.p)"); var r = _registry.CallFunction(f, JToken.Parse("{'p':'value'}"), new JObject()); Assert.Equal("value", r.Value <string>()); }
public void HelloWorldTest(string function, string input, string context) { var f = IntrinsicFunction.Parse(function); var r = _registry.CallFunction(f, JToken.Parse(input), JObject.Parse(context)); Assert.Equal("hello world", r.Value <string>()); }
public void TestJsonPath() { var f = IntrinsicFunction.Parse("States.StringToJson($.someString)"); var res = _registry.CallFunction(f, JObject.Parse("{'someString': '{\\\"number\\\": 20}','zebra': 'stripe'}"), new JObject()); Assert.Equal(JObject.Parse("{'number': 20}"), res); }
public void TestObjectArrayItem() { var f = IntrinsicFunction.Parse("States.Array($.a)"); var r = _registry.CallFunction(f, JObject.Parse("{'a':{'b':33}}"), new JObject()); Assert.True(r is JArray); Assert.Equal(JToken.Parse("{'b':33}"), r.First); }
public void TestArrayParameterCount(string definition, int count) { var f = IntrinsicFunction.Parse(definition); var r = _registry.CallFunction(f, "'a'", new JObject()); Assert.True(r is JArray); Assert.Equal(count, r.Count()); }
public void TestWithNumberArgument() { var f = IntrinsicFunction.Parse("States.Format('hello {}',12.34)"); var r = _registry.CallFunction(f, "test", new JObject()); Assert.Equal("hello 12.34", r.Value <string>()); }
public void TestWithoutArgument() { var f = IntrinsicFunction.Parse("States.Format('value')"); var r = _registry.CallFunction(f, "test", new JObject()); Assert.Equal("value", r); }
public void TestPathArgument() { var f = IntrinsicFunction.Parse("States.Format('hello {}', $.p)"); var r = _registry.CallFunction(f, JToken.Parse("{'p':'world'}"), new JObject()); Assert.Equal("hello world", r.Value <string>()); }
public void TestEscapeCharsFunctionArgument() { var f = IntrinsicFunction.Parse("States.Format('hello \\\\{\\\\}')"); var r = _registry.CallFunction(f, "test", new JObject()); Assert.Equal("hello {}", r.Value <string>()); }
public void TestFunctionArgument() { var f = IntrinsicFunction.Parse("States.Format('hello {}', States.Format('world'))"); var r = _registry.CallFunction(f, "test", new JObject()); Assert.Equal("hello world", r.Value <string>()); }
public void IgnoreWhiteSpacesAroundQuoteStringParam(string test) { var f = IntrinsicFunction.Parse(test); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <StringIntrinsicParam>(f.Parameters[0]); Assert.Equal("hello", ((StringIntrinsicParam)f.Parameters[0]).Value); }
public void OneParameterIntrinsicFunctionWithDotAndUnder() { var f = IntrinsicFunction.Parse("Tes.t_('hello')"); Assert.Equal("Tes.t_", f.Name); Assert.Single(f.Parameters); Assert.IsType <StringIntrinsicParam>(f.Parameters[0]); Assert.Equal("hello", ((StringIntrinsicParam)f.Parameters[0]).Value); }
public void IgnoreWhiteSpacesAroundUnquotePathParam(string test) { var f = IntrinsicFunction.Parse(test); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <PathIntrinsicParam>(f.Parameters[0]); Assert.Equal("$.test", ((PathIntrinsicParam)f.Parameters[0]).Path); }
public void SubIntrinsicFunctiontest() { var f = IntrinsicFunction.Parse("Test(Test($.test))"); Assert.Equal("Test", f.Name); Assert.Single(f.Parameters); Assert.IsType <IntrinsicFunction>(f.Parameters[0]); Assert.Equal("Test", ((IntrinsicFunction)f.Parameters[0]).Name); Assert.IsType <PathIntrinsicParam>(((IntrinsicFunction)f.Parameters[0]).Parameters[0]); Assert.Equal("$.test", ((PathIntrinsicParam)((IntrinsicFunction)f.Parameters[0]).Parameters[0]).Path); }
public void TwoParametersIntrinsicFunction() { var f = IntrinsicFunction.Parse("Test('hello',33)"); Assert.Equal("Test", f.Name); Assert.Equal(2, f.Parameters.Length); Assert.IsType <StringIntrinsicParam>(f.Parameters[0]); Assert.Equal("hello", ((StringIntrinsicParam)f.Parameters[0]).Value); Assert.IsType <NumberIntrinsicParam>(f.Parameters[1]); Assert.Equal(33, ((NumberIntrinsicParam)f.Parameters[1]).Number); }
public void FourParametersIntrinsicFunction() { var f = IntrinsicFunction.Parse("Test('hello',33,null,$.test)"); Assert.Equal("Test", f.Name); Assert.Equal(4, f.Parameters.Length); Assert.IsType <StringIntrinsicParam>(f.Parameters[0]); Assert.Equal("hello", ((StringIntrinsicParam)f.Parameters[0]).Value); Assert.IsType <NumberIntrinsicParam>(f.Parameters[1]); Assert.Equal(33, ((NumberIntrinsicParam)f.Parameters[1]).Number); Assert.IsType <NullIntrinsicParam>(f.Parameters[2]); Assert.IsType <PathIntrinsicParam>(f.Parameters[3]); Assert.Equal("$.test", ((PathIntrinsicParam)f.Parameters[3]).Path); }
public object EvalFunction(FunctionCall funcref, object[] args, TypeDescriptor resultType) { if (funcref.Callee is IntrinsicFunction) { IntrinsicFunction ifun = (IntrinsicFunction)funcref.Callee; MethodBase mmodel = ifun.MethodModel; if (mmodel != null && mmodel.IsStatic) { object result = mmodel.ConvertArgumentsAndInvoke(null, args); return(result); } } return(DoEvalFunction(funcref, args)); }
public static JToken Array(IntrinsicFunction function, JToken input, JObject context, IntrinsicFunctionRegistry registry) { Ensure.IsNotNull <ArgumentNullException>(function); Ensure.IsNotNull <ArgumentNullException>(registry); var result = new JArray(); foreach (var p in function.Parameters) { result.Add(p switch { NullIntrinsicParam _ => null, StringIntrinsicParam s => s.Value, NumberIntrinsicParam n => n.Number, PathIntrinsicParam pa => GetPathValue(pa, input, context), IntrinsicFunction func => registry.CallFunction(func, input, context), _ => throw new InvalidIntrinsicFunctionException("Invalid Parameter type") });
public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler decomp, IFunctionBuilder builder) { Expression[] elements = null; if (args.Length == 1) { Expression valarr = args[0].Expr; valarr = decomp.ResolveVariableReference(decomp.CurrentILIndex, valarr); FunctionCall newarrCall = valarr as FunctionCall; if (newarrCall != null) { FunctionSpec fspec = newarrCall.Callee as FunctionSpec; IntrinsicFunction ifun = fspec == null ? null : fspec.IntrinsicRep; if (ifun != null && ifun.Action == IntrinsicFunction.EAction.NewArray) { ArrayParams aparams = (ArrayParams)ifun.Parameter; if (aparams.IsStatic) { newarrCall.IsInlined = true; for (int i = 0; i < aparams.Elements.Length; i++) { aparams.Elements[i].IsInlined = true; } elements = aparams.Elements; } } } } else { elements = args.Select(arg => arg.Expr).ToArray(); } if (elements == null) { throw new NotImplementedException(); } MakeStringArray(elements); decomp.Push( IntrinsicFunctions.StringConcat(elements), ""); return(true); }
public override bool Rewrite(CodeDescriptor decompilee, System.Reflection.MethodBase callee, Analysis.StackElement[] args, Analysis.IDecompiler stack, IFunctionBuilder builder) { Array arr = (Array)args[0].Sample; if (arr == null) { throw new InvalidOperationException("Unable to deduce array length"); } int numElems = arr.Length; Type tTE = arr.GetType().GetElementType(); Type tTA; callee.IsFunction(out tTA); FunctionCall newCall = IntrinsicFunctions.NewArray(tTE, LiteralReference.CreateConstant(numElems), arr); FunctionSpec fspec = (FunctionSpec)newCall.Callee; IntrinsicFunction ifun = fspec.IntrinsicRep; ArrayParams aparams = (ArrayParams)ifun.Parameter; for (int i = 0; i < numElems; i++) { aparams.Elements[i] = IntrinsicFunctions.GetArrayElement( args[0].Expr, LiteralReference.CreateConstant(i)); } object sample = null; try { sample = callee.Invoke(arr); } catch (Exception) { } Expression conv = IntrinsicFunctions.Cast(newCall, arr.GetType(), tTA); stack.Push(conv, sample); return(true); }
public void IgnoreWhiteSpacesAroundComplex() { var f = IntrinsicFunction.Parse("test( 1 , 'a' , a( $p , 'test' ) ) "); Assert.Equal("test", f.Name); Assert.Equal(3, f.Parameters.Length); Assert.IsType <NumberIntrinsicParam>(f.Parameters[0]); Assert.Equal(1, ((NumberIntrinsicParam)f.Parameters[0]).Number); Assert.IsType <StringIntrinsicParam>(f.Parameters[1]); Assert.Equal("a", ((StringIntrinsicParam)f.Parameters[1]).Value); Assert.IsType <IntrinsicFunction>(f.Parameters[2]); var f2 = (IntrinsicFunction)f.Parameters[2]; Assert.Equal("a", f2.Name); Assert.Equal(2, f2.Parameters.Length); Assert.IsType <PathIntrinsicParam>(f2.Parameters[0]); Assert.Equal("$p", ((PathIntrinsicParam)f2.Parameters[0]).Path); Assert.IsType <StringIntrinsicParam>(f2.Parameters[1]); Assert.Equal("test", ((StringIntrinsicParam)f2.Parameters[1]).Value); }
public void AcceptCall(CallStatement stmt) { CreateLabelForNextInstruction(stmt); FunctionSpec fspec = stmt.Callee as FunctionSpec; if (fspec == null) { throw new NotSupportedException(); } if (fspec.CILRep != null) { var cmc = fspec.CILRep.GetCustomOrInjectedAttribute <CompileMethodCall>(); if (cmc != null) { cmc.Compile(stmt, _backend); return; } } IntrinsicFunction ifun = fspec.IntrinsicRep; if (ifun != null) { switch (ifun.Action) { case IntrinsicFunction.EAction.Wait: return; // ignore for now default: throw new NotSupportedException(); } } throw new NotSupportedException(); }
public void IntrinsicFunctionParserError(string functionDefinition) { Assert.Throws <InvalidIntrinsicFunctionException>(() => IntrinsicFunction.Parse(functionDefinition)); }