Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 21e1d31

Browse files
committed
add hook_unload
1 parent a8eb651 commit 21e1d31

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

examples/hooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ function timer_cb ()
2525
return true; // return true to continue the timer
2626
}
2727

28+
function unload_cb ()
29+
{
30+
print("No don't kill me!");
31+
}
32+
2833
hook_command ('test', test_cb, 'there is no help for test =(');
2934
timer_hook = hook_timer (10000, timer_cb); // 10 sec
35+
hook_unload (unload_cb);

javascript.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ enum hook_type
5454
HOOK_PRINT,
5555
HOOK_SERVER,
5656
HOOK_TIMER,
57-
HOOK_UNHOOK
57+
HOOK_UNLOAD
5858
};
5959

6060
typedef struct
@@ -899,6 +899,24 @@ hjs_hooktimer (JSContext *context, unsigned argc, jsval *vp)
899899
return JS_TRUE;
900900
}
901901

902+
static JSBool
903+
hjs_hookunload (JSContext *context, unsigned argc, jsval *vp)
904+
{
905+
JSObject* funcobj;
906+
JSObject* userdata = NULL;
907+
script_hook* hook = new script_hook;
908+
js_script* script = hjs_script_find (context);
909+
910+
if (!JS_ConvertArguments (context, argc, JS_ARGV(context, vp), "o/o", &funcobj, &userdata))
911+
return JS_FALSE;
912+
913+
script->add_hook (hook, HOOK_UNLOAD, context, funcobj, userdata, NULL);
914+
915+
JS_SET_RVAL (context, vp, JSVAL_VOID);
916+
917+
return JS_TRUE;
918+
}
919+
902920
static JSBool
903921
hjs_unhook (JSContext *context, unsigned argc, jsval *vp)
904922
{
@@ -1067,7 +1085,7 @@ static JSFunctionSpec hexchat_functions[] = {
10671085
{"hook_server", hjs_hookserver, 4, JSPROP_READONLY|JSPROP_PERMANENT},
10681086
{"hook_timer", hjs_hooktimer, 3, JSPROP_READONLY|JSPROP_PERMANENT},
10691087
{"hook_print", hjs_hookprint, 5, JSPROP_READONLY|JSPROP_PERMANENT},
1070-
//{"hook_unhook", hjs_hookunhook, 3, JSPROP_READONLY|JSPROP_PERMANENT},
1088+
{"hook_unload", hjs_hookunload, 2, JSPROP_READONLY|JSPROP_PERMANENT},
10711089
{"unhook", hjs_unhook, 1, JSPROP_READONLY|JSPROP_PERMANENT},
10721090
{"get_list", hjs_getlist, 1, JSPROP_READONLY|JSPROP_PERMANENT},
10731091
{"find_context", hjs_findcontext, 2, JSPROP_READONLY|JSPROP_PERMANENT},
@@ -1184,7 +1202,19 @@ js_script::~js_script ()
11841202
{
11851203
for (script_hook* hook : hooks)
11861204
{
1187-
hexchat_unhook (ph, hook->hook);
1205+
if (hook->type == HOOK_UNLOAD)
1206+
{
1207+
JSFunction* fun = JS_ValueToFunction (hook->context, OBJECT_TO_JSVAL(hook->callback));
1208+
jsval argv[] = { OBJECT_TO_JSVAL(hook->userdata) };
1209+
jsval rval;
1210+
1211+
JS_CallFunction (hook->context, JS_GetGlobalForScopeChain (hook->context), fun, 1, argv, &rval);
1212+
}
1213+
else
1214+
{
1215+
hexchat_unhook (ph, hook->hook);
1216+
}
1217+
11881218
delete hook;
11891219
}
11901220

0 commit comments

Comments
 (0)