DLR scope extension. Thread safe.
상속: Microsoft.Scripting.Runtime.ScopeExtension
예제 #1
0
        // thread-safe:
        // Returns null on success, the lexically inner-most module on failure.
        internal RubyModule TryResolveConstantNoLock(RubyGlobalScope autoloadScope, string /*!*/ name, out ConstantStorage result)
        {
            var context = RubyContext;

            context.RequiresClassHierarchyLock();

            RubyScope scope = this;

            // lexical lookup first:
            RubyModule innerMostModule = null;

            do
            {
                RubyModule module = scope.Module;

                if (module != null)
                {
                    Debug.Assert(module.Context == context);

                    if (module.TryGetConstantNoLock(autoloadScope, name, out result))
                    {
                        return(null);
                    }

                    // remember the module:
                    if (innerMostModule == null)
                    {
                        innerMostModule = module;
                    }
                }

                scope = scope.Parent;
            } while (scope != null);

            // check the inner most module and it's base classes/mixins:
            if (innerMostModule != null)
            {
                if (innerMostModule.TryResolveConstantNoLock(autoloadScope, name, out result))
                {
                    return(null);
                }
            }
            else
            {
                innerMostModule = context.ObjectClass;
            }

            if (context.ObjectClass.TryResolveConstantNoLock(autoloadScope, name, out result))
            {
                return(null);
            }

            return(innerMostModule);
        }
예제 #2
0
        public RubyConstructor(RubyGlobalScope/*!*/ scope, NodeProvider/*!*/ nodeProvider)
            : base(nodeProvider, scope) {
            
            _newSite = CallSite<Func<CallSite, RubyModule, object, object, object, object>>.Create(
                RubyCallAction.Make(scope.Context, "new", RubyCallSignature.WithImplicitSelf(3))
            ); 

            _yamlInitializeSite = CallSite<Func<CallSite, object, object, Hash, object>>.Create(
                RubyCallAction.Make(scope.Context, "yaml_initialize", RubyCallSignature.WithImplicitSelf(3))
            );
        }
        public void OverloadResolution_Block() {
            var t = GetType();

            var gse = new RubyGlobalScope(Context, new Scope(), new object(), true);
            var scope = new RubyTopLevelScope(gse, null, new SymbolDictionary());
            var proc = new Proc(ProcKind.Proc, null, scope, new BlockDispatcher0((x, y) => null, BlockSignatureAttributes.None));

            var scopeArg = new DynamicMetaObject(Ast.Constant(proc.LocalScope), BindingRestrictions.Empty, proc.LocalScope);
            var contextArg = new DynamicMetaObject(Ast.Constant(Context), BindingRestrictions.Empty, Context);
            var instanceInt = new DynamicMetaObject(Ast.Constant(1), BindingRestrictions.Empty, 1);
            var str = "foo";
            var instanceStr = new DynamicMetaObject(Ast.Constant(str), BindingRestrictions.Empty, str);
            var procArg = new DynamicMetaObject(Ast.Constant(proc), BindingRestrictions.Empty, proc);
            var nullArg = new DynamicMetaObject(Ast.Constant(Ast.Constant(null)), BindingRestrictions.Empty, null);

            var arguments = new[] {
                // 1.times
                new CallArguments(scopeArg, instanceInt, new DynamicMetaObject[0], RubyCallSignature.WithScope(0)),
                // 1.times &nil             
                new CallArguments(scopeArg, instanceInt, new[] {  nullArg }, RubyCallSignature.WithScopeAndBlock(0)),
                // 1.times &p                            
                new CallArguments(contextArg, instanceInt, new[] {  procArg }, RubyCallSignature.WithBlock(0)),
                // obj.times &p                          
                new CallArguments(contextArg, instanceStr, new[] {  procArg }, RubyCallSignature.WithBlock(0)),
            };

            var results = new[] {
                "Times2",
                "Times1",
                "Times3",
                "Times4",
            };

            for (int i = 0; i < arguments.Length; i++) {
                var bindingTarget = RubyMethodGroupInfo.ResolveOverload("times", new[] {
                    t.GetMethod("Times1"),
                    t.GetMethod("Times2"),
                    t.GetMethod("Times3"),
                    t.GetMethod("Times4"),
                }, arguments[i], true, false);

                Assert(bindingTarget.Success);
                Assert(bindingTarget.Method.Name == results[i]);
            }
        }
예제 #4
0
 internal bool TryResolveConstant(RubyContext/*!*/ callerContext, RubyGlobalScope autoloadScope, string/*!*/ name, out ConstantStorage value) {
     return callerContext != Context ?
         TryResolveConstant(autoloadScope, name, out value) :
         TryResolveConstantNoLock(autoloadScope, name, out value);
 }
