public static SilverBoxedInstance Box(object obj, SilverScope scope = null) { if (obj == null) { return null; } if (_boxCache.ContainsKey(obj)) { _boxCache[obj].BoxedScope.MergeWithScope(scope ?? new SilverScope()); return _boxCache[obj]; } var boxed = new SilverBoxedInstance(obj, scope ?? new SilverScope()); _boxCache[obj] = boxed; if (scope != null) { string name; SilverScope _scope = scope.SearchForObject(obj, out name); if (_scope != null) { _scope[name] = boxed; } } return boxed; }
public static SilverBoxedInstance BoxNoCache(object obj, SilverScope scope = null) { if (obj == null) { return null; } var boxed = new SilverBoxedInstance(obj, scope ?? new SilverScope()); if (scope != null) { string name; SilverScope _scope = scope.SearchForObject(obj, out name); if (_scope != null) { _scope[name] = boxed; } } return boxed; }
private static dynamic InstanceEval(object self, string eval, SilverScope scope) { string xexpression = string.Format("{0};", eval); var lexer = new SilverLexer("source", xexpression); var stream = new CommonTokenStream(lexer.Queue); var parser = new SilverParser(stream); lexer.SetLines(parser); var instance = self as SilverInstance; if (instance == null) { return null; } Expression[] res = parser.program(); Expression block; if (res != null) { scope["self"] = scope["super"] = instance; scope["<ironsilver_context_invokemember>"] = true; string selfName; var selfScope = scope.SearchForObject(instance, out selfName); if (selfScope != null && selfName != null) { scope["<ironsilver_context_selfscope>"] = selfScope; scope["<ironsilver_context_selfname>"] = selfName; } block = AstExpression.Block(res); // We want eval'd expressions to execute in the current scope, not its own child scopes. This ensures assignment evals work properly. ((BlockExpression)block).Scope = scope; ((BlockExpression)block).SetChildrenScopes(scope); } else { return null; } dynamic val = CompilerServices.CreateLambdaForExpression(block)(); return val; }