Last active
July 10, 2016 08:06
-
-
Save ng420/ccd312a9137d16dfaa51cdbbe5bf68e2 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" | |
class complex { | |
public: | |
double real, img; | |
complex() {} | |
complex(double r, double i): real(r), img(i) {} | |
}; | |
complex add(complex a, complex b) { | |
complex r; | |
r.real = a.real + b.real; | |
r.img = a.img + b.img; | |
return r; | |
} | |
namespace HPHP { | |
class wrap_complex { | |
public: | |
complex* m_ptr; | |
~wrap_complex() { | |
delete m_ptr; | |
m_ptr = nullptr; | |
} | |
}; | |
void HHVM_METHOD(wrap_complex, __construct, double r, double i) { | |
auto data = Native::data<wrap_complex>(this_); | |
data->m_ptr = new complex(r, i); | |
} | |
void HHVM_FUNCTION(print_complex, const Object& c) { | |
auto data = Native::data<wrap_complex>(c); | |
printf("%lf %lf\n", data->m_ptr->real, data->m_ptr->img); | |
} | |
Object HHVM_FUNCTION(add, const Object& a, const Object &b) { | |
auto cls = Unit::lookupClass(HPHP::makeStaticString("wrap_complex")); | |
Object ret{cls}; | |
auto wa = HPHP::Native::data<wrap_complex>(a)->m_ptr; | |
auto wb = HPHP::Native::data<wrap_complex>(b)->m_ptr; | |
auto result = add(*wa, *wb); | |
auto wrap_result = HPHP::Native::data<wrap_complex>(ret); | |
wrap_result->m_ptr = new complex((const complex &) result); | |
return ret; | |
} | |
class EXAMPLEExtension : public Extension { | |
public: | |
EXAMPLEExtension(): Extension("example", "1.0") {} | |
void moduleInit() override { | |
HHVM_MALIAS(complex, __construct, wrap_complex, __construct); | |
HHVM_FE(print_complex); | |
HHVM_FE(add); | |
Native::registerNativeDataInfo<wrap_complex>(makeStaticString("wrap_complex")); | |
loadSystemlib(); | |
} | |
} s_example_extension; | |
HHVM_GET_MODULE(example); | |
} |
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 | |
<<__NativeData("wrap_complex")>> | |
class complex { | |
<<__Native>> | |
public function __construct(double $real, double $img): void; | |
} | |
<<__Native>> | |
function print_complex(complex $c): void; | |
<<__Native>> | |
function add(complex $a, complex $b): complex; |
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 | |
$c = new complex(2, 3); | |
$d = new complex(2, 3); | |
$a = add($c, $d); | |
print_complex($a); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment