ScriptCode is an instance of compiled code that is bound to a specific LanguageContext but not a specific ScriptScope. The code can be re-executed multiple times in different scopes. Hosting API counterpart for this class is CompiledCode.
示例#1
0
        /// <summary>
        /// Reloads a module from disk and executes the new module body.
        /// </summary>
        public void Reload()
        {
            if (_codeBlocks.Length > 0)
            {
                ScriptCode[] newCode = new ScriptCode[_codeBlocks.Length];

                for (int i = 0; i < _moduleContexts.Length; i++)
                {
                    if (_moduleContexts[i] != null)
                    {
                        _moduleContexts[i].ModuleReloading();
                    }
                }

                // get the new ScriptCode's...
                for (int i = 0; i < _codeBlocks.Length; i++)
                {
                    newCode[i] = _codeBlocks[i].LanguageContext.Reload(_codeBlocks[i], this);
                }

                // run the new code in the existing scope
                // we don't clear the scope before doing this
                _codeBlocks = newCode;

                for (int i = 0; i < _moduleContexts.Length; i++)
                {
                    if (_moduleContexts[i] != null)
                    {
                        _moduleContexts[i].ModuleReloaded();
                    }
                }

                Execute();
            }
        }
示例#2
0
 internal void AddScriptCode(ScriptCode code) {
     string name = code.SourceUnit.Path;
     name = name.Replace(Path.DirectorySeparatorChar, '.');
     if (name.EndsWith("__init__.py")) {
         name = name.Substring(0, name.Length - ".__init__.py".Length);
     }
     _codes[name] = code;
 }
示例#3
0
        /// <summary>
        /// Creates a ScriptModule consisting of multiple ScriptCode blocks (possibly with each
        /// ScriptCode block belonging to a different language). 
        /// Can ONLY be called from ScriptDomainManager.CreateModule factory (due to host notification).
        /// </summary>
        internal ScriptModule(string name, ScriptModuleKind kind, Scope scope, ScriptCode[] codeBlocks) {
            Assert.NotNull(name, scope, codeBlocks);
            Assert.NotNull(codeBlocks);

            _codeBlocks = ArrayUtils.Copy(codeBlocks);
            _name = name;
            _scope = scope;
            _kind = kind;
            _moduleContexts = ModuleContext.EmptyArray;
        }
示例#4
0
        public object Eval(object form)
        {
            ScriptSource scriptSource = Engine.CreateScriptSourceFromString("<internal>");

            Expression expr = Generator.Eval(GetLanguageContext(), form);
            LambdaExpression ast = Expression.Lambda(expr);
            ast = new GlobalLookupRewriter().RewriteLambda(ast);
            ScriptCode code = new ScriptCode(ast, GetSourceUnit(scriptSource));
            return code.Run();
        }
示例#5
0
        /// <summary>
        /// Executes against a specified scope and reports errors to the given error sink.
        /// </summary>
        public object Execute(Scope scope, ErrorSink errorSink)
        {
            ContractUtils.RequiresNotNull(scope, "scope");

            ScriptCode compiledCode = Compile(_language.GetCompilerOptions(scope), errorSink);

            if (compiledCode == null)
            {
                throw new SyntaxErrorException();
            }

            return(compiledCode.Run(scope));
        }
示例#6
0
 internal void AddScriptCode(ScriptCode code) {
     OnDiskScriptCode onDiskCode = code as OnDiskScriptCode;
     if (onDiskCode != null) {
         if (onDiskCode.ModuleName == "__main__") {
             _codes["__main__"] = onDiskCode;
         } else {
             string name = code.SourceUnit.Path;
             name = name.Replace(Path.DirectorySeparatorChar, '.');
             if (name.EndsWith("__init__.py")) {
                 name = name.Substring(0, name.Length - ".__init__.py".Length);
             }
             _codes[name] = onDiskCode;
         }
     }
 }
示例#7
0
        /// <summary>
        /// Compiles a list of source units into a single module.
        /// <c>scope</c> can be <c>null</c>.
        /// <c>options</c> can be <c>null</c>.
        /// <c>errorSink</c> can be <c>null</c>.
        /// </summary>
        public ScriptModule CompileModule(string name, ScriptModuleKind kind, Scope scope, CompilerOptions options, ErrorSink errorSink,
                                          params SourceUnit[] sourceUnits)
        {
            Contract.RequiresNotNull(name, "name");
            Contract.RequiresNotNullItems(sourceUnits, "sourceUnits");

            // TODO: Two phases: parse/compile?

            // compiles all source units:
            ScriptCode[] scriptCodes = new ScriptCode[sourceUnits.Length];
            for (int i = 0; i < sourceUnits.Length; i++)
            {
                scriptCodes[i] = LanguageContext.FromEngine(sourceUnits[i].Engine).CompileSourceCode(sourceUnits[i], options, errorSink);
            }

            return(CreateModule(name, kind, scope, scriptCodes));
        }
示例#8
0
        internal FunctionCode(ScriptCode code, CompileFlags compilerFlags)
            : this(code) {

            if ((compilerFlags & CompileFlags.CO_FUTURE_DIVISION) != 0)
                _flags |= FunctionAttributes.FutureDivision;
        }
示例#9
0
        internal FunctionCode(ScriptCode code, CompileFlags compilerFlags, string fileName)
            : this(code) {

            if ((compilerFlags & CompileFlags.CO_FUTURE_DIVISION) != 0) {
                _flags |= FunctionAttributes.FutureDivision;
            }
            _filename = fileName;
            _closureVars = PythonTuple.EMPTY;
        }
示例#10
0
            public CompiledFile(ScriptCode/*!*/ compiledCode) {
                Assert.NotNull(compiledCode);

                CompiledCode = compiledCode;
            }
示例#11
0
 private void AddCompiledFile(string/*!*/ fullPath, ScriptCode/*!*/ compiledCode) {
     if (_context.RubyOptions.SavePath != null) {
         lock (_compiledFileMutex) {
             // TODO: allocate eagerly (as soon as config gets fixed)
             if (_compiledFiles == null) {
                 _compiledFiles = new Dictionary<string, CompiledFile>();
             }
             _compiledFiles[fullPath] = new CompiledFile(compiledCode);
         }
     }
 }
示例#12
0
 internal object CompileAndRun(Scope globalScope, ScriptCode/*!*/ code) {
     return globalScope != null ? code.Run(globalScope) : code.Run();
 }
示例#13
0
 public CodeInfo(MethodBuilder builder, ScriptCode code, Type delegateType) {
     Builder = builder;
     Code = code;
     DelegateType = delegateType;
 }
示例#14
0
 internal FunctionCode(ScriptCode code) {
     _code = code;
     _closureVars = PythonTuple.EMPTY;
 }
示例#15
0
 public virtual ScriptCode Reload(ScriptCode original, ScriptModule module)
 {
     original.SourceUnit.Reload();
     return CompileSourceCode(original.SourceUnit, Engine.GetModuleCompilerOptions(module));
 }
示例#16
0
 public ModuleLoader(ScriptCode sc) {
     _sc = sc;
 }
示例#17
0
        internal Scope Execute(Scope globalScope, ScriptCode/*!*/ code) {
            if (globalScope == null || code.LanguageContext != _context) {
                if (globalScope == null) {
                    globalScope = code.CreateScope();
                }

                if (code.SourceUnit.Path != null) {
                    LoadedScripts[Platform.GetFullPath(code.SourceUnit.Path)] = globalScope;
                }
                code.Run(globalScope);
                return globalScope;
            } else {
                code.Run(globalScope);
                return null;
            }
        }
示例#18
0
 public virtual ScriptCode Reload(ScriptCode original, ScriptModule module)
 {
     original.SourceUnit.Reload();
     return(CompileSourceCode(original.SourceUnit, Engine.GetModuleCompilerOptions(module)));
 }
