Skip to content

Commit

Permalink
Factor out shaped storage alloc/interning.
Browse files Browse the repository at this point in the history
So we can use it for both Array and array.
  • Loading branch information
jnthn committed Nov 20, 2015
1 parent 9c82d4c commit ec6220f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/core/Array.pm
Expand Up @@ -56,32 +56,9 @@ my class Array { # declared in BOOTSTRAP
result
}

my constant \SHAPE-STORAGE-ROOT := do {
my Mu $root := nqp::newtype(nqp::knowhow(), 'Uninstantiable');
nqp::setparameterizer($root, -> $, $key {
my Mu $args := nqp::p6argvmarray();
my $dim_type := nqp::newtype(nqp::knowhow(), 'MultiDimArray');
nqp::composetype($dim_type, nqp::hash('array',
nqp::hash('dimensions', $key.elems)));
nqp::settypehll($dim_type, 'perl6');
$dim_type
});
nqp::settypehll($root, 'perl6');
$root
}
sub allocate-shaped-storage(\arr, @dims) {
my $key := nqp::list();
my $dims := nqp::list_i();
for @dims {
if nqp::istype($_, Whatever) {
X::NYI.new(feature => 'Jagged array shapes');
}
nqp::push($key, Mu);
nqp::push_i($dims, $_.Int);
}
my $storage := nqp::create(nqp::parameterizetype(SHAPE-STORAGE-ROOT, $key));
nqp::setdimensions($storage, $dims);
nqp::bindattr(arr, List, '$!reified', $storage);
nqp::bindattr(arr, List, '$!reified',
Rakudo::Internals.SHAPED-ARRAY-STORAGE(@dims, nqp::knowhow(), Mu));
arr
}

Expand Down
30 changes: 30 additions & 0 deletions src/core/Rakudo/Internals.pm
Expand Up @@ -318,6 +318,36 @@ my class Rakudo::Internals {
nqp::atkey($pvalcode,$key)
}
#?endif

my constant \SHAPE-STORAGE-ROOT := do {
my Mu $root := nqp::newtype(nqp::knowhow(), 'Uninstantiable');
nqp::setparameterizer($root, -> $, $key {
my $dims := $key.elems.pred;
my $type := $key.AT-POS(1);
my $dim_type := nqp::newtype($key.AT-POS(0), 'MultiDimArray');
nqp::composetype($dim_type, nqp::hash('array',
nqp::hash('dimensions', $dims, 'type', $type)));
nqp::settypehll($dim_type, 'perl6');
$dim_type
});
nqp::settypehll($root, 'perl6');
$root
}

method SHAPED-ARRAY-STORAGE(@dims, Mu \meta-obj, Mu \type-key) {
my $key := nqp::list(meta-obj);
my $dims := nqp::list_i();
for @dims {
if nqp::istype($_, Whatever) {
X::NYI.new(feature => 'Jagged array shapes');
}
nqp::push($key, type-key);
nqp::push_i($dims, $_.Int);
}
my $storage := nqp::create(nqp::parameterizetype(SHAPE-STORAGE-ROOT, $key));
nqp::setdimensions($storage, $dims);
$storage
}
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit ec6220f

Please sign in to comment.