예제 #5
0
            public bool Load(RubyGlobalScope/*!*/ autoloadScope) {
                if (_loaded) {
                    return false;
                }

                 using (autoloadScope.Context.ClassHierarchyUnlocker()) {
                     _loaded = true;
                     return autoloadScope.Context.Loader.LoadFile(autoloadScope.Scope, null, _path, LoadFlags.LoadOnce | LoadFlags.AppendExtensions);
                 }
            }
예제 #6
0
 internal MarshalReader(ReaderSites/*!*/ sites, BinaryReader/*!*/ reader, 
     RubyGlobalScope/*!*/ globalScope, Proc proc) {
     _sites = sites;
     _reader = reader;
     _globalScope = globalScope;
     _proc = proc;
     _symbols = new Dictionary<int, Symbol>();
     _objects = new Dictionary<int, object>();
 }
예제 #7
0
 /// <summary>
 /// Get constant defined in this module or any of its ancestors.
 /// </summary>
 public bool TryResolveConstant(RubyGlobalScope autoloadScope, string/*!*/ name, out object value) {
     return TryLookupConstant(true, true, autoloadScope, name, out value) != ConstantLookupResult.NotFound;
 }
예제 #8
0
        // thread-safe:
        // Returns null on success, the lexically inner-most module on failure.
        internal RubyModule TryResolveConstantNoLock(RubyGlobalScope autoloadScope, string/*!*/ name, out ConstantStorage result) {
            var context = RubyContext;
            context.RequiresClassHierarchyLock();
            
            RubyScope scope = this;

            // lexical lookup first:
            RubyModule innerMostModule = null;
            do {
                RubyModule module = scope.Module;

                if (module != null) {
                    if (module.TryGetConstant(context, autoloadScope, name, out result)) {
                        return null;
                    }

                    // remember the module:
                    if (innerMostModule == null) {
                        innerMostModule = module;
                    }
                }

                scope = scope.Parent;
            } while (scope != null);

            // check the inner most module and it's base classes/mixins:
            if (innerMostModule != null) {
                if (innerMostModule.TryResolveConstant(context, autoloadScope, name, out result)) {
                    return null;
                }
            } else {
                innerMostModule = context.ObjectClass;
            }

            if (context.ObjectClass.TryResolveConstant(context, autoloadScope, name, out result)) {
                return null;
            }

            return innerMostModule;
        }
예제 #9
0
 /// <summary>
 /// Get constant defined in this module or any of its ancestors.
 /// </summary>
 internal bool TryResolveConstantNoLock(RubyGlobalScope autoloadScope, string/*!*/ name, out ConstantStorage value) {
     Context.RequiresClassHierarchyLock();
     return TryLookupConstantNoLock(true, true, autoloadScope, name, out value) != ConstantLookupResult.NotFound;
 }
예제 #10
0
        public RubyConstructor(RubyGlobalScope/*!*/ scope, NodeProvider/*!*/ nodeProvider)
            : base(nodeProvider, scope) {            
            AddConstructor("tag:yaml.org,2002:str", ConstructRubyScalar);
            AddConstructor("tag:ruby.yaml.org,2002:range", ConstructRubyRange);
            AddConstructor("tag:ruby.yaml.org,2002:regexp", ConstructRubyRegexp);
            AddMultiConstructor("tag:ruby.yaml.org,2002:object:", ConstructPrivateObject);            
            AddMultiConstructor("tag:ruby.yaml.org,2002:struct:", ConstructRubyStruct);
            AddConstructor("tag:yaml.org,2002:binary", ConstructRubyBinary);
            AddConstructor("tag:yaml.org,2002:timestamp#ymd", ConstructRubyTimestampYMD);

            //AddConstructor("tag:yaml.org,2002:omap", ConstructRubyOmap);
            //AddMultiConstructor("tag:yaml.org,2002:seq:", ConstructSpecializedRubySequence);
            //AddMultiConstructor("tag:yaml.org,2002:map:", ConstructSpecializedRubyMap);
        }
예제 #11
0
 /// <summary>
 /// Get constant defined in this module.
 /// </summary>
 public bool TryGetConstantNoLock(RubyGlobalScope autoloadScope, string/*!*/ name, out object value) {
     Context.RequiresClassHierarchyLock();
     return TryLookupConstantNoLock(false, false, autoloadScope, name, out value) != ConstantLookupResult.NotFound;
 }
예제 #12
0
 /// <summary>
 /// Get constant defined in this module.
 /// </summary>
 public bool TryGetConstant(RubyGlobalScope autoloadScope, string/*!*/ name, out object value) {
     using (Context.ClassHierarchyLocker()) {
         return TryGetConstantNoLock(autoloadScope, name, out value);
     }
 }
예제 #13
0
 public RubyConstructor(RubyGlobalScope/*!*/ scope, NodeProvider/*!*/ nodeProvider)
     : base(nodeProvider, scope) {
 }
