From ddd3023d5dc875432c74d845ce4591ad8528891f Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sun, 17 Dec 2017 22:03:47 +0000 Subject: [PATCH] Refactor default return values of callbacks of perl/python modules See #1424 --- modules/modperl/codegen.pl | 47 +++++++++------------------------- modules/modperl/functions.in | 16 ++++++------ modules/modperl/startup.pl | 7 +---- modules/modpython/codegen.pl | 15 ++++------- modules/modpython/functions.in | 18 ++++++------- 5 files changed, 35 insertions(+), 68 deletions(-) diff --git a/modules/modperl/codegen.pl b/modules/modperl/codegen.pl index 4a9caeae..7d25beea 100755 --- a/modules/modperl/codegen.pl +++ b/modules/modperl/codegen.pl @@ -45,13 +45,6 @@ print $out <<'EOF'; * Don't change it manually. * ***************************************************************************/ -/*#include "module.h" -#include "swigperlrun.h" -#include -#include -#include -#include "pstring.h"*/ - namespace { template struct SvToPtr { @@ -73,15 +66,8 @@ namespace { return static_cast(SvUV(sv)); } } -/* -#define PSTART dSP; I32 ax; int ret = 0; ENTER; SAVETMPS; PUSHMARK(SP) -#define PCALL(name) PUTBACK; ret = call_pv(name, G_EVAL|G_ARRAY); SPAGAIN; SP -= ret; ax = (SP - PL_stack_base) + 1 -#define PEND PUTBACK; FREETMPS; LEAVE -#define PUSH_STR(s) XPUSHs(PString(s).GetSV()) -#define PUSH_PTR(type, p) XPUSHs(SWIG_NewInstanceObj(const_cast(p), SWIG_TypeQuery(#type), SWIG_SHADOW)) -*/ #define PSTART_IDF(Func) PSTART; XPUSHs(GetPerlObj()); PUSH_STR(#Func) -#define PCALLMOD(Error, Success) PCALL("ZNC::Core::CallModFunc"); if (SvTRUE(ERRSV)) { DEBUG("Perl hook died with: " + PString(ERRSV)); Error; } else { Success; } PEND +#define PCALLMOD(Error, Success) PCALL("ZNC::Core::CallModFunc"); if (SvTRUE(ERRSV)) { DEBUG("Perl hook died with: " + PString(ERRSV)); Error; } else { if (!SvIV(ST(0))) { Error; } Success; } PEND EOF @@ -89,30 +75,18 @@ while (<$in>) { my ($type, $name, $args, $default) = /(\S+)\s+(\w+)\((.*)\)(?:=(\w+))?/ or next; $type =~ s/(EModRet)/CModule::$1/; $type =~ s/^\s*(.*?)\s*$/$1/; - unless (defined $default) { - given ($type) { - when ('bool') { $default = 'true' } - when ('CModule::EModRet') { $default = 'CONTINUE' } - when ('CString') { $default = '""' } - when (/\*$/) { $default = "($type)nullptr" } - } - } my @arg = map { my ($t, $v) = /^\s*(.*\W)\s*(\w+)\s*$/; $t =~ s/^\s*(.*?)\s*$/$1/; my ($tt, $tm) = $t =~ /^(.*?)\s*?(\*|&)?$/; {type=>$t, var=>$v, base=>$tt, mod=>$tm//''} } split /,/, $args; - say $out "$type CPerlModule::$name($args) {"; - say $out "\t$type result = $default;" if $type ne 'void'; - say $out "\tPSTART_IDF($name);"; - given ($type) { - when ('CString') { print $out "\tPUSH_STR($default);" } - when (/\*$/) { my $t=$type; $t=~s/^const//; print $out "\tPUSH_PTR($t, $default);" } - when ('void') { print $out "\tmXPUSHi(0);" } - default { print $out "\tmXPUSHi(static_cast($default));" } + unless (defined $default) { + $default = "CModule::$name(" . (join ', ', map { $_->{var} } @arg) . ")"; } - say $out " // Default value"; + say $out "$type CPerlModule::$name($args) {"; + say $out "\t$type result{};" if $type ne 'void'; + say $out "\tPSTART_IDF($name);"; for my $a (@arg) { given ($a->{type}) { when (/(vector\s*<\s*(.*)\*\s*>)/) { @@ -131,9 +105,12 @@ while (<$in>) { default { say $out "\tmXPUSHi($a->{var});" } } } - say $out "\tPCALLMOD(,"; - my $x = 0; - say $out "\t\tresult = ".sv($type)."(ST(0));" if $type ne 'void'; + say $out "\tPCALLMOD("; + print $out "\t\t"; + print $out "result = " if $type ne 'void'; + say $out "$default;,"; + my $x = 1; + say $out "\t\tresult = ".sv($type)."(ST(1));" if $type ne 'void'; for my $a (@arg) { $x++; say $out "\t\t$a->{var} = PString(ST($x));" if $a->{base} eq 'CString' && $a->{mod} eq '&'; diff --git a/modules/modperl/functions.in b/modules/modperl/functions.in index 9aa2e3a0..9fca687a 100644 --- a/modules/modperl/functions.in +++ b/modules/modperl/functions.in @@ -1,10 +1,10 @@ -bool OnBoot()=true -bool WebRequiresLogin()=true -bool WebRequiresAdmin()=false +bool OnBoot() +bool WebRequiresLogin() +bool WebRequiresAdmin() CString GetWebMenuTitle() -bool OnWebPreRequest(CWebSock& WebSock, const CString& sPageName)=false -bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false -VWebSubPages* _GetSubPages() +bool OnWebPreRequest(CWebSock& WebSock, const CString& sPageName) +bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) +VWebSubPages* _GetSubPages()=nullptr void OnPreRehash() void OnPostRehash() void OnIRCDisconnected() @@ -59,10 +59,10 @@ EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) EModRet OnPrivNotice(CNick& Nick, CString& sMessage) EModRet OnChanNotice(CNick& Nick, CChan& Channel, CString& sMessage) EModRet OnTopic(CNick& Nick, CChan& Channel, CString& sTopic) -bool OnServerCapAvailable(const CString& sCap)=false +bool OnServerCapAvailable(const CString& sCap) void OnServerCapResult(const CString& sCap, bool bSuccess) EModRet OnTimerAutoJoin(CChan& Channel) -bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false +bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) EModRet OnAddNetwork(CIRCNetwork& Network, CString& sErrorRet) EModRet OnDeleteNetwork(CIRCNetwork& Network) EModRet OnSendToClient(CString& sLine, CClient& Client) diff --git a/modules/modperl/startup.pl b/modules/modperl/startup.pl index 6aa2023b..4e5f7efb 100644 --- a/modules/modperl/startup.pl +++ b/modules/modperl/startup.pl @@ -221,14 +221,9 @@ sub ModInfoByPath { sub CallModFunc { my $pmod = shift; my $func = shift; - my $default = shift; my @arg = @_; my $res = $pmod->$func(@arg); -# print "Returned from $func(@_): $res, (@arg)\n"; - unless (defined $res) { - $res = $default if defined $default; - } - ($res, @arg) + (defined $res, $res, @arg) } sub CallTimer { diff --git a/modules/modpython/codegen.pl b/modules/modpython/codegen.pl index 898a63a8..a6dd607c 100755 --- a/modules/modpython/codegen.pl +++ b/modules/modpython/codegen.pl @@ -248,14 +248,6 @@ while (<$in>) { my ($type, $name, $args, $default) = /(\S+)\s+(\w+)\((.*)\)(?:=(\w+))?/ or next; $type =~ s/(EModRet)/CModule::$1/; $type =~ s/^\s*(.*?)\s*$/$1/; - unless (defined $default) { - given ($type) { - when ('bool') { $default = 'true' } - when ('CModule::EModRet') { $default = 'CONTINUE' } - when ('CString') { $default = '""' } - when (/\*$/) { $default = "($type)nullptr" } - } - } my @arg = map { my ($t, $v) = /^\s*(.*\W)\s*(\w+)\s*$/; $t =~ s/^\s*(.*?)\s*$/$1/; @@ -263,10 +255,13 @@ while (<$in>) { {type=>$t, var=>$v, base=>$tb, mod=>$tm//'', pyvar=>"pyArg_$v", error=>"can't convert parameter '$v' to PyObject"} } split /,/, $args; + unless (defined $default) { + $default = "CModule::$name(" . (join ', ', map { $_->{var} } @arg) . ")"; + } + unshift @arg, {type=>'$func$', var=>"", base=>"", mod=>"", pyvar=>"pyName", error=>"can't convert string '$name' to PyObject"}; my $cleanup = ''; - $default = '' if $type eq 'void'; say $out "$type CPyModule::$name($args) {"; for my $a (@arg) { @@ -413,7 +408,7 @@ while (<$in>) { say $out "\t\t} else { result = (CModule::EModRet)x; }"; } when ('bool') { - say $out "\t\tint x = PyObject_IsTrue(pyRes);"; + say $out "\t\tint x = PyObject_IsTrue(pyRes);"; say $out "\t\tif (-1 == x) {"; say $out "\t\t\tCString sPyErr = m_pModPython->GetPyExceptionStr();"; say $out "\t\t\tDEBUG".'("modpython: " << (GetUser() ? GetUser()->GetUserName() : CString("")) << "/" << GetModName() << '."\"/$name was expected to return EModRet but: \" << sPyErr);"; diff --git a/modules/modpython/functions.in b/modules/modpython/functions.in index 49ed60cf..38566793 100644 --- a/modules/modpython/functions.in +++ b/modules/modpython/functions.in @@ -1,10 +1,10 @@ -bool OnBoot()=true -bool WebRequiresLogin()=true -bool WebRequiresAdmin()=false +bool OnBoot() +bool WebRequiresLogin() +bool WebRequiresAdmin() CString GetWebMenuTitle() -bool OnWebPreRequest(CWebSock& WebSock, const CString& sPageName)=false -bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false -VWebSubPages* _GetSubPages() +bool OnWebPreRequest(CWebSock& WebSock, const CString& sPageName) +bool OnWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) +VWebSubPages* _GetSubPages()=nullptr void OnPreRehash() void OnPostRehash() void OnIRCDisconnected() @@ -59,10 +59,10 @@ EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) EModRet OnPrivNotice(CNick& Nick, CString& sMessage) EModRet OnChanNotice(CNick& Nick, CChan& Channel, CString& sMessage) EModRet OnTopic(CNick& Nick, CChan& Channel, CString& sTopic) -bool OnServerCapAvailable(const CString& sCap)=false +bool OnServerCapAvailable(const CString& sCap) void OnServerCapResult(const CString& sCap, bool bSuccess) EModRet OnTimerAutoJoin(CChan& Channel) -bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false +bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl) EModRet OnAddNetwork(CIRCNetwork& Network, CString& sErrorRet) EModRet OnDeleteNetwork(CIRCNetwork& Network) EModRet OnSendToClient(CString& sLine, CClient& Client) @@ -106,7 +106,7 @@ void OnClientConnect(CZNCSock* pSock, const CString& sHost, unsigned short uPort void OnFailedLogin(const CString& sUsername, const CString& sRemoteIP) EModRet OnUnknownUserRaw(CClient* pClient, CString& sLine) EModRet OnUnknownUserRawMessage(CMessage& Message) -bool IsClientCapSupported(CClient* pClient, const CString& sCap, bool bState)=false +bool IsClientCapSupported(CClient* pClient, const CString& sCap, bool bState) void OnClientCapRequest(CClient* pClient, const CString& sCap, bool bState) EModRet OnModuleLoading(const CString& sModName, const CString& sArgs, CModInfo::EModuleType eType, bool& bSuccess, CString& sRetMsg) EModRet OnModuleUnloading(CModule* pModule, bool& bSuccess, CString& sRetMsg)