Skip to content

Commit a5ca2f3

Browse files
author
Martijn Otto
committed
Merged and updated pull request #303
1 parent e7807cf commit a5ca2f3

File tree

2 files changed

+14
-47
lines changed

2 files changed

+14
-47
lines changed

zend/callable.cpp

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313
*/
1414
namespace Php {
1515

16-
/**
17-
* Map function names to their implementation
18-
*
19-
* This is braindead, there should be a way to get this information
20-
* from the "This" zval in the execute_data, I just can't find it
21-
* @todo Find a better way for this
22-
* @var std::map<std::string, Callable*>
23-
*/
24-
static std::map<std::string, Callable*> callables;
25-
2616
/**
2717
* Function that is called by the Zend engine every time that a function gets called
2818
* @param ht
@@ -34,34 +24,21 @@ static std::map<std::string, Callable*> callables;
3424
*/
3525
void Callable::invoke(INTERNAL_FUNCTION_PARAMETERS)
3626
{
37-
// find the function name
38-
const char *name = get_active_function_name();
39-
const char *classname = get_active_class_name(nullptr);
27+
uint32_t argc = EX(func)->common.num_args;
28+
zend_arg_info* info = EX(func)->common.arg_info;
4029

41-
// the callable we are retrieving
42-
Callable *callable = nullptr;
30+
// Sanity check
31+
assert(info[argc].class_name != nullptr && info[argc].name == nullptr);
4332

44-
// are we invoking a member function?
45-
if (classname && classname[0])
46-
{
47-
// construct the full name to search for
48-
auto fullname = std::string{ classname } + "::" + name;
49-
50-
// and find the callable in the map
51-
callable = callables.find(fullname)->second;
52-
}
53-
else
54-
{
55-
// retrieve the callable from the map without mangling
56-
callable = callables.find(name)->second;
57-
}
33+
// the callable we are retrieving
34+
Callable *callable = reinterpret_cast<Callable*>(info[argc].class_name);
5835

5936
// check if sufficient parameters were passed (for some reason this check
6037
// is not done by Zend, so we do it here ourselves)
6138
if (ZEND_NUM_ARGS() < callable->_required)
6239
{
6340
// PHP itself only generates a warning when this happens, so we do the same too
64-
Php::warning << name << "() expects at least " << callable->_required << " parameter(s), " << ZEND_NUM_ARGS() << " given" << std::flush;
41+
Php::warning << get_active_function_name() << "() expects at least " << callable->_required << " parameter(s), " << ZEND_NUM_ARGS() << " given" << std::flush;
6542

6643
// and we return null
6744
RETURN_NULL();
@@ -109,22 +86,8 @@ void Callable::initialize(zend_function_entry *entry, const char *classname, int
10986
}
11087
else
11188
{
112-
// if we have a classname we have to combine the two for a unique name
113-
// otherwise similar functions - like __construct - will overwrite functions
114-
// declared before
115-
if (classname)
116-
{
117-
// build the unique name
118-
auto name = std::string{ classname } + "::" + _name;
119-
120-
// add it to the map
121-
callables[name] = const_cast<Callable*>(this);
122-
}
123-
else
124-
{
125-
// no need to mangle the name
126-
callables[_name] = const_cast<Callable*>(this);
127-
}
89+
// install ourselves in the extra argument
90+
_argv[_argc + 1].class_name = reinterpret_cast<const char*>(this);
12891

12992
// we use our own invoke method, which does a lookup
13093
// in the map we just installed ourselves in

zend/callable.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Callable
3535
_callback(callback),
3636
_name(name),
3737
_argc(arguments.size()),
38-
_argv(new zend_internal_arg_info[_argc + 1])
38+
_argv(new zend_internal_arg_info[_argc + 2])
3939
{
4040
// the first record is initialized with information about the function,
4141
// so we skip that here
@@ -50,6 +50,10 @@ class Callable
5050
// fill the arg info
5151
fill(&_argv[i++], argument);
5252
}
53+
54+
// initialize the extra argument
55+
_argv[i].class_name = nullptr;
56+
_argv[i].name = nullptr;
5357
}
5458

5559
/**

0 commit comments

Comments
 (0)