@no_wasm_backend('https://github.com/emscripten-core/emscripten/issues/9527')
def test_eval_ctors(self):
    if (('-O2' not in str(self.emcc_args)) or ('-O1' in str(self.emcc_args))):
        self.skipTest('need js optimizations')
    if (not self.get_setting('WASM')):
        self.skipTest('this test uses wasm binaries')
    orig_args = self.emcc_args
    print('leave printf in ctor')
    self.emcc_args = (orig_args + ['-s', 'EVAL_CTORS=1'])
    self.do_run('\n      #include <stdio.h>\n      struct C {\n        C() { printf("constructing!\\n"); } // don\'t remove this!\n      };\n      C c;\n      int main() {}\n    ', 'constructing!\n')

    def get_code_size():
        if self.is_wasm():
            return self.count_wasm_contents('src.cpp.o.wasm', 'funcs')
        else:
            return os.path.getsize('src.cpp.o.js')

    def get_mem_size():
        if self.is_wasm():
            return self.count_wasm_contents('src.cpp.o.wasm', 'memory-data')
        if self.uses_memory_init_file():
            return os.path.getsize('src.cpp.o.js.mem')
        return 0

    def do_test(test):
        self.emcc_args = (orig_args + ['-s', 'EVAL_CTORS=1'])
        test()
        ec_code_size = get_code_size()
        ec_mem_size = get_mem_size()
        self.emcc_args = orig_args
        test()
        code_size = get_code_size()
        mem_size = get_mem_size()
        if mem_size:
            print('mem: ', mem_size, '=>', ec_mem_size)
            self.assertGreater(ec_mem_size, mem_size)
        print('code:', code_size, '=>', ec_code_size)
        self.assertLess(ec_code_size, code_size)
    print('remove ctor of just assigns to memory')

    def test1():
        self.do_run('\n        #include <stdio.h>\n        struct C {\n          int x;\n          C() {\n            volatile int y = 10;\n            y++;\n            x = y;\n          }\n        };\n        C c;\n        int main() {\n          printf("x: %d\\n", c.x);\n        }\n      ', 'x: 11\n')
    do_test(test1)
    if self.is_wasm_backend():
        return
    print('libcxx - remove 2 ctors from iostream code')
    src = open(path_from_root('tests', 'hello_libcxx.cpp')).read()
    output = 'hello, world!'

    def test2():
        self.do_run(src, output)
    do_test(test2)
    print('assertions too')
    self.set_setting('ASSERTIONS', 1)
    self.do_run(src, output)
    self.set_setting('ASSERTIONS', 0)
    print('remove just some, leave others')

    def test3():
        self.do_run('\n#include <iostream>\n#include <string>\n\nclass std_string {\npublic:\n  std_string(): ptr(nullptr) { std::cout << "std_string()\\n"; }\n  std_string(const char* s): ptr(s) { std::cout << "std_string(const char* s)" << std::endl; }\n  std_string(const std_string& s): ptr(s.ptr) { std::cout << "std_string(const std_string& s) " << std::endl; }\n  const char* data() const { return ptr; }\nprivate:\n  const char* ptr;\n};\n\nconst std_string txtTestString("212121\\0");\nconst std::string s2text("someweirdtext");\n\nint main() {\n  std::cout << s2text << std::endl;\n  std::cout << txtTestString.data() << std::endl;\n  std::cout << txtTestString.data() << std::endl;\n  return 0;\n}\n      ', 'std_string(const char* s)\nsomeweirdtext\n212121\n212121\n')
    do_test(test3)
@no_wasm_backend('https://github.com/emscripten-core/emscripten/issues/9527')
def test_eval_ctors(self):
    if (('-O2' not in str(self.emcc_args)) or ('-O1' in str(self.emcc_args))):
        self.skipTest('need js optimizations')
    if (not self.get_setting('WASM')):
        self.skipTest('this test uses wasm binaries')
    orig_args = self.emcc_args
    print('leave printf in ctor')
    self.emcc_args = (orig_args + ['-s', 'EVAL_CTORS=1'])
    self.do_run('\n      #include <stdio.h>\n      struct C {\n        C() { printf("constructing!\\n"); } // don\'t remove this!\n      };\n      C c;\n      int main() {}\n    ', 'constructing!\n')

    def get_code_size():
        if self.is_wasm():
            return self.count_wasm_contents('src.cpp.o.wasm', 'funcs')
        else:
            return os.path.getsize('src.cpp.o.js')

    def get_mem_size():
        if self.is_wasm():
            return self.count_wasm_contents('src.cpp.o.wasm', 'memory-data')
        if self.uses_memory_init_file():
            return os.path.getsize('src.cpp.o.js.mem')
        return 0

    def do_test(test):
        self.emcc_args = (orig_args + ['-s', 'EVAL_CTORS=1'])
        test()
        ec_code_size = get_code_size()
        ec_mem_size = get_mem_size()
        self.emcc_args = orig_args
        test()
        code_size = get_code_size()
        mem_size = get_mem_size()
        if mem_size:
            print('mem: ', mem_size, '=>', ec_mem_size)
            self.assertGreater(ec_mem_size, mem_size)
        print('code:', code_size, '=>', ec_code_size)
        self.assertLess(ec_code_size, code_size)
    print('remove ctor of just assigns to memory')

    def test1():
        self.do_run('\n        #include <stdio.h>\n        struct C {\n          int x;\n          C() {\n            volatile int y = 10;\n            y++;\n            x = y;\n          }\n        };\n        C c;\n        int main() {\n          printf("x: %d\\n", c.x);\n        }\n      ', 'x: 11\n')
    do_test(test1)
    if self.is_wasm_backend():
        return
    print('libcxx - remove 2 ctors from iostream code')
    src = open(path_from_root('tests', 'hello_libcxx.cpp')).read()
    output = 'hello, world!'

    def test2():
        self.do_run(src, output)
    do_test(test2)
    print('assertions too')
    self.set_setting('ASSERTIONS', 1)
    self.do_run(src, output)
    self.set_setting('ASSERTIONS', 0)
    print('remove just some, leave others')

    def test3():
        self.do_run('\n#include <iostream>\n#include <string>\n\nclass std_string {\npublic:\n  std_string(): ptr(nullptr) { std::cout << "std_string()\\n"; }\n  std_string(const char* s): ptr(s) { std::cout << "std_string(const char* s)" << std::endl; }\n  std_string(const std_string& s): ptr(s.ptr) { std::cout << "std_string(const std_string& s) " << std::endl; }\n  const char* data() const { return ptr; }\nprivate:\n  const char* ptr;\n};\n\nconst std_string txtTestString("212121\\0");\nconst std::string s2text("someweirdtext");\n\nint main() {\n  std::cout << s2text << std::endl;\n  std::cout << txtTestString.data() << std::endl;\n  std::cout << txtTestString.data() << std::endl;\n  return 0;\n}\n      ', 'std_string(const char* s)\nsomeweirdtext\n212121\n212121\n')
    do_test(test3)