@@ -35,18 +35,6 @@ local function is_callable(obj)
35
35
return false
36
36
end
37
37
38
- local function is_callable (obj )
39
- local t_obj = type (obj )
40
- if t_obj == ' function' then
41
- return true
42
- end
43
- if t_obj == ' table' then
44
- local mt = getmetatable (obj )
45
- return (type (mt ) == ' table' and type (mt .__call ) == ' function' )
46
- end
47
- return false
48
- end
49
-
50
38
local function uri_escape (str )
51
39
local res = {}
52
40
if type (str ) == ' table' then
@@ -726,15 +714,15 @@ local function match_route(self, method, route)
726
714
end
727
715
728
716
local function set_helper (self , name , sub )
729
- if sub == nil or type (sub ) == ' function ' then
717
+ if sub == nil or is_callable (sub ) then
730
718
self .helpers [ name ] = sub
731
719
return self
732
720
end
733
721
errorf (" Wrong type for helper function: %s" , type (sub ))
734
722
end
735
723
736
724
local function set_hook (self , name , sub )
737
- if sub == nil or type (sub ) == ' function ' then
725
+ if sub == nil or is_callable (sub ) then
738
726
self .hooks [ name ] = sub
739
727
return self
740
728
end
@@ -778,7 +766,7 @@ local function ctx_action(tx)
778
766
local action = tx .endpoint .action
779
767
if tx .httpd .options .cache_controllers then
780
768
if tx .httpd .cache [ ctx ] ~= nil then
781
- if type (tx .httpd .cache [ ctx ][ action ]) ~= ' function ' then
769
+ if is_callable (tx .httpd .cache [ ctx ][ action ]) then
782
770
errorf (" Controller '%s' doesn't contain function '%s'" ,
783
771
ctx , action )
784
772
end
@@ -807,7 +795,7 @@ local function ctx_action(tx)
807
795
errorf (" require '%s' didn't return table" , ctx )
808
796
end
809
797
810
- if type (mod [ action ]) ~= ' function ' then
798
+ if is_callable (mod [ action ]) then
811
799
errorf (" Controller '%s' doesn't contain function '%s'" , ctx , action )
812
800
end
813
801
@@ -849,8 +837,8 @@ local function add_route(self, opts, sub)
849
837
850
838
sub = ctx_action
851
839
852
- elseif type (sub ) ~= ' function ' then
853
- errorf (" wrong argument: expected function , but received %s" ,
840
+ elseif not is_callable (sub ) then
841
+ errorf (" wrong argument: expected callable , but received %s" ,
854
842
type (sub ))
855
843
end
856
844
@@ -1081,7 +1069,7 @@ local function httpd_http11_handler(session)
1081
1069
if type (body ) == ' string' then
1082
1070
-- Plain string
1083
1071
hdrs [' content-length' ] = # body
1084
- elseif type (body ) == ' function ' then
1072
+ elseif is_callable (body ) then
1085
1073
-- Generating function
1086
1074
gen = body
1087
1075
hdrs [' transfer-encoding' ] = ' chunked'
0 commit comments