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