Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,39 @@ function splitI64(value) {
export function indentify(text, indent) {
// Don't try to indentify huge strings - we may run out of memory
if (text.length > 1024 * 1024) return text;
if (typeof indent == 'number') {
const len = indent;
indent = '';
for (let i = 0; i < len; i++) {
indent += ' ';

indent = ' '.repeat(indent);

// Perform indentation in a smart fashion that does not leak indentation
// inside multiline strings enclosed in `` characters.
let out = '';
for (let i = 0; i < text.length; ++i) {
// Output a C++ comment as-is, don't get confused by ` inside a C++ comment.
if (text[i] == '/' && text[i + 1] == '/') {
for (; i < text.length && text[i] != '\n'; ++i) {
out += text[i];
}
}

if (text[i] == '/' && text[i + 1] == '*') {
// Skip /* so that /*/ won't be mistaken as start& end of a /* */ comment.
out += text[i++];
out += text[i++];
for (; i < text.length && !(text[i - 1] == '*' && text[i] == '/'); ++i) {
out += text[i];
}
}

if (text[i] == '`') {
out += text[i++]; // Emit `
for (; i < text.length && text[i] != '`'; ++i) {
out += text[i];
}
}
out += text[i];
if (text[i] == '\n') out += indent;
}
return text.replace(/\n/g, `\n${indent}`);
return out;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I wonder at what point we should just acorn for this rather than adding complexity here? Or maybe we should just skip this process completely when the string contains any backticks at all?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I did already go through the trouble of implementing this logic.. so simplest to land this. If it turns out to be buggy in a way that is not seen by the harness, and is difficult to fix, then we can look at alternatives.

}

// Correction tools
Expand Down
10 changes: 10 additions & 0 deletions test/jslib/test_multiline_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this files to test/jslib?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check

#include <stdlib.h>

char *test_multiline_string(void);

int main() {
char *str = test_multiline_string();
printf("%s\n", str);
free(str);
}
15 changes: 15 additions & 0 deletions test/jslib/test_multiline_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mergeInto(LibraryManager.library, {
test_multiline_string__deps: ['$stringToNewUTF8'],
test_multiline_string__sig: 'p',
test_multiline_string: function() {
// do not get confused by ` inside a comment.
var a = `abc
def
ghi`;
/* or a ` inside a C comment. */
var b = `abc
def
ghi`;
return stringToNewUTF8(a + b);
}
});
5 changes: 5 additions & 0 deletions test/jslib/test_multiline_string.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
abc
def
ghiabc
def
ghi
9 changes: 9 additions & 0 deletions test/test_jslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,12 @@ class ParentClass {}
}
''')
self.do_runf('src.c', 'MyClass: 42', cflags=['--js-library', 'lib.js'])

# Tests that JS library functions containing multiline strings are not disturbed by e.g. inserting indentation into the output.
@parameterized({
'': ([],),
'single_file': (['-sSINGLE_FILE'],),
'closure': (['--closure', '1'],),
})
def test_multiline_string(self, args):
self.do_run_in_out_file_test('jslib/test_multiline_string.c', cflags=['--js-library', test_file('jslib/test_multiline_string.js')] + args)
Loading