Skip to content

Commit

Permalink
Added __CLASS__ keyword, which yields the runtime class name of the i…
Browse files Browse the repository at this point in the history
…nvoking instance
  • Loading branch information
leonerd committed Jul 28, 2023
1 parent 3a4d94e commit f0de3fe
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 271 deletions.
23 changes: 23 additions & 0 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,29 @@ Perl_class_add_ADJUST(pTHX_ HV *stash, CV *cv)
av_push(aux->xhv_class_adjust_blocks, (SV *)cv);
}

OP *
Perl_ck_classname(pTHX_ OP *o)
{
if(!CvIsMETHOD(PL_compcv))
croak("Cannot use __CLASS__ outside of a method or field initializer expression");

return o;
}

PP(pp_classname)
{
dSP;

SV *self = PAD_SVl(PADIX_SELF);
assert(SvTYPE(SvRV(self)) == SVt_PVOBJ);

EXTEND(SP, 1);
PUSHs(sv_newmortal());
sv_ref(*SP, SvRV(self), true);

RETURN;
}

/*
* ex: set ts=8 sts=4 sw=4 et:
*/
1 change: 1 addition & 0 deletions embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@
# define ck_anoncode(a) Perl_ck_anoncode(aTHX_ a)
# define ck_backtick(a) Perl_ck_backtick(aTHX_ a)
# define ck_bitop(a) Perl_ck_bitop(aTHX_ a)
# define ck_classname(a) Perl_ck_classname(aTHX_ a)
# define ck_cmp(a) Perl_ck_cmp(aTHX_ a)
# define ck_concat(a) Perl_ck_concat(aTHX_ a)
# define ck_defined(a) Perl_ck_defined(aTHX_ a)
Expand Down
2 changes: 1 addition & 1 deletion ext/Opcode/Opcode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ This tag holds opcodes related to loading modules and getting information
about calling environment and args.
require dofile
caller runcv
caller runcv classname
=item :still_to_be_decided
Expand Down
4 changes: 2 additions & 2 deletions ext/Pod-Functions/t/Functions.t
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ Keywords related to Perl modules:
do, import, no, package, require, use
Keywords related to classes and object-orientation:
bless, class, dbmclose, dbmopen, field, method, package,
ref, tie, tied, untie, use
__CLASS__, bless, class, dbmclose, dbmopen, field, method,
package, ref, tie, tied, untie, use
Low-level socket functions:
accept, bind, connect, getpeername, getsockname,
Expand Down
23 changes: 19 additions & 4 deletions keywords.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f0de3fe

Please sign in to comment.