1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
Package: dyncall
Library: test
File: test/plain_c++/test_main.cc
Description:
License:
Copyright (c) 2007-2011 Daniel Adler <dadler@uni-goettingen.de>,
Tassilo Philipp <tphilipp@potion-studios.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "test_framework.h"
#include "../../dyncall/dyncall.h"
/* needed by SunPro .. otherwise printf not included */
#if defined(__SUNPRO_CC) || defined(__ANDROID__)
#include <stdio.h>
#else
#include <cstdio>
#endif
#include "../common/platformInit.h"
/* -------------------------------------------------------------------------
* test: identity function calls
* ------------------------------------------------------------------------- */
#define DEF_FUNCS(API,NAME) \
void API fun_##NAME##_v() { } \
DCbool API fun_##NAME##_b(DCbool x) { return x; } \
DCint API fun_##NAME##_i(DCint x) { return x; } \
DClong API fun_##NAME##_j(DClong x) { return x; } \
DClonglong API fun_##NAME##_l(DClonglong x) { return x; } \
DCfloat API fun_##NAME##_f(DCfloat x) { return x; } \
DCdouble API fun_##NAME##_d(DCdouble x) { return x; } \
DCpointer API fun_##NAME##_p(DCpointer x) { return x; }
/* __cdecl */
#if !defined(DC__OS_Win32)
# define __declspec(X)
# define __cdecl
#endif
/* -------------------------------------------------------------------------
* test: identity this calls
* ------------------------------------------------------------------------- */
union ValueUnion
{
DCbool B;
DCint i;
DClong j;
DClonglong l;
DCfloat f;
DCdouble d;
DCpointer p;
};
/* C++ class using __cdecl this call */
// #define VTBI_DESTRUCTOR 0
/*
* the layout of the VTable is non-standard and it is not clear what is the initial real first method index.
* so for it turns out that:
* on vc/x86 : 1
* on GCC/x86 : 2
*/
#if defined DC__C_MSVC
#define VTBI_BASE 1
#else
#define VTBI_BASE 2
#endif
#define VTBI_SET_BOOL VTBI_BASE+0
#define VTBI_GET_BOOL VTBI_BASE+1
#define VTBI_SET_INT VTBI_BASE+2
#define VTBI_GET_INT VTBI_BASE+3
#define VTBI_SET_LONG VTBI_BASE+4
#define VTBI_GET_LONG VTBI_BASE+5
#define VTBI_SET_LONG_LONG VTBI_BASE+6
#define VTBI_GET_LONG_LONG VTBI_BASE+7
#define VTBI_SET_FLOAT VTBI_BASE+8
#define VTBI_GET_FLOAT VTBI_BASE+9
#define VTBI_SET_DOUBLE VTBI_BASE+10
#define VTBI_GET_DOUBLE VTBI_BASE+11
#define VTBI_SET_POINTER VTBI_BASE+12
#define VTBI_GET_POINTER VTBI_BASE+13
class Value
{
public:
virtual ~Value() {}
virtual void __cdecl setBool(DCbool x) { mValue.B = x; }
virtual DCbool __cdecl getBool() { return mValue.B; }
virtual void __cdecl setInt(DCint x) { mValue.i = x; }
virtual DCint __cdecl getInt() { return mValue.i; }
virtual void __cdecl setLong(DClong x) { mValue.j = x; }
virtual DClong __cdecl getLong() { return mValue.j; }
virtual void __cdecl setLongLong(DClonglong x) { mValue.l = x; }
virtual DClonglong __cdecl getLongLong() { return mValue.l; }
virtual void __cdecl setFloat(DCfloat x) { mValue.f = x; }
virtual DCfloat __cdecl getFloat() { return mValue.f; }
virtual void __cdecl setDouble(DCdouble x) { mValue.d = x; }
virtual DCdouble __cdecl getDouble() { return mValue.d; }
virtual void __cdecl setPtr(DCpointer x) { mValue.p = x; }
virtual DCpointer __cdecl getPtr() { return mValue.p; }
private:
ValueUnion mValue;
};
/* C++ class using (on win32: microsoft) this call */
class ValueMS
{
public:
virtual ~ValueMS() {}
virtual void setBool(DCbool x) { mValue.B = x; }
virtual DCbool getBool() { return mValue.B; }
virtual void setInt(DCint x) { mValue.i = x; }
virtual DCint getInt() { return mValue.i; }
virtual void setLong(DClong x) { mValue.j = x; }
virtual DClong getLong() { return mValue.j; }
virtual void setLongLong(DClonglong x) { mValue.l = x; }
virtual DClonglong getLongLong() { return mValue.l; }
virtual void setFloat(DCfloat x) { mValue.f = x; }
virtual DCfloat getFloat() { return mValue.f; }
virtual void setDouble(DCdouble x) { mValue.d = x; }
virtual DCdouble getDouble() { return mValue.d; }
virtual void setPtr(DCpointer x) { mValue.p = x; }
virtual DCpointer getPtr() { return mValue.p; }
private:
ValueUnion mValue;
};
template<typename T>
void testCallValue(DCCallVM* pc)
{
T o;
T* pThis = &o;
DCpointer* vtbl = *( (DCpointer**) pThis ); /* vtbl is located at beginning of class */
/* set/get bool (TRUE) */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgBool(pc,DC_TRUE);
dcCallVoid(pc, vtbl[VTBI_SET_BOOL] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_TRUE );
/* set/get bool (FALSE) */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgBool(pc,DC_FALSE);
dcCallVoid(pc, vtbl[VTBI_SET_BOOL] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallBool(pc, vtbl[VTBI_GET_BOOL] ) == DC_FALSE );
/* set/get int */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgInt(pc,1234);
dcCallVoid(pc, vtbl[VTBI_SET_INT] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallInt(pc, vtbl[VTBI_GET_INT] ) == 1234 );
/* set/get long */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgLong(pc,0xCAFEBABEUL);
dcCallVoid(pc, vtbl[VTBI_SET_LONG] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallLong(pc, vtbl[VTBI_GET_LONG] ) == (DClong)0xCAFEBABEUL );
/* set/get long long */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgLongLong(pc,0xCAFEBABEDEADC0DELL);
dcCallVoid(pc, vtbl[VTBI_SET_LONG_LONG] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallLongLong(pc, vtbl[VTBI_GET_LONG_LONG] ) == (DClonglong)0xCAFEBABEDEADC0DELL );
/* set/get float */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgFloat(pc,1.2345f);
dcCallVoid(pc, vtbl[VTBI_SET_FLOAT] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallFloat(pc, vtbl[VTBI_GET_FLOAT] ) == 1.2345f );
/* set/get double */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgDouble(pc,1.23456789);
dcCallVoid(pc, vtbl[VTBI_SET_DOUBLE] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallDouble(pc, vtbl[VTBI_GET_DOUBLE] ) == 1.23456789 );
/* set/get pointer */
dcReset(pc);
dcArgPointer(pc, pThis);
dcArgPointer(pc, (DCpointer) 0xCAFEBABE );
dcCallVoid(pc, vtbl[VTBI_SET_POINTER] );
dcReset(pc);
dcArgPointer(pc, pThis);
DC_TEST( dcCallPointer(pc, vtbl[VTBI_GET_POINTER] ) == ( (DCpointer) 0xCAFEBABE ) );
}
#ifdef DC__OS_Win32
DC_DEFINE_TEST_FUNC_BEGIN(testCallThisMS)
DCCallVM* pc = dcNewCallVM(4096);
dcMode(pc,DC_CALL_C_X86_WIN32_THIS_MS);
dcReset(pc);
testCallValue<ValueMS>(pc);
dcFree(pc);
DC_DEFINE_TEST_FUNC_END
#endif
DC_DEFINE_TEST_FUNC_BEGIN(testCallThisC)
DCCallVM* pc = dcNewCallVM(4096);
dcReset(pc);
testCallValue<Value>(pc);
dcFree(pc);
DC_DEFINE_TEST_FUNC_END
extern "C" {
int main(int argc, char* argv[])
{
dcTest_initPlatform();
int b = TRUE;
#if defined(DC__OS_Win32) // ThisCall temporarily only for win 32 @@@
b = b && testCallThisC();
printf("ThisC:%d\n",b);
#if defined(DC__C_MSVC)
b = b && testCallThisMS();
printf("ThisMS:%d\n",b);
#endif
#endif
printf("result: plain_cpp: %d\n", b);
dcTest_deInitPlatform();
return !b;
}
} // extern "C"
|