Arquivos
hhvm/hphp/runtime/ext/ext_closure.cpp
T
Owen Yamauchi 88cd96db87 Devirtualize ObjectData, part 1
The C++ vtable pointers and the m_cls pointers in ObjectData are
redundant for C++ extension classes; they both indicate the ObjectData's
dynamic type.

This diff gives ObjectData the same treatment that the ArrayData
hierarchy got, replacing virtual functions with manual dispatch based on
attributes and m_cls. We can't do direct comparison against m_cls,
because of inheritance, except when we know inheritance is not in the
picture.

The vtable isn't gone yet, because I need to do something about C++
destructors, which still require virtual dispatch. That's going to
require a lot of grunt work, so I wanted to put this diff up to keep
that stuff separate.

I did, however, make ~ObjectData non-virtual and try to compile, to
flush out all the dynamic_casts of ObjectData.

- The type-casting functions and clone() are pretty straightforward. The
  only trick is that specialized clone() functions now have to call
  cloneImpl() instead of the base class' clone(), otherwise infinite
  recursion. I added an ObjectData attribute to indicate whether clone()
  is special.

- t___{sleep,wakeup} didn't even need to be virtual, it turns out.
  ObjectData's implementation goes through the normal PHP-method lookup
  path, which effectively does virtual dispatch and routes us to any
  specialized implementations anyway.

Differential Revision: D940852
2013-08-27 11:58:28 -07:00

85 linhas
3.0 KiB
C++

/*
+----------------------------------------------------------------------+
| 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 props to instance variables
assert(m_cls->numDeclProperties() == numArgs);
TypedValue* beforeCurUseVar = sp + numArgs;
TypedValue* curProperty = propVec();
for (int i = 0; i < numArgs; i++) {
// teleport the references in here so we don't incref
tvCopy(*--beforeCurUseVar, *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_VMStatics = thiz->m_VMStatics;
closure->m_thisOrClass = thiz->m_thisOrClass;
closure->m_func = thiz->m_func;
return closure;
}
HphpArray* c_Closure::getStaticLocals() {
if (m_VMStatics.get() == NULL) {
m_VMStatics = ArrayData::Make(1);
}
return m_VMStatics.get();
}
///////////////////////////////////////////////////////////////////////////////
}