pmuellr / nitro_pie

Python bindings for JavaScriptCore

This URL has Read+Write access

nitro_pie / nps / nps.py
55efaee9 » pmuellr 2009-04-22 added nps (shell), fixed do... 1 #!/usr/bin/env python
2
3 import os
4 import sys
5
6 #-------------------------------------------------------------------------------
7 def callback_print(context, function, thisObject, args):
8 line = ""
9
10 for arg in args:
11 if JSObject.isJSObject(arg):
12 printable = arg.toString()
13 else:
14 printable = str(arg)
15
16 line = line + printable
17
18 print line
19
20 #-------------------------------------------------------------------------------
21
22 lib_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../lib"))
23 if lib_path not in sys.path: sys.path.insert(0, lib_path)
24
25 from nitro_pie import *
26
27 if len(sys.argv) < 2:
28 print "Excepting the name of a script to run"
29 sys.exit(1)
30
31 script_file_name = sys.argv[1]
32 script_file = open(script_file_name)
33 script = script_file.read()
34 script_file.close()
35
36 context = JSContext()
37
38 globalObject = context.getGlobalObject()
39
40 function = context.makeFunctionWithCallback("print", callback_print)
41 globalObject.setProperty("print", function)
42
43 try:
44 globalObject = context.getGlobalObject()
45 globalObject = None
46 result = context.eval(script, globalObject, script_file_name, 1)
47
48 except JSException, e:
49 e = e.value
50
51 name = e.getProperty("name") if e.hasProperty("name") else ""
52 message = e.getProperty("message") if e.hasProperty("message") else "???"
53 sourceURL = e.getProperty("sourceURL") if e.hasProperty("sourceURL") else "???"
54 line = e.getProperty("line") if e.hasProperty("line") else -1
55
56 print "Exception thrown: %s: %s: at %s[%d]" % (name, message, sourceURL, line)
57
58 props = e.getPropertyNames()
59 known_props = "name message sourceURL line".split()
60 for prop in props:
61 if prop in known_props: continue
62 print " %s: %s" % (prop, e.getProperty(prop))
63 print
64
65 context.release()