예제 #14
0
        private ConstantLookupResult TryLookupConstant(bool included, bool inherited, RubyGlobalScope autoloadScope, 
            string/*!*/ name, out object value) {

            Debug.Assert(included || !inherited);

            value = null;
            while (true) {
                object result;

                bool found = included ? 
                    TryResolveConstantNoAutoloadCheck(inherited, name, out result) :
                    TryGetConstantNoAutoloadCheck(name, out result);

                if (!found) {
                    return ConstantLookupResult.NotFound;
                }

                var autoloaded = result as AutoloadedConstant;
                if (autoloaded == null) {
                    value = result;
                    return ConstantLookupResult.Found;
                }

                if (autoloadScope == null) {
                    return ConstantLookupResult.FoundAutoload;
                }

                // autoloaded constants are removed before the associated file is loaded:
                RemoveConstant(name);

                // load file and try lookup again:
                if (!autoloaded.Load(autoloadScope)) {
                    return ConstantLookupResult.NotFound;
                }
            }
        }
예제 #15
0
 public bool TryGetConstant(RubyGlobalScope autoloadScope, string/*!*/ name, out object value) {
     ConstantStorage storage;
     var result = TryGetConstant(autoloadScope, name, out storage);
     value = storage.Value;
     return result;
 }
예제 #16
0
 /// <summary>
 /// Get constant defined in this module or any of its ancestors. 
 /// Autoloads if autoloadScope is not null.
 /// </summary>
 /// <remarks>
 /// Thread safe.
 /// </remarks>
 internal bool TryResolveConstant(RubyGlobalScope autoloadScope, string/*!*/ name, out ConstantStorage value) {
     using (Context.ClassHierarchyLocker()) {
         return TryResolveConstantNoLock(autoloadScope, name, out value);
     }
 }        
예제 #17
0
 public SafeConstructor(/*!*/NodeProvider nodeProvider, RubyGlobalScope/*!*/ scope)
     : base(nodeProvider, scope) {
 }
예제 #18
0
        private ConstantLookupResult TryLookupConstantNoLock(bool included, bool inherited, RubyGlobalScope autoloadScope,
            string/*!*/ name, out ConstantStorage value) {

            Context.RequiresClassHierarchyLock();
            Debug.Assert(included || !inherited);

            value = default(ConstantStorage);
            while (true) {
                ConstantStorage result;

                RubyModule owner = included ? 
                    TryResolveConstantNoAutoloadCheck(inherited, name, out result) :
                    (TryGetConstantNoAutoloadCheck(name, out result) ? this : null);

                if (owner == null) {
                    return ConstantLookupResult.NotFound;
                }

                var autoloaded = result.Value as AutoloadedConstant;
                if (autoloaded == null) {
                    value = result;
                    return ConstantLookupResult.Found;
                }

                if (autoloadScope == null) {
                    return ConstantLookupResult.FoundAutoload;
                }

                if (autoloadScope.Context != Context) {
                    throw RubyExceptions.CreateTypeError(String.Format("Cannot autoload constants to a foreign runtime #{0}", autoloadScope.Context.RuntimeId));
                }

                // autoloaded constants are removed before the associated file is loaded:
                object _;
                owner.TryRemoveConstantNoLock(name, out _);
                               
                // load file and try lookup again (releases the class hierarchy lock when loading the file):
                if (!autoloaded.Load(autoloadScope)) {
                    return ConstantLookupResult.NotFound;
                }
            }
        }
예제 #19
0
파일: RubyYaml.cs 프로젝트: bclubb/ironruby
 private static RubyConstructor/*!*/ MakeConstructor(RubyGlobalScope/*!*/ scope, TextReader/*!*/ reader) {
     return new RubyConstructor(scope, MakeComposer(reader));
 }
예제 #20
0
 public Constructor(NodeProvider/*!*/ nodeProvider, RubyGlobalScope/*!*/ scope)
     : base(nodeProvider, scope) {
 }
예제 #21
0
 private static RubyConstructor/*!*/ MakeConstructor(RubyGlobalScope/*!*/ scope, Stream/*!*/ stream) {
     return new RubyConstructor(scope, MakeComposer(scope.Context, stream));
 }
예제 #22
0
파일: RubyClass.cs 프로젝트: kashano/main
 internal void SetGlobalScope(RubyGlobalScope/*!*/ value) {
     Assert.NotNull(value);
     _globalScope = value;
 }
예제 #23
0
            public bool Load(RubyGlobalScope/*!*/ autoloadScope) {
                if (_loaded) {
                    return false;
                }

                _loaded = true;
                return autoloadScope.Context.Loader.LoadFile(autoloadScope.Scope, null, _path, LoadFlags.LoadOnce | LoadFlags.AppendExtensions);
            }