19
19
20
20
from support import run_command
21
21
from shared import (
22
- ASM2WASM , binary_format_check , delete_from_orbit ,
22
+ ASM2WASM , WASM_OPT , binary_format_check , delete_from_orbit ,
23
23
fail , fail_with_error , fail_if_not_identical , options , tests
24
24
)
25
25
@@ -28,112 +28,119 @@ def test_asm2wasm():
28
28
print '[ checking asm2wasm testcases... ]\n '
29
29
30
30
for asm in tests :
31
- if asm .endswith ('.asm.js' ):
32
- for precise in [0 , 1 , 2 ]:
33
- for opts in [1 , 0 ]:
34
- cmd = ASM2WASM + [os .path .join (options .binaryen_test , asm )]
35
- wasm = asm .replace ('.asm.js' , '.fromasm' )
36
- if not precise :
37
- cmd += ['--emit-potential-traps' , '--ignore-implicit-traps' ]
38
- wasm += '.imprecise'
39
- elif precise == 2 :
40
- cmd += ['--emit-clamped-potential-traps' ]
41
- wasm += '.clamp'
42
- if not opts :
43
- wasm += '.no-opts'
44
- if precise :
45
- cmd += ['-O0' ] # test that -O0 does nothing
31
+ if not asm .endswith ('.asm.js' ):
32
+ continue
33
+ for precise in [0 , 1 , 2 ]:
34
+ for opts in [1 , 0 ]:
35
+ cmd = ASM2WASM + [os .path .join (options .binaryen_test , asm )]
36
+ wasm = asm .replace ('.asm.js' , '.fromasm' )
37
+ if not precise :
38
+ cmd += ['--emit-potential-traps' , '--ignore-implicit-traps' ]
39
+ wasm += '.imprecise'
40
+ elif precise == 2 :
41
+ cmd += ['--emit-clamped-potential-traps' ]
42
+ wasm += '.clamp'
43
+ if not opts :
44
+ wasm += '.no-opts'
45
+ if precise :
46
+ cmd += ['-O0' ] # test that -O0 does nothing
47
+ else :
48
+ cmd += ['-O' ]
49
+ if 'debugInfo' in asm :
50
+ cmd += ['-g' ]
51
+ if 'noffi' in asm :
52
+ cmd += ['--no-legalize-javascript-ffi' ]
53
+ if precise and opts :
54
+ # test mem init importing
55
+ open ('a.mem' , 'wb' ).write (asm )
56
+ cmd += ['--mem-init=a.mem' ]
57
+ if asm [0 ] == 'e' :
58
+ cmd += ['--mem-base=1024' ]
59
+ if 'i64' in asm or 'wasm-only' in asm or 'noffi' in asm :
60
+ cmd += ['--wasm-only' ]
61
+ wasm = os .path .join (options .binaryen_test , wasm )
62
+ print '..' , asm , wasm
63
+
64
+ def do_asm2wasm_test ():
65
+ actual = run_command (cmd )
66
+
67
+ # verify output
68
+ if not os .path .exists (wasm ):
69
+ fail_with_error ('output .wast file %s does not exist' % wasm )
70
+ expected = open (wasm , 'rb' ).read ()
71
+ if actual != expected :
72
+ fail (actual , expected )
73
+
74
+ binary_format_check (wasm , verify_final_result = False )
75
+
76
+ # test both normally and with pass debug (so each inter-pass state
77
+ # is validated)
78
+ old_pass_debug = os .environ .get ('BINARYEN_PASS_DEBUG' )
79
+ try :
80
+ os .environ ['BINARYEN_PASS_DEBUG' ] = '1'
81
+ print "With BINARYEN_PASS_DEBUG=1:"
82
+ do_asm2wasm_test ()
83
+ del os .environ ['BINARYEN_PASS_DEBUG' ]
84
+ print "With BINARYEN_PASS_DEBUG disabled:"
85
+ do_asm2wasm_test ()
86
+ finally :
87
+ if old_pass_debug is not None :
88
+ os .environ ['BINARYEN_PASS_DEBUG' ] = old_pass_debug
46
89
else :
47
- cmd += ['-O' ]
48
- if 'debugInfo' in asm :
49
- cmd += ['-g' ]
50
- if 'noffi' in asm :
51
- cmd += ['--no-legalize-javascript-ffi' ]
52
- if precise and opts :
53
- # test mem init importing
54
- open ('a.mem' , 'wb' ).write (asm )
55
- cmd += ['--mem-init=a.mem' ]
56
- if asm [0 ] == 'e' :
57
- cmd += ['--mem-base=1024' ]
58
- if 'i64' in asm or 'wasm-only' in asm or 'noffi' in asm :
59
- cmd += ['--wasm-only' ]
60
- wasm = os .path .join (options .binaryen_test , wasm )
61
- print '..' , asm , wasm
62
-
63
- def do_asm2wasm_test ():
64
- actual = run_command (cmd )
65
-
66
- # verify output
67
- if not os .path .exists (wasm ):
68
- fail_with_error ('output .wast file %s does not exist' % wasm )
69
- expected = open (wasm , 'rb' ).read ()
70
- if actual != expected :
71
- fail (actual , expected )
72
-
73
- binary_format_check (wasm , verify_final_result = False )
74
-
75
- # test both normally and with pass debug (so each inter-pass state is validated)
76
- old_pass_debug = os .environ .get ('BINARYEN_PASS_DEBUG' )
77
- try :
78
- os .environ ['BINARYEN_PASS_DEBUG' ] = '1'
79
- print "With BINARYEN_PASS_DEBUG=1:"
80
- do_asm2wasm_test ()
81
- del os .environ ['BINARYEN_PASS_DEBUG' ]
82
- print "With BINARYEN_PASS_DEBUG disabled:"
83
- do_asm2wasm_test ()
84
- finally :
85
- if old_pass_debug is not None :
86
- os .environ ['BINARYEN_PASS_DEBUG' ] = old_pass_debug
87
- else :
88
- if 'BINARYEN_PASS_DEBUG' in os .environ :
89
- del os .environ ['BINARYEN_PASS_DEBUG' ]
90
-
91
- # verify in wasm
92
- if options .interpreter :
93
- # remove imports, spec interpreter doesn't know what to do with them
94
- subprocess .check_call (WASM_OPT + ['--remove-imports' , wasm ], stdout = open ('ztemp.wast' , 'w' ), stderr = subprocess .PIPE )
95
- proc = subprocess .Popen ([options .interpreter , 'ztemp.wast' ], stderr = subprocess .PIPE )
96
- out , err = proc .communicate ()
97
- if proc .returncode != 0 :
98
- try : # to parse the error
99
- reported = err .split (':' )[1 ]
100
- start , end = reported .split ('-' )
101
- start_line , start_col = map (int , start .split ('.' ))
102
- lines = open ('ztemp.wast' ).read ().split ('\n ' )
103
- print
104
- print '=' * 80
105
- print lines [start_line - 1 ]
106
- print (' ' * (start_col - 1 )) + '^'
107
- print (' ' * (start_col - 2 )) + '/_\\ '
108
- print '=' * 80
109
- print err
110
- except Exception , e :
111
- fail_with_error ('wasm interpreter error: ' + err ) # failed to pretty-print
112
- fail_with_error ('wasm interpreter error' )
113
-
114
- # verify debug info
115
- if 'debugInfo' in asm :
116
- jsmap = 'a.wasm.map'
117
- cmd += ['--source-map' , jsmap ,
118
- '--source-map-url' , 'http://example.org/' + jsmap ,
119
- '-o' , 'a.wasm' ]
120
- run_command (cmd )
121
- if not os .path .isfile (jsmap ):
122
- fail_with_error ('Debug info map not created: %s' % jsmap )
123
- with open (wasm + '.map' , 'rb' ) as expected :
124
- with open (jsmap , 'rb' ) as actual :
125
- fail_if_not_identical (actual .read (), expected .read ())
126
- with open ('a.wasm' , 'rb' ) as binary :
127
- url_section_name = bytearray ([16 ]) + bytearray ('sourceMappingURL' )
128
- payload = 'http://example.org/' + jsmap
129
- assert len (payload ) < 256 , 'name too long'
130
- url_section_contents = bytearray ([len (payload )]) + bytearray (payload )
131
- print url_section_name
132
- binary_contents = bytearray (binary .read ())
133
- if url_section_name not in binary_contents :
134
- fail_with_error ('source map url section not found in binary' )
135
- if url_section_contents not in binary_contents [binary_contents .index (url_section_name ):]:
136
- fail_with_error ('source map url not found in url section' )
90
+ if 'BINARYEN_PASS_DEBUG' in os .environ :
91
+ del os .environ ['BINARYEN_PASS_DEBUG' ]
92
+
93
+ # verify in wasm
94
+ if options .interpreter :
95
+ # remove imports, spec interpreter doesn't know what to do with them
96
+ subprocess .check_call (WASM_OPT + ['--remove-imports' , wasm ],
97
+ stdout = open ('ztemp.wast' , 'w' ),
98
+ stderr = subprocess .PIPE )
99
+ proc = subprocess .Popen ([options .interpreter , 'ztemp.wast' ],
100
+ stderr = subprocess .PIPE )
101
+ out , err = proc .communicate ()
102
+ if proc .returncode != 0 :
103
+ try : # to parse the error
104
+ reported = err .split (':' )[1 ]
105
+ start , end = reported .split ('-' )
106
+ start_line , start_col = map (int , start .split ('.' ))
107
+ lines = open ('ztemp.wast' ).read ().split ('\n ' )
108
+ print
109
+ print '=' * 80
110
+ print lines [start_line - 1 ]
111
+ print (' ' * (start_col - 1 )) + '^'
112
+ print (' ' * (start_col - 2 )) + '/_\\ '
113
+ print '=' * 80
114
+ print err
115
+ except Exception :
116
+ # failed to pretty-print
117
+ fail_with_error ('wasm interpreter error: ' + err )
118
+ fail_with_error ('wasm interpreter error' )
119
+
120
+ # verify debug info
121
+ if 'debugInfo' in asm :
122
+ jsmap = 'a.wasm.map'
123
+ cmd += ['--source-map' , jsmap ,
124
+ '--source-map-url' , 'http://example.org/' + jsmap ,
125
+ '-o' , 'a.wasm' ]
126
+ run_command (cmd )
127
+ if not os .path .isfile (jsmap ):
128
+ fail_with_error ('Debug info map not created: %s' % jsmap )
129
+ with open (wasm + '.map' , 'rb' ) as expected :
130
+ with open (jsmap , 'rb' ) as actual :
131
+ fail_if_not_identical (actual .read (), expected .read ())
132
+ with open ('a.wasm' , 'rb' ) as binary :
133
+ url_section_name = bytearray ([16 ]) + bytearray ('sourceMappingURL' )
134
+ url = 'http://example.org/' + jsmap
135
+ assert len (url ) < 256 , 'name too long'
136
+ url_section_contents = bytearray ([len (url )]) + bytearray (url )
137
+ print url_section_name
138
+ binary_contents = bytearray (binary .read ())
139
+ if url_section_name not in binary_contents :
140
+ fail_with_error ('source map url section not found in binary' )
141
+ url_section_index = binary_contents .index (url_section_name )
142
+ if url_section_contents not in binary_contents [url_section_index :]:
143
+ fail_with_error ('source map url not found in url section' )
137
144
138
145
139
146
def test_asm2wasm_binary ():
@@ -147,6 +154,7 @@ def test_asm2wasm_binary():
147
154
run_command (ASM2WASM + [asmjs , '-o' , 'b.wast' , '-S' ])
148
155
assert open ('b.wast' , 'rb' ).read ()[0 ] != '\0 ' , 'we emit text with -S'
149
156
157
+
150
158
if __name__ == '__main__' :
151
159
test_asm2wasm ()
152
160
test_asm2wasm_binary ()
0 commit comments