Skip to content

Commit

Permalink
Add nqp::islist().
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Jun 3, 2012
1 parent 27720d1 commit 98f6a4e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/PAST/NQP.pir
Expand Up @@ -612,6 +612,7 @@ entry to produce the node to be returned.
maphash['existskey'] = 'exists__IQs'
maphash['existspos'] = 'exists__IQi'
maphash['elems'] = 'elements__IP'
maphash['islist'] = 'nqp_islist__IP'
maphash['iterator'] = 'iter__PP'
maphash['push'] = 'push__0PP'
maphash['push_s'] = 'push__0Ps'
Expand Down
25 changes: 25 additions & 0 deletions src/ops/nqp.ops
Expand Up @@ -29,6 +29,7 @@ static INTVAL initialized = 0;
static INTVAL stable_id = 0;
static INTVAL smo_id = 0;
static INTVAL disp_id = 0;
static INTVAL qrpa_id = 0;

/* Built-in meta-objects. */
static PMC *KnowHOW = NULL;
Expand Down Expand Up @@ -82,6 +83,16 @@ static void SC_write_barrier_st(PARROT_INTERP, STable *st) {
}
}


/* Test for something being a list (RPA or QRPA). This isn't
* declared 'static' because we want to be able to call it from
* other C functions (in both nqp and rakudo) as well. */
INTVAL
nqp_islist(PMC *pmc) {
INTVAL type = pmc->vtable->base_type;
return (INTVAL)(type == qrpa_id || type == enum_class_ResizablePMCArray);
}

END_OPS_PREAMBLE

/*
Expand All @@ -101,6 +112,7 @@ inline op nqp_dynop_setup() :base_core {
stable_id = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, "STable", 0));
smo_id = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, "SixModelObject", 0));
disp_id = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, "DispatcherSub", 0));
qrpa_id = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, "QRPA", 0));

/* Initialize the object model. */
SixModelObject_initialize(interp, &KnowHOW, &KnowHOWAttribute);
Expand Down Expand Up @@ -1854,6 +1866,19 @@ inline op repr_hint_for(out INT, in PMC, in PMC, in STR) :base_core {

/*

=item nqp_islist

Checks if the type of thing in $2 is a nqp list (either QRPA or RPA).

=cut

*/
inline op nqp_islist(out INT, in PMC) :base_core {
$1 = nqp_islist($2)
}

/*

=item is_container

Checks if the type of thing in $2 is a container or not. Puts a non-zero
Expand Down
18 changes: 17 additions & 1 deletion t/nqp/59-nqpop.t
Expand Up @@ -2,7 +2,7 @@

# Test nqp::op pseudo-functions.

plan(80);
plan(92);


ok( nqp::add_i(5,2) == 7, 'nqp::add_i');
Expand Down Expand Up @@ -108,3 +108,19 @@ ok( nqp::shift($iter) eq 'a', 'nqp::iterator');
ok( nqp::shift($iter) eq 'b', 'nqp::iterator');
ok( nqp::shift($iter) == 3.0, 'nqp::iterator');
ok( nqp::elems($list) == 3, "iterator doesn't modify list");
ok( nqp::islist($list), "nqp::islist works");

my $qlist := nqp::qlist(0, 'a', 'b', 3.0);
ok( nqp::elems($qlist) == 4, 'nqp::elems');
ok( nqp::atpos($qlist, 0) == 0, 'nqp::atpos');
ok( nqp::atpos($qlist, 2) eq 'b', 'nqp::atpos');
nqp::push($qlist, 'four');
ok( nqp::elems($qlist) == 5, 'nqp::push');
ok( nqp::shift($qlist) == 0, 'nqp::shift');
ok( nqp::pop($qlist) eq 'four', 'nqp::pop');
my $qiter := nqp::iterator($qlist);
ok( nqp::shift($qiter) eq 'a', 'nqp::iterator');
ok( nqp::shift($qiter) eq 'b', 'nqp::iterator');
ok( nqp::shift($qiter) == 3.0, 'nqp::iterator');
ok( nqp::elems($qlist) == 3, "iterator doesn't modify qlist");
ok( nqp::islist($qlist), "nqp::islist works");
21 changes: 20 additions & 1 deletion t/pmc/qrpa.t
Expand Up @@ -19,12 +19,15 @@ out-of-bounds test. Checks INT and PMC keys.

.include 'except_types.pasm'
.loadlib 'nqp_group'
.loadlib 'nqp_ops'

.sub main :main
.include 'fp_equality.pasm'
.include 'test_more.pir'

plan(122)
nqp_dynop_setup

plan(125)

# init_tests()
resize_tests()
Expand Down Expand Up @@ -55,6 +58,7 @@ out-of-bounds test. Checks INT and PMC keys.
splice_replace1()
splice_replace2()
iterate_subclass_of_rpa()
nqp_islist_op()
# method_forms_of_unshift_etc()
# sort_with_broken_cmp()
# equality_tests()
Expand Down Expand Up @@ -1037,6 +1041,21 @@ too_low_end:
.end


.sub 'nqp_islist_op'
$P0 = new ['ResizablePMCArray']
$I0 = nqp_islist $P0
ok($I0, 'RPA is a nqp list')

$P0 = new ['QRPA']
$I0 = nqp_islist $P0
ok($I0, 'QRPA is a nqp list')

$P0 = new ['Integer']
$I0 = nqp_islist $P0
nok($I0, 'Integer isnt a nqp list')
.end


.sub iterate_subclass_of_rpa
.local pmc arr, it
$P0 = subclass 'QRPA', 'MyArray'
Expand Down

0 comments on commit 98f6a4e

Please sign in to comment.