From b1d3074e31344f2f192c7f9fe0e7029eaf2a8fdd Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sat, 3 Aug 2019 01:36:25 +0200 Subject: [PATCH] bak --- v8.cpp | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 150 insertions(+), 19 deletions(-) diff --git a/v8.cpp b/v8.cpp index 394b435b5711b..990e03de08e41 100644 --- a/v8.cpp +++ b/v8.cpp @@ -260,8 +260,20 @@ class rust_qual_type : public rust_primitive_type { // Unqualified types. +template > +struct copy_cast { + static T arg(O val) { + return *reinterpret_cast(&val); + } + static O ret(T val) { + return *reinterpret_cast(&val); + } +}; + class map_type_base { public: + template + using cast = copy_cast; }; template @@ -442,10 +454,22 @@ class map_type>> using tgt_t = std::remove_pointer_t; public: - using opaque_type = std::add_pointer_t>; + using opaque_type = opaque_qual_t*; using rust_repr = rust_pointer_type; }; + +template > +struct lvalue_cast { + static T&& arg(O& val) { + return std::move(*reinterpret_cast(&val)); + } + static O& ret(T& val) { + return std::move(*reinterpret_cast(&val)); + } +}; + + template class map_type>> : public map_type_base { @@ -453,8 +477,21 @@ class map_type>> using tgt_t = std::remove_reference_t; public: - using opaque_type = std::add_lvalue_reference>; + using opaque_type = opaque_qual_t*; using rust_repr = rust_pointer_type; + template + using cast = lvalue_cast; +}; + +template > +struct rvalue_cast { + static T&& arg(O&& val) { + return reinterpret_cast(val); + } + static O&& ret(T&& val) { + return reinterpret_cast(val); + } + }; template @@ -464,8 +501,11 @@ class map_type>> using tgt_t = std::remove_reference_t; public: - using opaque_type = std::add_rvalue_reference>; + using opaque_type = opaque_qual_t*; using rust_repr = rust_pointer_type; + + //template + //using cast = rvalue_cast; }; // Qualified types @@ -602,27 +642,69 @@ inline typename cast_retty::ret_type dyn_cast(Y *Val) { // ====== Function and method wrappers ====== template > +//using cast = typename map_type::template cast; class cast { public: - static auto& arg(opaque_t& arg) { - auto& r = *reinterpret_cast*>(&arg); + static T* arg(opaque_t* arg) { + auto r = reinterpret_cast>(arg); return r; } - static opaque_t&& ret(T&& result) { - return std::move(reinterpret_cast&&>(result)); + static opaque_t ret(T&& result) { + return reinterpret_cast>(result); } - static opaque_t& ret(T& result) { - return reinterpret_cast&>(result); + //static opaque_t ret(T& result) { + // return reinterpret_cast>(result); + //} + + //static opaque_t ret(T result) { + // return *reinterpret_cast*>(&result); + //} + + static opaque_t ret(T* result) { + return reinterpret_cast>(result); } +}; + + +template +struct xcast {}; - static opaque_t ret(T result) { - return *reinterpret_cast*>(&result); +template +struct xcast>> { + using O = opaque_t; + static T arg(O val) { + return reinterpret_cast(val); } + static O ret(T val) { + return reinterpret_cast(val); + } +}; + - static auto* ret(T* result) { - return reinterpret_cast*>(result); +template +struct xcast>> { + using O = opaque_t; + static T arg(O val) { + return reinterpret_cast(*val); + } + static O ret(T val) { + return reinterpret_cast(&val); + } +}; + +template +static constexpr bool is_value_type_v = std::is_arithmetic_v || std::is_class_v || std::is_union_v; + +template +struct xcast>> { + using O = opaque_t; + static T&& arg(O&& val) { + return reinterpret_cast(val); + } + static O&& ret(T&& val) { + return reinterpret_cast(val); } }; @@ -636,7 +718,7 @@ class wrap_function { template struct make_wrapper { static void invoke(opaque_t... args) { - fn(cast::arg(args)...); + fn(xcast::arg(args)...); } }; @@ -644,7 +726,7 @@ class wrap_function { template struct make_wrapper { static opaque_t invoke(opaque_t... args) { - return cast::ret(fn(cast::arg(args)...)); + return xcast::ret(xcast::arg(args)...); } }; @@ -671,8 +753,9 @@ class wrap_method { template struct make_wrapper { static opaque_t invoke(opaque_qual_t self, opaque_t... args) { - return cast::ret( - (cast::arg(self)->*method)(cast::arg(args)...)); + auto r = + (cast::arg(self)->*method)(cast::arg(args)...); + return return cast::ret(std::move(r)); } }; @@ -724,7 +807,7 @@ void print_type_() { template struct fn_printer_ { static void print_fn() { - print_type_(); + print_type_(); static constexpr auto wrapped = invoke; p(cxx_typename()); } @@ -759,12 +842,57 @@ void ff_const(const aap a) {} void ff_const_lval(const aap& a) {} void ff_const_rval(const aap&& a) {} +struct sss { + v8::Persistent&& a; + v8::Persistent& b; + v8::Persistent* c; +}; + v8::Persistent&& funfunfun(const char* const* a, aap b, v8::Local d) { static void* invalid = nullptr; return reinterpret_cast&&>(invalid); } + + +v8::Persistent&& + moe1(/*const char* const* a, aap b, v8::Local d*/) { + static void* invalid = nullptr; + return reinterpret_cast&&>(invalid); +} + +v8::Persistent& + moe2(/*const char* const* a, aap b, v8::Local d*/) { + int a = 1; + static int* invalid = &a; + return reinterpret_cast&>(invalid); +} + +const v8::Persistent* + moe3(/*const char* const* a, aap b, v8::Local d*/) { + int a = 1; + static int* invalid = &a; + return reinterpret_cast*>(invalid); +} + +std::unique_ptr moe4() {}; + +const int moe5(const char* poep, v8::Persistent& bla) {}; + +template +opaque_t wrap(T (*f)(A...), opaque_t... args) { + return xcast::ret(f(xcast::arg(args)...)); +} + +void testes() { + auto a = wrap(moe3); + auto b = wrap(moe2); + auto c = wrap(moe1); + auto d = wrap(moe4); + auto e = wrap(moe5, "lul", b); +} + struct klas { int foo0() { return 1; @@ -786,6 +914,7 @@ struct klas { }; int main() { + testes(); test_type(int); test_type(size_t); test_type(int*); @@ -799,6 +928,7 @@ int main() { test_fn(&void_fun1); int a[3]; test_type(decltype(a)); +#if 0 std::string strings[10]; test_type(decltype(strings)); test_type(aap); @@ -836,11 +966,12 @@ int main() { xxxx aaaa = &v8::Persistent>:: operator=; +#endif #define X_CXXConstructor(f) #define X_CXXDestructor(f) #define X_CXXMethod(f) test_fn(f); #define X_Disabled(f) #define X_Function(f) test_fn(f); -#include "o.h" +//#include "o.h" }