public void ConstScope() { string program = @" a = testconst b = """" try: testconst = 5 except exc: b = exc "; Runner r = new Runner(); ConstScope scope = new ConstScope( r.state.scope ); scope.setConstant( "testconst", 10 ); r.pushScope( scope ); runAndDump( "ConstScope", r, program, null ); }
public object callMethod( State state, string name, object[] args ) { if( name == "test2" ) return "test worked " + String.Join( ",", args.Select( x => x.ToStringI() ).ToArray() ); else if( name == "dumpcontext" ) { var cxt = state.securityContext; return "Current context: " + ( cxt == null ? "none" : cxt.name ); } else if( name == "complex" ) { int which = (int)args[0]; if( which == 1 ) { return new AsyncAction() { action = AsyncAction.Action.Call, function = (FValue)args[1], args = new object[] { "added\r\n" }, frame = new StackTrace.StackFrame() { line = 0, col = 0, unitName = "test", funcName = "PtTest.{0}".FormatI( name ) } }; } else if( which == 2 ) { return new AsyncAction[] { new AsyncAction() { action = AsyncAction.Action.Variable, name = "shouldexist", value = "oh cool" }, new AsyncAction() { action = AsyncAction.Action.Callback, callback = st => { var rv = "{0}".FormatI( st.scope.get( "shouldexist" ) ); st.pushResult( rv ); } } }; } else if( which == 3 ) { var constscope = new ConstScope( state.scope ); constscope.setConstant( "testconst", "bob" ); return new AsyncAction[] { new AsyncAction() { action = AsyncAction.Action.Code, code = Compiler.Compile( "test", @" def innerfunc(x): return [x + 1, testconst] " ) }, new AsyncAction() { action = AsyncAction.Action.Call, name = "innerfunc", args = new object[] { 5 }, frame = new StackTrace.StackFrame() { line = 0, col = 0, unitName = "test", funcName = "PtTest.{0}".FormatI( name ) } }, new AsyncAction() { action = AsyncAction.Action.PushScope, scope = constscope } }; } else if( which == 4 ) { return new AsyncAction[] { new AsyncAction() { action = AsyncAction.Action.Code, code = Compiler.Compile( "test", @" def innerfunc(pt): return pt.dumpcontext() " ) }, new AsyncAction() { action = AsyncAction.Action.Call, name = "innerfunc", args = new object[] { this }, frame = new StackTrace.StackFrame() }, new AsyncAction() { action = AsyncAction.Action.PushSecurityContext, securityContext = new TestSC( "Context 1" ) } }; } else if( which == 5 ) { return new AsyncAction[] { new AsyncAction() { action = AsyncAction.Action.Code, code = Compiler.Compile( "test", @" def innerfunc(pt): return pt.dumpcontext() " ) }, new AsyncAction() { action = AsyncAction.Action.Call, name = "innerfunc", args = new object[] { this }, frame = new StackTrace.StackFrame() } }; } else /* if( which == 6 ) */ { throw new ArgumentException( "No idea what you're talking about!" ); } } else return null; }
/// <summary> /// Registers the string object in the const scope. /// </summary> /// <remarks> /// This could probably be factored out into a helper class. /// </remarks> public static void RegisterObject( ConstScope scope ) { scope.setConstant( "string", new MetalObject() { indexLookup = (state, idx) => { throw CoralException.GetArg( "Can't index the string object" ); }, memberLookup = (state, name) => { state.pushResult( new LValue() { read = st => { if( name == "join" ) { return new FValue( (st2,args) => MethodJoin( st2, args ) ); } else { throw CoralException.GetArg( "Unknown method on string object" ); } }, write = (st,val) => { throw CoralException.GetInvOp( "Can't write to the string object" ); } } ); } } ); }