In perl.git, the branch smueller/hash_vtable_make_hash has been updated <http://perl5.git.perl.org/perl.git/commitdiff/238d34d64798510221207797a8bce63565b01df9?hp=21241ff4cd37bb2b8b324be6b3a2c1122e911a8c>
- Log ----------------------------------------------------------------- commit 238d34d64798510221207797a8bce63565b01df9 Author: Steffen Mueller <[email protected]> Date: Mon Jan 30 12:40:11 2017 +0100 Hash vtables: Remove debugging code M ext/Hash-Pluggable/Pluggable.xs commit bba1caafba704597defbd446c89ef5cf25619c1d Author: Steffen Mueller <[email protected]> Date: Mon Jan 30 12:39:42 2017 +0100 Hash vtables: Hobo-documentation in the sources At least explain what this does in a comment for future me. :) M ext/Hash-Pluggable/Pluggable.xs commit 4e2ffb3273173af492a2eb385d47e58a5f687f97 Author: Steffen Mueller <[email protected]> Date: Mon Jan 30 12:38:55 2017 +0100 Hash vtables: croak on invalid vtable names M ext/Hash-Pluggable/Pluggable.xs commit 0a79f10bae6e7b421222aad80a9286ff95e45747 Author: Steffen Mueller <[email protected]> Date: Mon Jan 30 12:38:07 2017 +0100 Hash vtables: Eschew mortal copy I don't think it's necessary here as we're not looking to co-opt it into a hash key. M ext/Hash-Pluggable/Pluggable.xs ----------------------------------------------------------------------- Summary of changes: ext/Hash-Pluggable/Pluggable.xs | 44 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/ext/Hash-Pluggable/Pluggable.xs b/ext/Hash-Pluggable/Pluggable.xs index ccb5d5c300..77411e61b1 100644 --- a/ext/Hash-Pluggable/Pluggable.xs +++ b/ext/Hash-Pluggable/Pluggable.xs @@ -4,6 +4,39 @@ #include "perl.h" #include "XSUB.h" +/* In lieu of proper docs, let's outline what this is about: + * + * This module exposes the hash vtable mechanism to Perl insofar + * that it allows Perl programs to create hashes with alternate + * vtables/implementations. + * + * To do so, this module exports a new keyword "make_hash" (better + * naming suggestions welcome) whose first parameter indicates + * the type of hash implementation (which vtable) to use. The + * remaining parameters are used the same way as you'd initialize + * an anonymous hash with {}. + * + * The hash vtables is determined from the first parameter to + * make_hash by looking it up in a global registry. Non-existent + * vtable names cause an exception to be thrown. + * Said global registry is simply the hash + * %Hash::Pluggable::VtableRegistry + * which contains "name" => vtable pointer mappings. + * It should generally only be accessed directly from XS extensions + * which implement vtables rather than from Perl code directly. + * + * It seems like good practice to use vtable names of the form + * "My::Module/fancy_vtable" + * to avoid potential collisions. But this might prove too clunky + * in practice? + * + * At a future time, the implementation might be changed such that + * the vtable pointer is looked up at compile time at least for + * cases of constant strings used for vtable names in make_hash + * calls. + */ + + /* For chaining the keyword plugin */ int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **); /* Our anonhash-alike custom OP */ @@ -28,12 +61,17 @@ pp_pluggable_anonhash(pTHX) /* TODO This logic should only be executed in the cases that the vtable * couldn't be resolved statically. But that's not implemented yet. */ MARK++; - hash_type_sv = SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK; + hash_type_sv = *MARK; vtable_registry = get_hv("Hash::Pluggable::VtableRegistry", GV_ADD); he = hv_fetch_ent(vtable_registry, hash_type_sv, 0, 0); if (he) { vtbl = INT2PTR(HV_VTBL *, SvIV(HeVAL(he))); } + else { + /* Couldn't look up vtable: Barf! */ + Perl_croak(aTHX_ "No hash vtable for vtable name '%s'", + SvPV_nolen_const(hash_type_sv)); + } hv = newHV_type(vtbl); retval = sv_2mortal( newRV_noinc(MUTABLE_SV(hv)) ); @@ -124,10 +162,6 @@ parse_make_hash_keyword(pTHX_ OP **op_ptr) anonhash_op->op_ppaddr = pp_pluggable_anonhash; - - printf("----------------------------\n"); - op_dump(anonhash_op); - *op_ptr = anonhash_op; } -- Perl5 Master Repository
