Skip to content

Commit 75759c5

Browse files
committed
Add more notes about what hooks will perhaps be, just to put these attributes into more context
1 parent 9516647 commit 75759c5

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

ppcs/ppcTODO-attributes-v2.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,49 @@ void apply_attribute_CallMeOnce(pTHX_ SV *target, SV *attrvalue)
105105
}
106106
```
107107
108-
As there is no interesting result returned from the attribute callback function, it must perform whatever work it needs to implement the requested behaviour purely as a side-effect of running it. While a few built-in attributes can be implemented perhaps by adjusting SV flags (such as `:lvalue` simply calling `CvLVALUE_on(cv)`), the majority of interesting use-cases would need to apply some form of extension to the target entity, such as Magic or the newly-proposed "Hooks" mechanism. These hooks will be described in a separate PPC document.
108+
As there is no interesting result returned from the attribute callback function, it must perform whatever work it needs to implement the requested behaviour purely as a side-effect of running it. While a few built-in attributes can be implemented perhaps by adjusting SV flags (such as `:lvalue` simply calling `CvLVALUE_on(cv)`), the majority of interesting use-cases would need to apply some form of extension to the target entity, such as Magic or the newly-proposed "Hooks" mechanism.
109+
110+
### An Aside On Hooks
111+
112+
While hooks will be described in a separate PPC document, it may be useful to provide a brief outline here in order to better evaluate how this proposal would interact with them. This section is specifically not considered part of this specification, but simply gives a high-level overview of the approximate shape these hooks will likely take.
113+
114+
Hooks are an evolution of (or perhaps a replacement of) the existing mechanism that Perl core currently calls "magic". Magic is defined by a set of trigger functions that can be attached to an SV, that provide additional behaviour and associate value storage to certain activities that happen to the SV it is attached to. Hooks will do the same thing.
115+
116+
The current "magic" mechanism in Perl defines a few trigger functions related to book-keeping the magic mechanism itself (the `clear` and `free` actions), as well as actions related to scalar variables (the `set` and `get` actions, as well as the rarely-used `len`). Magic does not define any actions related to other kinds of variables (arrays, hashes, globs), nor does it provide actions relating to values (such as when they are copied), or to various life-cycle management of subroutines (stored in `SVt_PVCVs`). These are the limitations that "hooks" attempts to overcome.
117+
118+
Hooks will be defined by a similar structure to magic, though having more fields relating to other lifecycle activities. Likely how this will work is that all hooks would start with a standard structure that has a field identifying what kind of hook this particular structure is, as well as the fields common to all such hooks:
119+
120+
```c
121+
struct HookFunctionsCommon {
122+
U32 ver; /* identifies an API version for flexibility */
123+
U32 flags; /* contains some flags indicating what specific shape of hook this is */
124+
void (*free)(pTHX_ SV *sv, Hook *hk);
125+
void (*clear)(pTHX_ SV *sv, Hook *hk);
126+
};
127+
```
128+
129+
Thereafter, specific sets of hook function structures can be defined for various situations, such as viral value-attached hooks, or hooks on different kinds of variables. Two such examples:
130+
131+
```c
132+
struct HookFunctionsValue {
133+
HOOK_COMMON_FIELDS;
134+
/* for copying this scalar value-shaped hook from one SV to the next */
135+
void (*copy)(pTHX_ SV *osv, SV *nsv, Hook *hk);
136+
};
137+
138+
struct HookFunctionsSubroutine {
139+
HOOK_COMMON_FIELDS;
140+
/* to handle various stages of parsing the subroutine */
141+
void (*pre_blockstart)(pTHX_ SV *sv, Hook *hk);
142+
void (*post_blockend)(pTHX_ SV *sv, Hook *hk);
143+
...
144+
/* to handle calls to this subroutine */
145+
OP *(*callchecker)(pTHX_ SV *sv, OP *o, Hook *hk);
146+
...
147+
};
148+
```
149+
150+
This all said, it is still entirely undecided whether this new mechanism will be named "hooks" and replace magic, or simply be a next iteration of the existing "magic" concept and extend from it. Those are discussions for a different PPC document.
109151

110152
## Backwards Compatibility
111153

0 commit comments

Comments
 (0)