示例#1
0
        private List <Scope> GenerateScriptScopes()
        {
            List <Scope> scopes = new List <Scope>(_scriptCodes.Length);
            ScriptModule sm     = ScriptDomainManager.CurrentManager.Host.DefaultModule as ScriptModule;

            for (int i = 0; i < _scriptCodes.Length; i++)
            {
                ScriptCode scriptCode = _scriptCodes[i];

                // Force creation of names used in other script codes into all optimized dictionaries
                ScopeAllocator        allocator = _allocators[scriptCode.LanguageContext];
                IAttributesCollection iac       = CreateLanguageDictionary(scriptCode.LanguageContext, allocator);
                Scope scope = new Scope(sm.Scope, iac);

                // module context is filled later:
                CodeContext codeContext = new CodeContext(scope, scriptCode.LanguageContext);

                IModuleDictionaryInitialization ici = iac as IModuleDictionaryInitialization;
                if (ici != null)
                {
                    ici.InitializeModuleDictionary(codeContext);
                }

                scopes.Add(scope);
                _codeContexts.Add(codeContext);
            }
            return(scopes);
        }
示例#2
0
        internal static object Load(object filename, bool loadinmemory)
        {
            CodeContext cc = IronScheme.Compiler.BaseHelper.cc; // sneaky....

            string path = GetPath(filename as string);

            switch (Path.GetExtension(path))
            {
            case ".exe":
            case ".dll":
                const string newbf = "build/ironscheme.boot.dll";
                if (File.Exists(newbf) && File.GetLastWriteTime(newbf) > File.GetLastWriteTime(path))
                {
                    File.Delete("ironscheme.boot.old.dll");
                    File.Delete("ironscheme.boot.pdb");
                    File.Move("ironscheme.boot.dll", "ironscheme.boot.old.dll");
                    File.Move(newbf, "ironscheme.boot.dll");

                    const string newpdb = "build/ironscheme.boot.pdb";
                    if (File.Exists(newpdb))
                    {
                        File.Move(newpdb, "ironscheme.boot.pdb");
                    }
                }

                // just reference.?
                MethodInfo entry = null;

                Type ilmergefixup = typeof(Builtins).Assembly.GetType("#", false);

                if (ilmergefixup != null && BootfileAssembly == null)
                {
                    entry            = ilmergefixup.GetMethod("Initialize");
                    BootfileAssembly = typeof(Builtins).Assembly;
                }
                else
                {
                    Assembly ext = AssemblyLoad(path, loadinmemory);
                    // now that it is loaded, make sure we remove compiletime types
                    IronScheme.Compiler.ClrGenerator.ClearTypesFrom(ext);

                    foreach (Type t in ext.GetExportedTypes())
                    {
                        if (t.BaseType == typeof(CustomSymbolDictionary))
                        {
                            List <Type> ii = new List <Type>(t.GetInterfaces());
                            if (ii.Contains(typeof(IModuleDictionaryInitialization)))
                            {
                                entry = t.GetMethod("Initialize");
                                if (entry != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                if (entry == null)
                {
                    // what now?
                    throw new ArgumentException("No entry point");
                }
                else
                {
                    IModuleDictionaryInitialization init = Activator.CreateInstance(entry.DeclaringType) as
                                                           IModuleDictionaryInitialization;

                    CodeContext ccc = new CodeContext(cc, init as IAttributesCollection);
                    init.InitializeModuleDictionary(ccc);

                    var t = Delegate.CreateDelegate(typeof(CallTarget0), ccc, entry) as CallTarget0;
                    return(t());
                }

            default:
            {
                // check for already compiled version
                string cfn = Path.ChangeExtension(path, ".dll");
                if (File.Exists(cfn))
                {
                    DateTime ct = File.GetLastWriteTime(cfn);
                    if (!File.Exists(path) || ct >= File.GetLastWriteTime(path))
                    {
                        if (File.GetLastWriteTime(Path.Combine(ApplicationDirectory, "IronScheme.dll")) <= ct ||
                            cfn.EndsWith("ironscheme.boot.dll"))
                        {
                            path = cfn;
                            goto case ".dll";
                        }
                    }
                }

                cfn = Path.ChangeExtension(path, ".new.dll");
                if (File.Exists(cfn))
                {
                    DateTime ct = File.GetLastWriteTime(cfn);
                    if (!File.Exists(path) || ct >= File.GetLastWriteTime(path))
                    {
                        if (File.GetLastWriteTime(Path.Combine(ApplicationDirectory, "IronScheme.dll")) <= ct ||
                            cfn.EndsWith("ironscheme.boot.dll"))
                        {
                            path = cfn;
                            goto case ".dll";
                        }
                    }
                }

                if (!File.Exists(path))
                {
                    throw new FileNotFoundException("Not found", path);
                }

                SourceUnit su = ScriptDomainManager.CurrentManager.Host.TryGetSourceFileUnit(cc.LanguageContext.Engine, path, Encoding.Default);
#if DEBUG
                Stopwatch sw = Stopwatch.StartNew();
#endif
                ScriptModule sm = ScriptDomainManager.CurrentManager.CompileModule(Path.GetFileNameWithoutExtension(path), su);

                Compiler.SimpleGenerator.ClearGlobals();
#if DEBUG
                Trace.WriteLine(sw.ElapsedMilliseconds, "Compile module: " + sm.FileName);
                sw = Stopwatch.StartNew();
#endif
                object result = sm.GetScripts()[0].Run(sm);
#if DEBUG
                Trace.WriteLine(sw.ElapsedMilliseconds, "Run script: " + sm.GetScripts()[0].SourceUnit);
#endif
                return(result);
            }
            }
        }