summaryrefslogtreecommitdiff
path: root/include/script.h
blob: 860b9bc6399133392bb555ceb8e38fd24c50a114 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 *  Script.h
 *
 *  Class that can be used to evaluate a PHP script in the current PHP context.
 *
 *  The difference between directly calling eval() is that the script object
 *  will first evaluate the string, and then it can be executed multiple times.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */

/**
 *  Forward declarations
 */
struct _zend_op_array;

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Forward declarations
 */
class Opcodes;

/**
 *  Class definition
 */
class PHPCPP_EXPORT Script
{
public:
    /**
     *  Constructor
     *
     *  The constructor will not throw any exceptions, even when invalid
     *  PHP code is passed to it that can not be evaluated. You should call
     *  the valid() to find out if the script was valid (could be parsed).
     *
     *  @param  name        Name of the PHP script
     *  @param  source      PHP source code to be evaluated
     *  @param  size        Length of the source code
     */
    Script(const char *name, const char *source, size_t size) _NOEXCEPT;

    /**
     *  Alternative constructor without a size
     *  @param  name        Name of the PHP script
     *  @param  source      PHP source code to be evaluated
     */
    Script(const char *name, const char *source) _NOEXCEPT : Script(name, source, ::strlen(source)) {}

    /**
     *  Alternative constructor without a name
     *  @param  source      PHP source code to be evaluated
     *  @param  size        Length of the source code
     */
    Script(const char *source, size_t size) _NOEXCEPT : Script("Unknown", source, size) {}

    /**
     *  Alternative constructor without a name and without a size
     *  @param  source      PHP source code to be evaluated
     */
    Script(const char *source) _NOEXCEPT : Script("Unknown", source, ::strlen(source)) {}

    /**
     *  Constructor based on a std::string
     *  @param  source      PHP source code to be evaluated
     */
    Script(const std::string &source) _NOEXCEPT : Script("Unknown", source.c_str(), source.size()) {}

    /**
     *  Destructor
     */
    virtual ~Script();

    /**
     *  Is the script a valid PHP script without syntax errors?
     *  @return bool
     */
    bool valid() const;

    /**
     *  Execute the script
     *  The return value of the script is returned
     *  @return Value
     */
    Value execute() const;

private:
    /**
     *  The opcodes
     *  @var Opcodes
     */
    Opcodes *_opcodes;

    /**
     *  Helper function to compile the source code
     *  @param  name        name of the script
     *  @param  script      actual PHP code
     *  @param  size        length of the string
     *  @return opcodes
     */
    static struct _zend_op_array *compile(const char *name, const char *phpcode, size_t size);

};

/**
 *  End of namespace
 */
}