compuphase / pawn

Pawn is a quick and small scripting language that requires few resources.

Home Page:http://www.compuphase.com/pawn/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ALS7 hooking method

tagartagar opened this issue · comments

In Pawn 3.2 it was possible to "hook" a function using a compiler bug. Preprocessor handled "#if defined" statements in a second-round of preprocessing, read more here.

Now it's not working, because the bug was fixed. Please, return that behaviour, because ALS hooking is a critical functionality.

ALS7 hooking method

It's called ALS4, not 7.

In Pawn 3.2 it was possible to "hook" a function using a compiler bug.

Besides 3.2 it works in Pawn 4.0 as well. The behavior has changed in commit 6de1822 to be precise, so ALS4 doesn't work in the development version, but the 4.0 release is unaffected.

Anyway, here's an example of how it worked before 6de1822:

#include <console>


main()
{
    print("This is a message from the main() hook #1.\n");
#if defined hook1_main
    hook1_main();
#endif
    return 1;
}
#if defined _ALS_main
    #undef main
#else
    #define _ALS_main
#endif
#define main hook1_main
forward hook1_main();

main()
{
    print("This is a message from the main() hook #2.\n");
#if defined hook2_main
    hook2_main();
#endif
    return 1;
}
#if defined _ALS_main
    #undef main
#else
    #define _ALS_main
#endif
#define main hook2_main
forward hook2_main();

main()
{
    print("This is a message from the main() hook #3.\n");
#if defined hook3_main
    hook3_main();
#endif
    return 1;
}
#if defined _ALS_main
    #undef main
#else
    #define _ALS_main
#endif
#define main hook3_main
forward hook3_main();


main()
{
    print("main()\n");
}

Output:

This is a message from the main() hook #1.
This is a message from the main() hook #2.
This is a message from the main() hook #3.
main()

After that breaking commit the above example doesn't work. The compiler ends up in an endless loop repeatedly printing the following messages:

test.p(39) : warning 208: function "hook3_main" is used before definition, a forward declaration is recommended
test.p(23) : warning 208: function "hook2_main" is used before definition, a forward declaration is recommended
test.p(7) : warning 208: function "hook1_main" is used before definition, a forward declaration is recommended
test.p(39) : warning 208: function "hook3_main" is used before definition, a forward declaration is recommended
test.p(23) : warning 208: function "hook2_main" is used before definition, a forward declaration is recommended
test.p(7) : warning 208: function "hook1_main" is used before definition, a forward declaration is recommended
...

It would be great if the old behavior could have been brought back.

Corrected in commit 9895412.
The warnings for the three hook functions are still being output, but the compiler does not fall in an endless loop anymore.