-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathext_closure.cpp
85 lines (74 loc) · 3.11 KB
/
ext_closure.cpp
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/ext/ext_closure.h"
#include "hphp/runtime/base/builtin-functions.h"
#include "hphp/runtime/vm/jit/translator-inline.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
c_Closure::~c_Closure() {
// same as ar->hasThis()
if (m_thisOrClass && !(intptr_t(m_thisOrClass) & 1LL)) {
decRefObj(m_thisOrClass);
}
}
void c_Closure::t___construct() {
raise_error("Can't create a Closure directly");
}
const StaticString s_uuinvoke("__invoke");
void c_Closure::init(int numArgs, ActRec* ar, TypedValue* sp) {
auto const invokeFunc = getVMClass()->lookupMethod(s_uuinvoke.get());
m_thisOrClass = ar->m_this;
if (ar->hasThis()) {
if (invokeFunc->attrs() & AttrStatic) {
// Only set the class for static closures.
m_thisOrClass = reinterpret_cast<ObjectData*>(
reinterpret_cast<intptr_t>(ar->getThis()->getVMClass()) | 1LL
);
} else {
ar->getThis()->incRefCount();
}
}
// Change my __invoke's m_cls to be the same as my creator's
Class* scope = ar->m_func->cls();
m_func = invokeFunc->cloneAndSetClass(scope);
/*
* Copy the use vars to instance variables, and initialize any
* instance properties that are for static locals to KindOfUninit.
*/
auto const numDeclProperties = getVMClass()->numDeclProperties();
assert(numDeclProperties - numArgs == m_func->numStaticLocals());
TypedValue* beforeCurUseVar = sp + numArgs;
TypedValue* curProperty = propVec();
int i = 0;
assert(numArgs <= numDeclProperties);
for (; i < numArgs; i++) {
// teleport the references in here so we don't incref
tvCopy(*--beforeCurUseVar, *curProperty++);
}
for (; i < numDeclProperties; ++i) {
tvWriteUninit(curProperty++);
}
}
c_Closure* c_Closure::Clone(ObjectData* obj) {
auto thiz = static_cast<c_Closure*>(obj);
auto closure = static_cast<c_Closure*>(obj->cloneImpl());
closure->m_thisOrClass = thiz->m_thisOrClass;
closure->m_func = thiz->m_func;
return closure;
}
///////////////////////////////////////////////////////////////////////////////
}