Skip to content

Commit 3b49f69

Browse files
committed
do not remove braces in asmLastOpts when emitting source maps #3466
1 parent 2fca108 commit 3b49f69

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

tests/test_core.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6914,23 +6914,11 @@ def build_and_check():
69146914
import json
69156915
Building.emcc(src_filename, Settings.serialize() + self.emcc_args +
69166916
Building.COMPILER_TEST_OPTS, out_filename, stderr=PIPE)
6917-
with open(out_filename) as f: out_file = f.read()
69186917
# after removing the @line and @sourceMappingURL comments, the build
69196918
# result should be identical to the non-source-mapped debug version.
69206919
# this is worth checking because the parser AST swaps strings for token
69216920
# objects when generating source maps, so we want to make sure the
69226921
# optimizer can deal with both types.
6923-
out_file = re.sub(' *//[@#].*$', '', out_file, flags=re.MULTILINE)
6924-
def clean(code):
6925-
code = code.replace('// EMSCRIPTEN_GENERATED_FUNCTIONS: ["_malloc","__Z3foov","_free","_main"]', '')
6926-
code = re.sub(';', ';\n', code) # put statements each on a new line
6927-
code = re.sub(r'\n+[ \n]*\n+', '\n', code)
6928-
code = re.sub(' L\d+ ?:', '', code) # ignore labels; they can change in each compile
6929-
code = code.replace('{\n}', '{}')
6930-
lines = code.split('\n')
6931-
lines = filter(lambda line: ': do {' not in line and ' break L' not in line, lines) # ignore labels; they can change in each compile
6932-
return '\n'.join(sorted(lines))
6933-
self.assertIdentical(clean(no_maps_file), clean(out_file))
69346922
map_filename = out_filename + '.map'
69356923
data = json.load(open(map_filename, 'r'))
69366924
self.assertPathsIdentical(out_filename, data['file'])

tests/test_other.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,3 +4848,24 @@ def test_file_packager_eval(self):
48484848
out, err = Popen([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'temp.txt', '--no-closure'], stdout=PIPE, stderr=PIPE).communicate()
48494849
assert BAD not in out, out[max(out.index(BAD)-80, 0) : min(out.index(BAD)+80, len(out)-1)]
48504850

4851+
def test_debug_asmLastOpts(self):
4852+
open('src.c', 'w').write(r'''
4853+
#include <stdio.h>
4854+
struct Dtlink_t
4855+
{ struct Dtlink_t* right; /* right child */
4856+
union
4857+
{ unsigned int _hash; /* hash value */
4858+
struct Dtlink_t* _left; /* left child */
4859+
} hl;
4860+
};
4861+
int treecount(register struct Dtlink_t* e)
4862+
{
4863+
return e ? treecount(e->hl._left) + treecount(e->right) + 1 : 0;
4864+
}
4865+
int main() {
4866+
printf("hello, world!\n");
4867+
}
4868+
''')
4869+
out, err = Popen([PYTHON, EMCC, 'src.c', '-s', 'EXPORTED_FUNCTIONS=["_main", "_treecount"]', '--minify', '0', '-g4', '-Oz']).communicate()
4870+
self.assertContained('hello, world!', run_js('a.out.js'))
4871+

tools/js-optimizer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7443,12 +7443,14 @@ function asmLastOpts(ast) {
74437443
var stats = getStatements(node);
74447444
if (stats) statsStack.pop();
74457445
});
7446-
// convert { singleton } into singleton
7447-
traverse(fun, function(node, type) {
7448-
if (type === 'block' && node[1] && node[1].length === 1) {
7449-
return node[1][0];
7450-
}
7451-
});
7446+
if (!debug) { // dangerous in debug mode, as without braces things can end up on the same line, together with comments
7447+
// convert { singleton } into singleton
7448+
traverse(fun, function(node, type) {
7449+
if (type === 'block' && node[1] && node[1].length === 1) {
7450+
return node[1][0];
7451+
}
7452+
});
7453+
}
74527454
});
74537455
}
74547456

0 commit comments

Comments
 (0)