10
from pypy.lang.js.interpreter import *
11
from pypy.lang.js.jsobj import W_Builtin, W_String, ThrowException, w_Undefined
12
from pypy.lang.js import jsparser
17
PyPy's JavaScript Interpreter:
18
-f filename - Loads a file
19
-n to not be interactive
20
-h show this help message
21
-d jump to a pdb in case of failure
29
histfile = os.path.join(os.environ["HOME"], ".jspypyhist")
31
getattr(readline, "clear_history", lambda : None)()
32
readline.read_history_file(histfile)
36
atexit.register(readline.write_history_file, histfile)
38
class Usage(Exception):
39
def __init__(self, msg):
42
def loadjs(ctx, args, this):
44
t = load_file(filename.ToString())
47
def tracejs(ctx, args, this):
52
def quitjs(ctx, args, this):
57
# XXX: note. This will not work when translated, because
58
# globals cannot be modified (ie. interactive is always True).
59
# so I'm adding support which will not be translated, probably
60
# for further consideration
67
opts, args = getopt.getopt(argv[1:], "hdnf:", ["help",])
68
except getopt.error, msg:
73
for option, value in opts:
75
filenames.append(value)
78
if option in ("-h", "--help"):
79
raise Usage(help_message)
84
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
85
print >> sys.stderr, "\t for help use --help"
88
interp = Interpreter()
90
interp.w_Global.Put('quit', W_Builtin(quitjs))
91
interp.w_Global.Put('load', W_Builtin(loadjs))
92
interp.w_Global.Put('trace', W_Builtin(tracejs))
93
for filename in filenames:
95
loadjs(interp.global_context, [W_String(filename)], None)
96
# XXX we should catch more stuff here, like not implemented
98
except (jsparser.JsSyntaxError, ThrowException), e:
99
if isinstance(e, jsparser.JsSyntaxError):
100
print "\nSyntax error!"
101
elif isinstance(e, ThrowException):
102
print "\nJS Exception thrown!"
106
# res = interp.run(load_source(raw_input("js-pypy> ")))
107
# if res is not None:
110
MyCmd(interp, debug).cmdloop()
112
class MyCmd(cmd.Cmd):
114
def __init__(self, interp, debug):
115
cmd.Cmd.__init__(self)
122
self.prompt = self.__class__.prompt
126
def default(self, line):
127
# let's count lines and continue till matching proper nr of {
128
# XXX: '{' will count as well
129
# we can avoid this by using our own's tokeniser, when we possess one
133
opens = line.count('{')
134
closes = line.count('}')
135
self.level += opens - closes
136
self.lines.append(line)
138
self.prompt = ' ... '
141
print "\nError!!! Too many closing braces"
146
res = self.interp.run(load_source("\n".join(self.lines)))
147
# XXX we should catch more stuff here, like not implemented
149
except (jsparser.JsSyntaxError, ThrowException), e:
150
e_info = sys.exc_info()
153
pdb.post_mortem(e_info[2])
155
if isinstance(e, jsparser.JsSyntaxError):
156
print "\nSyntax error!"
157
elif isinstance(e, ThrowException):
158
print e.exception.ToString()
162
if (res is not None) and (res is not w_Undefined):
164
print res.GetValue().ToString()
165
except ThrowException, e:
166
print e.exception.ToString()
168
if __name__ == "__main__":
170
py.test.config.parse([])