Add <<__NativeData("Type")>> registry for over-allocating ObjectData
commit7eb3b80924a91a87bfe3daad89d1ec4cae57890d
authorSara Golemon <sgolemon@fb.com>
Sun, 23 Feb 2014 01:52:33 +0000 (22 17:52 -0800)
committerSara Golemon <sgolemon@fb.com>
Mon, 24 Feb 2014 20:00:52 +0000 (24 12:00 -0800)
tree873bcdb4f8f6471ec23d6914e19a78bb893d128c
parent4e273cf4dbd828645bae65e8f184ae3c9b8c5a0a
Add <<__NativeData("Type")>> registry for over-allocating ObjectData

Allows storing arbitrary data in an ObjectData structure
from extension code.

Allocates extra space (as requested by Native::registerNativeDataInfo())
in the ObjectData sturcture (prior to the formal struct) and calls
Init, Clone, and Destroy functions (as registered).

Standard class implementation implement these via their empty constructor,
copy constructor, and destructor respectively.

For example:

ext_foo.php
  <?hh
  <<__NativeData("MyClass")>>
  class MyClass {
    /* blah blah blah */
  }

ext_foo.cpp
  struct MyClass : Native::NativeData {
   public:
    // Called by `$x = new MyClass;` or `$x = clone $MyClassInstance;`
    // to initialize the C++ object
    MyClass() {}
    // Called by `$x = clone $MyClassInstance`
    // to copy from an existing C++ object
    MyClass& operator=(const MyClass& src) {
      if (src.m_foo) {
        m_foo = newFoo(src.m_foo);
      }
      return *this;
    }
    // Called when object goes out of scope or request end
    ~MyClass() {
      if (m_foo) {
        delete m_foo;
      }
    }

    // etc...

   private:
    Foo *m_foo = nullptr;
  }

Further down in the Extension structure:
  void moduleInit() override {
    Native::registerNativeDataInfo<MyClass>(s_MyClass.get());
  }

Reviewed By: @jdelong

Differential Revision: D1171055
26 files changed:
hphp/runtime/base/memory-manager.cpp
hphp/runtime/base/object-data.cpp
hphp/runtime/base/object-data.h
hphp/runtime/base/typed-value.h
hphp/runtime/ext/icu/ext_icu_calendar.cpp
hphp/runtime/ext/icu/ext_icu_calendar.h
hphp/runtime/ext/icu/ext_icu_calendar.php
hphp/runtime/ext/icu/ext_icu_date_fmt.cpp
hphp/runtime/ext/icu/ext_icu_date_fmt.h
hphp/runtime/ext/icu/ext_icu_date_fmt.php
hphp/runtime/ext/icu/ext_icu_iterator.cpp
hphp/runtime/ext/icu/ext_icu_iterator.h
hphp/runtime/ext/icu/ext_icu_iterator.php
hphp/runtime/ext/icu/ext_icu_num_fmt.cpp
hphp/runtime/ext/icu/ext_icu_num_fmt.h
hphp/runtime/ext/icu/ext_icu_num_fmt.php
hphp/runtime/ext/icu/ext_icu_timezone.cpp
hphp/runtime/ext/icu/ext_icu_timezone.h
hphp/runtime/ext/icu/ext_icu_timezone.php
hphp/runtime/ext/icu/icu.cpp
hphp/runtime/ext/icu/icu.h
hphp/runtime/vm/class.cpp
hphp/runtime/vm/class.h
hphp/runtime/vm/native-data.cpp [new file with mode: 0644]
hphp/runtime/vm/native-data.h [new file with mode: 0644]
hphp/runtime/vm/preclass-emit.cpp