50
50
import static com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol .FUN_PY_TRUFFLE_BYTE_ARRAY_TO_NATIVE ;
51
51
import static com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol .FUN_PY_TRUFFLE_STRING_TO_CSTR ;
52
52
import static com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol .FUN_WHCAR_SIZE ;
53
+ import static com .oracle .graal .python .builtins .objects .cext .capi .NativeMember .MD_STATE ;
53
54
import static com .oracle .graal .python .builtins .objects .cext .capi .NativeMember .OB_REFCNT ;
54
55
import static com .oracle .graal .python .nodes .SpecialMethodNames .__COMPLEX__ ;
55
56
import static com .oracle .graal .python .runtime .exception .PythonErrorType .SystemError ;
99
100
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodesFactory .VoidPtrToJavaNodeGen ;
100
101
import com .oracle .graal .python .builtins .objects .cext .capi .DynamicObjectNativeWrapper .PrimitiveNativeWrapper ;
101
102
import com .oracle .graal .python .builtins .objects .cext .capi .DynamicObjectNativeWrapper .PythonObjectNativeWrapper ;
103
+ import com .oracle .graal .python .builtins .objects .cext .capi .DynamicObjectNativeWrapper .WriteNativeMemberNode ;
102
104
import com .oracle .graal .python .builtins .objects .cext .capi .ExternalFunctionNodes .DefaultCheckFunctionResultNode ;
103
105
import com .oracle .graal .python .builtins .objects .cext .capi .ExternalFunctionNodes .MethKeywordsRoot ;
104
106
import com .oracle .graal .python .builtins .objects .cext .capi .ExternalFunctionNodes .MethNoargsRoot ;
@@ -3759,7 +3761,7 @@ static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, PythonAb
3759
3761
}
3760
3762
3761
3763
/**
3762
- * Equivalent to {@code PyModule_ExecDef}.
3764
+ * Equivalent of {@code PyModule_ExecDef}.
3763
3765
*/
3764
3766
@ GenerateUncached
3765
3767
public abstract static class ExecModuleNode extends MultiPhaseExtensionModuleInitNode {
@@ -3774,6 +3776,8 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
3774
3776
@ Cached PythonObjectFactory factory ,
3775
3777
@ Cached ModuleGetNameNode getNameNode ,
3776
3778
@ Cached PCallCapiFunction callGetterNode ,
3779
+ @ Cached WriteNativeMemberNode writeNativeMemberNode ,
3780
+ @ Cached PCallCapiFunction callMallocNode ,
3777
3781
@ CachedLibrary (limit = "3" ) InteropLibrary interopLib ,
3778
3782
@ Cached ToBorrowedRefNode moduleToNativeNode ,
3779
3783
@ Cached PRaiseNode raiseNode ) {
@@ -3790,12 +3794,21 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
3790
3794
throw raiseNode .raise (PythonBuiltinClassType .SystemError , "Cannot create module from definition because: %m" , e );
3791
3795
}
3792
3796
3793
- if (mSize >= 0 ) {
3794
- // TODO(fa): allocate md_state
3795
- }
3796
-
3797
- // parse slot definitions
3798
3797
try {
3798
+ // allocate md_state if necessary
3799
+ if (mSize >= 0 ) {
3800
+ // The cast is not nice but it will at least fail if we change the wrapper type.
3801
+ PythonNativeWrapper moduleWrapper = (PythonNativeWrapper ) moduleToNativeNode .execute (capiContext , module );
3802
+ /*
3803
+ * TODO(fa): We currently leak 'md_state' and need to use a shared finalizer or
3804
+ * similar. We ignore that for now since the size will usually be very small
3805
+ * and/or we could also use a Truffle buffer object.
3806
+ */
3807
+ Object moduleStatePtr = callMallocNode .call (capiContext , NativeCAPISymbol .FUN_PYMEM_RAWMALLOC , mSize );
3808
+ writeNativeMemberNode .execute (module , moduleWrapper , MD_STATE .getMemberName (), moduleStatePtr );
3809
+ }
3810
+
3811
+ // parse slot definitions
3799
3812
Object slotDefinitions = callGetterNode .call (capiContext , FUN_GET_PYMODULEDEF_M_SLOTS , moduleDef );
3800
3813
if (interopLib .isNull (slotDefinitions )) {
3801
3814
return 0 ;
0 commit comments