comparison pydershell/pydershell.py @ 23:35ab0ad3c294

Added more docs.
author Atul Varma <varmaa@toolness.com>
date Mon, 07 Sep 2009 16:25:58 -0700
parents 9413bebf2ee6
children dace90a7f5e3
comparison
equal deleted inserted replaced
22:9413bebf2ee6 23:35ab0ad3c294
11 """ 11 """
12 Watches active JS contexts and triggers their operation callbacks 12 Watches active JS contexts and triggers their operation callbacks
13 at a regular interval. 13 at a regular interval.
14 """ 14 """
15 15
16 # Default interval, in seconds, that the operation callbacks are
17 # triggered at.
16 DEFAULT_INTERVAL = 0.25 18 DEFAULT_INTERVAL = 0.25
17 19
18 def __init__(self, interval=DEFAULT_INTERVAL): 20 def __init__(self, interval=DEFAULT_INTERVAL):
19 threading.Thread.__init__(self) 21 threading.Thread.__init__(self)
20 self._lock = threading.Lock() 22 self._lock = threading.Lock()
224 self.__py_to_js = {} 226 self.__py_to_js = {}
225 self.__type_protos = {} 227 self.__type_protos = {}
226 self.root = self.wrap_jsobject(root, root) 228 self.root = self.wrap_jsobject(root, root)
227 229
228 def finish(self): 230 def finish(self):
231 """
232 Cleans up all resources used by the sandbox, breaking any reference
233 cycles created due to issue #2 in pydermonkey:
234
235 http://code.google.com/p/pydermonkey/issues/detail?id=2
236 """
237
229 for jsobj in self.__py_to_js.values(): 238 for jsobj in self.__py_to_js.values():
230 self.cx.clear_object_private(jsobj) 239 self.cx.clear_object_private(jsobj)
231 del self.__py_to_js 240 del self.__py_to_js
232 del self.__type_protos 241 del self.__type_protos
233 del self.curr_exc 242 del self.curr_exc
311 self.cx.define_property(jsproto, name, jsmethod) 320 self.cx.define_property(jsproto, name, jsmethod)
312 self.__type_protos[pyproto] = jsproto 321 self.__type_protos[pyproto] = jsproto
313 return self.cx.new_object(value, self.__type_protos[pyproto]) 322 return self.cx.new_object(value, self.__type_protos[pyproto])
314 323
315 def wrap_pyobject(self, value): 324 def wrap_pyobject(self, value):
325 """
326 Wraps the given Python object for export to untrusted JS.
327
328 If the Python object isn't of a type that can be exposed to JS,
329 a TypeError is raised.
330 """
331
316 if (isinstance(value, (int, basestring, float, bool)) or 332 if (isinstance(value, (int, basestring, float, bool)) or
317 value is pydermonkey.undefined or 333 value is pydermonkey.undefined or
318 value is None): 334 value is None):
319 return value 335 return value
320 if isinstance(value, SafeJsObjectWrapper): 336 if isinstance(value, SafeJsObjectWrapper):
327 else: 343 else:
328 raise TypeError("Can't expose objects of type '' to JS." % 344 raise TypeError("Can't expose objects of type '' to JS." %
329 type(value).__name__) 345 type(value).__name__)
330 346
331 def wrap_jsobject(self, jsvalue, this=None): 347 def wrap_jsobject(self, jsvalue, this=None):
348 """
349 Wraps the given pydermonkey.Object for import to trusted
350 Python code. If the type is just a primitive, it's simply
351 returned, since no wrapping is needed.
352 """
353
332 if this is None: 354 if this is None:
333 this = self.root.wrapped_jsobject 355 this = self.root.wrapped_jsobject
334 if isinstance(jsvalue, pydermonkey.Function): 356 if isinstance(jsvalue, pydermonkey.Function):
335 if jsvalue.is_python: 357 if jsvalue.is_python:
336 # It's a Python function, just unwrap it. 358 # It's a Python function, just unwrap it.
349 else: 371 else:
350 # It's a primitive value. 372 # It's a primitive value.
351 return jsvalue 373 return jsvalue
352 374
353 def run_script(self, filename): 375 def run_script(self, filename):
376 """
377 Runs the given JS script, returning 0 on success, -1 on failure.
378 """
379
354 retval = -1 380 retval = -1
355 contents = open(filename).read() 381 contents = open(filename).read()
356 cx = self.cx 382 cx = self.cx
357 try: 383 try:
358 cx.evaluate_script(self.root.wrapped_jsobject, contents, 384 cx.evaluate_script(self.root.wrapped_jsobject, contents,