forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunnableScriptCode.cs
More file actions
100 lines (82 loc) · 3.21 KB
/
RunnableScriptCode.cs
File metadata and controls
100 lines (82 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
namespace IronPython.Compiler {
internal abstract class RunnableScriptCode : ScriptCode {
internal FunctionCode _code;
private readonly Compiler.Ast.PythonAst _ast;
public RunnableScriptCode(Compiler.Ast.PythonAst ast)
: base(ast.SourceUnit) {
_ast = ast;
}
public override object Run() {
return base.Run();
}
public override object Run(Scope scope) {
throw new NotImplementedException();
}
protected static CodeContext/*!*/ CreateTopLevelCodeContext(PythonDictionary/*!*/ dict, LanguageContext/*!*/ context) {
ModuleContext modContext = new ModuleContext(dict, (PythonContext)context);
return modContext.GlobalContext;
}
protected static CodeContext GetContextForScope(Scope scope, SourceUnit sourceUnit) {
if (!(scope.GetExtension(sourceUnit.LanguageContext.ContextId) is PythonScopeExtension ext)) {
ext = sourceUnit.LanguageContext.EnsureScopeExtension(scope) as PythonScopeExtension;
}
CodeContext ctx = ext.ModuleContext.GlobalContext;
return ctx;
}
protected FunctionCode EnsureFunctionCode(Delegate/*!*/ dlg) {
return EnsureFunctionCode(dlg, false, true);
}
protected FunctionCode EnsureFunctionCode(Delegate/*!*/ dlg, bool tracing, bool register) {
Debug.Assert(dlg != null);
if (_code == null) {
Interlocked.CompareExchange(
ref _code,
new FunctionCode(
(PythonContext)SourceUnit.LanguageContext,
dlg,
_ast,
_ast.GetDocumentation(_ast),
tracing,
register
),
null
);
}
return _code;
}
public Compiler.Ast.PythonAst Ast {
get {
return _ast;
}
}
public FunctionCode Code {
get {
return _code;
}
}
public abstract FunctionCode GetFunctionCode(bool register);
protected void PushFrame(CodeContext context, FunctionCode code) {
if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) {
PythonOps.PushFrame(context, code);
}
}
protected void PopFrame() {
if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) {
List<FunctionStack> stack = PythonOps.GetFunctionStack();
stack.RemoveAt(stack.Count - 1);
}
}
}
}