Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A few rough-drafts for a new object dumper library
- Loading branch information
1 parent
0001e72
commit 824cab3
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Rosella { namespace Dumper | ||
{ | ||
class DumpHandler | ||
{ | ||
function can_dump(var obj) | ||
{ | ||
return 1; | ||
} | ||
|
||
function dump(var dump, var obj, string pprefix, string prefix, var sb) | ||
{ | ||
try { | ||
string txt = string(obj); | ||
push(sb, Winxed_Builtins.sprintf("'%s'", [txt])); | ||
} catch { | ||
int addr; | ||
push(sb, Winxed_Builtins.sprintf("PMC (%s)", [typeof(obj)]); | ||
} | ||
} | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace Rosella | ||
{ | ||
class Dumper | ||
{ | ||
var role_dumpers; | ||
var type_dumpers; | ||
var fallback_dumper; | ||
|
||
function Dumper() | ||
{ | ||
self.role_dumpers = { | ||
"array" : new Rosella.Dumper.DumpHandler.Array(), | ||
"hash" : new Rosella.Dumper.DumpHandler.Hash() | ||
}; | ||
self.type_dumpers = {}; | ||
self.fallback_dumper = new Rosella.Dumper.DumpHandler(); | ||
} | ||
|
||
function dump(var obj) | ||
{ | ||
var sb = new "StringBuilder"; | ||
self.__dump_recursive(obj, sb, ""); | ||
return string(sb); | ||
} | ||
|
||
function __dump_recurse(var obj, var sb, string prefix) | ||
{ | ||
string type = typeof(obj); | ||
for (string key in self.type_dumpers) | ||
{ | ||
if (type == key) { | ||
self.type_dumpers[key].dump(self, obj, prefix, prefix + "\t", sb); | ||
return; | ||
} | ||
} | ||
for (string role in self.role_dumpers) | ||
{ | ||
int does_it = 0; | ||
${ does does_it, obj, role }; | ||
if (does_it) { | ||
self.role_dumpers[role].dump(self, obj, prefix, prefix + "\t", sb); | ||
return; | ||
} | ||
} | ||
self.fallback_dumper.dump(self, obj, prefix, prefix + "\t", sb); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Rosella { namespace Dumper { namespace DumpHandler | ||
{ | ||
class Array : Rosella.Dumper.DumpHandler | ||
{ | ||
function can_handle(var obj) | ||
{ | ||
int can_do = 0; | ||
${ can can_do, obj, "array" }; | ||
return can_do; | ||
} | ||
|
||
function dump(var dump, var obj, string pprefix, string prefix, var sb) | ||
{ | ||
push(sb, "["); | ||
for (int i = 0; i < elements(obj); i++) { | ||
push(sb, Winxed_Builtins.sprintf("%s%d => ", [prefix, i])); | ||
dump.__dump_recursive(obj[i], sb); | ||
} | ||
|
||
push(sb, pprefix); | ||
push(sb, "]"); | ||
} | ||
} | ||
}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Rosella { namespace Dumper { namespace DumpHandler | ||
{ | ||
class Hash : Rosella.Dumper.DumpHandler | ||
{ | ||
function can_handle(var obj) | ||
{ | ||
int can_do = 0; | ||
${ can can_do, obj, "hash" }; | ||
return can_do; | ||
} | ||
|
||
function dump(var dump, var obj, string pprefix, string prefix, var sb) | ||
{ | ||
push(sb, "{"); | ||
for (string key in obj) { | ||
push(sb, Winxed_Builtins.sprintf("%s%s => ", [prefix, key])); | ||
dump.__dump_recursive(obj[key], sb); | ||
} | ||
|
||
push(sb, pprefix); | ||
push(sb, "}"); | ||
} | ||
} | ||
}}} |