Ejemplo n.º 1
0
/**
 * sp points to the last use variable on the stack.
 * returns the closure so that translator-x64 can just return "rax".
 */
c_Closure* c_Closure::init(int numArgs, ActRec* ar, TypedValue* sp) {
  static StringData* invokeName = StringData::GetStaticString("__invoke");
  Func* invokeFunc = getVMClass()->lookupMethod(invokeName);

  if (invokeFunc->attrs() & AttrStatic) {
    // Only set the class for static closures
    m_thisOrClass = (ObjectData*)(intptr_t(ar->m_func->cls()) | 1LL);
  } else {
    // I don't care if it is a $this or a late bound class because we will just
    // put it back in the same place on an ActRec.
    m_thisOrClass = ar->m_this;
    if (ar->hasThis()) {
      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
    *curProperty++ = *--beforeCurUseVar;
  }
  return this;
}