
Added by roScripts on August-25-2007, 2:40 pm
************************************************************
Developed by R.Arul Kumaran [arul@shockwave-india.com] *
for more code keep visiting [www.shockwave-india.com/blog] *
************************************************************
version 3.7 Last updated on 1 Feb, 2006
Thanks to Ampre[ampreind@hotmail.com] for the initial improvements to my Object.toAS() code
*/
/*
| Object.toString() - Create a actionscript string for the given object with out circular references
| Useful for tracing objects for local and remote debugging
|
| ActionScript(Object, 'name_of_the_variable') - gets complete reusable actionscript of the Object including circular reference
*/
_global.ActionScript = function(obj, name) {
ActionScript._Dump = [];
var str = name+' = '+obj.toString(name)+ActionScript.lineBrake+ActionScript._Dump.join(ActionScript.lineBrake);
delete ActionScript._Dump;
return str;
};
ActionScript.lineBrake = "\n";
ActionScript.spacer = "\t";
ActionScript.makeString = function(t, name, spacer) {
var y = typeof (t);
switch (y) {
case "object" :
return (ActionScript.getString(t, name, spacer, t instanceof Array ? 1 : 0));
case "function" :
return (ActionScript.getString(t, name, spacer, 2));
case "boolean" :
case "number" :
return (t);
case "string" :
return (t.toString());
case "movieclip" :
var m = t._target.split('/').join('.');
return (m.length>1 ? '_root'+m : '_root');
default :
return (y == '' ? t : y);
}
};
ActionScript.getString = function(t, name, spacer, mode) {
var s = new String();
if (spacer == undefined) {
spacer = '';
}
t.__name__ = name;
t.IsCircularReference = true;
ASSetPropFlags(t, "IsCircularReference,__name__", 1);
for (var i in t) {
var special = false;
var l = i.length;
while (l--) {
var c = i.charAt(l);
if ("abcdefghijklmnopqrstuvwxyz0123456789_".indexOf(c.toLowerCase())<0) {
special = true;
break;
}
}
if (!t[i].IsCircularReference) {
if (special) {
ActionScript._Dump.push(t.__name__+'[\"'+i+'\"] = '+ActionScript.makeString(t[i], name+'.'+i, spacer+ActionScript.spacer)+';');
} else {
switch (mode) {
case 0 :
//Objects
var rS = ActionScript.makeString(t[i], name+'.'+i, spacer+ActionScript.spacer);
if (rS != '' && rS != undefined) {
s += ActionScript.lineBrake+spacer+ActionScript.spacer+i+':'+rS+", ";
}
break;
default :
//Object definition
if (isNaN(i)) {
var rS = ActionScript.makeString(t[i], name+'.'+i, '');
if (rS != '' && rS != undefined) {
ActionScript._Dump.push(t.__name__+'.'+i+' = '+rS+';');
}
}
}
}
} else {
if (isNaN(i)) {
ActionScript._Dump.push(special ? t.__name__+'[\"'+i+'\"] = '+t[i].__name__ : t.__name__+'.'+i+' = '+t[i].__name__+';');
}
}
}
switch (mode) {
case 1 :
//Array
for (var i = 0; i<t.length; i++) {
if (!t[i].IsCircularReference) {
var rS = ActionScript.makeString(t[i], name, spacer+ActionScript.spacer);
if (rS != '' && rS != undefined) {
s += ActionScript.lineBrake+spacer+ActionScript.spacer+rS+", ";
}
} else {
ActionScript._Dump.push(t.__name__+'['+i+'] = '+t[i].__name__+';');
}
}
if (s.length) {
s = "["+substring(s, 1, s.length-2)+ActionScript.lineBrake+spacer+"]";
} else {
s = "[]";
}
break;
case 2 :
//Function
ActionScript._Dump.push(t.__name__+" = function(){\n}");
s = "";
//s = "function(){\n"+spacer+"}";
break;
default :
//Generic Objects
if (s.length) {
s = "{"+substring(s, 1, s.length-2)+ActionScript.lineBrake+spacer+"}";
} else {
s = "{}";
}
}
delete t.IsCircularReference;
delete t.__name__;
return s;
};
Object.prototype.toString = function(name, spacer) {
return ActionScript.getString(this, name, spacer, 0);
};
Array.prototype.toString = function(name, spacer) {
return ActionScript.getString(this, name, spacer, 1);
};
var o = Function;
o.prototype.toString = function(name, spacer) {
return ActionScript.getString(this, name, spacer, 2);
};
String.prototype.toString = function() {
return "\""+this.split("'").join("\\'").split('\"').join('\\\"').split('\r').join('\\r').split(ActionScript.lineBrake).join('\\n')+"\"";
};
ASSetPropFlags(o.prototype, "toString", 1);
ASSetPropFlags(String.prototype, "toString", 1);
delete o;
/*
//Example:-
#include "toString.as"
var a = {num:20, str:"Escaped \"Q\"uote's and \rline \nbrakes", arr:['str', 30], obj:{aa:33, bol:true}};
a.self = a;
a.blank = undefined;
a.func = function() {
trace("me");
};
a.func.arr = [1, 2];
a.func.ref = a;
a.obj.parent = a.obj;
a.obj.parents = a;
a.arr.push(a);
a.arr.push(a.arr);
a.arr.ref = a.arr;
a.arr.func = function() {
};
trace(a);
//traces the following
{
blank:undefined,
num:20,
str:"Escaped \"Q\"uote\'s and \rline \nbrakes",
arr:[
"str",
30
],
obj:{
aa:33,
bol:true
}
}
trace(ActionScript(a, 'a'));
//traces the following
a = {
blank:undefined,
num:20,
str:"Escaped \"Q\"uote\'s and \rline \nbrakes",
arr:[
"str",
30
],
obj:{
aa:33,
bol:true
}
}
a.func.ref = a;
a.func.arr = [
1,
2
];
a.func = function(){
}
a.self = a;
a.arr.func = function(){
}
a.arr.ref = a.arr;
a.arr[2] = a;
a.arr[3] = a.arr;
a.obj.parents = a;
a.obj.parent = a.obj;
*/
Added by roScripts on March-26-2008, 5:14 pm
Added by roScripts on March-26-2008, 5:13 pm
Added by roScripts on March-26-2008, 5:09 pm
Added by roScripts on March-26-2008, 5:08 pm
Added by roScripts on March-26-2008, 5:07 pm