Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ironpython3/Src/IronPython/Compiler/PythonScriptCode.cs at master · elfscript/ironpython3 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
elfscript
/
ironpython3
Public
forked from
IronLanguages/ironpython3
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
ironpython3
/
Src
/
IronPython
/
Compiler
/
PythonScriptCode.cs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
184 lines (149 loc) · 6.72 KB
master
Breadcrumbs
ironpython3
/
Src
/
IronPython
/
Compiler
/
PythonScriptCode.cs
Copy path
Top
File metadata and controls
Code
Blame
184 lines (149 loc) · 6.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// 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
.
Linq
.
Expressions
;
using
Microsoft
.
Scripting
.
Ast
;
using
MSAst
=
System
.
Linq
.
Expressions
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Threading
;
using
Microsoft
.
Scripting
;
using
Microsoft
.
Scripting
.
Generation
;
using
Microsoft
.
Scripting
.
Runtime
;
using
Microsoft
.
Scripting
.
Utils
;
using
IronPython
.
Compiler
.
Ast
;
using
IronPython
.
Runtime
;
using
IronPython
.
Runtime
.
Operations
;
namespace
IronPython
.
Compiler
{
/// <summary>
/// Represents a script code which can be dynamically bound to execute against
/// arbitrary Scope objects. This is used for code when the user runs against
/// a particular scope as well as for exec and eval code as well. It is also
/// used when tracing is enabled.
/// </summary>
internal
class
PythonScriptCode
:
RunnableScriptCode
{
private
CodeContext
_defaultContext
;
private
LookupCompilationDelegate
/*!*/
_target
,
_tracingTarget
;
// lazily compiled targets
public
PythonScriptCode
(
PythonAst
/*!*/
ast
)
:
base
(
ast
)
{
Assert
.
NotNull
(
ast
)
;
Debug
.
Assert
(
ast
.
Type
==
typeof
(
Expression
<
LookupCompilationDelegate
>
)
)
;
}
public
override
object
Run
(
)
{
if
(
SourceUnit
.
Kind
==
SourceCodeKind
.
Expression
)
{
return
EvalWrapper
(
DefaultContext
)
;
}
return
RunWorker
(
DefaultContext
)
;
}
public
override
object
Run
(
Scope
scope
)
{
CodeContext
ctx
=
GetContextForScope
(
scope
,
SourceUnit
)
;
if
(
SourceUnit
.
Kind
==
SourceCodeKind
.
Expression
)
{
return
EvalWrapper
(
ctx
)
;
}
return
RunWorker
(
ctx
)
;
}
private
object
RunWorker
(
CodeContext
ctx
)
{
LookupCompilationDelegate
target
=
GetTarget
(
true
)
;
Exception
e
=
PythonOps
.
SaveCurrentException
(
)
;
PushFrame
(
ctx
,
_code
)
;
try
{
return
target
(
ctx
,
_code
)
;
}
finally
{
PythonOps
.
RestoreCurrentException
(
e
)
;
PopFrame
(
)
;
}
}
private
LookupCompilationDelegate
GetTarget
(
bool
register
)
{
LookupCompilationDelegate
target
;
PythonContext
pc
=
(
PythonContext
)
Ast
.
CompilerContext
.
SourceUnit
.
LanguageContext
;
if
(
!
pc
.
EnableTracing
)
{
EnsureTarget
(
register
)
;
target
=
_target
;
}
else
{
EnsureTracingTarget
(
)
;
target
=
_tracingTarget
;
}
return
target
;
}
public
override
FunctionCode
GetFunctionCode
(
bool
register
)
{
GetTarget
(
register
)
;
return
_code
;
}
public
override
Scope
/*!*/
CreateScope
(
)
{
return
new
Scope
(
)
;
}
// wrapper so we can do minimal code gen for eval code
private
object
EvalWrapper
(
CodeContext
ctx
)
{
try
{
return
RunWorker
(
ctx
)
;
}
catch
(
Exception
e
)
{
PythonOps
.
UpdateStackTrace
(
e
,
ctx
,
Code
,
0
)
;
throw
;
}
}
private
LookupCompilationDelegate
CompileBody
(
LightExpression
<
LookupCompilationDelegate
>
lambda
)
{
LookupCompilationDelegate
func
;
var
extractConstant
=
ExtractConstant
(
lambda
)
;
if
(
extractConstant
!=
null
)
{
// skip compiling for really simple code
object
value
=
extractConstant
.
Value
;
return
(
codeCtx
,
functionCode
)
=>
value
;
}
PythonContext
pc
=
(
PythonContext
)
Ast
.
CompilerContext
.
SourceUnit
.
LanguageContext
;
if
(
ShouldInterpret
(
pc
)
)
{
func
=
lambda
.
Compile
(
pc
.
Options
.
CompilationThreshold
)
;
}
else
{
func
=
lambda
.
ReduceToLambda
(
)
.
Compile
(
pc
.
EmitDebugSymbols
(
Ast
.
CompilerContext
.
SourceUnit
)
)
;
}
return
func
;
}
private
bool
ShouldInterpret
(
PythonContext
pc
)
{
return
pc
.
ShouldInterpret
(
(
PythonCompilerOptions
)
Ast
.
CompilerContext
.
Options
,
Ast
.
CompilerContext
.
SourceUnit
)
;
}
private
static
PythonConstantExpression
ExtractConstant
(
LightExpression
<
LookupCompilationDelegate
>
lambda
)
{
if
(
!
(
lambda
.
Body
is
BlockExpression
body
)
||
body
.
Expressions
.
Count
!=
2
||
!
(
body
.
Expressions
[
0
]
is
DebugInfoExpression
)
||
body
.
Expressions
[
1
]
.
NodeType
!=
ExpressionType
.
Convert
||
!
(
(
(
MSAst
.
UnaryExpression
)
body
.
Expressions
[
1
]
)
.
Operand
is
PythonConstantExpression
)
)
{
return
null
;
}
return
(
PythonConstantExpression
)
(
(
MSAst
.
UnaryExpression
)
body
.
Expressions
[
1
]
)
.
Operand
;
}
private
void
EnsureTarget
(
bool
register
)
{
if
(
_target
==
null
)
{
_target
=
CompileBody
(
(
LightExpression
<
LookupCompilationDelegate
>
)
Ast
.
GetLambda
(
)
)
;
EnsureFunctionCode
(
_target
,
false
,
register
)
;
}
}
private
CodeContext
DefaultContext
{
get
{
if
(
_defaultContext
==
null
)
{
_defaultContext
=
CreateTopLevelCodeContext
(
new
PythonDictionary
(
)
,
Ast
.
CompilerContext
.
SourceUnit
.
LanguageContext
)
;
}
return
_defaultContext
;
}
}
private
void
EnsureTracingTarget
(
)
{
if
(
_tracingTarget
==
null
)
{
PythonContext
pc
=
(
PythonContext
)
Ast
.
CompilerContext
.
SourceUnit
.
LanguageContext
;
var
debugProperties
=
new
PythonDebuggingPayload
(
null
)
;
var
debugInfo
=
new
Microsoft
.
Scripting
.
Debugging
.
CompilerServices
.
DebugLambdaInfo
(
null
,
// IDebugCompilerSupport
null
,
// lambda alias
false
,
// optimize for leaf frames
null
,
// hidden variables
null
,
// variable aliases
debugProperties
// custom payload
)
;
var
lambda
=
(
Expression
<
LookupCompilationDelegate
>
)
pc
.
DebugContext
.
TransformLambda
(
(
MSAst
.
LambdaExpression
)
Ast
.
GetLambda
(
)
.
Reduce
(
)
,
debugInfo
)
;
LookupCompilationDelegate
func
=
ShouldInterpret
(
pc
)
?
lambda
.
LightCompile
(
pc
.
Options
.
CompilationThreshold
)
:
lambda
.
Compile
(
pc
.
EmitDebugSymbols
(
Ast
.
CompilerContext
.
SourceUnit
)
)
;
_tracingTarget
=
func
;
debugProperties
.
Code
=
EnsureFunctionCode
(
_tracingTarget
,
true
,
true
)
;
}
}
}
}
You can’t perform that action at this time.