Permalink
Browse files

LexPad: Add register_type method

This allows for the type of a lexical to be introspected before
retrieval.  This is useful because attempting to store the lexical in
the wrong type of register throws an error.
  • Loading branch information...
1 parent 912b204 commit 0c99d32436fff7e5d21da6070f799792d4ab0d3a @Benabik Benabik committed Apr 12, 2012
Showing with 64 additions and 2 deletions.
  1. +36 −1 src/pmc/lexpad.pmc
  2. +28 −1 t/pmc/lexpad.t
View
@@ -447,8 +447,43 @@ Get iterator for declared lexicals.
return VTABLE_get_iter(INTERP, lexinfo);
}
-}
+/*
+
+=item C<INTVAL register_type(STRING *name)>
+
+Returns a number based on the type of the variable named name.
+
+=over 4
+=item * -1 = Not Found
+
+=item * 0 = Integer
+
+=item * 1 = Num
+
+=item * 2 = String
+
+=item * 3 = PMC
+
+=back
+
+=cut
+
+*/
+
+ METHOD INTVAL register_type(STRING *name) {
+ PMC *info;
+ HashBucket *b;
+ INTVAL ret = -1;
+
+ GET_ATTR_lexinfo(INTERP, SELF, info);
+ b = register_bucket(INTERP, info, name);
+ if (b)
+ ret = (INTVAL) b->value & 3;
+ RETURN(INTVAL ret);
+ }
+
+}
/*
View
@@ -18,12 +18,13 @@ Tests the LexPad PMC.
.sub main :main
.include 'test_more.pir'
- plan(9)
+ plan(14)
new_test()
test_keyed()
test_keyed_empty()
test_iter()
+ test_register_type()
.end
.sub new_test
@@ -102,6 +103,32 @@ iter_loop:
iter_done:
.end
+.sub 'test_register_type'
+ .local int i
+ .local num n, type
+ .local string s
+ .local pmc pad, p
+
+ .lex 'I', i
+ .lex 'N', n
+ .lex 'S', s
+ .lex 'P', p
+
+ pad = getinterp
+ pad = pad['lexpad']
+
+ type = pad.'register_type'('?')
+ is(type, -1, 'type for non-existant lexical')
+ type = pad.'register_type'('I')
+ is(type, 0, 'type for integer lexical')
+ type = pad.'register_type'('N')
+ is(type, 1, 'type for num lexical')
+ type = pad.'register_type'('S')
+ is(type, 2, 'type for string lexical')
+ type = pad.'register_type'('P')
+ is(type, 3, 'type for PMC lexical')
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100

0 comments on commit 0c99d32

Please sign in to comment.