public void WrapImported()
        {
            // referenced module
            var refdMod = new ModuleName("refd", new Version());
            var refd = SymbolStore.Create();
            var a = SymbolStore.Create();
            var b = Symbol.CreateReference(EntityRef.Command.Create("c"), NoSourcePosition.Instance);
            a.Declare("b", b);
            var nsa = new MergedNamespace(a);
            refd.Declare("a", Symbol.CreateNamespace(nsa, NoSourcePosition.Instance));

            // referencing module
            var external = SymbolStore.Create(conflictUnionSource: refd.Select(_exportFromModule(refdMod)));
            var mlv = ModuleLevelView.Create(external);
            Symbol syma;
            Assert.IsTrue(mlv.TryGet("a", out syma), "external symbol is accessible");

            // retrive namespace
            NamespaceSymbol nssyma;
            Assert.That(syma, Is.InstanceOf<NamespaceSymbol>(), "symbol a");
            Assert.IsTrue(syma.TryGetNamespaceSymbol(out nssyma), "looking up a results in a namespace symbol");

            // retrieve referenced symbol
            Symbol symb;
            Assert.That(nssyma.Namespace.TryGet("b", out symb), Is.True, "external symbol a.b is accessible");
            Assert.That(symb, Is.InstanceOf<ReferenceSymbol>(), "external symbol a.b");
            Assert.That(symb, Is.SameAs(b));

            // check that namespace is wrapped
            Assert.That(nssyma.Namespace,Is.InstanceOf<LocalNamespace>(),"namespace a when looked up locally");
            var localns = (LocalNamespace) nssyma.Namespace;
            var symd = Symbol.CreateReference(EntityRef.Command.Create("e"),NoSourcePosition.Instance);
            localns.DeclareExports(new KeyValuePair<string, Symbol>("d",symd).Singleton());

            Symbol symd2;
            Assert.That(nssyma.Namespace.TryGet("d",out symd2),Is.True,"Symbol a.d looked up locally");
            Assert.That(symd2,Is.EqualTo(symd),"Symbol retrieved locally compared to the symbol declared");
            
            Assert.That(nsa.TryGet("d",out symd2),Is.False,"Existence of symbol a.d looked up from referenced module");
        }
Beispiel #2
0
        public void AliasedNamespaceLookup()
        {

            var ldr = new Loader(options);
            Compile(ldr, @"
function f = 17;
");
            Symbol f;
            if (!ldr.TopLevelSymbols.TryGet("f", out f))
                Assert.Fail("Expected module level symbol f to exist.");

            var scopea = SymbolStore.Create();
            scopea.Declare("g", f);
            var nsa = new MergedNamespace(scopea);
            var a = Symbol.CreateNamespace(nsa, NoSourcePosition.Instance);

            var scopeb = SymbolStore.Create();
            scopeb.Declare("a", a);
            var nsb = new MergedNamespace(scopeb);
            var b = Symbol.CreateNamespace(nsb, NoSourcePosition.Instance);

            ldr.TopLevelSymbols.Declare("b", b);

            Compile(ldr, @"
declare(z = sym(""b"",""a""));
function main()
{
    return z.g;
}
");

            Expect(17);
        }
        public void WrapMergedNoConflict()
        {
            // Reference module #1
            var refd1Mod = new ModuleName("refd1", new Version());
            var refd1 = SymbolStore.Create();
            var a1 = SymbolStore.Create();
            var b = Symbol.CreateReference(EntityRef.Command.Create("c"), NoSourcePosition.Instance);
            a1.Declare("b", b);
            var nsa1 = new MergedNamespace(a1);
            refd1.Declare("a", Symbol.CreateNamespace(nsa1, NoSourcePosition.Instance));

            // Referenced module #2
            var refd2Mod = new ModuleName("refd2", new Version());
            var refd2 = SymbolStore.Create();
            var a2 = SymbolStore.Create();
            var f = Symbol.CreateReference(EntityRef.Function.Create("f",refd2Mod), NoSourcePosition.Instance);
            a2.Declare("f", f);
            var nsa2 = new MergedNamespace(a2);
            refd2.Declare("a", Symbol.CreateNamespace(nsa2, NoSourcePosition.Instance));

            // Referencing module
            var external = SymbolStore.Create(conflictUnionSource: refd1.Select(_exportFromModule(refd1Mod)).Append(refd2.Select(_exportFromModule(refd2Mod))));
            var mlv = ModuleLevelView.Create(external);
            Symbol syma;
            Assert.IsTrue(mlv.TryGet("a", out syma), "external symbol is accessible");

            // retrive namespace
            NamespaceSymbol nssyma;
            Assert.That(syma, Is.InstanceOf<NamespaceSymbol>(), "symbol a");
            Assert.IsTrue(syma.TryGetNamespaceSymbol(out nssyma), "looking up `a` results in a namespace symbol");

            // retrieve referenced symbol b
            Symbol symb;
            Assert.That(nssyma.Namespace.TryGet("b", out symb), Is.True, "external symbol a.b is accessible");
            Assert.That(symb, Is.InstanceOf<ReferenceSymbol>(), "external symbol a.b");
            Assert.That(symb, Is.SameAs(b));

            // retrieve reference symbol f
            Assert.That(nssyma.Namespace.TryGet("f", out symb), Is.True, "external symbol a.f is accessible");
            Assert.That(symb, Is.InstanceOf<ReferenceSymbol>(), "external symbol a.f");
            Assert.That(symb, Is.SameAs(f));

            // check that namespace is wrapped
            Assert.That(nssyma.Namespace, Is.InstanceOf<LocalNamespace>(), "namespace a when looked up locally");
            var localns = (LocalNamespace)nssyma.Namespace;
            var symd = Symbol.CreateReference(EntityRef.Command.Create("e"), NoSourcePosition.Instance);
            // shadows f, but doesn't modify the external namespace that defines f
            localns.DeclareExports(new KeyValuePair<string, Symbol>("f", symd).Singleton()); 

            Symbol symd2;
            Assert.That(nssyma.Namespace.TryGet("f", out symd2), Is.True, "Symbol a.f looked up locally");
            Assert.That(symd2, Is.EqualTo(symd), "Symbol retrieved locally compared to the symbol declared");

            // Check original namespaces (should be unmodified)
            Assert.That(nsa1.TryGet("f", out symd2), Is.False, "Existence of symbol a.f looked up from referenced module #1");
            Assert.That(nsa2.TryGet("f", out symd2), Is.True, "Existence of symbol a.f looked up from referenced module #2");
            Assert.That(symd2,Is.EqualTo(f),"a.f looked up from module #2 (should be unmodified)");
        }