Skip to content

Commit ab8309d

Browse files
Merge fixes for Float formatting, POSIX exclusive create, time zone changes, CFFI buffers, weak map walks and the crashes found in the batches
# Conflicts: # Changes.md # test/regression/emscripten-skip.txt
2 parents a6a50f1 + b1ee807 commit ab8309d

41 files changed

Lines changed: 976 additions & 63 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/test.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ jobs:
7171
if: ${{ !cancelled() && steps.runtime.outcome == 'success' }}
7272
working-directory: test/haxe
7373
run: bash ../../.github/workflows/traced.sh bin/TestMain
74+
# compile.hxml builds with the generational collector, so the default
75+
# one needs its own build
76+
- name: runtime build (default gc)
77+
id: runtime_default
78+
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
79+
working-directory: test/haxe
80+
run: haxe compile-default.hxml -D $HXCPP_ARCH_FLAG
81+
- name: runtime run (default gc)
82+
if: ${{ !cancelled() && steps.runtime_default.outcome == 'success' }}
83+
working-directory: test/haxe
84+
run: bash ../../.github/workflows/traced.sh bin/TestMain
7485

7586
- name: native build
7687
id: native
@@ -261,6 +272,12 @@ jobs:
261272
run: |
262273
haxe compile.hxml --debug -D HXCPP_GC_DEBUG_ALWAYS_MOVE -D $HXCPP_ARCH_FLAG
263274
bin/TestMain-debug
275+
- name: runtime (moving gc, not generational)
276+
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
277+
working-directory: test/haxe
278+
run: |
279+
haxe compile-default.hxml -D HXCPP_GC_MOVING -D $HXCPP_ARCH_FLAG
280+
bin/TestMain
264281
- name: runtime (gc verify)
265282
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
266283
working-directory: test/haxe

‎Changes.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
libhxcpp 6.0.0 (unreleased)
33
------------------------------------------------------------
44

