41
41
package com .oracle .graal .python .builtins .modules ;
42
42
43
43
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
44
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__GETATTR__ ;
44
45
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
45
46
46
47
import java .util .List ;
50
51
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
51
52
import com .oracle .graal .python .builtins .PythonBuiltins ;
52
53
import com .oracle .graal .python .builtins .objects .PNone ;
54
+ import com .oracle .graal .python .builtins .objects .module .PythonModule ;
53
55
import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
54
56
import com .oracle .graal .python .builtins .objects .str .PString ;
55
57
import com .oracle .graal .python .nodes .ErrorMessages ;
61
63
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
62
64
import com .oracle .graal .python .runtime .PythonCore ;
63
65
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
66
+ import com .oracle .truffle .api .CompilerDirectives ;
67
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
64
68
import com .oracle .truffle .api .TruffleLanguage .Env ;
65
69
import com .oracle .truffle .api .dsl .Cached ;
66
70
import com .oracle .truffle .api .dsl .Fallback ;
67
71
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
68
72
import com .oracle .truffle .api .dsl .NodeFactory ;
69
73
import com .oracle .truffle .api .dsl .Specialization ;
74
+ import com .oracle .truffle .api .frame .VirtualFrame ;
70
75
import com .oracle .truffle .api .interop .TruffleObject ;
71
76
import com .oracle .truffle .api .library .CachedLibrary ;
72
77
73
- @ CoreFunctions (defineModule = "java" )
78
+ @ CoreFunctions (defineModule = JavaModuleBuiltins . JAVA )
74
79
public class JavaModuleBuiltins extends PythonBuiltins {
80
+ protected static final String JAVA = "java" ;
81
+
75
82
@ Override
76
83
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
77
84
return JavaModuleBuiltinsFactory .getFactories ();
@@ -83,6 +90,13 @@ public void initialize(PythonCore core) {
83
90
builtinConstants .put ("__path__" , "java!" );
84
91
}
85
92
93
+ @ Override
94
+ public void postInitialize (PythonCore core ) {
95
+ super .postInitialize (core );
96
+ PythonModule javaModule = core .lookupBuiltinModule (JAVA );
97
+ javaModule .setAttribute (__GETATTR__ , javaModule .getAttribute (GetAttrNode .JAVA_GETATTR ));
98
+ }
99
+
86
100
@ Builtin (name = "type" , minNumOfPositionalArgs = 1 )
87
101
@ GenerateNodeFactory
88
102
abstract static class TypeNode extends PythonUnaryBuiltinNode {
@@ -219,4 +233,30 @@ boolean fallback(Object object, Object klass) {
219
233
throw raise (TypeError , ErrorMessages .UNSUPPORTED_INSTANCEOF , object , klass );
220
234
}
221
235
}
236
+
237
+ @ Builtin (name = GetAttrNode .JAVA_GETATTR , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , declaresExplicitSelf = true )
238
+ @ GenerateNodeFactory
239
+ abstract static class GetAttrNode extends PythonBuiltinNode {
240
+
241
+ protected static final String JAVA_GETATTR = "java_getattr" ;
242
+ private static final String JAVA_PKG_LOADER = "JavaPackageLoader" ;
243
+ private static final String MAKE_GETATTR = "_make_getattr" ;
244
+
245
+ @ CompilationFinal protected Object getAttr ;
246
+
247
+ private Object getAttr (VirtualFrame frame , PythonModule mod , PythonObjectLibrary lib ) {
248
+ if (getAttr == null ) {
249
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
250
+ Object javaLoader = lib .lookupAttributeStrict (mod , frame , JAVA_PKG_LOADER );
251
+ getAttr = lib .lookupAndCallRegularMethod (javaLoader , frame , MAKE_GETATTR , JAVA );
252
+ }
253
+ return getAttr ;
254
+ }
255
+
256
+ @ Specialization
257
+ Object none (VirtualFrame frame , PythonModule mod , Object name ,
258
+ @ CachedLibrary (limit = "3" ) PythonObjectLibrary lib ) {
259
+ return lib .callObject (getAttr (frame , mod , lib ), frame , name );
260
+ }
261
+ }
222
262
}
0 commit comments