55
55
import com .oracle .graal .python .builtins .objects .PNone ;
56
56
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
57
57
import com .oracle .graal .python .builtins .objects .function .PFunction ;
58
+ import com .oracle .graal .python .builtins .objects .method .PBuiltinMethod ;
59
+ import com .oracle .graal .python .builtins .objects .method .PMethod ;
60
+ import com .oracle .graal .python .builtins .objects .module .PythonModule ;
58
61
import com .oracle .graal .python .nodes .SpecialAttributeNames ;
62
+ import com .oracle .graal .python .nodes .attributes .GetAttributeNode ;
59
63
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
64
+ import com .oracle .graal .python .nodes .util .CastToStringNode ;
60
65
import com .oracle .graal .python .runtime .PythonContext ;
61
66
import com .oracle .graal .python .runtime .PythonCore ;
62
67
import com .oracle .graal .python .runtime .PythonOptions ;
63
68
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
64
69
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
70
+ import com .oracle .truffle .api .CompilerDirectives ;
65
71
import com .oracle .truffle .api .TruffleFile ;
66
72
import com .oracle .truffle .api .TruffleLanguage .Env ;
67
73
import com .oracle .truffle .api .dsl .Cached ;
@@ -215,6 +221,9 @@ protected boolean isMimeType(String lang) {
215
221
@ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , keywordArguments = {"name" })
216
222
@ GenerateNodeFactory
217
223
public abstract static class ExportSymbolNode extends PythonBuiltinNode {
224
+ @ Child private GetAttributeNode getNameAttributeNode ;
225
+ @ Child private CastToStringNode castToStringNode ;
226
+
218
227
@ Specialization
219
228
@ TruffleBoundary
220
229
public Object exportSymbol (Object value , String name ) {
@@ -235,6 +244,36 @@ public Object exportSymbol(PBuiltinFunction fun, @SuppressWarnings("unused") PNo
235
244
getContext ().getEnv ().exportSymbol (fun .getName (), fun );
236
245
return fun ;
237
246
}
247
+
248
+ @ Specialization (guards = "isModule(fun.getSelf())" )
249
+ @ TruffleBoundary
250
+ public Object exportSymbol (PMethod fun , @ SuppressWarnings ("unused" ) PNone name ) {
251
+ getContext ().getEnv ().exportSymbol (getMethodName (fun ), fun );
252
+ return fun ;
253
+ }
254
+
255
+ @ Specialization (guards = "isModule(fun.getSelf())" )
256
+ @ TruffleBoundary
257
+ public Object exportSymbol (PBuiltinMethod fun , @ SuppressWarnings ("unused" ) PNone name ) {
258
+ getContext ().getEnv ().exportSymbol (getMethodName (fun ), fun );
259
+ return fun ;
260
+ }
261
+
262
+ private String getMethodName (Object o ) {
263
+ if (getNameAttributeNode == null ) {
264
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
265
+ getNameAttributeNode = insert (GetAttributeNode .create (SpecialAttributeNames .__NAME__ , null ));
266
+ }
267
+ if (castToStringNode == null ) {
268
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
269
+ castToStringNode = insert (CastToStringNode .create ());
270
+ }
271
+ return castToStringNode .execute (getNameAttributeNode .executeObject (o ));
272
+ }
273
+
274
+ protected static boolean isModule (Object o ) {
275
+ return o instanceof PythonModule ;
276
+ }
238
277
}
239
278
240
279
@ Builtin (name = "__read__" , fixedNumOfPositionalArgs = 2 )
0 commit comments