Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ForeignCode, to wrap foreign code objects.
This means they behave sufficiently Perl 6-like to not blow up:

    say Sub.^methods

And similar.
  • Loading branch information
jnthn committed Apr 9, 2013
1 parent a7e8fbf commit 5cb7046
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/Perl6/Metamodel/BOOTSTRAP.pm
Expand Up @@ -73,6 +73,7 @@ my stub PROCESS metaclass Perl6::Metamodel::ModuleHOW { ... };
my stub Grammar metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Junction metaclass Perl6::Metamodel::ClassHOW { ... };
my stub Metamodel metaclass Perl6::Metamodel::PackageHOW { ... };
my stub ForeignCode metaclass Perl6::Metamodel::ClassHOW { ... };

# We stick all the declarative bits inside of a BEGIN, so they get
# serialized.
Expand Down Expand Up @@ -1605,8 +1606,21 @@ BEGIN {
Bool.HOW.publish_boolification_spec(Bool);
Bool.HOW.compose_repr(Bool);

# class ObjAt {
# has str $!value;
# }
ObjAt.HOW.add_attribute(ObjAt, BOOTSTRAPATTR.new(:name<$!value>, :type(str), :box_target(1), :package(ObjAt)));
ObjAt.HOW.compose_repr(ObjAt);

# class ForeignCode {
# has $!do; # Code object we delegate to
# ... # Uncomposed
# }
ForeignCode.HOW.add_parent(ForeignCode, Any);
ForeignCode.HOW.add_attribute(ForeignCode, BOOTSTRAPATTR.new(:name<$!do>, :type(Mu), :package(ForeignCode)));
ForeignCode.HOW.compose_repr(ForeignCode);
ForeignCode.HOW.add_parrot_vtable_mapping(ForeignCode, 'invoke', nqp::null());
ForeignCode.HOW.add_parrot_vtable_handler_mapping(ForeignCode, 'invoke', '$!do');

# Set up Stash type, using a Parrot hash under the hood for storage.
Stash.HOW.add_parent(Stash, Hash);
Expand Down Expand Up @@ -1651,6 +1665,7 @@ BEGIN {
Perl6::Metamodel::ClassHOW.add_stash(Array);
Perl6::Metamodel::ClassHOW.add_stash(Hash);
Perl6::Metamodel::ClassHOW.add_stash(ObjAt);
Perl6::Metamodel::ClassHOW.add_stash(ForeignCode);

# Make Parrot invoke v-table construct a capture and delegate off
# to postcircumfix:<( )>.
Expand Down Expand Up @@ -1747,6 +1762,7 @@ BEGIN {
EXPORT::DEFAULT.WHO<WrapDispatcher> := Perl6::Metamodel::WrapDispatcher;
EXPORT::DEFAULT.WHO<StaticLexPad> := Perl6::Metamodel::StaticLexPad;
EXPORT::DEFAULT.WHO<Metamodel> := Metamodel;
EXPORT::DEFAULT.WHO<ForeignCode> := ForeignCode;
}
EXPORT::DEFAULT.WHO<NQPCursorRole> := NQPCursorRole;

Expand Down Expand Up @@ -1862,5 +1878,10 @@ nqp::sethllconfig('perl6', nqp::hash(
my $result := nqp::create(Hash);
nqp::bindattr($result, EnumMap, '$!storage', $hash);
$result
},
'foreign_transform_code', -> $code {
my $result := nqp::create(ForeignCode);
nqp::bindattr($result, ForeignCode, '$!do', $code);
$result
}
));
6 changes: 3 additions & 3 deletions src/Perl6/Metamodel/MethodContainer.pm
Expand Up @@ -40,17 +40,17 @@ role Perl6::Metamodel::MethodContainer {
# Always need local methods on the list.
my @meths;
for @!method_order {
@meths.push($_);
@meths.push(nqp::hllizefor($_, 'perl6'));
}

# If local flag was not passed, include those from parents.
unless $local {
for self.parents($obj, :all($all), :excl($excl)) {
for $_.HOW.method_table($_) {
@meths.push($_.value);
@meths.push(nqp::hllizefor($_.value, 'perl6'));
}
for $_.HOW.submethod_table($_) {
@meths.push($_.value);
@meths.push(nqp::hllizefor($_.value, 'perl6'));
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/core/ForeignCode.pm
@@ -0,0 +1,15 @@
# Takes a foreign code object and tries to make it feel somewhat like a Perl
# 6 one. Note that it doesn't have signature information we can know about.
my class ForeignCode does Callable {
method arity() { self.signature.arity }

method count() { self.signature.count }

method signature(ForeignCode:D:) { (sub (|) { }).signature }

method name() { (nqp::can($!do, 'name') ?? $!do.name !! nqp::getcodename($!do)) || '<anon>' }

multi method gist(ForeignCode:D:) { self.name }

multi method Str(ForeignCode:D:) { self.name }
}
3 changes: 2 additions & 1 deletion tools/build/Makefile.in
@@ -1,4 +1,4 @@
# Copyright (C) 2006-2012, The Perl Foundation.
# Copyright (C) 2006-2013, The Perl Foundation.
# $Id$

SHELL = @shell@
Expand Down Expand Up @@ -234,6 +234,7 @@ CORE_SOURCES = \
src/core/Bag.pm \
src/core/ObjAt.pm \
src/core/Version.pm \
src/core/ForeignCode.pm \
src/core/operators.pm \
src/core/metaops.pm \
src/core/terms.pm \
Expand Down

0 comments on commit 5cb7046

Please sign in to comment.