laurent@371: function pyjs_extend(klass, base) { laurent@371: function klass_object_inherit() {} laurent@371: klass_object_inherit.prototype = base.prototype; laurent@371: klass_object = new klass_object_inherit(); laurent@371: for (var i in base.prototype.__class__) { laurent@371: v = base.prototype.__class__[i]; laurent@371: if (typeof v == "function" && (v.class_method || v.static_method || v.unbound_method)) laurent@371: { laurent@371: klass_object[i] = v; laurent@371: } laurent@371: } laurent@371: laurent@371: function klass_inherit() {} laurent@371: klass_inherit.prototype = klass_object; laurent@371: klass.prototype = new klass_inherit(); laurent@371: klass_object.constructor = klass; laurent@371: klass.prototype.__class__ = klass_object; laurent@371: laurent@371: for (var i in base.prototype) { laurent@371: v = base.prototype[i]; laurent@371: if (typeof v == "function" && v.instance_method) laurent@371: { laurent@371: klass.prototype[i] = v; laurent@371: } laurent@371: } laurent@371: } laurent@371: laurent@371: /* creates a class, derived from bases, with methods and variables */ laurent@371: function pyjs_type(clsname, bases, methods) laurent@371: { laurent@371: var fn_cls = function() {}; laurent@371: fn_cls.__name__ = clsname; laurent@371: var fn = function() { laurent@371: var instance = new fn_cls(); laurent@371: if(instance.__init__) instance.__init__.apply(instance, arguments); laurent@371: return instance; laurent@371: } laurent@371: fn_cls.__initialize__ = function() { laurent@371: if (fn_cls.__was_initialized__) return; laurent@371: fn_cls.__was_initialized__ = true; laurent@371: fn_cls.__extend_baseclasses(); laurent@371: fn_cls.prototype.__class__.__new__ = fn; laurent@371: fn_cls.prototype.__class__.__name__ = clsname; laurent@371: } laurent@371: fn_cls.__extend_baseclasses = function() { laurent@371: var bi; laurent@371: for (bi in fn_cls.__baseclasses) laurent@371: { laurent@371: var b = fn_cls.__baseclasses[bi]; laurent@371: if (b.__was_initialized__) laurent@371: { laurent@371: continue; laurent@371: } laurent@371: b.__initialize__(); laurent@371: } laurent@371: for (bi in fn_cls.__baseclasses) laurent@371: { laurent@371: var b = fn_cls.__baseclasses[bi]; laurent@371: pyjs_extend(fn_cls, b); laurent@371: } laurent@371: } laurent@371: if (!bases) { laurent@371: bases = [pyjslib.__Object]; laurent@371: } laurent@371: fn_cls.__baseclasses = bases; laurent@371: laurent@371: fn_cls.__initialize__(); laurent@371: laurent@371: for (k in methods) { laurent@371: var mth = methods[k]; laurent@371: var mtype = typeof mth; laurent@371: if (mtype == "function" ) { laurent@371: fn_cls.prototype[k] = mth; laurent@371: fn_cls.prototype.__class__[k] = function () { laurent@371: return fn_cls.prototype[k].call.apply( laurent@371: fn_cls.prototype[k], arguments); laurent@371: }; laurent@371: fn_cls.prototype.__class__[k].unbound_method = true; laurent@371: fn_cls.prototype.instance_method = true; laurent@371: fn_cls.prototype.__class__[k].__name__ = k; laurent@371: fn_cls.prototype[k].__name__ = k; laurent@371: } else { laurent@371: fn_cls.prototype.__class__[k] = mth; laurent@371: } laurent@371: } laurent@371: return fn; laurent@371: } laurent@371: function pyjs_kwargs_call(obj, func, star_args, args) laurent@371: { laurent@371: var call_args; laurent@371: laurent@371: if (star_args) laurent@371: { laurent@371: if (!pyjslib.isIteratable(star_args)) laurent@371: { laurent@371: throw (pyjslib.TypeError(func.__name__ + "() arguments after * must be a sequence" + pyjslib.repr(star_args))); laurent@371: } laurent@371: call_args = Array(); laurent@371: var __i = star_args.__iter__(); laurent@371: var i = 0; laurent@371: try { laurent@371: while (true) { laurent@371: call_args[i]=__i.next(); laurent@371: i++; laurent@371: } laurent@371: } catch (e) { laurent@371: if (e != pyjslib.StopIteration) { laurent@371: throw e; laurent@371: } laurent@371: } laurent@371: laurent@371: if (args) laurent@371: { laurent@371: var n = star_args.length; laurent@371: for (var i=0; i < args.length; i++) { laurent@371: call_args[n+i]=args[i]; laurent@371: } laurent@371: } laurent@371: } laurent@371: else laurent@371: { laurent@371: call_args = args; laurent@371: } laurent@371: return func.apply(obj, call_args); laurent@371: } laurent@371: laurent@371: function pyjs_kwargs_function_call(func, star_args, args) laurent@371: { laurent@371: return pyjs_kwargs_call(null, func, star_args, args); laurent@371: } laurent@371: laurent@371: function pyjs_kwargs_method_call(obj, method_name, star_args, args) laurent@371: { laurent@371: var method = obj[method_name]; laurent@371: if (method.parse_kwargs) laurent@371: { laurent@371: args = method.parse_kwargs.apply(null, args); laurent@371: } laurent@371: return pyjs_kwargs_call(obj, method, star_args, args); laurent@371: } laurent@371: laurent@371: //String.prototype.__getitem__ = String.prototype.charAt; laurent@371: //String.prototype.upper = String.prototype.toUpperCase; laurent@371: //String.prototype.lower = String.prototype.toLowerCase; laurent@371: //String.prototype.find=pyjslib.String_find; laurent@371: //String.prototype.join=pyjslib.String_join; laurent@371: //String.prototype.isdigit=pyjslib.String_isdigit; laurent@371: //String.prototype.__iter__=pyjslib.String___iter__; laurent@371: // laurent@371: //String.prototype.__replace=String.prototype.replace; laurent@371: //String.prototype.replace=pyjslib.String_replace; laurent@371: // laurent@371: //String.prototype.split=pyjslib.String_split; laurent@371: //String.prototype.strip=pyjslib.String_strip; laurent@371: //String.prototype.lstrip=pyjslib.String_lstrip; laurent@371: //String.prototype.rstrip=pyjslib.String_rstrip; laurent@371: //String.prototype.startswith=pyjslib.String_startswith; laurent@371: laurent@371: var str = String; laurent@371: