summaryrefslogtreecommitdiff
path: root/zend/fastcall.cpp
blob: 3ecd5987d33d79a5624de798df081f5db5b5bb0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 *  fastcall.cpp
 *
 *  This file holds some PHP functions implementation in C directly.
 *
 */

#include "includes.h"

namespace Php {

    Value eval(const std::string &phpCode) {
        // we need the tsrm_ls variable
        TSRMLS_FETCH();

        // the current exception
        zval* oldException = EG(exception);

        // the return zval
        zval* retval = nullptr;
        if (zend_eval_stringl_ex((char *)phpCode.c_str(), (int32_t)phpCode.length(), retval, (char *)"", 1 TSRMLS_CC) != SUCCESS)
        {
            // throw an exception, php couldn't evaluate code
            throw Exception("PHP couldn't evaluate: " + phpCode);

            // unreachable, but let's return at least something to prevent compiler warnings
            return nullptr;
        }
        else
        {
            // was an exception thrown inside the function? In that case we throw a C++ new exception
            // to give the C++ code the chance to catch it
            if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception) TSRMLS_CC);

            // no (additional) exception was thrown
            return retval ? Value(retval) : nullptr;
        }
    }

}