40
40
*/
41
41
package com .oracle .graal .python .builtins .objects .code ;
42
42
43
- import static com .oracle .graal .python .nodes .truffle .TruffleStringMigrationPythonTypes .assertNoJavaString ;
44
-
45
- import java .util .ArrayList ;
46
- import java .util .List ;
43
+ import java .util .Arrays ;
47
44
48
45
import org .graalvm .polyglot .io .ByteSequence ;
49
46
50
47
import com .oracle .graal .python .PythonLanguage ;
51
48
import com .oracle .graal .python .builtins .modules .MarshalModuleBuiltins ;
52
49
import com .oracle .graal .python .builtins .objects .function .Signature ;
53
- import com .oracle .graal .python .builtins .objects .str .PString ;
54
50
import com .oracle .graal .python .compiler .CodeUnit ;
55
51
import com .oracle .graal .python .nodes .IndirectCallNode ;
56
52
import com .oracle .graal .python .nodes .PNodeWithContext ;
57
53
import com .oracle .graal .python .nodes .PRootNode ;
58
54
import com .oracle .graal .python .nodes .bytecode .PBytecodeGeneratorFunctionRootNode ;
59
55
import com .oracle .graal .python .nodes .bytecode .PBytecodeRootNode ;
60
56
import com .oracle .graal .python .nodes .util .BadOPCodeNode ;
61
- import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
62
57
import com .oracle .graal .python .runtime .ExecutionContext .IndirectCallContext ;
63
58
import com .oracle .graal .python .runtime .PythonContext ;
64
59
import com .oracle .graal .python .runtime .PythonOptions ;
@@ -101,8 +96,8 @@ public Assumption needNotPassExceptionAssumption() {
101
96
public PCode execute (VirtualFrame frame , int argcount ,
102
97
int posonlyargcount , int kwonlyargcount ,
103
98
int nlocals , int stacksize , int flags ,
104
- byte [] codedata , Object [] constants , Object [] names ,
105
- Object [] varnames , Object [] freevars , Object [] cellvars ,
99
+ byte [] codedata , Object [] constants , TruffleString [] names ,
100
+ TruffleString [] varnames , TruffleString [] freevars , TruffleString [] cellvars ,
106
101
TruffleString filename , TruffleString name , int firstlineno ,
107
102
byte [] lnotab ) {
108
103
@@ -122,8 +117,8 @@ public PCode execute(VirtualFrame frame, int argcount,
122
117
private static PCode createCode (PythonLanguage language , PythonContext context , @ SuppressWarnings ("unused" ) int argcount ,
123
118
@ SuppressWarnings ("unused" ) int posonlyargcount , @ SuppressWarnings ("unused" ) int kwonlyargcount ,
124
119
int nlocals , int stacksize , int flags ,
125
- byte [] codedata , Object [] constants , Object [] names ,
126
- Object [] varnames , Object [] freevars , Object [] cellvars ,
120
+ byte [] codedata , Object [] constants , TruffleString [] names ,
121
+ TruffleString [] varnames , TruffleString [] freevars , TruffleString [] cellvars ,
127
122
TruffleString filename , TruffleString name , int firstlineno ,
128
123
byte [] lnotab ) {
129
124
@@ -132,7 +127,7 @@ private static PCode createCode(PythonLanguage language, PythonContext context,
132
127
ct = language .createCachedCallTarget (l -> new BadOPCodeNode (l , name ), BadOPCodeNode .class , filename , name );
133
128
} else {
134
129
if (context .getOption (PythonOptions .EnableBytecodeInterpreter )) {
135
- ct = create ().deserializeForBytecodeInterpreter (language , codedata );
130
+ ct = create ().deserializeForBytecodeInterpreter (language , codedata , cellvars , freevars );
136
131
} else {
137
132
RootNode rootNode = context .getSerializer ().deserialize (context , codedata , toStringArray (cellvars ), toStringArray (freevars ));
138
133
ct = PythonUtils .getOrCreateCallTarget (rootNode );
@@ -146,8 +141,17 @@ private static PCode createCode(PythonLanguage language, PythonContext context,
146
141
firstlineno , lnotab );
147
142
}
148
143
149
- private RootCallTarget deserializeForBytecodeInterpreter (PythonLanguage language , byte [] data ) {
144
+ private RootCallTarget deserializeForBytecodeInterpreter (PythonLanguage language , byte [] data , TruffleString [] cellvars , TruffleString [] freevars ) {
150
145
CodeUnit code = MarshalModuleBuiltins .deserializeCodeUnit (data );
146
+ if (cellvars != null && !Arrays .equals (code .cellvars , cellvars ) || freevars != null && !Arrays .equals (code .freevars , freevars )) {
147
+ code = new CodeUnit (code .name , code .qualname , code .argCount , code .kwOnlyArgCount , code .positionalOnlyArgCount , code .stacksize , code .code ,
148
+ code .srcOffsetTable , code .flags , code .names , code .varnames ,
149
+ cellvars != null ? cellvars : code .cellvars , freevars != null ? freevars : code .freevars ,
150
+ code .cell2arg , code .constants , code .primitiveConstants , code .exceptionHandlerRanges , code .conditionProfileCount ,
151
+ code .startOffset , code .startLine ,
152
+ code .outputCanQuicken , code .variableShouldUnbox ,
153
+ code .generalizeInputsMap , code .generalizeVarsMap );
154
+ }
151
155
RootNode rootNode = new PBytecodeRootNode (language , code , null , null );
152
156
if (code .isGeneratorOrCoroutine ()) {
153
157
rootNode = new PBytecodeGeneratorFunctionRootNode (language , rootNode .getFrameDescriptor (), (PBytecodeRootNode ) rootNode , code .name );
@@ -176,18 +180,15 @@ public static PCode createCode(PythonContext context, int flags, byte[] codedata
176
180
}
177
181
178
182
@ TruffleBoundary
179
- private static String [] toStringArray (Object [] array ) {
180
- List <String > list = new ArrayList <>(array .length );
181
- for (Object item : array ) {
182
- item = assertNoJavaString (item );
183
- if (item instanceof TruffleString ) {
184
- list .add (((TruffleString ) item ).toJavaStringUncached ());
185
- }
186
- if (item instanceof PString ) {
187
- list .add (CastToJavaStringNode .getUncached ().execute (item ));
188
- }
183
+ private static String [] toStringArray (TruffleString [] array ) {
184
+ if (array == null ) {
185
+ return null ;
186
+ }
187
+ String [] result = new String [array .length ];
188
+ for (int i = 0 ; i < array .length ; i ++) {
189
+ result [i ] = array [i ].toJavaStringUncached ();
189
190
}
190
- return list . toArray ( new String [ list . size ()]) ;
191
+ return result ;
191
192
}
192
193
193
194
public static CreateCodeNode create () {
0 commit comments