jdk/src/share/classes/com/sun/tools/hat/resources/hat.js
author ohair
Wed, 30 Apr 2008 17:34:41 -0700
changeset 468 642c8c0be52e
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6695553: Cleanup GPLv2+SPL legal notices in hat sources Summary: Just correcting the legal notices on the HAT sources. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    29
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
var hatPkg = Packages.com.sun.tools.hat.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This is JavaScript interface for heap analysis using HAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (Heap Analysis Tool). HAT classes are refered from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * this file. In particular, refer to classes in hat.model 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * HAT model objects are wrapped as convenient script objects so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * fields may be accessed in natural syntax. For eg. Java fields can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * accessed with obj.field_name syntax and array elements can be accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with array[index] syntax. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// returns an enumeration that wraps elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
// the input enumeration elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
function wrapperEnumeration(e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    return new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        hasMoreElements: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            return e.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return wrapJavaValue(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// returns an enumeration that filters out elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
// of input enumeration using the filter function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
function filterEnumeration(e, func, wrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    var next = undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    var index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    function findNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        var tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            tmp = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (wrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                tmp = wrapJavaValue(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (func(tmp, index, e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                next = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    return new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        hasMoreElements: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            findNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return next != undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (next == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // user may not have called hasMoreElements?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                findNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (next == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                throw "NoSuchElementException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            var res = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            next = undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
// enumeration that has no elements ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
var emptyEnumeration = new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        hasMoreElements: function() { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw "NoSuchElementException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
function wrapRoot(root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            id: root.idString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            description: root.description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            referrer: wrapJavaValue(root.referer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            type: root.typeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
function JavaClassProto() {    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    function jclass(obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return obj['wrapped-object'];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // return whether given class is subclass of this class or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    this.isSubclassOf = function(other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        var tmp = jclass(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        var otherid = objectid(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        while (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (otherid.equals(tmp.idString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            tmp = tmp.superclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // return whether given class is superclass of this class or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    this.isSuperclassOf = function(other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return other.isSubclassOf(this); 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // includes direct and indirect superclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    this.superclasses = function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        var tmp = this.superclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        while (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            res[res.length] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            tmp = tmp.superclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns an array containing subclasses of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param indirect should include indirect subclasses or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                 default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    this.subclasses = function(indirect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (indirect == undefined) indirect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        var classes = jclass(this).subclasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (var i in classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            var subclass = wrapJavaValue(classes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            res[res.length] = subclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (indirect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                res = res.concat(subclass.subclasses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    this.toString = function() { return jclass(this).toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
var theJavaClassProto = new JavaClassProto();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
// Script wrapper for HAT model objects, values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
// wraps a Java value as appropriate for script object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
function wrapJavaValue(thing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    if (thing == null || thing == undefined ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        thing instanceof hatPkg.model.HackJavaValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
	return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (thing instanceof hatPkg.model.JavaValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // map primitive values to closest JavaScript primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (thing instanceof hatPkg.model.JavaBoolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return thing.toString() == "true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else if (thing instanceof hatPkg.model.JavaChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return thing.toString() + '';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return java.lang.Double.parseDouble(thing.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }			
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // wrap Java object as script object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return wrapJavaObject(thing);		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
// wrap Java object with appropriate script object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
function wrapJavaObject(thing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // HAT Java model object wrapper. Handles all cases 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // (instance, object/primitive array and Class objects)	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    function javaObject(jobject) {		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // FIXME: Do I need this? or can I assume that these would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // have been resolved already?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (jobject instanceof hatPkg.model.JavaObjectRef) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            jobject = jobject.dereference();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (jobject instanceof hatPkg.model.HackJavaValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                print(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (jobject instanceof hatPkg.model.JavaObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return new JavaObjectWrapper(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else if (jobject instanceof hatPkg.model.JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return new JavaClassWrapper(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else if (jobject instanceof hatPkg.model.JavaObjectArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return new JavaObjectArrayWrapper(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (jobject instanceof hatPkg.model.JavaValueArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return new JavaValueArrayWrapper(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            print("unknown heap object type: " + jobject.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return jobject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // returns wrapper for Java instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    function JavaObjectWrapper(instance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        var things = instance.fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        var fields = instance.clazz.fieldsForInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // instance fields can be accessed in natural syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return new JSAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            __getIds__ : function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    var res = new Array(fields.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        res[i] = fields[i].name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            __has__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        if (name == fields[i].name) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return name == 'class' || name == 'toString' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                           name == 'wrapped-object';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            __get__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        if(fields[i].name == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            return wrapJavaValue(things[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    if (name == 'class') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        return wrapJavaValue(instance.clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    } else if (name == 'toString') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        return function() { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            return instance.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    } else if (name == 'wrapped-object') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }				
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // return wrapper for Java Class objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    function JavaClassWrapper(jclass) {	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        var fields = jclass.statics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // to access static fields of given Class cl, use 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // cl.statics.<static-field-name> syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        this.statics = new JSAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            __getIds__ : function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                var res = new Array(fields.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    res[i] = fields[i].field.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            __has__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    if (name == fields[i].field.name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }					
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return theJavaClassProto[name] != undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            __get__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                for (var i in fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (name == fields[i].field.name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        return wrapJavaValue(fields[i].value);	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    }					
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                return theJavaClassProto[name];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (jclass.superclass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            this.superclass = wrapJavaValue(jclass.superclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            this.superclass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        this.loader = wrapJavaValue(jclass.getLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        this.signers = wrapJavaValue(jclass.getSigners());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this.protectionDomain = wrapJavaValue(jclass.getProtectionDomain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this.instanceSize = jclass.instanceSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        this.name = jclass.name; 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        this.fields = jclass.fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        this['wrapped-object'] = jclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        this.__proto__ = this.statics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // returns wrapper for Java object arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    function JavaObjectArrayWrapper(array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        var elements = array.elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // array elements can be accessed in natural syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // also, 'length' property is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return new JSAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            __getIds__ : function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                var res = new Array(elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                for (var i = 0; i < elements.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    res[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            __has__: function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                return (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        name >= 0 && name < elements.length)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        name == 'length' || name == 'class' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        name == 'toString' || name == 'wrapped-object';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            __get__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    name >= 0 && name < elements.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    return wrapJavaValue(elements[name]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } else if (name == 'length') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    return elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } else if (name == 'class') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    return wrapJavaValue(array.clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                } else if (name == 'toString') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    return function() { return array.toString(); }          
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                } else if (name == 'wrapped-object') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }				
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }			
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // returns wrapper for Java primitive arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    function JavaValueArrayWrapper(array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        var type = String(java.lang.Character.toString(array.elementType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        var elements = array.elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // array elements can be accessed in natural syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // also, 'length' property is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return new JSAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            __getIds__ : function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                var r = new Array(array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                for (var i = 0; i < array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    r[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            __has__: function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                return (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        name >= 0 && name < array.length) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        name == 'length' || name == 'class' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        name == 'toString' || name == 'wrapped-object';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            __get__: function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    name >= 0 && name < array.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    return elements[name];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (name == 'length') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    return array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                } else if (name == 'toString') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    return function() { return array.valueString(true); } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                } else if (name == 'wrapped-object') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                } else if (name == 'class') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    return wrapJavaValue(array.clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    return javaObject(thing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
// unwrap a script object to corresponding HAT object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
function unwrapJavaObject(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if (!(jobject instanceof hatPkg.model.JavaHeapObject)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            jobject = jobject["wrapped-object"];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            print("unwrapJavaObject: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            jobject = undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    return jobject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 * readHeapDump parses a heap dump file and returns script wrapper object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 * @param file  Heap dump file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * @param stack flag to tell if allocation site traces are available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 * @param refs  flag to tell if backward references are needed or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * @param debug debug level for HAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * @return heap as a JavaScript object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
function readHeapDump(file, stack, refs, debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // default value of debug is 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (!debug) debug = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    // by default, we assume no stack traces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    if (!stack) stack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    // by default, backward references are resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (!refs) refs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    // read the heap dump 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    var heap = hatPkg.parser.HprofReader.readFile(file, stack, debug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    // resolve it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    heap.resolve(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    // wrap Snapshot as convenient script object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    return wrapHeapSnapshot(heap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * The result object supports the following methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 *  forEachClass  -- calls a callback for each Java Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 *  forEachObject -- calls a callback for each Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 *  findClass -- finds Java Class of given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 *  findObject -- finds object from given object id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 *  objects -- returns all objects of given class as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 *  classes -- returns all classes in the heap as an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 *  reachables -- returns all objects reachable from a given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *  livepaths -- returns an array of live paths because of which an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 *               object alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 *  describeRef -- returns description for a reference from a 'from' 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 *              object to a 'to' object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
function wrapHeapSnapshot(heap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    function getClazz(clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (clazz == undefined) clazz = "java.lang.Object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        var type = typeof(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (type == "string") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            clazz = heap.findClass(clazz);		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } else if (type == "object") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            clazz = unwrapJavaObject(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw "class expected";;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // return heap as a script object with useful methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    return {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        snapshot: heap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * Class iteration: Calls callback function for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * Java Class in the heap. Default callback function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * is 'print'. If callback returns true, the iteration 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * @param callback function to be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        forEachClass: function(callback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            if (callback == undefined) callback = print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            var classes = this.snapshot.classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            while (classes.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (callback(wrapJavaValue(classes.nextElement())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * Returns an Enumeration of all roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        roots: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            var e = this.snapshot.roots;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                hasMoreElements: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    return e.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    return wrapRoot(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * Returns an Enumeration for all Java classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        classes: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return wrapIterator(this.snapshot.classes, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * Object iteration: Calls callback function for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * Java Object in the heap. Default callback function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * is 'print'.If callback returns true, the iteration 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         * is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * @param callback function to be called. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * @param clazz Class whose objects are retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         *        Optional, default is 'java.lang.Object'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * @param includeSubtypes flag to tell if objects of subtypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         *        are included or not. optional, default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        forEachObject: function(callback, clazz, includeSubtypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (includeSubtypes == undefined) includeSubtypes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (callback == undefined) callback = print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            clazz = getClazz(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                var instances = clazz.getInstances(includeSubtypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                while (instances.hasNextElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    if (callback(wrapJavaValue(instances.nextElement())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        /** 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * Returns an enumeration of Java objects in the heap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * @param clazz Class whose objects are retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         *        Optional, default is 'java.lang.Object'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * @param includeSubtypes flag to tell if objects of subtypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         *        are included or not. optional, default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         * @param where (optional) filter expression or function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         *        filter the objects. The expression has to return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         *        to include object passed to it in the result array. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         *        Built-in variable 'it' refers to the current object in 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         *        filter expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        objects: function(clazz, includeSubtypes, where) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (includeSubtypes == undefined) includeSubtypes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (where) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                if (typeof(where) == 'string') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    where = new Function("it", "return " + where);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            clazz = getClazz(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                var instances = clazz.getInstances(includeSubtypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if (where) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    return filterEnumeration(instances, where, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    return wrapperEnumeration(instances);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                return emptyEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * Find Java Class of given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * @param name class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        findClass: function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            var clazz = this.snapshot.findClass(name + '');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return wrapJavaValue(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * Find Java Object from given object id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         * @param id object id as string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        findObject: function(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return wrapJavaValue(this.snapshot.findThing(id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * Returns an enumeration of objects in the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * queue waiting to be finalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        finalizables: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            var tmp = this.snapshot.getFinalizerObjects();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return wrapperEnumeration(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         * Returns an array that contains objects referred from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * given Java object directly or indirectly (i.e., all 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * transitively referred objects are returned).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * @param jobject Java object whose reachables are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        reachables: function (jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return reachables(jobject, this.snapshot.reachableExcludes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         * Returns array of paths of references by which the given 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * Java object is live. Each path itself is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * objects in the chain of references. Each path supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * toHtml method that returns html description of the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * @param jobject Java object whose live paths are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * @param weak flag to indicate whether to include paths with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         *             weak references or not. default is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        livepaths: function (jobject, weak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (weak == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                weak = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            function wrapRefChain(refChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                var path = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                // compute path array from refChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                var tmp = refChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                while (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    var obj = tmp.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    path[path.length] = wrapJavaValue(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    tmp = tmp.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                function computeDescription(html) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    var root = refChain.obj.root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    var desc = root.description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    if (root.referer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        var ref = root.referer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        desc += " (from " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            (html? toHtml(ref) : ref.toString()) + ')';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    desc += '->';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    var tmp = refChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    while (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        var next = tmp.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        var obj = tmp.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        desc += html? toHtml(obj) : obj.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            desc += " (" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                    obj.describeReferenceTo(next.obj, heap)  + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                                    ") ->";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        tmp = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                return new JSAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    __getIds__ : function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        var res = new Array(path.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        for (var i = 0; i < path.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                            res[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    __has__ : function (name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        return (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                            name >= 0 && name < path.length) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                            name == 'length' || name == 'toHtml' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                            name == 'toString';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    __get__ : function(name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        if (typeof(name) == 'number' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            name >= 0 && name < path.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            return path[name];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        } else if (name == 'length') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                            return path.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        } else if (name == 'toHtml') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            return function() { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                               return computeDescription(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        } else if (name == 'toString') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            return function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                               return computeDescription(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            var refChains = this.snapshot.rootsetReferencesTo(jobject, weak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            var paths = new Array(refChains.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            for (var i in refChains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                paths[i] = wrapRefChain(refChains[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * Return description string for reference from 'from' object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         * to 'to' Java object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * @param from source Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         * @param to destination Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        describeRef: function (from, to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            from = unwrapJavaObject(from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            to = unwrapJavaObject(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return from.describeReferenceTo(to, this.snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
// per-object functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
 * Returns allocation site trace (if available) of a Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
 * @param jobject object whose allocation site trace is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
function allocTrace(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        jobject = unwrapJavaObject(jobject);			
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        var trace = jobject.allocatedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return (trace != null) ? trace.frames : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        print("allocTrace: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 * Returns Class object for given Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 * @param jobject object whose Class object is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
function classof(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    return wrapJavaValue(jobject.clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 * Find referers (a.k.a in-coming references). Calls callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 * for each referrer of the given Java object. If the callback 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
 * returns true, the iteration is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
 * @param callback function to call for each referer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
 * @param jobject object whose referers are retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
function forEachReferrer(callback, jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    var refs = jobject.referers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    while (refs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (callback(wrapJavaValue(refs.nextElement()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
 * Compares two Java objects for object identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
 * @param o1, o2 objects to compare for identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
function identical(o1, o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    return objectid(o1) == objectid(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
 * Returns Java object id as string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
 * @param jobject object whose id is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
function objectid(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        return String(jobject.idString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        print("objectid: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
 * Prints allocation site trace of given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
 * @param jobject object whose allocation site trace is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
function printAllocTrace(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    var frames = this.allocTrace(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    if (frames == null || frames.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        print("allocation site trace unavailable for " + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
              objectid(jobject));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    print(objectid(jobject) + " was allocated at ..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    for (var i in frames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        var frame = frames[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        var src = frame.sourceFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (src == null) src = '<unknown source>';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        print('\t' + frame.className + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
             frame.methodName + '(' + frame.methodSignature + ') [' +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
             src + ':' + frame.lineNumber + ']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
 * Returns an enumeration of referrers of the given Java object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
 * @param jobject Java object whose referrers are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
function referrers(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return wrapperEnumeration(jobject.referers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        print("referrers: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        return emptyEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
 * Returns an array that contains objects referred from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
 * given Java object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
 * @param jobject Java object whose referees are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
function referees(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    if (jobject != undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            jobject.visitReferencedObjects(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                new hatPkg.model.JavaHeapObjectVisitor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    visit: function(other) { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        res[res.length] = wrapJavaValue(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    exclude: function(clazz, field) { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        return false; 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    mightExclude: function() { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                        return false; 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            print("referees: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
 * Returns an array that contains objects referred from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
 * given Java object directly or indirectly (i.e., all 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
 * transitively referred objects are returned).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * @param jobject Java object whose reachables are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 * @param excludes optional comma separated list of fields to be 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
 *                 removed in reachables computation. Fields are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 *                 written as class_name.field_name form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
function reachables(jobject, excludes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    if (excludes == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        excludes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    } else if (typeof(excludes) == 'string') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        var st = new java.util.StringTokenizer(excludes, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        var excludedFields = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            excludedFields[excludedFields.length] = st.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (excludedFields.length > 0) { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            excludes = new hatPkg.model.ReachableExcludes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        isExcluded: function (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                            for (var index in excludedFields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                if (field.equals(excludedFields[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            // nothing to filter...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            excludes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    } else if (! (excludes instanceof hatPkg.model.ReachableExcludes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        excludes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    var ro = new hatPkg.model.ReachableObjects(jobject, excludes);  
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    var tmp = ro.reachables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    var res = new Array(tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    for (var i in tmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        res[i] = wrapJavaValue(tmp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 * Returns whether 'from' object refers to 'to' object or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
 * @param from Java object that is source of the reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
 * @param to Java object that is destination of the reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
function refers(from, to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        var tmp = unwrapJavaObject(from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (tmp instanceof hatPkg.model.JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            from = from.statics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        } else if (tmp instanceof hatPkg.model.JavaValueArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        for (var i in from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (identical(from[i], to)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        print("refers: " + from + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 * If rootset includes given jobject, return Root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * object explanining the reason why it is a root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
 * @param jobject object whose Root is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
function root(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return wrapRoot(jobject.root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
 * Returns size of the given Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
 * @param jobject object whose size is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
function sizeof(jobject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        jobject = unwrapJavaObject(jobject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        return jobject.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    } catch (e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        print("sizeof: " + jobject + ", " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
 * Returns String by replacing Unicode chars and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
 * HTML special chars (such as '<') with entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
 * @param str string to be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
function encodeHtml(str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    return hatPkg.util.Misc.encodeHtml(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
 * Returns HTML string for the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
 * @param obj object for which HTML string is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
function toHtml(obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    if (obj == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return "undefined";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    var tmp = unwrapJavaObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    if (tmp != undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        var id = tmp.idString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (tmp instanceof Packages.com.sun.tools.hat.internal.model.JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            var name = tmp.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return "<a href='/class/" + id + "'>class " + name + "</a>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            var name = tmp.clazz.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            return "<a href='/object/" + id + "'>" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                   name + "@" + id + "</a>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    } else if ((typeof(obj) == 'object') || (obj instanceof JSAdapter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        if (obj instanceof java.lang.Object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            // script wrapped Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            obj = wrapIterator(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            // special case for enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            if (obj instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                var res = "[ ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                while (obj.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    res += toHtml(obj.nextElement()) + ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                res += "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                return res; 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        } else if (obj instanceof Array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            // script array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            var res = "[ ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            for (var i in obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                res += toHtml(obj[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                if (i != obj.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    res += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            res += " ]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            // if the object has a toHtml function property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            // just use that...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (typeof(obj.toHtml) == 'function') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                return obj.toHtml();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                // script object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                var res = "{ ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                for (var i in obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    res +=  i + ":" + toHtml(obj[i]) + ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                res += "}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        // JavaScript primitive value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
 * Generic array/iterator/enumeration [or even object!] manipulation 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
 * functions. These functions accept an array/iteration/enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
 * and expression String or function. These functions iterate each 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
 * element of array and apply the expression/function on each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
// private function to wrap an Iterator as an Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
function wrapIterator(itr, wrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    if (itr instanceof java.util.Iterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                   hasMoreElements: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                       return itr.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                   },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                   nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                       return wrap? wrapJavaValue(itr.next()) : itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
               };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return itr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
 * Converts an enumeration/iterator/object into an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
 * @param obj enumeration/iterator/object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
 * @return array that contains values of enumeration/iterator/object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
function toArray(obj) {	
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    obj = wrapIterator(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    if (obj instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        while (obj.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            res[res.length] = obj.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    } else if (obj instanceof Array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        for (var index in obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            res[res.length] = obj[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
 * Returns whether the given array/iterator/enumeration contains 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
 * an element that satisfies the given boolean expression specified 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
 * in code. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
 * @param code  expression string or function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
 * @return boolean result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
 * The code evaluated can refer to the following built-in variables. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
 * 'it' -> currently visited element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
 * 'index' -> index of the current element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
 * 'array' -> array that is being iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
function contains(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    var func = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    if (typeof(func) != 'function') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        func = new Function("it", "index", "array",  "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        var index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            var it = array.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            if (func(it, index, array)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            var it = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            if (func(it, index, array)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
 * concatenates two arrays/iterators/enumerators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
 * @param array1 array/iterator/enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
 * @param array2 array/iterator/enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
 * @return concatenated array or composite enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
function concat(array1, array2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    array1 = wrapIterator(array1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    array2 = wrapIterator(array2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    if (array1 instanceof Array && array2 instanceof Array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        return array1.concat(array2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    } else if (array1 instanceof java.util.Enumeration &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
               array2 instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        return new Packages.com.sun.tools.hat.internal.util.CompositeEnumeration(array1, array2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
 * Returns the number of array/iterator/enumeration elements 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
 * that satisfy the given boolean expression specified in code. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
 * The code evaluated can refer to the following built-in variables. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
 * @param code  expression string or function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
 * @return number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
 * 'it' -> currently visited element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
 * 'index' -> index of the current element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
 * 'array' -> array that is being iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
function count(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    if (code == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        return length(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    var func = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    if (typeof(func) != 'function') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        func = new Function("it", "index", "array",  "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    var result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        var index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            var it = array.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            if (func(it, index, array)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            var it = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            if (func(it, index, array)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
 * filter function returns an array/enumeration that contains 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
 * elements of the input array/iterator/enumeration that satisfy 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
 * the given boolean expression. The boolean expression code can 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
 * refer to the following built-in variables. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
 * @param code  expression string or function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
 * @return array/enumeration that contains the filtered elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
 * 'it' -> currently visited element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
 * 'index' -> index of the current element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
 * 'array' -> array that is being iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
 * 'result' -> result array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
function filter(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    var func = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    if (typeof(code) != 'function') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        func = new Function("it", "index", "array", "result", "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        return filterEnumeration(array, func, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        var result = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            var it = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            if (func(it, index, array, result)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                result[result.length] = it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
 * Returns the number of elements of array/iterator/enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
function length(array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    if (array instanceof Array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        return array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    } else if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        var cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            array.nextElement(); 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        var cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
 * Transforms the given object or array by evaluating given code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
 * on each element of the object or array. The code evaluated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
 * can refer to the following built-in variables. 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
 * @param code  expression string or function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
 * @return array/enumeration that contains mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
 * 'it' -> currently visited element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
 * 'index' -> index of the current element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
 * 'array' -> array that is being iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
 * 'result' -> result array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 * map function returns an array/enumeration of values created 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 * by repeatedly calling code on each element of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
 * array/iterator/enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
function map(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    var func = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    if(typeof(code) != 'function') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        func = new Function("it", "index", "array", "result", "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        var index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        var result = new java.util.Enumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            hasMoreElements: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                return array.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            nextElement: function() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                return func(array.nextElement(), index++, array, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        var result = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            var it = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            result[result.length] = func(it, index, array, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
// private function used by min, max functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
function minmax(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    if (typeof(code) == 'string') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        code = new Function("lhs", "rhs", "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        if (! array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        var res = array.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            var next = array.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            if (code(next, res)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                res = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        if (array.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            return undefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        var res = array[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        for (var index = 1; index < array.length; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (code(array[index], res)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                res = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
 * Returns the maximum element of the array/iterator/enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
 * @param code (optional) comparision expression or function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
 *        by default numerical maximum is computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
function max(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    if (code == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        code = function (lhs, rhs) { return lhs > rhs; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    return minmax(array, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
 * Returns the minimum element of the array/iterator/enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
 * @param array input array/iterator/enumeration that is iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
 * @param code (optional) comparision expression or function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
 *        by default numerical minimum is computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
function min(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    if (code == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        code = function (lhs, rhs) { return lhs < rhs; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    } 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    return minmax(array, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
 * sort function sorts the input array. optionally accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
 * code to compare the elements. If code is not supplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
 * numerical sort is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
 * @param array input array/iterator/enumeration that is sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
 * @param code  expression string or function 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
 * @return sorted array 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
 * The comparison expression can refer to the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
 * built-in variables:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
 * 'lhs' -> 'left side' element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
 * 'rhs' -> 'right side' element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
function sort(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    // we need an array to sort, so convert non-arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    array = toArray(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    // by default use numerical comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    var func = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    if (code == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        func = function(lhs, rhs) { return lhs - rhs; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    } else if (typeof(code) == 'string') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        func = new Function("lhs", "rhs", "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    return array.sort(func);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
 * Returns the sum of the elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
 * @param array input array that is summed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
 * @param code optional expression used to map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
 *        input elements before sum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
function sum(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    if (code != undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        array = map(array, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    var result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            result += Number(array.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            result += Number(array[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
 * Returns array of unique elements from the given input 
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
 * array/iterator/enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
 * @param array from which unique elements are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
 * @param code optional expression (or function) giving unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
 *             attribute/property for each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
 *             by default, objectid is used for uniqueness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
function unique(array, code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    array = wrapIterator(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    if (code == undefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        code = new Function("it", "return objectid(it);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    } else if (typeof(code) == 'string') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        code = new Function("it", "return " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    var tmp = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    if (array instanceof java.util.Enumeration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        while (array.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            var it = array.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            tmp[code(it)] = it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        for (var index in array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            var it = array[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            tmp[code(it)] = it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    var res = new Array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    for (var index in tmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        res[res.length] = tmp[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
}