svgui/pyjs/lib/_pyjs.js
changeset 728 e0424e96e3fd
parent 721 ecf4d203c4d4
equal deleted inserted replaced
727:3edd2f19bce2 728:e0424e96e3fd
       
     1 function pyjs_extend(klass, base) {
       
     2     function klass_object_inherit() {}
       
     3     klass_object_inherit.prototype = base.prototype;
       
     4     klass_object = new klass_object_inherit();
       
     5     for (var i in base.prototype.__class__) {
       
     6         v = base.prototype.__class__[i];
       
     7         if (typeof v == "function" && (v.class_method || v.static_method || v.unbound_method))
       
     8         {
       
     9             klass_object[i] = v;
       
    10         }
       
    11     }
       
    12 
       
    13     function klass_inherit() {}
       
    14     klass_inherit.prototype = klass_object;
       
    15     klass.prototype = new klass_inherit();
       
    16     klass_object.constructor = klass;
       
    17     klass.prototype.__class__ = klass_object;
       
    18 
       
    19     for (var i in base.prototype) {
       
    20         v = base.prototype[i];
       
    21         if (typeof v == "function" && v.instance_method)
       
    22         {
       
    23             klass.prototype[i] = v;
       
    24         }
       
    25     }
       
    26 }
       
    27 
       
    28 /* creates a class, derived from bases, with methods and variables */
       
    29 function pyjs_type(clsname, bases, methods)
       
    30 {
       
    31     var fn_cls = function() {};
       
    32     fn_cls.__name__ = clsname;
       
    33     var fn = function() {
       
    34         var instance = new fn_cls();
       
    35         if(instance.__init__) instance.__init__.apply(instance, arguments);
       
    36         return instance;
       
    37     }
       
    38     fn_cls.__initialize__ = function() {
       
    39         if (fn_cls.__was_initialized__) return;
       
    40         fn_cls.__was_initialized__ = true;
       
    41         fn_cls.__extend_baseclasses();
       
    42         fn_cls.prototype.__class__.__new__ = fn;
       
    43         fn_cls.prototype.__class__.__name__ = clsname;
       
    44     }
       
    45     fn_cls.__extend_baseclasses = function() {
       
    46         var bi;
       
    47         for (bi in fn_cls.__baseclasses)
       
    48         {
       
    49             var b = fn_cls.__baseclasses[bi];
       
    50             if (b.__was_initialized__)
       
    51             {
       
    52                 continue;
       
    53             }
       
    54             b.__initialize__();
       
    55         }
       
    56         for (bi in fn_cls.__baseclasses)
       
    57         {
       
    58             var b = fn_cls.__baseclasses[bi];
       
    59             pyjs_extend(fn_cls, b);
       
    60         }
       
    61     }
       
    62     if (!bases) {
       
    63         bases = [pyjslib.__Object];
       
    64     }
       
    65     fn_cls.__baseclasses = bases;
       
    66 
       
    67     fn_cls.__initialize__();
       
    68 
       
    69     for (k in methods) {
       
    70         var mth = methods[k];
       
    71         var mtype = typeof mth;
       
    72         if (mtype == "function" ) {
       
    73             fn_cls.prototype[k] = mth;
       
    74             fn_cls.prototype.__class__[k] = function () {
       
    75                 return fn_cls.prototype[k].call.apply(
       
    76                        fn_cls.prototype[k], arguments);
       
    77             };
       
    78             fn_cls.prototype.__class__[k].unbound_method = true;
       
    79             fn_cls.prototype.instance_method = true;
       
    80             fn_cls.prototype.__class__[k].__name__ = k;
       
    81             fn_cls.prototype[k].__name__ = k;
       
    82         } else {
       
    83             fn_cls.prototype.__class__[k] = mth;
       
    84         }
       
    85     }
       
    86     return fn;
       
    87 }
       
    88 function pyjs_kwargs_call(obj, func, star_args, args)
       
    89 {
       
    90     var call_args;
       
    91 
       
    92     if (star_args)
       
    93     {
       
    94         if (!pyjslib.isIteratable(star_args))
       
    95         {
       
    96             throw (pyjslib.TypeError(func.__name__ + "() arguments after * must be a sequence" + pyjslib.repr(star_args)));
       
    97         }
       
    98         call_args = Array();
       
    99         var __i = star_args.__iter__();
       
   100         var i = 0;
       
   101         try {
       
   102             while (true) {
       
   103                 call_args[i]=__i.next();
       
   104                 i++;
       
   105             }
       
   106         } catch (e) {
       
   107             if (e != pyjslib.StopIteration) {
       
   108                 throw e;
       
   109             }
       
   110         }
       
   111 
       
   112         if (args)
       
   113         {
       
   114             var n = star_args.length;
       
   115             for (var i=0; i < args.length; i++) {
       
   116                 call_args[n+i]=args[i];
       
   117             }
       
   118         }
       
   119     }
       
   120     else
       
   121     {
       
   122         call_args = args;
       
   123     }
       
   124     return func.apply(obj, call_args);
       
   125 }
       
   126 
       
   127 function pyjs_kwargs_function_call(func, star_args, args)
       
   128 {
       
   129     return pyjs_kwargs_call(null, func, star_args, args);
       
   130 }
       
   131 
       
   132 function pyjs_kwargs_method_call(obj, method_name, star_args, args)
       
   133 {
       
   134     var method = obj[method_name];
       
   135     if (method.parse_kwargs)
       
   136     {
       
   137         args = method.parse_kwargs.apply(null, args);
       
   138     }
       
   139     return pyjs_kwargs_call(obj, method, star_args, args);
       
   140 }
       
   141 
       
   142 //String.prototype.__getitem__ = String.prototype.charAt;
       
   143 //String.prototype.upper = String.prototype.toUpperCase;
       
   144 //String.prototype.lower = String.prototype.toLowerCase;
       
   145 //String.prototype.find=pyjslib.String_find;
       
   146 //String.prototype.join=pyjslib.String_join;
       
   147 //String.prototype.isdigit=pyjslib.String_isdigit;
       
   148 //String.prototype.__iter__=pyjslib.String___iter__;
       
   149 //
       
   150 //String.prototype.__replace=String.prototype.replace;
       
   151 //String.prototype.replace=pyjslib.String_replace;
       
   152 //
       
   153 //String.prototype.split=pyjslib.String_split;
       
   154 //String.prototype.strip=pyjslib.String_strip;
       
   155 //String.prototype.lstrip=pyjslib.String_lstrip;
       
   156 //String.prototype.rstrip=pyjslib.String_rstrip;
       
   157 //String.prototype.startswith=pyjslib.String_startswith;
       
   158 
       
   159 var str = String;
       
   160