示例#19
0
 internal FunctionCode(ScriptCode code) {
     _code = code;
 }
 protected override CodeGen CreateCodeGen(ScriptCode scriptCode)
 {
     return CompilerHelpers.CreateDynamicCodeGenerator(scriptCode.CompilerContext);
 }
 protected abstract CodeGen CreateCodeGen(ScriptCode scriptCode);
 protected override SlotFactory CreateSlotFactory(ScriptCode scriptCode)
 {
     return _languages[scriptCode.LanguageContext] = new TupleSlotFactory(typeof(ModuleGlobalDictionary<>));
 }
 protected abstract SlotFactory CreateSlotFactory(ScriptCode scriptCode);
示例#24
0
        /// <summary>
        /// Creates a module.
        /// <c>dictionary</c> can be <c>null</c>
        /// </summary>
        /// <returns></returns>
        public IScriptModule CreateModule(string name, ScriptModuleKind kind, IAttributesCollection dictionary, params ICompiledCode[] compiledCodes)
        {
            Contract.RequiresNotNullItems(compiledCodes, "compiledCodes");

            ScriptCode[] script_codes = new ScriptCode[compiledCodes.Length];
            for (int i = 0; i < compiledCodes.Length; i++) {
                script_codes[i] = ScriptCode.FromCompiledCode(compiledCodes[i] as CompiledCode);
                if (script_codes[i] == null) {
                    throw new ArgumentException(Resources.RemoteCodeModuleComposition, String.Format("{0}[{1}]", "compiledCodes", i));
                }
            }

            return _manager.CreateModule(name, kind, new Scope(dictionary), script_codes);
        }
        private ScopeAllocator CreateStorageAllocator(ScriptCode scriptCode)
        {
            ScopeAllocator allocator;
            if (!_allocators.TryGetValue(scriptCode.LanguageContext, out allocator)) {
                var sf = CreateSlotFactory(scriptCode) as StaticFieldSlotFactory;
                var mgf = new ModuleGlobalFactory(sf);
                var sf2 = new StaticFieldSlotFactory(sf.TypeGen);
                GlobalFieldAllocator gfa = new GlobalFieldAllocator(mgf);
                var gfa2 = new GlobalFieldAllocator(sf2);

                // Locals and globals are allocated from the same namespace for optimized modules
                ScopeAllocator global = new ScopeAllocator(null, gfa);
                allocator = new ScopeAllocator(global, gfa2);

                _allocators[scriptCode.LanguageContext] = allocator;
            }

            return allocator;
        }
示例#26
0
 public CodeInfo(MethodBuilder builder, ScriptCode code, Type delegateType)
 {
     Builder      = builder;
     Code         = code;
     DelegateType = delegateType;
 }
示例#27
0
        /// <summary>
        /// Compiles a list of source units into a single module.
        /// <c>scope</c> can be <c>null</c>.
        /// <c>options</c> can be <c>null</c>.
        /// <c>errorSink</c> can be <c>null</c>.
        /// </summary>
        public ScriptModule CompileModule(string name, ScriptModuleKind kind, Scope scope, CompilerOptions options, ErrorSink errorSink, 
            params SourceUnit[] sourceUnits)
        {
            Contract.RequiresNotNull(name, "name");
            Contract.RequiresNotNullItems(sourceUnits, "sourceUnits");

            // TODO: Two phases: parse/compile?

            // compiles all source units:
            ScriptCode[] scriptCodes = new ScriptCode[sourceUnits.Length];
            for (int i = 0; i < sourceUnits.Length; i++) {
                scriptCodes[i] = LanguageContext.FromEngine(sourceUnits[i].Engine).CompileSourceCode(sourceUnits[i], options, errorSink);
            }

            return CreateModule(name, kind, scope, scriptCodes);
        }
