This commit is contained in:
Kovid Goyal 2015-08-03 17:53:46 +05:30
parent 2cd0c92224
commit 3c7aa1b49a

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
__all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python'] __all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python']
import errno, os, sys, numbers import errno, os, sys, numbers, hashlib
from functools import partial from functools import partial
from calibre.constants import plugins from calibre.constants import plugins
@ -23,22 +23,27 @@ fs = '''
exports.readFileSync = Duktape.readfile; exports.readFileSync = Duktape.readfile;
''' '''
vm = ''' vm = '''
exports.createContext = Duktape.create_context; function handle_result(result) {
exports.runInContext = function(code, ctx) {
var result = Duktape.run_in_context(code, ctx);
if (result[0]) return result[1]; if (result[0]) return result[1];
var cls = Error; var cls = Error;
var e = result[1]; var e = result[1];
if (e.name) { if (e.name) {
try { try {
cls = eval(e.name); cls = eval(e.name);
} catch(e) {} } catch(ex) {}
} }
var err = cls(e.message); var err = new cls(e.message);
err.fileName = e.fileName; err.fileName = e.fileName;
err.lineNumber = e.lineNumber; err.lineNumber = e.lineNumber;
err.stack = e.stack; err.stack = e.stack;
throw err; throw err;
}
exports.createContext = Duktape.create_context;
exports.runInContext = function(code, ctx) {
return handle_result(Duktape.run_in_context(code, ctx));
};
exports.runInThisContext = function(code, options) {
return handle_result(Duktape.run_in_this_context(code, options.filename));
}; };
''' '''
path = ''' path = '''
@ -48,6 +53,9 @@ util = '''
exports.inspect = function(x) { return x.toString(); }; exports.inspect = function(x) { return x.toString(); };
''' '''
def sha1sum(x):
return hashlib.sha1(x).hexdigest()
def load_file(base_dirs, builtin_modules, name): def load_file(base_dirs, builtin_modules, name):
ans = builtin_modules.get(name) ans = builtin_modules.get(name)
if ans is not None: if ans is not None:
@ -82,6 +90,7 @@ class Function(object):
def __init__(self, func): def __init__(self, func):
self.func = func self.func = func
self.name = func.name
def __repr__(self): def __repr__(self):
# For some reason x._Formals is undefined in duktape # For some reason x._Formals is undefined in duktape
@ -171,7 +180,9 @@ class Context(object):
self.g.Duktape.pyreadfile = readfile self.g.Duktape.pyreadfile = readfile
self.g.Duktape.create_context = partial(create_context, base_dirs) self.g.Duktape.create_context = partial(create_context, base_dirs)
self.g.Duktape.run_in_context = run_in_context self.g.Duktape.run_in_context = run_in_context
self.g.Duktape.run_in_this_context = self.run_in_this_context
self.g.Duktape.cwd = os.getcwdu self.g.Duktape.cwd = os.getcwdu
self.g.Duktape.sha1sum = sha1sum
self.eval(''' self.eval('''
console = { console = {
log: function() { print(Array.prototype.join.call(arguments, ' ')); }, log: function() { print(Array.prototype.join.call(arguments, ' ')); },
@ -247,6 +258,12 @@ class Context(object):
except dukpy.JSError as e: except dukpy.JSError as e:
raise JSError(e) raise JSError(e)
def run_in_this_context(self, code, fname='<eval>'):
try:
return [True, self._ctx.eval(code, False, fname or '<eval>')]
except dukpy.JSError as e:
return [False, JSError(e).as_dict()]
def eval_file(self, path, noreturn=False): def eval_file(self, path, noreturn=False):
try: try:
return self._ctx.eval_file(path, noreturn) return self._ctx.eval_file(path, noreturn)