@@ -12,16 +12,19 @@ use crate::host::v8::error::TypeError;
1212use crate :: host:: v8:: from_value:: cast;
1313use crate :: host:: v8:: string:: StringConst ;
1414
15- pub ( super ) fn get_hook_function < ' s > (
16- scope : & mut PinScope < ' s , ' _ > ,
15+ /// Returns the hook function `name` on `hooks_obj`.
16+ pub ( super ) fn get_hook_function < ' scope > (
17+ scope : & mut PinScope < ' scope , ' _ > ,
1718 hooks_obj : Local < ' _ , Object > ,
1819 name : & ' static StringConst ,
19- ) -> ExcResult < Local < ' s , Function > > {
20+ ) -> ExcResult < Local < ' scope , Function > > {
2021 let key = name. string ( scope) ;
2122 let object = property ( scope, hooks_obj, key) ?;
2223 cast ! ( scope, object, Function , "module function hook `{}`" , name. as_str( ) ) . map_err ( |e| e. throw ( scope) )
2324}
2425
26+ /// Registers all the module function `hooks`
27+ /// and sets the given `AbiVersion` to `abi`.
2528pub ( super ) fn set_hook_slots (
2629 scope : & mut PinScope < ' _ , ' _ > ,
2730 abi : AbiVersion ,
@@ -34,7 +37,7 @@ pub(super) fn set_hook_slots(
3437 for & ( hook, func) in hooks {
3538 hooks_info
3639 . register ( hook, abi)
37- . map_err ( |_| TypeError ( "cannot call register_hooks multiple times" ) . throw ( scope) ) ?;
40+ . map_err ( |_| TypeError ( "cannot call ` register_hooks` multiple times" ) . throw ( scope) ) ?;
3841 ctx. set_embedder_data ( hook. to_slot_index ( ) , func. into ( ) ) ;
3942 }
4043 Ok ( ( ) )
@@ -47,7 +50,8 @@ pub(in crate::host::v8) enum ModuleHook {
4750}
4851
4952impl ModuleHook {
50- /// Get the `v8::Context::{get,set}_embedder_data` slot that holds this hook.
53+ /// Returns the index for the slot that holds the module function hook.
54+ /// The index is passed to `v8::Context::{get,set}_embedder_data`.
5155 fn to_slot_index ( self ) -> i32 {
5256 match self {
5357 ModuleHook :: DescribeModule => 20 ,
@@ -56,13 +60,17 @@ impl ModuleHook {
5660 }
5761}
5862
63+ /// Holds the `AbiVersion` used by the module
64+ /// and the module hooks registered by the module
65+ /// for that version.
5966#[ derive( Default ) ]
6067struct HooksInfo {
6168 abi : OnceCell < AbiVersion > ,
6269 registered : EnumMap < ModuleHook , OnceCell < ( ) > > ,
6370}
6471
6572impl HooksInfo {
73+ /// Returns, and possibly creates, the [`HooksInfo`] stored in `ctx`.
6674 fn get_or_create ( ctx : & Context ) -> Rc < Self > {
6775 ctx. get_slot ( ) . unwrap_or_else ( || {
6876 let this = Rc :: < Self > :: default ( ) ;
@@ -78,16 +86,18 @@ impl HooksInfo {
7886 self . registered [ hook] . set ( ( ) )
7987 }
8088
89+ /// Returns the `AbiVersion` for the given `hook`, if any.
8190 fn get ( & self , hook : ModuleHook ) -> Option < AbiVersion > {
8291 self . registered [ hook] . get ( ) . and ( self . abi . get ( ) . copied ( ) )
8392 }
8493}
8594
8695#[ derive( Copy , Clone ) ]
87- pub ( in crate :: host:: v8) struct HookFunction < ' s > ( pub AbiVersion , pub Local < ' s , Function > ) ;
96+ /// The actual callable module hook function and its abi version.
97+ pub ( in super :: super ) struct HookFunction < ' scope > ( pub AbiVersion , pub Local < ' scope , Function > ) ;
8898
8999/// Returns the hook function previously registered in [`register_hooks`].
90- pub ( in crate :: host :: v8 ) fn get_hook < ' scope > (
100+ pub ( in super :: super ) fn get_hook < ' scope > (
91101 scope : & mut PinScope < ' scope , ' _ > ,
92102 hook : ModuleHook ,
93103) -> Option < HookFunction < ' scope > > {
0 commit comments