示例#28
0
        internal void SaveCompiledCode() {
            string savePath = _context.RubyOptions.SavePath;
            if (savePath != null) {
                lock (_compiledFileMutex) {
                    var assemblyPath = Path.Combine(savePath, Path.GetFileName(_context.RubyOptions.MainFile) + ".dll");

                    Utils.Log(String.Format("SAVING to {0}", Path.GetFullPath(assemblyPath)), "LOADER");

                    // TODO: allocate eagerly (as soon as config gets fixed)
                    if (_compiledFiles == null) {
                        _compiledFiles = new Dictionary<string, CompiledFile>();
                    }

                    ScriptCode[] codes = new ScriptCode[_compiledFiles.Count];
                    int i = 0;
                    foreach (CompiledFile file in _compiledFiles.Values) {
                        codes[i++] = file.CompiledCode;
                    }

                    ScriptCode.SaveToAssembly(assemblyPath, codes);
                }
            }
        }
        protected override SlotFactory CreateSlotFactory(ScriptCode scriptCode)
        {
            AssemblyGen ag = null;

            if (scriptCode.SourceUnit.Kind == SourceCodeKind.Default && scriptCode.CodeBlock.Name != "ironscheme.boot.new")
            {
              if (ScriptDomainManager.Options.DebugMode)
              {
                if ((ScriptDomainManager.Options.AssemblyGenAttributes & AssemblyGenAttributes.SaveAndReloadAssemblies) != 0)
                {
                  ag = ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly;
                    //CreateModuleAssembly(scriptCode);
                }
                else
                {
                  ag = ScriptDomainManager.CurrentManager.Snippets.DebugAssembly;
                }
              }
              else
              {
                if ((ScriptDomainManager.Options.AssemblyGenAttributes & AssemblyGenAttributes.SaveAndReloadAssemblies) != 0)
                {
                  ag = ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly;
                    //CreateModuleAssembly(scriptCode);
                }
                else
                {
                  ag = ScriptDomainManager.CurrentManager.Snippets.Assembly;
                }
              }
            }
            else
            {
              ag = ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly; //CreateModuleAssembly(scriptCode);
            }

            ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly = ag;

            TypeGen tg = GenerateModuleGlobalsType(ag, scriptCode);

            if (scriptCode.LibraryGlobals != null)
            {
              foreach (var kvp in scriptCode.LibraryGlobals)
              {
                var k = kvp.Key;
                var v = kvp.Value;

                var cg = v.Block.CreateGlobalMethodStub(tg);

                if (cg != null)
                {
                  CodeGen._codeBlockStubs[v.Block] = cg;
                  CodeGen._codeBlockLookup[k] = cg;
                }
              }
            }

            if (scriptCode.LibraryGlobalsX != null)
            {
              foreach (var kvp in scriptCode.LibraryGlobalsX)
              {
                var k = kvp.Key;
                var v = kvp.Value;

                var cg = v.Block.CreateGlobalMethodStub(tg);

                if (cg != null)
                {
                  CodeGen._codeBlockStubsX[v.Block] = cg;
                  CodeGen._codeBlockLookupX[k] = cg;
                }
              }
            }

            if (scriptCode.LibraryGlobalsN != null)
            {
              foreach (var kvp in scriptCode.LibraryGlobalsN)
              {
                var k = kvp.Key;
                var v = kvp.Value;

                var cgd = new List<CodeGenDescriptor>();

                foreach (var i in v)
                {
                  var cg = i.codeblock.Block.CreateGlobalMethodStub(tg);

                  if (cg != null)
                  {
                    CodeGen._codeBlockStubsN[i.codeblock.Block] = cg;

                    cgd.Add(new CodeGenDescriptor
                    {
                      arity = i.arity,
                      varargs = i.varargs,
                      cg = cg,
                    });
                  }
                }

                CodeGen._codeBlockLookupN[k] = cgd.ToArray();

              }
            }

            StaticFieldSlotFactory factory = new StaticFieldSlotFactory(tg);

            _languages[scriptCode.LanguageContext] = new LanguageInfo(factory, tg);

            return factory;
        }