5+
* Fixed converting a weak map to another value type losing live keys, about one in five in a map whose keys had mostly died: a collection during the conversion folded the map's buckets (#204), moving entries into buckets the conversion had already passed. A collection now leaves the fold to a later one while anything walks the map, which keys(), values(), toString and conversion do (libhxcpp #204)
6+
* Fixed the moving collector (HXCPP_GC_MOVING) giving the whole heap back on Gc.run(true) and whenever the allocator's free list filled, as the other modes stopped doing in #230 and #231: a heap with a small live set stayed near 9 MB and collected far more often, and Gc.run(true) shrank it to 1 MB. Only Gc.compact() releases everything now; the collector still moves objects on those collections (libhxcpp #231)
7+
* Sped up converting a Float to a String, which rounded it with printf to 15, 16 and 17 digits and read each back with strtod to find the shortest digits that read back. Grisu3 finds them with integer arithmetic, and the old search does the doubles it cannot decide, about 0.5% in Loitsch's measurements. The text is the same for every value: Std.string on Floats went from 996k to 8.8M conversions a second (libhxcpp #390)
8+
* Fixed a thread whose cpp.Lib.load was failing crashing the process at exit on Windows, about once in a hundred exits: a failing load reads haxelib's files to look further, and exit frees the C runtime's table of open files while other threads still run. The loader reads them with Win32 calls instead (libhxcpp #371)
9+
* Fixed Gc.trace with path printing overflowing the native stack on a deep graph, as it marked by recursing all the way down: it now recurses 256 objects deep, and paths below that start with "... + higher". It also counts instances that are large allocations and instances with no object fields, which it missed, and prints paths through large objects from their root (libhxcpp #389)
10+
* Fixed val_buffer losing the text of a value after a NUL character: it measured the UTF-8 text with strlen. It now appends the text's full byte count (libhxcpp #393)
11+
* Fixed Date's local fields keeping the previous time zone after the zone changed: the last time converted is cached per thread, and the cache was keyed by the time alone, so a date read before the change read the old zone until another time was converted. The cache now also keys on the zone the C library has, and Sys.putEnv("TZ") applies the new zone at once, where localtime_r and the Windows CRT had kept the old one until something called tzset (libhxcpp #392)
12+
* Fixed cpp.NativeFile.file_open with an exclusive-create mode such as "wbx" truncating a file that exists on Linux, macOS and other POSIX systems: files are opened with open(2) to make them close-on-exec, and the mode was mapped without O_EXCL. Such a mode now fails on a file that exists and leaves it as it was, and x without w fails too (libhxcpp #391)
513
* Sped up byte-wise reads from TCP sockets, such as Socket.input.readLine and readUntil, which called recv for every byte, in a GC-free zone of its own. Reading one byte now takes what else the kernel already holds, up to 4 KB, and every other read, select, fast select, both Poll calls and TLS take those bytes first, so blocking, timeouts, nonblocking reads, end of stream, shutdown and reads from several threads behave as they did. Reading 128 KiB of 128-byte lines with readLine went from 78 ms to 2.0 ms; through a 16 KiB BufferInput it takes 0.8 ms (libhxcpp #338)
614
* cpp.net.Poll keeps its registrations in the kernel, with epoll on Linux and Android and kqueue on macOS, iOS and the BSDs, so an events call costs what is ready instead of every socket registered: poll() looked at all of them on every call, 0.3 ms a call for 4,096 idle sockets on Linux. prepare tells the kernel only what changed since the last one, and the results are as before, including a socket closed after prepare reported as ready to read and write. Where the kernel will not make a queue, and on Emscripten, Poll uses poll() as before. Windows keeps select, which WSAPoll did not beat: at 1,024 idle sockets it took 0.12 ms a call and WSAPoll 0.71 ms (libhxcpp #320)
715
* Sorting an array of Ints, Floats or other plain values with a comparator no longer holds a box for every element for the whole sort. An array past 16,384 elements sorts runs of that size, each through boxes as before, and merges them, boxing each value as the merge reaches it, so the boxes alive at once are those of one run. Sorting 200,000 Ints (sort_int) peaks at 29.6 MB instead of 32.4 MB (stock hxcpp 4.3.53: 28.2 MB) and runs 1.19x faster (libhxcpp #256)

‎bench/src/Main.hx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Main {
3939
new StringBuild(), new StringIndex(), new StringSubstr(), new StringSplit(), new StringCase(), new SortInt(), new SortString(),
4040
new SortObject(), new DequePush(), new DequeBurst(), new CffiCallVector(), new CffiStringView(), new EnumFormat(),
4141
new MapInt64(), new MapInt64Values(), new ArrayPushDynamic(), new ArrayReserveDynamic(), new ArrayPop(),
42-
new SearchFrom(), new StringLongNeedle(), new ParseNumbers(), new AnonFieldSmall(), new AnonFieldWide(), new SqliteRows(),
42+
new SearchFrom(), new StringLongNeedle(), new ParseNumbers(), new FloatFormat(), new AnonFieldSmall(), new AnonFieldWide(), new SqliteRows(),
4343
new SocketRead(), new CffiBuffer(), new LargeResize(), new LargeRecycle(), new LargeCopy(), new LargeCachePurge(),
4444
new GcWeakMapInt(), new GcObjectIds(), new GcLargeStackRoots(), new GcRegexWait(), new GcZombieReservations(), new GcFinalizerPromotion(), new GcIdleMinor(), new GcRememberedBurst(), new GcObjectIdBurst(), new GcWeakMapChain(),
4545
new SocketReadLine(false), new SocketReadLine(true),

‎bench/src/StringBenchmarks.hx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,33 @@ class ParseNumbers extends Benchmark {
239239
return (sum + Std.int(fsum)) | 0;
240240
}
241241
}
242+
243+
/** Std.string on Floats, the default shortest form that reads back (libhxcpp #390). **/
244+
class FloatFormat extends Benchmark {
245+
static inline var N = 300000;
246+
247+
final values:Array<Float> = [];
248+
249+
public function new() {
250+
super("float_format", "conversions", N);
251+
}
252+
253+
override public function setup() {
254+
// The corpus of #390, and values of other sizes and lengths
255+
for (i in 0...65536)
256+
values.push(0.125 + ((i * 7919) % 65521) / 65521.0);
257+
var rng = new Rng(390);
258+
for (i in 0...65536)
259+
values.push((rng.next() - 0x3fffffff) * Math.pow(10, rng.below(40) - 20));
260+
}
261+
262+
override public function run():Int {
263+
var hash = 0;
264+
for (i in 0...N) {
265+
var text = Std.string(values[i & 131071]);
266+
for (c in 0...text.length)
267+
hash = (hash * 31 + StringTools.fastCodeAt(text, c)) | 0;
268+
}
269+
return hash;
270+
}
271+
}

‎src/String.cpp‎

Lines changed: 264 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,10 +1095,273 @@ static int hxTrimZeros(const char *inDigits, int inCount)
10951095
return inCount;
10961096
}
10971097

1098+
// Grisu3, from Florian Loitsch's "Printing Floating-Point Numbers Quickly and
1099+
// Accurately with Integers" (2010), as in double-conversion: the shortest
1100+
// digits that read back, and the nearest of them, with 64-bit integers in
1101+
// place of printf and strtod (libhxcpp #390). It declines a double when its
1102+
// precision cannot decide, about 0.5% of them in the paper, and the search
1103+
// below does those
1104+
struct HxDiyFp
1105+
{
1106+
unsigned long long f;
1107+
int e;
1108+
};
1109+
1110+
static inline HxDiyFp hxDiyFp(unsigned long long inF, int inE)
1111+
{
1112+
HxDiyFp result;
1113+
result.f = inF;
1114+
result.e = inE;
1115+
return result;
1116+
}
1117+
1118+
// The upper 64 bits of the product, rounded
1119+
static inline HxDiyFp hxDiyTimes(const HxDiyFp &inA, const HxDiyFp &inB)
1120+
{
1121+
const unsigned long long M32 = 0xFFFFFFFFULL;
1122+
unsigned long long a = inA.f >> 32, b = inA.f & M32;
1123+
unsigned long long c = inB.f >> 32, d = inB.f & M32;
1124+
unsigned long long ac = a*c, bc = b*c, ad = a*d, bd = b*d;
1125+
unsigned long long tmp = (bd >> 32) + (ad & M32) + (bc & M32) + (1ULL << 31);
1126+
return hxDiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), inA.e + inB.e + 64);
1127+
}
1128+
1129+
static inline HxDiyFp hxDiyNormalize(HxDiyFp inV)
1130+
{
1131+
while(!(inV.f & 0xFFC0000000000000ULL))
1132+
{
1133+
inV.f <<= 10;
1134+
inV.e -= 10;
1135+
}
1136+
while(!(inV.f & 0x8000000000000000ULL))
1137+
{
1138+
inV.f <<= 1;
1139+
inV.e--;
1140+
}
1141+
return inV;
1142+
}
1143+
1144+
struct HxCachedPower
1145+
{
1146+
unsigned long long f;
1147+
short e;
1148+
short decimal;
1149+
};
1150+
1151+
// 10^decimal ~ f * 2^e, for every eighth power from 10^-348 to 10^340
1152+
static const HxCachedPower sHxCachedPowers[] = {
1153+
{ 0xfa8fd5a0081c0288ULL, -1220, -348 },
1154+
{ 0xbaaee17fa23ebf76ULL, -1193, -340 },
1155+
{ 0x8b16fb203055ac76ULL, -1166, -332 },
1156+
{ 0xcf42894a5dce35eaULL, -1140, -324 },
1157+
{ 0x9a6bb0aa55653b2dULL, -1113, -316 },
1158+
{ 0xe61acf033d1a45dfULL, -1087, -308 },
1159+
{ 0xab70fe17c79ac6caULL, -1060, -300 },
1160+
{ 0xff77b1fcbebcdc4fULL, -1034, -292 },
1161+
{ 0xbe5691ef416bd60cULL, -1007, -284 },
1162+
{ 0x8dd01fad907ffc3cULL, -980, -276 },
1163+
{ 0xd3515c2831559a83ULL, -954, -268 },
1164+
{ 0x9d71ac8fada6c9b5ULL, -927, -260 },
1165+
{ 0xea9c227723ee8bcbULL, -901, -252 },
1166+
{ 0xaecc49914078536dULL, -874, -244 },
1167+
{ 0x823c12795db6ce57ULL, -847, -236 },
1168+
{ 0xc21094364dfb5637ULL, -821, -228 },
1169+
{ 0x9096ea6f3848984fULL, -794, -220 },
1170+
{ 0xd77485cb25823ac7ULL, -768, -212 },
1171+
{ 0xa086cfcd97bf97f4ULL, -741, -204 },
1172+
{ 0xef340a98172aace5ULL, -715, -196 },
1173+
{ 0xb23867fb2a35b28eULL, -688, -188 },
1174+
{ 0x84c8d4dfd2c63f3bULL, -661, -180 },
1175+
{ 0xc5dd44271ad3cdbaULL, -635, -172 },
1176+
{ 0x936b9fcebb25c996ULL, -608, -164 },
1177+
{ 0xdbac6c247d62a584ULL, -582, -156 },
1178+
{ 0xa3ab66580d5fdaf6ULL, -555, -148 },
1179+
{ 0xf3e2f893dec3f126ULL, -529, -140 },
1180+
{ 0xb5b5ada8aaff80b8ULL, -502, -132 },
1181+
{ 0x87625f056c7c4a8bULL, -475, -124 },
1182+
{ 0xc9bcff6034c13053ULL, -449, -116 },
1183+
{ 0x964e858c91ba2655ULL, -422, -108 },
1184+
{ 0xdff9772470297ebdULL, -396, -100 },
1185+
{ 0xa6dfbd9fb8e5b88fULL, -369, -92 },
1186+
{ 0xf8a95fcf88747d94ULL, -343, -84 },
1187+
{ 0xb94470938fa89bcfULL, -316, -76 },
1188+
{ 0x8a08f0f8bf0f156bULL, -289, -68 },
1189+
{ 0xcdb02555653131b6ULL, -263, -60 },
1190+
{ 0x993fe2c6d07b7facULL, -236, -52 },
1191+
{ 0xe45c10c42a2b3b06ULL, -210, -44 },
1192+
{ 0xaa242499697392d3ULL, -183, -36 },
1193+
{ 0xfd87b5f28300ca0eULL, -157, -28 },
1194+
{ 0xbce5086492111aebULL, -130, -20 },
1195+
{ 0x8cbccc096f5088ccULL, -103, -12 },
1196+
{ 0xd1b71758e219652cULL, -77, -4 },
1197+
{ 0x9c40000000000000ULL, -50, 4 },
1198+
{ 0xe8d4a51000000000ULL, -24, 12 },
1199+
{ 0xad78ebc5ac620000ULL, 3, 20 },
1200+
{ 0x813f3978f8940984ULL, 30, 28 },
1201+
{ 0xc097ce7bc90715b3ULL, 56, 36 },
1202+
{ 0x8f7e32ce7bea5c70ULL, 83, 44 },
1203+
{ 0xd5d238a4abe98068ULL, 109, 52 },
1204+
{ 0x9f4f2726179a2245ULL, 136, 60 },
1205+
{ 0xed63a231d4c4fb27ULL, 162, 68 },
1206+
{ 0xb0de65388cc8ada8ULL, 189, 76 },
1207+
{ 0x83c7088e1aab65dbULL, 216, 84 },
1208+
{ 0xc45d1df942711d9aULL, 242, 92 },
1209+
{ 0x924d692ca61be758ULL, 269, 100 },
1210+
{ 0xda01ee641a708deaULL, 295, 108 },
1211+
{ 0xa26da3999aef774aULL, 322, 116 },
1212+
{ 0xf209787bb47d6b85ULL, 348, 124 },
1213+
{ 0xb454e4a179dd1877ULL, 375, 132 },
1214+
{ 0x865b86925b9bc5c2ULL, 402, 140 },
1215+
{ 0xc83553c5c8965d3dULL, 428, 148 },
1216+
{ 0x952ab45cfa97a0b3ULL, 455, 156 },
1217+
{ 0xde469fbd99a05fe3ULL, 481, 164 },
1218+
{ 0xa59bc234db398c25ULL, 508, 172 },
1219+
{ 0xf6c69a72a3989f5cULL, 534, 180 },
1220+
{ 0xb7dcbf5354e9beceULL, 561, 188 },
1221+
{ 0x88fcf317f22241e2ULL, 588, 196 },
1222+
{ 0xcc20ce9bd35c78a5ULL, 614, 204 },
1223+
{ 0x98165af37b2153dfULL, 641, 212 },
1224+
{ 0xe2a0b5dc971f303aULL, 667, 220 },
1225+
{ 0xa8d9d1535ce3b396ULL, 694, 228 },
1226+
{ 0xfb9b7cd9a4a7443cULL, 720, 236 },
1227+
{ 0xbb764c4ca7a44410ULL, 747, 244 },
1228+
{ 0x8bab8eefb6409c1aULL, 774, 252 },
1229+
{ 0xd01fef10a657842cULL, 800, 260 },
1230+
{ 0x9b10a4e5e9913129ULL, 827, 268 },
1231+
{ 0xe7109bfba19c0c9dULL, 853, 276 },
1232+
{ 0xac2820d9623bf429ULL, 880, 284 },
1233+
{ 0x80444b5e7aa7cf85ULL, 907, 292 },
1234+
{ 0xbf21e44003acdd2dULL, 933, 300 },
1235+
{ 0x8e679c2f5e44ff8fULL, 960, 308 },
1236+
{ 0xd433179d9c8cb841ULL, 986, 316 },
1237+
{ 0x9e19db92b4e31ba9ULL, 1013, 324 },
1238+
{ 0xeb96bf6ebadf77d9ULL, 1039, 332 },
1239+
{ 0xaf87023b9bf0ee6bULL, 1066, 340 },
1240+
};
1241+
1242+
static const unsigned int sHxSmallPowersOfTen[] = { 0, 1, 10, 100, 1000, 10000, 100000,
1243+
1000000, 10000000, 100000000, 1000000000 };
1244+
1245+
// Moves the last digit towards w while that stays inside the interval, and
1246+
// says whether the result is certainly the nearest and certainly inside
1247+
static bool hxGrisuRoundWeed(char *ioDigits, int inLength, unsigned long long inDistanceTooHighW,
1248+
unsigned long long inUnsafeInterval, unsigned long long inRest,
1249+
unsigned long long inTenKappa, unsigned long long inUnit)
1250+
{
1251+
unsigned long long smallDistance = inDistanceTooHighW - inUnit;
1252+
unsigned long long bigDistance = inDistanceTooHighW + inUnit;
1253+
while(inRest < smallDistance && inUnsafeInterval - inRest >= inTenKappa &&
1254+
(inRest + inTenKappa < smallDistance ||
1255+
smallDistance - inRest >= inRest + inTenKappa - smallDistance))
1256+
{
1257+
ioDigits[inLength-1]--;
1258+
inRest += inTenKappa;
1259+
}
1260+
if (inRest < bigDistance && inUnsafeInterval - inRest >= inTenKappa &&
1261+
(inRest + inTenKappa < bigDistance ||
1262+
bigDistance - inRest > inRest + inTenKappa - bigDistance))
1263+
return false;
1264+
return 2*inUnit <= inRest && inRest <= inUnsafeInterval - 4*inUnit;
1265+
}
1266+
1267+
static bool hxGrisuDigitGen(HxDiyFp inLow, HxDiyFp inW, HxDiyFp inHigh, char *outDigits,
1268+
int &outLength, int &outKappa)
1269+
{
1270+
unsigned long long unit = 1;
1271+
HxDiyFp tooLow = hxDiyFp(inLow.f - unit, inLow.e);
1272+
HxDiyFp tooHigh = hxDiyFp(inHigh.f + unit, inHigh.e);
1273+
unsigned long long unsafeInterval = tooHigh.f - tooLow.f;
1274+
int shift = -inW.e;
1275+
unsigned long long one = 1ULL << shift;
1276+
unsigned int integrals = (unsigned int)(tooHigh.f >> shift);
1277+
unsigned long long fractionals = tooHigh.f & (one - 1);
1278+
1279+
int bits = 64 - shift;
1280+
int kappa = ((bits + 1) * 1233 >> 12) + 1;
1281+
if (integrals < sHxSmallPowersOfTen[kappa])
1282+
kappa--;
1283+
unsigned int divisor = sHxSmallPowersOfTen[kappa];
1284+
1285+
outLength = 0;
1286+
while(kappa > 0)
1287+
{
1288+
outDigits[outLength++] = (char)('0' + integrals / divisor);
1289+
integrals %= divisor;
1290+
kappa--;
1291+
unsigned long long rest = ((unsigned long long)integrals << shift) + fractionals;
1292+
if (rest < unsafeInterval)
1293+
{
1294+
outKappa = kappa;
1295+
return hxGrisuRoundWeed(outDigits, outLength, tooHigh.f - inW.f, unsafeInterval, rest,
1296+
(unsigned long long)divisor << shift, unit);
1297+
}
1298+
divisor /= 10;
1299+
}
1300+
while(outLength < 18)
1301+
{
1302+
fractionals *= 10;
1303+
unit *= 10;
1304+
unsafeInterval *= 10;
1305+
outDigits[outLength++] = (char)('0' + (int)(fractionals >> shift));
1306+
fractionals &= one - 1;
1307+
kappa--;
1308+
if (fractionals < unsafeInterval)
1309+
{
1310+
outKappa = kappa;
1311+
return hxGrisuRoundWeed(outDigits, outLength, (tooHigh.f - inW.f) * unit, unsafeInterval,
1312+
fractionals, one, unit);
1313+
}
1314+
}
1315+
return false;
1316+
}
1317+
1318+
// For inValue finite and positive: false when Grisu3 cannot be sure
1319+
static bool hxGrisuDigits(double inValue, char *outDigits, int &outCount, int &outExp)
1320+
{
1321+
unsigned long long bits;
1322+
memcpy(&bits, &inValue, sizeof(bits));
1323+
unsigned long long mantissa = bits & 0x000FFFFFFFFFFFFFULL;
1324+
int biased = (int)((bits >> 52) & 0x7FF);
1325+
HxDiyFp v = biased ? hxDiyFp(mantissa | 0x0010000000000000ULL, biased - 1075) :
1326+
hxDiyFp(mantissa, -1074);
1327+
1328+
// The midpoints to the doubles either side, the lower one nearer just
1329+
// above a power of two
1330+
HxDiyFp plus = hxDiyNormalize(hxDiyFp((v.f << 1) + 1, v.e - 1));
1331+
HxDiyFp minus = (mantissa==0 && biased>1) ? hxDiyFp((v.f << 2) - 1, v.e - 2) :
1332+
hxDiyFp((v.f << 1) - 1, v.e - 1);
1333+
minus.f <<= minus.e - plus.e;
1334+
minus.e = plus.e;
1335+
HxDiyFp w = hxDiyNormalize(v);
1336+
1337+
// A cached power that scales w to a binary exponent from -60 to -32
1338+
int minExponent = -60 - (w.e + 64);
1339+
int k = (int)ceil((minExponent + 63) * 0.30102999566398114);
1340+
const HxCachedPower &power = sHxCachedPowers[(348 + k - 1) / 8 + 1];
1341+
HxDiyFp tenMk = hxDiyFp(power.f, power.e);
1342+
1343+
char digits[18];
1344+
int length = 0;
1345+
int kappa = 0;
1346+
if (!hxGrisuDigitGen(hxDiyTimes(minus, tenMk), hxDiyTimes(w, tenMk), hxDiyTimes(plus, tenMk),
1347+
digits, length, kappa) || length>17)
1348+
return false;
1349+
memcpy(outDigits, digits, length);
1350+
outCount = hxTrimZeros(outDigits, length);
1351+
outExp = length - 1 - power.decimal + kappa;
1352+
return true;
1353+
}
1354+
10981355
// The fewest significant digits that read back as inValue, which is finite
10991356
// and positive, choosing the nearest such digits, like std::to_chars
11001357
static int hxShortestDigits(double inValue, char *outDigits, int &outExp)
11011358
{
1359+
int count = 0;
1360+
if (hxGrisuDigits(inValue,outDigits,count,outExp))
1361+
return count;
1362+
1363+
// Rounding with printf and reading back with strtod, which decides
1364+
HxCNumericScope cNumeric;
11021365
int binExp;
11031366
bool powerOfTwo = inValue>DBL_MIN && frexp(inValue,&binExp)==0.5;
11041367
if (inValue>=DBL_MIN && !powerOfTwo)
@@ -1149,9 +1412,9 @@ String::String(const double &inRHS)
11491412
return;
11501413
}
11511414
char buf[100];
1152-
HxCNumericScope cNumeric;
11531415
if (HX_DOUBLE_PATTERN_CUSTOM)
11541416
{
1417+
HxCNumericScope cNumeric;
11551418
HX_SNPRINTF_C(buf,100,HX_DOUBLE_PATTERN,inRHS);
11561419
}
11571420
else

‎src/hx/CFFI.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,13 @@ void val_buffer(buffer inBuffer,value inValue)
715715
hx::Object *obj = (hx::Object *)inValue;
716716
if (obj)
717717
{
718-
buffer_append(inBuffer, obj->toString().__CStr());
718+
// By byte count, so a NUL in the text does not end it. The length
719+
// goes in too: given 0, the UTF-16 conversion stops at the first NUL
720+
String text = obj->toString();
721+
int len = text.length;
722+
const char *utf8 = text.utf8_str(0, false, &len);
723+
if (utf8 && len>0)
724+
buffer_append_sub(inBuffer, utf8, len);
719725
}
720726
else
721727
{

0 commit comments

Comments
 (0)