3
# This is a helper script to validate a file for asm.js.
5
# cmdline usage: 'python validate_asmjs.py filename.{html/js}'
6
# Prints a line starting with 'OK: ' on success, and returns process exit code 0.
7
# On failure, prints a line starting with 'FAIL: ', and returns a nonzero process exit code.
9
# python usage: 'validate_asmjs("filename.{html/js}", muteOutput=True/False)'
10
# Returns True/False depending on whether the file was valid asm.js.
12
# This script depends on the SpiderMonkey JS engine, which must be present in PATH in order for this script to function.
14
import subprocess, sys, re, tempfile, os, time
17
# Looks up SpiderMonkey engine using the variable SPIDERMONKEY_ENGINE in ~/.emscripten, and if not set up there, via PATH.
18
def find_spidermonkey_engine():
19
sm_engine = shared.SPIDERMONKEY_ENGINE if hasattr(shared, 'SPIDERMONKEY_ENGINE') else ['']
20
if not sm_engine or len(sm_engine[0]) == 0 or not os.path.exists(sm_engine[0]):
21
sm_engine[0] = shared.Building.which('js')
22
if sm_engine[0] == None:
23
return ['js-not-found']
26
# Given a .js file, returns True/False depending on if that file is valid asm.js
27
def validate_asmjs_jsfile(filename, muteOutput):
28
process = subprocess.Popen(find_spidermonkey_engine() + ['-c', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
29
(stdout, stderr) = process.communicate()
31
if len(stdout.strip()) > 0:
33
if len(stderr.strip()) > 0:
34
# Pretty-print the output not to contain a spurious warning.
35
stderr = stderr.replace('warning: successfully compiled asm.js', ' successfully compiled asm.js')
37
print >> sys.stderr, stderr.strip()
38
if 'successfully compiled asm.js' in stderr:
43
# This tool takes as input a file built with Emscripten (either .html or .js) and validates it for asm.js.
44
# Returns True/False denoting whether the file was valid asm.js. In case of a .html file, all <script>content</script> tags are searched,
45
# and the ones containing a "use asm" section are validated.
46
def validate_asmjs(filename, muteOutput):
47
if filename.endswith('.html'):
48
html = open(filename, 'r').read()
49
matches = re.findall('''<\w*script\w*.*?>(.*?)<\w*/script\w*>''', html, re.DOTALL | re.MULTILINE)
52
if '"use asm"' in match:
53
numAsmJsBlocks = numAsmJsBlocks + 1
54
tmp_js = tempfile.mkstemp(suffix='.js')
55
os.write(tmp_js[0], match)
57
valid_asmjs = validate_asmjs_jsfile(tmp_js[1], muteOutput)
61
if numAsmJsBlocks == 0:
63
print >> sys.stderr, 'Error: the file does not contain any "use asm" modules.'
68
return validate_asmjs_jsfile(filename, muteOutput)
72
print 'Usage: validate_asmjs <filename>'
74
if validate_asmjs(sys.argv[1], muteOutput=False):
75
print "OK: File '" + sys.argv[1] + "' validates as asm.js"
78
print "FAIL: File '" + sys.argv[1] + "' is not valid asm.js"
81
if __name__ == '__main__':