示例#30
0
        internal object CompileAndRun(Scope/*!*/ globalScope, ScriptCode/*!*/ code, bool tryEvaluate) {
            long ts1 = Stopwatch.GetTimestamp();
            code.EnsureCompiled();
            long ts2 = Stopwatch.GetTimestamp();
            Interlocked.Add(ref _ILGenerationTimeTicks, ts2 - ts1);

            return code.Run(globalScope);
        }
 /// <summary>
 /// Creates a new assembly for generating a module, ensuring a unique filename like "filename.N.exe" for the generated assembly
 /// </summary>
 AssemblyGen CreateModuleAssembly(ScriptCode scriptCode)
 {
     var su = scriptCode.CompilerContext.SourceUnit;
       var ag = CreateModuleAssembly(su.Id);
       ag.SetSourceUnit(su);
       return ag;
 }
示例#32
0
        /// <summary>
        /// Reloads a module from disk and executes the new module body.
        /// </summary>
        public void Reload() {
            if (_codeBlocks.Length > 0) {
                ScriptCode[] newCode = new ScriptCode[_codeBlocks.Length];

                for (int i = 0; i < _moduleContexts.Length; i++) {
                    if (_moduleContexts[i] != null) {
                        _moduleContexts[i].ModuleReloading();
                    }
                }
                
                // get the new ScriptCode's...
                for (int i = 0; i < _codeBlocks.Length; i++) {
                    newCode[i] = _codeBlocks[i].LanguageContext.Reload(_codeBlocks[i], this);
                }

                // run the new code in the existing scope
                // we don't clear the scope before doing this
                _codeBlocks = newCode;

                for (int i = 0; i < _moduleContexts.Length; i++) {
                    if (_moduleContexts[i] != null) {
                        _moduleContexts[i].ModuleReloaded();
                    }
                }

                Execute();
            }
        }
 private TypeGen GenerateModuleGlobalsType(AssemblyGen ag, ScriptCode sc)
 {
     var n = sc.CodeBlock.Name;
       switch (n)
       {
     case "visit-code":
     case "invoke-code":
     case "guard-code":
       TypeGen tg = ag.DefinePublicType("syntax-" + sc.CodeBlock.Name, typeof(CustomSymbolDictionary));
       tg.AddCodeContextField();
       tg.DefaultConstructor = tg.TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
       return tg;
     default:
       return GenerateModuleGlobalsType(ag);
       }
 }
        protected override CodeGen CreateCodeGen(ScriptCode scriptCode)
        {
            LanguageInfo li = _languages[scriptCode.LanguageContext];

            return li.TypeGen.DefineMethod(CompilerHelpers.PublicStatic,
                "Initialize",
                typeof(object),
                new Type[] { typeof(CodeContext) },
                null);
        }
示例#35
0
        private void AstLocations1() {
            // DumpExpression uses private reflection:
            if (_driver.PartialTrust) return;

            var sourceUnit = Context.CreateSnippet(@"
def add a,b
  a + b
end

add 1, 1
add 'foo', 'bar'
", SourceCodeKind.Expression);

            var options = new RubyCompilerOptions();
            var parser = new Parser();
            var tokens = new List<KeyValuePair<SourceSpan, Tokens>>();

            parser.TokenSink = (token, span) => { tokens.Add(new KeyValuePair<SourceSpan, Tokens>(span, token)); };
            var ast = parser.Parse(sourceUnit, options, Context.RuntimeErrorSink);

            const int Id = 0x12345678;

            var lambda = CallSiteTracer.Transform<DlrMainCallTarget>(ast, sourceUnit, options, Id);
            var code = new ScriptCode(lambda, sourceUnit);

            var locations = new List<int>();
            CallSiteTracer.Register((context, args, result, id, location) => {
                locations.Add(location);
                Debug.Assert(id == Id);
                Debug.Assert(location > 0);

                //Console.WriteLine("-- {0} ---------", location);
                //Console.WriteLine(this);
                //Console.WriteLine(AstUtils.DumpExpression(result.Restrictions.ToExpression()));
                //Console.WriteLine();
                //Console.WriteLine(AstUtils.DumpExpression(result.Expression));
                //Console.WriteLine("----------------");
            });

            code.Run();

            Debug.Assert(locations.Count == 4 && locations[0] == 31 && locations[1] == 19 && locations[2] == 41 && locations[3] == 19);
        }