forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEngine.py
More file actions
162 lines (130 loc) · 4.62 KB
/
JsEngine.py
File metadata and controls
162 lines (130 loc) · 4.62 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: RaNaN
"""
from imp import find_module
from os.path import join, exists
from urllib import quote
ENGINE = ""
DEBUG = False
JS = False
PYV8 = False
RHINO = False
if not ENGINE:
try:
import subprocess
subprocess.Popen(["js", "-v"], bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
p = subprocess.Popen(["js", "-e", "print(23+19)"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
#integrity check
if out.strip() == "42":
ENGINE = "js"
JS = True
except:
pass
if not ENGINE or DEBUG:
try:
find_module("PyV8")
ENGINE = "pyv8"
PYV8 = True
except:
pass
if not ENGINE or DEBUG:
try:
path = "" #path where to find rhino
if exists("/usr/share/java/js.jar"):
path = "/usr/share/java/js.jar"
elif exists("js.jar"):
path = "js.jar"
elif exists(join(pypath, "js.jar")): #may raises an exception, but js.jar wasnt found anyway
path = join(pypath, "js.jar")
if not path:
raise Exception
import subprocess
p = subprocess.Popen(["java", "-cp", path, "org.mozilla.javascript.tools.shell.Main", "-e", "print(23+19)"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
#integrity check
if out.strip() == "42":
ENGINE = "rhino"
RHINO = True
except:
pass
class JsEngine():
def __init__(self):
self.engine = ENGINE
self.init = False
def __nonzero__(self):
return False if not ENGINE else True
def eval(self, script):
if not self.init:
if ENGINE == "pyv8" or (DEBUG and PYV8):
import PyV8
global PyV8
self.init = True
if type(script) == unicode:
script = script.encode("utf8")
if not ENGINE:
raise Exception("No JS Engine")
if not DEBUG:
if ENGINE == "pyv8":
return self.eval_pyv8(script)
elif ENGINE == "js":
return self.eval_js(script)
elif ENGINE == "rhino":
return self.eval_rhino(script)
else:
results = []
if PYV8:
res = self.eval_pyv8(script)
print "PyV8:", res
results.append(res)
if JS:
res = self.eval_js(script)
print "JS:", res
results.append(res)
if RHINO:
res = self.eval_rhino(script)
print "Rhino:", res
results.append(res)
warning = False
for x in results:
for y in results:
if x != y:
warning = True
if warning: print "### WARNING ###: Different results"
return results[0]
def eval_pyv8(self, script):
rt = PyV8.JSContext()
rt.enter()
return rt.eval(script)
def eval_js(self, script):
script = "print(eval(unescape('%s')))" % quote(script)
p = subprocess.Popen(["js", "-e", script], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=-1)
out, err = p.communicate()
res = out.strip()
return res
def eval_rhino(self, script):
script = "print(eval(unescape('%s')))" % quote(script)
p = subprocess.Popen(["java", "-cp", path, "org.mozilla.javascript.tools.shell.Main", "-e", script],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=-1)
out, err = p.communicate()
res = out.strip()
return res.decode("utf8").encode("ISO-8859-1")
def error(self):
return _("No js engine detected, please install either Spidermonkey, ossp-js, pyv8 or rhino")
if __name__ == "__main__":
js = JsEngine()
test = u'"ü"+"ä"'
js.eval(test)