Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
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?

}

// Correction tools
Expand Down
10 changes: 10 additions & 0 deletions test/test_multiline_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
Copy link
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?

#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/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/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_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15283,3 +15283,12 @@ def test_binary_encode(self, extra):
console.log('OK');'''
write_file('test.js', read_file(path_from_root('src/binaryDecode.js')) + '\nvar src = ' + binary_encoded + ';\n' + test_js)
self.assertContained('OK', self.run_js('test.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('test_multiline_string.c', cflags=['--js-library', test_file('test_multiline_string.js')] + args)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add this test instead to test/test_jslib.js?

Loading