comparison pydershell/pydershell.py @ 28:f974d4a80473 default tip

Some fixes.
author Atul Varma <varmaa@toolness.com>
date Tue, 08 Sep 2009 15:27:02 -0700
parents 7f12db104246
children
comparison
equal deleted inserted replaced
27:7f12db104246 28:f974d4a80473
394 "to untrusted JS code") 394 "to untrusted JS code")
395 return self.__wrap_pycallable(value) 395 return self.__wrap_pycallable(value)
396 elif isinstance(value, JsExposedObject): 396 elif isinstance(value, JsExposedObject):
397 return self.__wrap_pyinstance(value) 397 return self.__wrap_pyinstance(value)
398 else: 398 else:
399 raise TypeError("Can't expose objects of type '' to JS." % 399 raise TypeError("Can't expose objects of type '%s' to JS." %
400 type(value).__name__) 400 type(value).__name__)
401 401
402 def wrap_jsobject(self, jsvalue, this=None): 402 def wrap_jsobject(self, jsvalue, this=None):
403 """ 403 """
404 Wraps the given pydermonkey.Object for import to trusted 404 Wraps the given pydermonkey.Object for import to trusted
428 return jsvalue 428 return jsvalue
429 429
430 def new_array(self, *contents): 430 def new_array(self, *contents):
431 array = self.wrap_jsobject(self.cx.new_array_object()) 431 array = self.wrap_jsobject(self.cx.new_array_object())
432 for item in contents: 432 for item in contents:
433 array.push(self.wrap_pyobject(item)) 433 array.push(item)
434 return array 434 return array
435 435
436 def new_object(self, **contents): 436 def new_object(self, **contents):
437 obj = self.wrap_jsobject(self.cx.new_object()) 437 obj = self.wrap_jsobject(self.cx.new_object())
438 for name in contents: 438 for name in contents:
439 obj[name] = self.wrap_pyobject(contents[name]) 439 obj[name] = contents[name]
440 return obj
440 441
441 def evaluate(self, code, filename='<string>', lineno=1): 442 def evaluate(self, code, filename='<string>', lineno=1):
442 retval = self.cx.evaluate_script(self.root.wrapped_jsobject, 443 retval = self.cx.evaluate_script(self.root.wrapped_jsobject,
443 code, filename, lineno) 444 code, filename, lineno)
444 return self.wrap_jsobject(retval) 445 return self.wrap_jsobject(retval)
445 446
446 def run_script(self, filename): 447 def run_script(self, filename, callback=None):
447 """ 448 """
448 Runs the given JS script, returning 0 on success, -1 on failure. 449 Runs the given JS script, returning 0 on success, -1 on failure.
449 """ 450 """
450 451
451 retval = -1 452 retval = -1
452 contents = open(filename).read() 453 contents = open(filename).read()
453 cx = self.cx 454 cx = self.cx
454 try: 455 try:
455 cx.evaluate_script(self.root.wrapped_jsobject, contents, 456 result = cx.evaluate_script(self.root.wrapped_jsobject,
456 filename, 1) 457 contents, filename, 1)
458 if callback:
459 callback(self.wrap_jsobject(result))
457 retval = 0 460 retval = 0
458 except pydermonkey.error, e: 461 except pydermonkey.error, e:
459 print format_stack(self.js_stack) 462 print format_stack(self.js_stack)
460 print e.args[1] 463 print e.args[1]
461 except InternalError, e: 464 except InternalError, e: