public async Task CompileWithExplicitlyClonedContext() { /* * Creating a "master binder" that you never directly use, but rather * clones for each time you evaluate a snippet of Lizzie code, allows * you to avoid having to run through all the reflection initialization * when binding to your context type, probably saving you a lot of * resources when binding to the same type in multiple threads. */ var masterBinder = new Binder <SimpleValues>(); LambdaCompiler.BindFunctions <SimpleValues>(masterBinder); masterBinder["bar"] = 10; // Cloning our binder and evaluating a snippet of Lizzie code. var binder1 = masterBinder.Clone(); binder1["bar2"] = 2; var lambda1 = LambdaCompiler.CompileAsync <SimpleValues>(new SimpleValues(), (Binder <SimpleValues>)binder1, @" var(@foo, +(55, bar, bar2)) "); var result1 = lambda1(); // Cloning a new binder and evaluating a new snippet of Lizzie code. var binder2 = masterBinder.Clone(); Assert.IsFalse(binder2.ContainsKey("bar2")); var lambda2 = LambdaCompiler.CompileAsync <SimpleValues>(new SimpleValues(), (Binder <SimpleValues>)binder2, @" var(@foo, +(67, bar)) "); var result2 = await lambda2(); // Sanity checking result. Assert.AreEqual(67, result1); Assert.AreEqual(77, result2); }
public void CloneWithoutStack() { var original = new Binder <SimpleValues>(); var clone = original.Clone(); foreach (var ix in original.StaticItems) { Assert.AreEqual(clone[ix], original[ix]); } foreach (var ix in clone.StaticItems) { Assert.AreEqual(clone[ix], original[ix]); } Assert.AreEqual(0, clone.StackCount); }
public void CloneWithPushedStack() { var original = new Binder <SimpleValues>(); original.PushStack(); original["foo"] = 57; original.PushStack(); original["bar"] = 77; var clone = original.Clone(); foreach (var ix in original.StaticItems) { Assert.AreEqual(clone[ix], original[ix]); } foreach (var ix in clone.StaticItems) { Assert.AreEqual(clone[ix], original[ix]); } Assert.AreEqual(2, clone.StackCount); Assert.AreEqual(77, clone["bar"]); clone.PopStack(); Assert.AreEqual(57, clone["foo"]); }