summaryrefslogtreecommitdiff
path: root/zend/global.cpp
blob: e7fef53e1718858de259b7b9e7367210922e0ff3 (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
/**
 *  Global.cpp
 *
 *  Implementation for the global variable
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */
#include "includes.h"

/**
 *  Namespace
 */
namespace Php {

/**
 *  Move constructor
 *  @param  global
 */
Global::Global(Global &&global) _NOEXCEPT :
    Value(std::move(global)),
    _name(global._name),
    _exists(global._exists)
{
    // remove from other global to avoid double free
    global._name = nullptr;
}

/**
 *  Constructor for non-existing var
 *
 *  @param  name    Name for the variable that does not exist
 */
Global::Global(const char *name) :
    Value(),
    _name(zend_string_init(name, ::strlen(name), 1)),
    _exists(false) {}

/**
 *  Alternative constructor for non-existing var
 *  @param  name
 */
Global::Global(const std::string &name) :
    Value(),
    _name(zend_string_init(name.data(), name.size(), 1)),
    _exists(false) {}

/**
 *  Constructor to wrap zval for existing global bar
 *  @param  name
 *  @param  val
 */
Global::Global(const char *name, struct _zval_struct *val) :
    Value(val, true),
    _name(zend_string_init(name, ::strlen(name), 1)),
    _exists(true) {}

/**
 *  Alternative constructor to wrap zval
 *  @param  name
 *  @param  val
 */
Global::Global(const std::string &name, struct _zval_struct *val) :
    Value(val, true),
    _name(zend_string_init(name.data(), name.size(), 1)),
    _exists(true) {}

/**
 *  Destructor
 */
Global::~Global()
{
    // release the string
    if (_name) zend_string_release(_name);
}

/**
 *  Function that is called when the value is updated
 *  @return Value
 */
Global &Global::update()
{
    // skip if the variable already exists
    if (_exists) return *this;

    // we need the TSRMLS variable
    TSRMLS_FETCH();

    // add the variable to the globals
    zend_hash_add(EG(current_execute_data)->symbol_table, _name, _val);

    // add one extra reference because the variable now is a global var too
    Z_TRY_ADDREF_P(_val);

    // remember that the variable now exists
    _exists = true;

    // done
    return *this;
}

/**
 *  End of namespace
 */
}