Skip to content

Commit

Permalink
add line breaks to data segment arrays to limit line length
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Oct 14, 2024
1 parent 8c0e9dd commit 75c9dbe
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5105,6 +5105,8 @@ wasmCWriteExports(
}
}

#define DATA_SEGMENT_CHUNK_LENGTH 18

/* TODO: add support for multiple modules */
static
void
Expand All @@ -5121,30 +5123,50 @@ wasmCWriteDataSegments(
U32 dataSegmentIndex = 0;
for (; dataSegmentIndex < dataSegmentCount; dataSegmentIndex++) {
const WasmDataSegment dataSegment = module->dataSegments.dataSegments[dataSegmentIndex];
const size_t byteCount = dataSegment.bytes.length;

fputs("const U8 ", file);
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
if (pretty) {
fputs("[] = {\n", file);
fputs("[] = {", file);
} else {
fputs("[]={\n", file);
fputs("[]={", file);
}
if (pretty) {
fputs(indentation, file);
if (byteCount > DATA_SEGMENT_CHUNK_LENGTH) {
fputc('\n', file);
if (pretty) {
fputs(indentation, file);
}
}
{
U32 byteIndex = 0;
for (; byteIndex < dataSegment.bytes.length; byteIndex++) {
fprintf(file, "0x%x", dataSegment.bytes.data[byteIndex]);
if (pretty) {
fputs(", ", file);
for (; byteIndex < byteCount; byteIndex++) {
U8 value = dataSegment.bytes.data[byteIndex];
if (byteIndex > 0) {
if (pretty) {
fputs(", ", file);
} else {
fputc(',', file);
}
if (byteIndex % DATA_SEGMENT_CHUNK_LENGTH == 0) {
fputs("\n", file);
if (pretty) {
fputs(indentation, file);
}
}
}
if (value < 10) {
fprintf(file, "%u", value);
} else {
fputc(',', file);
fprintf(file, "0x%x", value);
}
}
}
fputs("\n};\n\n", file);
if (byteCount > DATA_SEGMENT_CHUNK_LENGTH) {
fputc('\n', file);
}
fputs("};\n\n", file);
}
break;
}
Expand Down

0 comments on commit 75c9dbe

Please sign in to comment.