Skip to content

Commit 8d5e9d4

Browse files
committed
Add a way to introspect if a lexical is native.
1 parent 9ed3bc1 commit 8d5e9d4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/QAST/Operations.nqp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,15 @@ QAST::Operations.add_core_op('callercode', -> $qastcomp, $op {
10241024
$ops.result($reg);
10251025
$ops
10261026
});
1027+
QAST::Operations.add_core_op('lexprimspec', -> $qastcomp, $op {
1028+
unless +@($op) == 2 {
1029+
nqp::die("Operation 'lexprimspec' expects two operands");
1030+
}
1031+
$qastcomp.as_post(QAST::Op.new(
1032+
:op('callmethod'), :name('get_lex_type'), :returns(int),
1033+
$op[0], $op[1]
1034+
))
1035+
});
10271036

10281037
# Exception handling/munging.
10291038
my $exc_exclude := 0;

src/pmc/nqplexpad.pmc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#include "pmc_nqplexinfo.h"
22

3+
/* Possible options for boxed primitives. */
4+
#define STORAGE_SPEC_BP_NONE 0
5+
#define STORAGE_SPEC_BP_INT 1
6+
#define STORAGE_SPEC_BP_NUM 2
7+
#define STORAGE_SPEC_BP_STR 3
8+
39
/* Locates the register number for getting the specified name
410
* and type of lexical. */
511
static INTVAL register_number_for_get(PARROT_INTERP, Hash *hash,
@@ -316,6 +322,24 @@ Return the LexInfo PMC, if any or a Null PMC.
316322
GET_ATTR_lexinfo(INTERP, SELF, lexinfo);
317323
RETURN(PMC *lexinfo);
318324
}
325+
326+
METHOD get_lex_type(STRING *name) {
327+
Hash *hash;
328+
HashBucket *b;
329+
INTVAL spec;
330+
GET_ATTR_lexinfo_hash(INTERP, SELF, hash);
331+
b = Parrot_hash_get_bucket(interp, hash, name);
332+
if (!b)
333+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LEX_NOT_FOUND,
334+
"Lexical '%Ss' not found", name);
335+
switch ((INTVAL)b->value & 3) {
336+
case REGNO_INT: spec = STORAGE_SPEC_BP_INT; break;
337+
case REGNO_NUM: spec = STORAGE_SPEC_BP_NUM; break;
338+
case REGNO_STR: spec = STORAGE_SPEC_BP_STR; break;
339+
default: spec = STORAGE_SPEC_BP_NONE; break;
340+
}
341+
RETURN(INTVAL spec);
342+
}
319343

320344
/*
321345

0 commit comments

Comments
 (0)