Remove fs churn in spidermonkey (cuts test time in half)

This commit is contained in:
Matt Basta 2013-10-14 11:44:57 -07:00
Родитель 7db29d451b
Коммит 62e31c66d3
1 изменённых файлов: 12 добавлений и 42 удалений

Просмотреть файл

@ -1,8 +1,5 @@
import codecs
import os
import re import re
import subprocess import subprocess
import tempfile
import simplejson as json import simplejson as json
@ -69,63 +66,36 @@ class JSReflectException(Exception):
return self return self
def prepare_code(code):
"""Prepare code for tree generation."""
code = unicodehelper.decode(code)
# Acceptable unicode characters still need to be stripped. Just remove the
# slash: a character is necessary to prevent bad identifier errors.
return JS_ESCAPE.sub("u", code)
def _get_tree(code, shell=SPIDERMONKEY_INSTALLATION): def _get_tree(code, shell=SPIDERMONKEY_INSTALLATION):
"""Return an AST tree of the JS passed in `code`.""" """Return an AST tree of the JS passed in `code`."""
if not code: if not code:
return None return None
code = prepare_code(code) # code = JS_ESCAPE.sub("u", unicodehelper.decode(code)).encode("utf_8")
code = json.dumps(JS_ESCAPE.sub("u", unicodehelper.decode(code)))
with tempfile.NamedTemporaryFile(mode="w+b", delete=False) as temp:
temp_name = temp.name
temp.write(code.encode("utf_8"))
data = """ data = """
try{options("allow_xml");}catch(e){} var stdin = JSON.parse(readline());
try{ try{
print(JSON.stringify(Reflect.parse(read(%s)))); print(JSON.stringify(Reflect.parse(stdin)));
} catch(e) { } catch(e) {
print(JSON.stringify({ print(JSON.stringify({
"error":true, "error":true,
"error_message":e.toString(), "error_message":e.toString(),
"line_number":e.lineNumber "line_number":e.lineNumber
})); }));
}""" % json.dumps(temp_name) }"""
try: cmd = [shell, "-e", data]
cmd = [shell, "-e", data, "-U"] shell_obj = subprocess.Popen(
shell_obj = subprocess.Popen( cmd, shell=False, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
cmd, shell=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stdout=subprocess.PIPE)
data, stderr = shell_obj.communicate() data, stderr = shell_obj.communicate(code)
# Spidermonkey dropped the -U flag on 29 Oct 2012
if stderr and ("Invalid short option: -U" in stderr or
"usage: js [options] [scriptfile]" in stderr):
cmd.remove("-U")
shell_obj = subprocess.Popen(
cmd, shell=False,
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
data, stderr = shell_obj.communicate() if stderr:
raise RuntimeError('Error calling %r: %s' % (cmd, stderr))
if stderr:
raise RuntimeError('Error calling %r: %s' % (cmd, stderr))
finally:
try:
os.unlink(temp_name)
except IOError:
pass
if not data: if not data:
raise JSReflectException("Reflection failed") raise JSReflectException("Reflection failed")