|
70 | 70 | import com.oracle.truffle.api.frame.VirtualFrame;
|
71 | 71 | import com.oracle.truffle.api.interop.UnsupportedMessageException;
|
72 | 72 | import com.oracle.truffle.api.library.CachedLibrary;
|
| 73 | +import com.oracle.truffle.api.profiles.ConditionProfile; |
73 | 74 |
|
74 | 75 | @CoreFunctions(extendClasses = {PythonBuiltinClassType.PFunction, PythonBuiltinClassType.PBuiltinFunction})
|
75 | 76 | public class AbstractFunctionBuiltins extends PythonBuiltins {
|
@@ -166,9 +167,27 @@ Object getClosure(Object self) {
|
166 | 167 | @GenerateNodeFactory
|
167 | 168 | public abstract static class GetGlobalsNode extends PythonBuiltinNode {
|
168 | 169 | @Specialization(guards = "!isBuiltinFunction(self)")
|
169 |
| - Object getGlobals(PFunction self) { |
| 170 | + Object getGlobals(PFunction self, |
| 171 | + @CachedLibrary(limit = "1") PythonObjectLibrary lib, |
| 172 | + @Cached("createBinaryProfile()") ConditionProfile moduleGlobals, |
| 173 | + @Cached("createBinaryProfile()") ConditionProfile moduleHasNoDict) { |
170 | 174 | // see the make_globals_function from lib-graalpython/functions.py
|
171 |
| - return self.getGlobals(); |
| 175 | + PythonObject globals = self.getGlobals(); |
| 176 | + if (moduleGlobals.profile(globals instanceof PythonModule)) { |
| 177 | + PHashingCollection dict = lib.getDict(globals); |
| 178 | + if (moduleHasNoDict.profile(dict == null)) { |
| 179 | + dict = factory().createDictFixedStorage(globals); |
| 180 | + try { |
| 181 | + lib.setDict(globals, dict); |
| 182 | + } catch (UnsupportedMessageException e) { |
| 183 | + CompilerDirectives.transferToInterpreter(); |
| 184 | + throw new IllegalStateException(e); |
| 185 | + } |
| 186 | + } |
| 187 | + return dict; |
| 188 | + } else { |
| 189 | + return globals; |
| 190 | + } |
172 | 191 | }
|
173 | 192 |
|
174 | 193 | @SuppressWarnings("unused")
|
|
0 commit comments