internal static dynamic Resolve(object rawName, object rawScope)
        {
            if (rawName.GetType() == typeof(InstanceReference))
            {
                var iref   = (InstanceReference)rawName;
                var lval   = CompilerServices.CompileExpression(iref.LValue, (NovaScope)rawScope);
                var gmArgs = new List <Expression>();
                gmArgs.Add(Expression.Constant(lval, typeof(object)));
                return(Dynamic(typeof(object), new InteropBinder.GetMember(iref.Key, (NovaScope)rawScope), gmArgs));
            }
            var name  = (string)rawName;
            var scope = (NovaScope)rawScope;

            if (name.StartsWith("$") && name != "$:")
            {
                scope = scope.GlobalScope;
                name  = name.Substring(1);
            }
            if (name.StartsWith("@") && scope["<nova_context_invokemember>"] != null)
            {
                if (name.StartsWith("@@"))
                {
                    var _val = Resolve("self", scope);
                    if (!(_val is NovaInstance))
                    {
                        // native object?
                        _val = Nova.Box((object)_val, scope);
                    }
                    var @class = ((NovaInstance)_val).Class;
                    return
                        (CompilerServices.CompileExpression(
                             NovaExpression.Variable(NovaExpression.InstanceRef(Expression.Constant(@class),
                                                                                Expression.Constant(name.Substring(2)))), scope));
                }
                return
                    (CompilerServices.CompileExpression(
                         NovaExpression.Variable(
                             NovaExpression.InstanceRef(NovaExpression.Variable(Expression.Constant("self")),
                                                        Expression.Constant(name.Substring(1)))), scope));
            }

            var val = scope[name];

            // The cast is needed here because if we get a non-nullable type (such as System.Int32) the check here will throw an exception.
            // By casting to System.Object we can avoid the exception since it is a boxed value that can be null.
            if ((object)val == null)
            {
                Type type;
                if ((type = NovaTypeResolver.Resolve(name)) != null)
                {
                    var @class = NovaClass.BoxClass(type);
                    scope.GlobalScope[@class.Name] = @class;
                    val = @class;
                }
            }
            return(val);
        }
Ejemplo n.º 2
0
 internal static dynamic Include(List <string> names)
 {
     // this has a different meaning when were defining classes
     if (!_inClassDefine)
     {
         var s = new StringBuilder();
         names.ForEach(name => s.AppendFormat("{0}.", name));
         s.Remove(s.Length - 1, 1);
         NovaTypeResolver.Include(s.ToString());
         return(null);
     }
     return(null);
 }
Ejemplo n.º 3
0
        internal static dynamic Begin(object rawTryExpression, List <Expression> rescueBlocksRaw,
                                      object rawEnsureBlock, object rawElseBlock, object rawScope)
        {
            var     tryExpression   = (Expression)rawTryExpression;
            var     ensureBlock     = (Expression)rawEnsureBlock;
            var     elseBlock       = (Expression)rawElseBlock;
            var     scope           = (NovaScope)rawScope;
            dynamic retVal          = null;
            var     exceptionRaised = false;
            var     ensureRun       = false;
            var     rescueBlocks    = new List <RescueExpression>();

            rescueBlocksRaw.ForEach(
                rawBlock =>
            {
                var block = rawBlock as RescueExpression;
                if (block != null)
                {
                    rescueBlocks.Add(block);
                }
            });

            try
            {
                retVal = CompilerServices.CompileExpression(tryExpression, scope);
            }
            catch (Exception e)
            {
                var NovaException = e as NovaException;
                var exType        = NovaException != null ? NovaException.ExceptionClass.Name : e.GetType().Name;
                var found         = false;
                exceptionRaised = true;
                foreach (var rescueBlock in rescueBlocks)
                {
                    var exceptionTypes = new List <string>();
                    if (!rescueBlock.IsWildcard)
                    {
                        foreach (var type in rescueBlock.ExceptionTypes)
                        {
                            var obj      = Resolve(type, scope);
                            var instance = obj as NovaInstance;
                            if (instance != null)
                            {
                                exceptionTypes.Add(instance.Class.Name);
                            }
                            else
                            {
                                var @class = obj as NovaClass;
                                if (@class != null)
                                {
                                    exceptionTypes.Add(@class.Name);
                                }
                                var s = obj as string;
                                if (s != null)
                                {
                                    exceptionTypes.Add(s);
                                }
                                var ss = obj as NovaString;
                                if (ss != null)
                                {
                                    exceptionTypes.Add(ss);
                                }
                            }
                        }
                    }
                    var exMatches = rescueBlock.IsWildcard;
                    if (!exMatches)
                    {
                        if ((from type in exceptionTypes select NovaTypeResolver.Resolve(type) into _exType where _exType != null let __exType = NovaTypeResolver.Resolve(exType) where __exType != null && __exType.IsSubclassOf(_exType) || __exType == _exType select _exType).Any())
                        {
                            exMatches = true;
                        }
                    }
                    found = exMatches;
                    if (!found)
                    {
                        if (exceptionTypes.Contains(exType))
                        {
                            found = true;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    var exception = e as NovaException;
                    if (exception != null)
                    {
                        scope[rescueBlock.VarName] = exception.InnerObject;
                    }
                    else
                    {
                        scope[rescueBlock.VarName] = e;
                    }
                    try
                    {
                        retVal = CompilerServices.CompileExpression(rescueBlock.Body, scope);
                    }
                    catch (Exception)
                    {
                        if (ensureBlock == null)
                        {
                            throw;
                        }
                        ensureRun = true;
                        CompilerServices.CompileExpression(ensureBlock, scope);
                        throw;
                    }
                    break;
                }
                if (!found)
                {
                    throw;
                }
            }
            finally
            {
                if (!exceptionRaised && elseBlock != null)
                {
                    CompilerServices.CompileExpression(elseBlock, scope);
                }
                if (!ensureRun && ensureBlock != null)
                {
                    CompilerServices.CompileExpression(ensureBlock, scope);
                }
            }
            return(retVal);
        }