PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DLR_Main/Languages/IronPython/IronPython/Compiler/OnDiskScriptCode.cs

https://bitbucket.org/mdavid/dlr
C# | 110 lines | 69 code | 19 blank | 22 comment | 5 complexity | b75f733baa8c71fe6e32c84f7e232c82 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using System.Reflection;
  19. using Microsoft.Scripting;
  20. using Microsoft.Scripting.Runtime;
  21. using Microsoft.Scripting.Utils;
  22. using IronPython.Runtime;
  23. using IronPython.Runtime.Operations;
  24. namespace IronPython.Compiler {
  25. /// <summary>
  26. /// A ScriptCode which has been loaded from an assembly which is saved on disk.
  27. /// </summary>
  28. class OnDiskScriptCode : RunnableScriptCode {
  29. private readonly LookupCompilationDelegate _target;
  30. private CodeContext _optimizedContext;
  31. private readonly string _moduleName;
  32. public OnDiskScriptCode(LookupCompilationDelegate code, SourceUnit sourceUnit, string moduleName) :
  33. base(MakeAstFromSourceUnit(sourceUnit)) {
  34. _target = code;
  35. _moduleName = moduleName;
  36. }
  37. /// <summary>
  38. /// Creates a fake PythonAst object which is represenative of the on-disk script code.
  39. /// </summary>
  40. private static Ast.PythonAst MakeAstFromSourceUnit(SourceUnit sourceUnit) {
  41. var compCtx = new CompilerContext(sourceUnit, new PythonCompilerOptions(), ErrorSink.Null);
  42. return new Ast.PythonAst(compCtx);
  43. }
  44. public override object Run() {
  45. CodeContext ctx = CreateContext();
  46. try {
  47. var funcCode = EnsureFunctionCode(_target, false, true);
  48. PushFrame(ctx, funcCode);
  49. return _target(ctx, funcCode);
  50. } finally {
  51. PopFrame();
  52. }
  53. }
  54. public override object Run(Scope scope) {
  55. if (scope == CreateScope()) {
  56. return Run();
  57. }
  58. throw new NotSupportedException();
  59. }
  60. public string ModuleName {
  61. get {
  62. return _moduleName;
  63. }
  64. }
  65. public override FunctionCode GetFunctionCode(bool register) {
  66. return EnsureFunctionCode(_target, false, register);
  67. }
  68. public override Scope CreateScope() {
  69. return CreateContext().GlobalScope;
  70. }
  71. internal CodeContext CreateContext() {
  72. if (_optimizedContext == null) {
  73. CachedOptimizedCodeAttribute[] attrs = (CachedOptimizedCodeAttribute[])_target.Method.GetCustomAttributes(typeof(CachedOptimizedCodeAttribute), false);
  74. // create the CompilerContext for the ScriptCode
  75. CachedOptimizedCodeAttribute optimizedCode = attrs[0];
  76. // create the storage for the global scope
  77. Dictionary<string, PythonGlobal> globals = new Dictionary<string, PythonGlobal>(StringComparer.Ordinal);
  78. PythonGlobal[] globalArray = new PythonGlobal[optimizedCode.Names.Length];
  79. var dict = new PythonDictionary(new GlobalDictionaryStorage(globals, globalArray));
  80. ModuleContext mc = new ModuleContext(dict, (PythonContext)SourceUnit.LanguageContext);
  81. CodeContext res = mc.GlobalContext;
  82. for (int i = 0; i < optimizedCode.Names.Length; i++) {
  83. string name = optimizedCode.Names[i];
  84. globalArray[i] = globals[name] = new PythonGlobal(res, name);
  85. }
  86. _optimizedContext = CreateTopLevelCodeContext(dict, (PythonContext)SourceUnit.LanguageContext);
  87. }
  88. return _optimizedContext;
  89. }
  90. }
  91. }