@@ -1072,35 +1072,57 @@ static Object doNative(PythonAbstractNativeObject s,
1072
1072
}
1073
1073
}
1074
1074
1075
- @ CApiBuiltin (ret = PY_UNICODE_PTR , args = {PyObject }, call = Direct )
1076
- abstract static class PyTruffle_Unicode_AsUnicodeAndSize_CharPtr extends CApiUnaryBuiltinNode {
1075
+ @ CApiBuiltin (ret = PY_UNICODE_PTR , args = {PyObject , PY_SSIZE_T_PTR }, call = Ignored )
1076
+ abstract static class PyTruffleUnicode_AsUnicodeAndSize extends CApiBinaryBuiltinNode {
1077
1077
1078
1078
@ Specialization
1079
- Object doUnicode (PString s ,
1079
+ static Object doUnicode (PString s , Object sizePtr ,
1080
1080
@ Bind ("this" ) Node inliningTarget ,
1081
- @ Cached InlinedConditionProfile profile ,
1081
+ @ CachedLibrary (limit = "2" ) InteropLibrary lib ,
1082
+ @ Cached InlinedConditionProfile hasSizeProfile ,
1083
+ @ Cached InlinedConditionProfile hasUnicodeProfile ,
1084
+ @ Cached CStructAccess .WriteLongNode writeLongNode ,
1082
1085
@ Cached UnicodeAsWideCharNode asWideCharNode ) {
1083
- if (profile .profile (inliningTarget , s .getWCharBytes () == null )) {
1084
- PBytes bytes = asWideCharNode .executeNativeOrder (inliningTarget , s , CStructs .wchar_t .size ());
1086
+ int wcharSize = CStructs .wchar_t .size ();
1087
+ if (hasUnicodeProfile .profile (inliningTarget , s .getWCharBytes () == null )) {
1088
+ PBytes bytes = asWideCharNode .executeNativeOrder (inliningTarget , s , wcharSize );
1085
1089
s .setWCharBytes (bytes );
1086
1090
}
1091
+ if (hasSizeProfile .profile (inliningTarget , !lib .isNull (sizePtr ))) {
1092
+ writeLongNode .write (sizePtr , s .getWCharBytes ().getSequenceStorage ().length () / wcharSize );
1093
+ }
1087
1094
return PySequenceArrayWrapper .ensureNativeSequence (s .getWCharBytes ());
1088
1095
}
1089
1096
1090
1097
@ Fallback
1091
- static Object doError (@ SuppressWarnings ("unused" ) Object s ,
1098
+ @ SuppressWarnings ("unused" )
1099
+ static Object doError (Object s , Object sizePtr ,
1092
1100
@ Cached PRaiseNode raiseNode ) {
1093
1101
throw raiseNode .raise (TypeError , BAD_ARG_TYPE_FOR_BUILTIN_OP );
1094
1102
}
1095
1103
}
1096
1104
1097
- @ CApiBuiltin (ret = Py_ssize_t , args = {PyObject }, call = Direct )
1098
- abstract static class PyTruffle_Unicode_AsUnicodeAndSize_Size extends CApiUnaryBuiltinNode {
1105
+ @ CApiBuiltin (ret = Int , args = {PyObject }, call = Ignored )
1106
+ abstract static class PyTruffleUnicode_FillUnicode extends CApiUnaryBuiltinNode {
1099
1107
1100
1108
@ Specialization
1101
- static Object doUnicode (PString s ) {
1102
- // PyTruffle_Unicode_AsUnicodeAndSize_CharPtr must have been be called before
1103
- return s .getWCharBytes ().getSequenceStorage ().length () / CStructs .wchar_t .size ();
1109
+ static Object doNative (PythonAbstractNativeObject s ,
1110
+ @ Bind ("this" ) Node inliningTarget ,
1111
+ @ Cached CastToTruffleStringNode cast ,
1112
+ @ Cached CStructAccess .WriteLongNode writeLongNode ,
1113
+ @ Cached UnicodeAsWideCharNode asWideCharNode ,
1114
+ @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
1115
+ @ Cached CStructAccess .WritePointerNode writePointerNode ,
1116
+ @ Cached CStructAccess .AllocateNode allocateNode ,
1117
+ @ Cached CStructAccess .WriteByteNode writeByteNode ) {
1118
+ int wcharSize = CStructs .wchar_t .size ();
1119
+ PBytes bytes = asWideCharNode .executeNativeOrder (inliningTarget , cast .castKnownString (inliningTarget , s ), wcharSize );
1120
+ int len = bufferLib .getBufferLength (bytes );
1121
+ Object mem = allocateNode .alloc (len + wcharSize , true );
1122
+ writeByteNode .writeByteArray (mem , bufferLib .getInternalOrCopiedByteArray (bytes ), len , 0 , 0 );
1123
+ writePointerNode .writeToObj (s , CFields .PyASCIIObject__wstr , mem );
1124
+ writeLongNode .writeToObject (s , CFields .PyCompactUnicodeObject__wstr_length , len / wcharSize );
1125
+ return 0 ;
1104
1126
}
1105
1127
}
1106
1128
0 commit comments