Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in a Reflect.Module class to provide a friendly wrapper around Pa…
…ckfileView
- Loading branch information
1 parent
f84af18
commit 1d7fbc3
Showing
3 changed files
with
117 additions
and
1 deletion.
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
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,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; |
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,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; | ||
} | ||
} |