Skip to content

Commit

Permalink
Add in a Reflect.Module class to provide a friendly wrapper around Pa…
Browse files Browse the repository at this point in the history
…ckfileView
  • Loading branch information
Whiteknight committed Nov 30, 2011
1 parent f84af18 commit 1d7fbc3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
4 changes: 3 additions & 1 deletion setup.winxed
Expand Up @@ -328,10 +328,12 @@ function setup_experimental_libraries(var rosella)
);

setup_unstable_lib(rosella, "reflect", ["Core"],
"reflect/Includes",
"reflect/Class",
"reflect/class/Member",
"reflect/class/Attribute",
"reflect/class/Method"
"reflect/class/Method",
"reflect/Module"
);

setup_unstable_lib(rosella, "date", [],
Expand Down
5 changes: 5 additions & 0 deletions src/unstable/reflect/Includes.winxed
@@ -0,0 +1,5 @@
// Constants.
const int SUB_FLAG_METHOD = 0x04;
const int SUB_FLAG_INIT = 0x400;
const int PF_FLAG_ANON = 0x08;
const int PF_FLAG_LOAD = 0x20;
109 changes: 109 additions & 0 deletions src/unstable/reflect/Module.winxed
@@ -0,0 +1,109 @@
namespace Rosella.Reflect.Module
{
function load(string name)
{
var pf_raw = load_packfile(name);
var pf = new Rosella.Reflect.Module(pf_raw);
pf.load();
return pf;
}

function get_current_packfile()
{
// TODO: Get a reference to the caller, get the packfile from there
}

function get_all_packfiles()
{
// TODO: Get a list of all loaded parrot packfiles. Can we do this
// without implicitly GC-registering all packfiles at the parrot-level?
}
}

class Rosella.Reflect.Module
{
var pf;
var functions;
var classes;
var namespaces;

function Module(var pf)
{
self.pf = pf;
}

function load()
{
Rosella.init_bytecode(self.pf, "load");
}

function path()
{
return string(self.pf);
}

function version()
{
// TODO: PackfileView doesn't yet have functionality to get the version
// information. Probably need to add that.
}

function classes()
{
if (self.classes == null)
self.__read_packfile_entries();
return self.classes;
}

function functions()
{
if (self.functions == null)
self.__read_packfile_entries();
return self.functions;
}

function namespaces()
{
if (self.namespaces == null)
self.__read_packfile_entries();
return self.namespaces;
}

function __read_packfile_entries()
{
var all_subs = self.pf.all_subs();
var all_namespaces = {};
var all_classes = {};
var functions = [];

for (var sub in all_subs) {
int sub_flags = sub.comp_flags();
int pf_flags = sub.pf_flags();

// Don't list subs marked :anon
if ((pf_flags & PF_FLAG_ANON) != 0)
continue;

var ns_pmc = sub.get_namespace();
var nsname = ns_pmc.get_name();
nsname.shift(); // drop HLL namespace
string ns = join(".", nsname);

if ((sub_flags & SUB_FLAG_METHOD) != 0) {
if (!(exists all_classes[ns])) {
var c = ns_pmc.get_class();
all_classes[ns] = new Rosella.Reflect.Class(c);
}
}
else {
if (!(exists all_namespaces[ns]))
all_namespaces[ns] = ns_pmc;
all_namespaces[ns].add_sub(sub);
push(functions, sub);
}
}
self.namespaces = all_namespaces;
self.classes = all_classes;
self.functions = functions;
}
}

0 comments on commit 1d7fbc3

Please sign in to comment.