예제 #1
0
파일: narwhal.py 프로젝트: toolness/narwhal
 def read(self, filename, args=None):
     path = self._real_path(filename)
     if not path:
         raise pydermonkey.error("invalid filename: %s" % filename)
     try:
         contents = open(path).read()
     except IOError, e:
         raise pydermonkey.error(str(e))
예제 #2
0
            def wrapper(func_cx, this, args):
                try:
                    arglist = []
                    for arg in args:
                        arglist.append(self.wrap_jsobject(arg))
                    instance = func_cx.get_object_private(this)
                    if instance is None or not isinstance(instance, pyproto):
                        raise pydermonkey.error("Method type mismatch")

                    # TODO: Fill in extra required params with
                    # pymonkey.undefined?  or automatically throw an
                    # exception to calling js code?
                    return self.wrap_pyobject(func(instance, *arglist))
                except pydermonkey.error:
                    raise
                except Exception:
                    raise InternalError()
예제 #3
0
    def _require(self, path):
        """
        Implementation for the global require() function, implemented
        as per the CommonJS SecurableModule specification:

        http://wiki.commonjs.org/wiki/CommonJS/Modules/SecurableModules
        """

        filename = self.fs.find_module(self.get_calling_script(), path)
        if not filename:
            raise pydermonkey.error('Module not found: %s' % path)
        if not filename in self.__modules:
            cx = self.cx
            module = cx.new_object(None, self.__root_proto)
            try: 
              # This throws an exception because it is already done in 
              # the __init__ method:
              cx.init_standard_classes(module)
              # I have not removed the line above, because I don't know if there
              # are cases where it is necessary.  
            except pydermonkey.error:
              try:
                errmsg = sys.exc_info()[1][0]
              except: 
                errmsg = ""
              if (errmsg != "Can't init standard classes on the same context twice."):
                raise
              # Importing standard classes twice is silently ignored at this point
              # All other exceptions are re-raised. km 23.9.2009
            except:
              raise
            exports = cx.new_object()
            cx.define_property(module, 'exports', exports)
            self._install_globals(self.wrap_jsobject(module))
            self.__modules[filename] = self.wrap_jsobject(exports)
            contents = self.fs.open(filename).read()
            cx.evaluate_script(module, contents, filename, 1)
        return self.__modules[filename]