~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tools/validate_asmjs.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# This is a helper script to validate a file for asm.js.
 
4
 
 
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.
 
8
 
 
9
# python usage: 'validate_asmjs("filename.{html/js}", muteOutput=True/False)'
 
10
# Returns True/False depending on whether the file was valid asm.js.
 
11
 
 
12
# This script depends on the SpiderMonkey JS engine, which must be present in PATH in order for this script to function.
 
13
 
 
14
import subprocess, sys, re, tempfile, os, time
 
15
import shared
 
16
 
 
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']
 
24
  return sm_engine
 
25
 
 
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()
 
30
  if not muteOutput:
 
31
    if len(stdout.strip()) > 0:
 
32
      print stdout.strip()
 
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')
 
36
 
 
37
      print >> sys.stderr, stderr.strip()
 
38
  if 'successfully compiled asm.js' in stderr:
 
39
    return True
 
40
  else:
 
41
    return False
 
42
 
 
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)
 
50
    numAsmJsBlocks = 0
 
51
    for match in matches:
 
52
      if '"use asm"' in match:
 
53
        numAsmJsBlocks = numAsmJsBlocks + 1
 
54
        tmp_js = tempfile.mkstemp(suffix='.js')
 
55
        os.write(tmp_js[0], match)
 
56
        os.close(tmp_js[0])
 
57
        valid_asmjs = validate_asmjs_jsfile(tmp_js[1], muteOutput)
 
58
        os.remove(tmp_js[1])
 
59
        if not valid_asmjs:
 
60
          return False
 
61
    if numAsmJsBlocks == 0:
 
62
      if not muteOutput:
 
63
        print >> sys.stderr, 'Error: the file does not contain any "use asm" modules.'
 
64
      return False
 
65
    else:
 
66
      return True
 
67
  else:
 
68
    return validate_asmjs_jsfile(filename, muteOutput)
 
69
 
 
70
def main():
 
71
  if len(sys.argv) < 2:
 
72
    print 'Usage: validate_asmjs <filename>'
 
73
    return 2
 
74
  if validate_asmjs(sys.argv[1], muteOutput=False):
 
75
    print "OK: File '" + sys.argv[1] + "' validates as asm.js"
 
76
    return 0
 
77
  else:
 
78
    print "FAIL: File '" + sys.argv[1] + "' is not valid asm.js"
 
79
    return 1
 
80
 
 
81
if __name__ == '__main__':
 
82
  sys.exit(main())