Last active
June 15, 2016 16:27
-
-
Save ng420/357f9a950ddabff25bafbc58de50e0b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hphp/runtime/ext/extension.h" | |
#include "hphp/runtime/base/execution-context.h" | |
#include "hphp/runtime/vm/native-data.h" | |
void add(int *x, int *y, int *result) { | |
*result = *x + *y; | |
} | |
namespace HPHP { | |
template<typename T> | |
struct SWIG_Ptr : public SweepableResourceData { | |
private: | |
T* m_ptr; | |
public: | |
DECLARE_RESOURCE_ALLOCATION(SWIG_Ptr) | |
CLASSNAME_IS("SWIG_Ptr") | |
const String& o_getClassNameHook() const override { return classnameof(); } | |
explicit SWIG_Ptr(T* ptr) : m_ptr(ptr) {} | |
virtual ~SWIG_Ptr() { close(); } | |
void close() { | |
if (m_ptr) { | |
delete m_ptr; | |
} | |
m_ptr = nullptr; | |
} | |
T* get() { return m_ptr; } | |
}; | |
template<typename T> inline void SWIG_Ptr<T>::sweep() { close(); } | |
Resource HHVM_FUNCTION(retPtr, int v) { | |
int* r = new int; | |
*r = v; | |
return Resource(req::make<SWIG_Ptr<int>>(r)); | |
} | |
int HHVM_FUNCTION(getVal, const Resource& a) { | |
auto ta = dyn_cast_or_null<SWIG_Ptr<int>>(a); | |
auto fa = ta->get(); | |
return *fa; | |
} | |
void HHVM_FUNCTION(add, const Resource& a, const Resource& b, const Resource& c) { | |
auto ta = dyn_cast_or_null<SWIG_Ptr<int>>(a); | |
auto fa = ta->get(); | |
auto tb = dyn_cast_or_null<SWIG_Ptr<int>>(b); | |
auto fb = tb->get(); | |
auto tc = dyn_cast_or_null<SWIG_Ptr<int>>(c); | |
auto fc = tc->get(); | |
add(fa, fb, fc); | |
// A return would be something like: return Resource(req::make<SWIG_Ptr<int>>(tc)); | |
} | |
class PtrExtension : public Extension { | |
public: | |
PtrExtension(): Extension("Ptr", "1.0") {} | |
void moduleInit() override { | |
HHVM_FE(add); | |
HHVM_FE(retPtr); | |
HHVM_FE(getVal); | |
loadSystemlib(); | |
} | |
} s_Ptr_extension; | |
HHVM_GET_MODULE(Ptr); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?hh | |
<<__Native>> | |
function add(resource $a, resource $b, resource $c): void; | |
<<__Native>> | |
function retPtr(int $x): Resource; | |
<<__Native>> | |
function getVal(resource $x): int; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = retPtr(37); | |
$b = retPtr(42); | |
$c = retPtr(0); // $c must be defined and not null. | |
print " a = ".getVal($a)."\n"; | |
print " b = ".getVal($b)."\n"; | |
# Call the add() function wuth some pointers | |
add($a, $b, $c); | |
print " ".getVal($a)." + ".getVal($b)." = ".getVal($c)."\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment