| 1 |
|
| /* You may distribute under the terms of either the GNU General Public License |
| 2 |
|
| * or the Artistic License (the same terms as Perl itself) |
| 3 |
|
| * |
| 4 |
|
| * (C) Paul Evans, 2021-2022 -- leonerd@leonerd.org.uk |
| 5 |
|
| */ |
| 6 |
|
| #define PERL_NO_GET_CONTEXT |
| 7 |
|
| |
| 8 |
|
| #include "EXTERN.h" |
| 9 |
|
| #include "perl.h" |
| 10 |
|
| #include "XSUB.h" |
| 11 |
|
| |
| 12 |
|
| #include "object_pad.h" |
| 13 |
|
| |
| 14 |
|
| struct Data { |
| 15 |
|
| unsigned int is_weak : 1; |
| 16 |
|
| SV *fieldname; |
| 17 |
|
| SV *classname; |
| 18 |
|
| }; |
| 19 |
|
| |
| 20 |
6 |
| static int magic_set(pTHX_ SV *sv, MAGIC *mg) |
| 21 |
|
| { |
| 22 |
6 |
| struct Data *data = (struct Data *)mg->mg_ptr; |
| 23 |
6 |
| SV *savesv = mg->mg_obj; |
| 24 |
|
| |
| 25 |
6 |
| bool ok = sv_derived_from_sv(sv, data->classname, 0); |
| 26 |
|
| |
| 27 |
6 |
▶ | if(ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ok) { |
3 |
3 |
6 |
|
| 28 |
3 |
| sv_setsv(savesv, sv); |
| 29 |
3 |
▶ | if(data->is_weak) |
Branch: 1 branch
| Branch | True | False | Total |
| if(data->is_weak) |
1 |
2 |
3 |
|
| 30 |
1 |
| sv_rvweaken(savesv); |
| 31 |
3 |
| return 1; |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
|
| /* Restore last known-good value */ |
| 35 |
3 |
| sv_setsv_nomg(sv, savesv); |
| 36 |
3 |
▶ | if(data->is_weak) |
Branch: 1 branch
| Branch | True | False | Total |
| if(data->is_weak) |
1 |
2 |
3 |
|
| 37 |
1 |
| sv_rvweaken(sv); |
| 38 |
|
| |
| 39 |
3 |
| croak("Field %" SVf " requires an object of type %" SVf, |
| 40 |
|
| SVfARG(data->fieldname), SVfARG(data->classname)); |
| 41 |
|
| |
| 42 |
|
| return 1; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| static const MGVTBL vtbl = { |
| 46 |
|
| .svt_set = &magic_set, |
| 47 |
|
| }; |
| 48 |
|
| |
| 49 |
3 |
| static bool isa_apply(pTHX_ FieldMeta *fieldmeta, SV *value, SV **attrdata_ptr, void *_funcdata) |
| 50 |
|
| { |
| 51 |
|
| struct Data *data; |
| 52 |
3 |
| Newx(data, 1, struct Data); |
| 53 |
|
| |
| 54 |
3 |
| data->is_weak = false; |
| 55 |
3 |
| data->fieldname = SvREFCNT_inc(mop_field_get_name(fieldmeta)); |
| 56 |
3 |
| data->classname = SvREFCNT_inc(value); |
| 57 |
|
| |
| 58 |
3 |
| *attrdata_ptr = (SV *)data; |
| 59 |
|
| |
| 60 |
3 |
| return TRUE; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
3 |
| static void isa_seal(pTHX_ FieldMeta *fieldmeta, SV *attrdata, void *_funcdata) |
| 64 |
|
| { |
| 65 |
3 |
| struct Data *data = (struct Data *)attrdata; |
| 66 |
|
| |
| 67 |
3 |
▶ | if(mop_field_get_attribute(fieldmeta, "weak")) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mop_field_get_attribute(fieldmeta, "weak")) |
1 |
2 |
3 |
|
| 68 |
1 |
| data->is_weak = true; |
| 69 |
3 |
| } |
| 70 |
|
| |
| 71 |
5 |
| static void isa_post_makefield(pTHX_ FieldMeta *fieldmeta, SV *attrdata, void *_funcdata, SV *field) |
| 72 |
|
| { |
| 73 |
5 |
| sv_magicext(field, newSV(0), PERL_MAGIC_ext, &vtbl, (char *)attrdata, 0); |
| 74 |
5 |
| } |
| 75 |
|
| |
| 76 |
|
| static const struct FieldHookFuncs isa_hooks = { |
| 77 |
|
| .ver = OBJECTPAD_ABIVERSION, |
| 78 |
|
| .flags = OBJECTPAD_FLAG_ATTR_MUST_VALUE, |
| 79 |
|
| .permit_hintkey = "Object::Pad::FieldAttr::Isa/Isa", |
| 80 |
|
| |
| 81 |
|
| .apply = &isa_apply, |
| 82 |
|
| .seal = &isa_seal, |
| 83 |
|
| .post_makefield = &isa_post_makefield, |
| 84 |
|
| }; |
| 85 |
|
| |
| 86 |
|
| MODULE = Object::Pad::FieldAttr::Isa PACKAGE = Object::Pad::FieldAttr::Isa |
| 87 |
|
| |
| 88 |
|
| BOOT: |
| 89 |
3 |
| register_field_attribute("Isa", &isa_hooks, NULL); |