Skip to content

Commit

Permalink
Offload symbol lookup onto Raku code
Browse files Browse the repository at this point in the history
And get rid of `find_exception` method. I don't see areason why
metamodel code should implement what Raku does better.

Added `&sym_lookup` local of Perl6::Metamodel::Configuration and a
method to register a lookup routine. Now `throw_or_die` uses
registration of the routine as a flag to start using core exception
classes instead of plain `nqp::die`.
  • Loading branch information
vrurg committed May 4, 2021
1 parent a524c3d commit 8427afe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
41 changes: 9 additions & 32 deletions src/Perl6/Metamodel/Configuration.nqp
Expand Up @@ -39,43 +39,20 @@ class Perl6::Metamodel::Configuration {
}
method role_to_role_applier_type() { $role_to_role_applier_type }

my $X_package := nqp::null();
method set_X_package($X) {
$X_package := $X;
}
method find_exception($exception) {
my $ex_type := nqp::null();
unless nqp::isnull($X_package) {
my @parts := nqp::split('::', $exception);
# If the first part of the long name is not X then pretend there is no such exception. Which is, actually
# and most likely, is true.
if nqp::iseq_s(nqp::shift(@parts), 'X') {
my $who := $X_package.WHO;
while +@parts {
my $name := nqp::shift(@parts);
if $who.EXISTS-KEY($name) {
if +@parts {
$who := $who.AT-KEY($name).WHO;
}
else {
$ex_type := $who.AT-KEY($name);
}
}
else {
# Signal the loop end
@parts := nqp::list();
}
}
}
}
$ex_type
my &sym_lookup := nqp::null();
method set_sym_lookup_routine(&slr) {
&sym_lookup := &slr;
}
method throw_or_die($exception, $die_message, *@pos, *%named) {
my $ex_type := self.find_exception($exception);
if nqp::isnull($ex_type) {
if nqp::isnull(&sym_lookup) {
nqp::die($die_message)
}
else {
# When &sym_lookup is registered we do have all core exception classes declared. Therefore we can use
# use &sym_lookup safely. If it fails to find a symbol then fully legit X::NoSuchSymbol will be thrown.
my $ex_type := &sym_lookup(nqp::hllizefor($exception, 'Raku'));
# HLLize all named arguments for exception constructor. Note that if an exception attribute is Bool the the
# caller of this method is responsible for using nqp::hllboolfor to produce a valid Bool instance.
my %hll_named;
for %named {
%hll_named{nqp::iterkey_s($_)} := nqp::hllizefor(nqp::iterval($_), 'Raku');
Expand Down
6 changes: 4 additions & 2 deletions src/core.c/Exception.pm6
Expand Up @@ -3314,7 +3314,9 @@ my class Exceptions::JSON {
}
}

# Provide means of accessing any X:: exception to the Metamodel.
Metamodel::Configuration.set_X_package(X);
# Provide Metamodel::Configuration with symbol lookup routine. We do it here because throw_or_die method learn about
# availability of all exception classes based on this registration. OTOH, it is better to provide them as soon as
# possible as this might improve diagnostics of CORE.setting compilation failures.
Metamodel::Configuration.set_sym_lookup_routine( -> $sym is raw { ::($sym) } );

# vim: expandtab shiftwidth=4

0 comments on commit 8427afe

Please sign in to comment.