@@ -1411,8 +1411,8 @@ private void generateExternalFunctionInvoker(List<CApiExternalFunctionSignatureD
14111411 lines .add ("package " + EXFUNC_INVOKER_PACKAGE + ";" );
14121412 lines .add ("" );
14131413 lines .add ("import java.lang.invoke.MethodHandle;" );
1414- lines .add ("import java.lang.invoke.MethodType;" );
14151414 lines .add ("" );
1415+ lines .add ("import " + NativeSimpleType .class .getCanonicalName () + ";" );
14161416 lines .add ("import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.EnsurePythonObjectNode;" );
14171417 lines .add ("import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTiming;" );
14181418 lines .add ("import com.oracle.graal.python.builtins.objects.function.PArguments;" );
@@ -1631,6 +1631,11 @@ private void generateNativeAccessSupport(Element[] origins) throws IOException {
16311631 lines .add (" return CAPTURE_STATE_LAYOUT.varHandle(MemoryLayout.PathElement.groupElement(stateName));" );
16321632 lines .add (" }" );
16331633 lines .add ("" );
1634+ lines .add (" private static MethodType createMethodType(boolean captureCallState, FunctionDescriptor functionDescriptor) {" );
1635+ lines .add (" MethodType methodType = functionDescriptor.toMethodType().insertParameterTypes(0, long.class);" );
1636+ lines .add (" return captureCallState ? methodType.insertParameterTypes(1, MemorySegment.class) : methodType;" );
1637+ lines .add (" }" );
1638+ lines .add ("" );
16341639 lines .add (" @Override" );
16351640 lines .add (" protected Object createArenaImpl() {" );
16361641 lines .add (" return Arena.ofShared();" );
@@ -1655,8 +1660,9 @@ private void generateNativeAccessSupport(Element[] origins) throws IOException {
16551660 lines .add ("" );
16561661 lines .add (" @Override" );
16571662 lines .add (" @SuppressWarnings(\" restricted\" )" );
1658- lines .add (" protected MethodHandle createDowncallHandleImpl(MethodType methodType, boolean critical, boolean captureCallState) {" );
1659- lines .add (" FunctionDescriptor functionDescriptor = createFunctionDescriptor(methodType, captureCallState ? 2 : 1);" );
1663+ lines .add (" protected MethodHandle createDowncallHandleImpl(boolean critical, boolean captureCallState, NativeSimpleType resType, NativeSimpleType[] argTypes) {" );
1664+ lines .add (" FunctionDescriptor functionDescriptor = createFunctionDescriptor(resType, argTypes);" );
1665+ lines .add (" MethodType methodType = createMethodType(captureCallState, functionDescriptor);" );
16601666 lines .add (" List<Linker.Option> options = new ArrayList<>();" );
16611667 lines .add (" if (critical) {" );
16621668 lines .add (" options.add(Linker.Option.critical(false));" );
@@ -1709,33 +1715,6 @@ private void generateNativeAccessSupport(Element[] origins) throws IOException {
17091715 lines .add (" return resType == NativeSimpleType.VOID ? FunctionDescriptor.ofVoid(argLayouts) : FunctionDescriptor.of(asLayout(resType), argLayouts);" );
17101716 lines .add (" }" );
17111717 lines .add ("" );
1712- lines .add (" private static FunctionDescriptor createFunctionDescriptor(MethodType methodType, int firstNativeArg) {" );
1713- lines .add (" Class<?>[] parameterTypes = methodType.parameterArray();" );
1714- lines .add (" MemoryLayout[] argLayouts = new MemoryLayout[parameterTypes.length - firstNativeArg];" );
1715- lines .add (" for (int i = firstNativeArg; i < parameterTypes.length; i++) {" );
1716- lines .add (" argLayouts[i - firstNativeArg] = asLayout(parameterTypes[i]);" );
1717- lines .add (" }" );
1718- lines .add (" Class<?> returnType = methodType.returnType();" );
1719- lines .add (" return returnType == void.class ? FunctionDescriptor.ofVoid(argLayouts) : FunctionDescriptor.of(asLayout(returnType), argLayouts);" );
1720- lines .add (" }" );
1721- lines .add ("" );
1722- lines .add (" private static MemoryLayout asLayout(Class<?> type) {" );
1723- lines .add (" if (type == byte.class) {" );
1724- lines .add (" return ValueLayout.JAVA_BYTE;" );
1725- lines .add (" } else if (type == short.class) {" );
1726- lines .add (" return ValueLayout.JAVA_SHORT;" );
1727- lines .add (" } else if (type == int.class) {" );
1728- lines .add (" return ValueLayout.JAVA_INT;" );
1729- lines .add (" } else if (type == long.class) {" );
1730- lines .add (" return ValueLayout.JAVA_LONG;" );
1731- lines .add (" } else if (type == float.class) {" );
1732- lines .add (" return ValueLayout.JAVA_FLOAT;" );
1733- lines .add (" } else if (type == double.class) {" );
1734- lines .add (" return ValueLayout.JAVA_DOUBLE;" );
1735- lines .add (" }" );
1736- lines .add (" throw shouldNotReachHere(\" Unsupported layout carrier: \" + type);" );
1737- lines .add (" }" );
1738- lines .add ("" );
17391718 lines .add (" private static MemoryLayout asLayout(NativeSimpleType type) {" );
17401719 lines .add (" return switch (type) {" );
17411720 lines .add (" case VOID -> throw shouldNotReachHere(\" VOID has no layout\" );" );
@@ -1765,7 +1744,6 @@ private void generateDummyNativeAccessSupport(Element[] origins) throws IOExcept
17651744 lines .add ("package " + NATIVE_ACCESS_PACKAGE + ";" );
17661745 lines .add ("" );
17671746 lines .add ("import java.lang.invoke.MethodHandle;" );
1768- lines .add ("import java.lang.invoke.MethodType;" );
17691747 lines .add ("" );
17701748 lines .add ("import " + NativeSimpleType .class .getCanonicalName () + ";" );
17711749 lines .add ("" );
@@ -1791,17 +1769,12 @@ private void generateDummyNativeAccessSupport(Element[] origins) throws IOExcept
17911769 lines .add (" }" );
17921770 lines .add ("" );
17931771 lines .add (" @Override" );
1794- lines .add (" protected MethodHandle createDowncallHandleImpl(MethodType methodType, boolean critical, boolean captureCallState) {" );
1795- lines .add (" throw unsupported();" );
1796- lines .add (" }" );
1797- lines .add ("" );
1798- lines .add (" @Override" );
1799- lines .add (" protected Object createCapturedCallStateImpl() {" );
1772+ lines .add (" protected MethodHandle createDowncallHandleImpl(boolean critical, boolean captureCallState, NativeSimpleType resType, NativeSimpleType[] argTypes) {" );
18001773 lines .add (" throw unsupported();" );
18011774 lines .add (" }" );
18021775 lines .add ("" );
18031776 lines .add (" @Override" );
1804- lines .add (" protected Object getCapturedCallStateSegmentImpl (Object state ) {" );
1777+ lines .add (" protected Object createCapturedCallStateImpl (Object arena ) {" );
18051778 lines .add (" throw unsupported();" );
18061779 lines .add (" }" );
18071780 lines .add ("" );
0 commit comments