Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial setting implementation.
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 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
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
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,57 @@ | ||
| class Snake::ModuleLoader; | ||
|
|
||
| my $loaded_setting; | ||
|
|
||
| method search_path($file) { | ||
| my $path; | ||
| for ['.', 'blib'] -> $prefix { | ||
| if nqp::stat("$prefix/$file", 0) { | ||
| $path := "$prefix/$file"; | ||
| last; | ||
| } | ||
| } | ||
| nqp::die("Unable to locate $file") if !$path; | ||
| $path | ||
| } | ||
|
|
||
| # Setting loading, how does it work? | ||
| # So this is slightly magical and mostly stolen from NQP, but as far as I grok | ||
| # it, this is the deal: | ||
| # | ||
| # 1) YOU_ARE_HERE creates the AST from Snake::Actions.CTXSAVE, which is | ||
| # inherited from HLL::Actions. This is a bit of code that looks up $*CTXSAVE, | ||
| # and if that object is able to .ctxsave(), calls that method on it. | ||
| # | ||
| # 2) The module loader sets $*CTXSAVE to itself, and loads the setting, which | ||
| # will call the code above as part of its mainline, triggering a call on | ||
| # .ctxsave(), where we save a reference to the context of the calling code, | ||
| # sc. the lexical environment we want code to run in. | ||
| method load_setting($name) { | ||
| my $setting; | ||
|
|
||
| if $name ne "NULL" { | ||
| unless $loaded_setting { | ||
| my $path := self.search_path("$name.setting.moarvm"); # XXX: Backend specific! | ||
| my $*CTXSAVE := self; | ||
| my $*MAIN_CTX := Snake::ModuleLoader; | ||
| nqp::loadbytecode($path); | ||
|
|
||
| if !nqp::defined($*MAIN_CTX) { | ||
| nqp::die("Couldn't load setting $name; maybe it's missing a YOU_ARE_HERE?"); | ||
| } | ||
| $loaded_setting := $*MAIN_CTX; | ||
| } | ||
| $setting := $loaded_setting; | ||
| } | ||
|
|
||
| $setting; | ||
| } | ||
|
|
||
| method ctxsave() { | ||
| $*MAIN_CTX := nqp::ctxcaller(nqp::ctx()); | ||
| $*CTXSAVE := 0; | ||
| } | ||
|
|
||
| nqp::bindhllsym('snake', 'ModuleLoader', Snake::ModuleLoader); | ||
|
|
||
| # vim: ft=perl6 |
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,37 @@ | ||
| class Snake::World is HLL::World; | ||
|
|
||
| use Snake::ModuleLoader; | ||
|
|
||
| my $loader := nqp::gethllsym("snake", "ModuleLoader"); | ||
|
|
||
| method load_setting($name) { | ||
| if $name ne "NULL" { | ||
| my $setting := $loader.load_setting($name); | ||
|
|
||
| my $set_outer := QAST::Op.new(:op<forceouterctx>, | ||
| QAST::BVal.new(:value($*UNIT)), | ||
| QAST::Op.new(:op<callmethod>, :name<load_setting>, | ||
| QAST::Op.new(:op<getcurhllsym>, | ||
| QAST::SVal.new(:value<ModuleLoader>) | ||
| ), | ||
| QAST::SVal.new(:value($name)), | ||
| ), | ||
| ); | ||
| if self.is_precompilation_mode() { | ||
| # TODO | ||
| nqp::die("World.load_setting for precompilation NYI"); | ||
| } | ||
| else { | ||
| self.add_load_dependency_task(:fixup_ast( | ||
| QAST::Op.new(:op<loadbytecode>, | ||
| QAST::SVal.new(:value<Snake/ModuleLoader.moarvm>) | ||
| ) | ||
| )); | ||
| self.add_fixup_task(:fixup_ast($set_outer)); | ||
| } | ||
|
|
||
| nqp::ctxlexpad($setting); | ||
| } | ||
| } | ||
|
|
||
| # vim: ft=perl6 |
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,4 @@ | ||
| def print(msg): | ||
| nqp::say(msg) | ||
|
|
||
| YOU_ARE_HERE |
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