@no_wasm2js('TODO: source maps in wasm2js')
def test_source_map(self):
    if ('-g' not in self.emcc_args):
        self.emcc_args.append('-g')
    src = '\n      #include <stdio.h>\n      #include <assert.h>\n\n      __attribute__((noinline)) int foo() {\n        printf("hi"); // line 6\n        return 1; // line 7\n      }\n\n      int main() {\n        printf("%d", foo()); // line 11\n        return 0; // line 12\n      }\n    '
    create_test_file('src.cpp', src)
    out_filename = 'a.out.js'
    wasm_filename = 'a.out.wasm'
    no_maps_filename = 'no-maps.out.js'
    assert ('-g4' not in self.emcc_args)
    building.emcc('src.cpp', ((self.serialize_settings() + self.emcc_args) + self.emcc_args), out_filename)
    shutil.move(out_filename, no_maps_filename)
    with open(no_maps_filename) as f:
        no_maps_file = f.read()
    no_maps_file = re.sub(' *//[@#].*$', '', no_maps_file, flags=re.MULTILINE)
    self.emcc_args.append('-g4')
    building.emcc(os.path.abspath('src.cpp'), ((self.serialize_settings() + self.emcc_args) + self.emcc_args), out_filename, stderr=PIPE)
    map_referent = (out_filename if (not self.get_setting('WASM')) else wasm_filename)
    map_filename = (map_referent + '.map')

    def encode_utf8(data):
        if isinstance(data, dict):
            for key in data:
                data[key] = encode_utf8(data[key])
            return data
        elif isinstance(data, list):
            for i in range(len(data)):
                data[i] = encode_utf8(data[i])
            return data
        elif isinstance(data, type(u'')):
            return data.encode('utf8')
        else:
            return data

    def source_map_file_loc(name):
        if shared.Settings.WASM_BACKEND:
            return name
        return os.path.abspath(name)
    data = json.load(open(map_filename))
    if (str is bytes):
        data = encode_utf8(data)
    if hasattr(data, 'file'):
        self.assertPathsIdentical(map_referent, data['file'])
    assert (len(data['sources']) == 1), data['sources']
    self.assertPathsIdentical(source_map_file_loc('src.cpp'), data['sources'][0])
    if hasattr(data, 'sourcesContent'):
        self.assertTextDataIdentical(src, data['sourcesContent'][0])
    mappings = json.loads(jsrun.run_js(path_from_root('tools', 'source-maps', 'sourcemap2json.js'), shared.NODE_JS, [map_filename]))
    if (str is bytes):
        mappings = encode_utf8(mappings)
    seen_lines = set()
    for m in mappings:
        self.assertPathsIdentical(source_map_file_loc('src.cpp'), m['source'])
        seen_lines.add(m['originalLine'])
    if is_optimizing(self.emcc_args):
        assert seen_lines.issuperset([11, 12]), seen_lines
    else:
        assert seen_lines.issuperset([6, 7, 11, 12]), seen_lines
@no_wasm2js('TODO: source maps in wasm2js')
def test_source_map(self):
    if ('-g' not in self.emcc_args):
        self.emcc_args.append('-g')
    src = '\n      #include <stdio.h>\n      #include <assert.h>\n\n      __attribute__((noinline)) int foo() {\n        printf("hi"); // line 6\n        return 1; // line 7\n      }\n\n      int main() {\n        printf("%d", foo()); // line 11\n        return 0; // line 12\n      }\n    '
    create_test_file('src.cpp', src)
    out_filename = 'a.out.js'
    wasm_filename = 'a.out.wasm'
    no_maps_filename = 'no-maps.out.js'
    assert ('-g4' not in self.emcc_args)
    building.emcc('src.cpp', ((self.serialize_settings() + self.emcc_args) + self.emcc_args), out_filename)
    shutil.move(out_filename, no_maps_filename)
    with open(no_maps_filename) as f:
        no_maps_file = f.read()
    no_maps_file = re.sub(' *//[@#].*$', , no_maps_file, flags=re.MULTILINE)
    self.emcc_args.append('-g4')
    building.emcc(os.path.abspath('src.cpp'), ((self.serialize_settings() + self.emcc_args) + self.emcc_args), out_filename, stderr=PIPE)
    map_referent = (out_filename if (not self.get_setting('WASM')) else wasm_filename)
    map_filename = (map_referent + '.map')

    def encode_utf8(data):
        if isinstance(data, dict):
            for key in data:
                data[key] = encode_utf8(data[key])
            return data
        elif isinstance(data, list):
            for i in range(len(data)):
                data[i] = encode_utf8(data[i])
            return data
        elif isinstance(data, type(u)):
            return data.encode('utf8')
        else:
            return data

    def source_map_file_loc(name):
        if shared.Settings.WASM_BACKEND:
            return name
        return os.path.abspath(name)
    data = json.load(open(map_filename))
    if (str is bytes):
        data = encode_utf8(data)
    if hasattr(data, 'file'):
        self.assertPathsIdentical(map_referent, data['file'])
    assert (len(data['sources']) == 1), data['sources']
    self.assertPathsIdentical(source_map_file_loc('src.cpp'), data['sources'][0])
    if hasattr(data, 'sourcesContent'):
        self.assertTextDataIdentical(src, data['sourcesContent'][0])
    mappings = json.loads(jsrun.run_js(path_from_root('tools', 'source-maps', 'sourcemap2json.js'), shared.NODE_JS, [map_filename]))
    if (str is bytes):
        mappings = encode_utf8(mappings)
    seen_lines = set()
    for m in mappings:
        self.assertPathsIdentical(source_map_file_loc('src.cpp'), m['source'])
        seen_lines.add(m['originalLine'])
    if is_optimizing(self.emcc_args):
        assert seen_lines.issuperset([11, 12]), seen_lines
    else:
        assert seen_lines.issuperset([6, 7, 11, 12]), seen_lines