0
|
1 /*
|
|
2
|
|
3 Package: dyncall
|
|
4 Library: test
|
|
5 File: test/callback_plain/callback_plain.c
|
|
6 Description:
|
|
7 License:
|
|
8
|
281
|
9 Copyright (c) 2011-2018 Daniel Adler <dadler@uni-goettingen.de>,
|
0
|
10 Tassilo Philipp <tphilipp@potion-studios.com>
|
|
11
|
|
12 Permission to use, copy, modify, and distribute this software for any
|
|
13 purpose with or without fee is hereby granted, provided that the above
|
|
14 copyright notice and this permission notice appear in all copies.
|
|
15
|
|
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
|
24 */
|
|
25
|
|
26 #include "../../dyncallback/dyncall_thunk.h"
|
|
27 #include "../../dyncallback/dyncall_callback.h"
|
|
28 #include "../common/platformInit.h"
|
|
29 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */
|
|
30
|
|
31
|
|
32 char cbHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata)
|
|
33 {
|
|
34 int* ud = (int*)userdata;
|
|
35 int arg1 = dcbArgInt (args);
|
|
36 float arg2 = dcbArgFloat (args);
|
|
37 short arg3 = dcbArgShort (args);
|
|
38 double arg4 = dcbArgDouble (args);
|
|
39 long long arg5 = dcbArgLongLong(args);
|
|
40
|
|
41 printf("reached callback\n");
|
|
42 printf("userdata (should be 1337): %d\n", *ud);
|
|
43 printf("1st argument (should be 123): %d\n", arg1);
|
|
44 printf("2nd argument (should be 23.f): %f\n", arg2);
|
|
45 printf("3rd argument (should be 3): %d\n", arg3);
|
|
46 printf("4th argument (should be 1.82): %f\n", arg4);
|
|
47 printf("5th argument (should be 9909): %lld\n", arg5);
|
|
48
|
|
49 if(*ud == 1337) *ud = 1;
|
|
50 if(arg1 == 123) ++*ud;
|
|
51 if(arg2 == 23.f) ++*ud;
|
|
52 if(arg3 == 3) ++*ud;
|
|
53 if(arg4 == 1.82) ++*ud;
|
|
54 if(arg5 == 9909) ++*ud;
|
7
|
55
|
0
|
56 result->s = 1234;
|
|
57 return 's';
|
|
58 }
|
|
59
|
|
60
|
|
61 int main()
|
|
62 {
|
|
63 DCCallback* cb;
|
|
64 short result = 0;
|
|
65 int userdata = 1337;
|
|
66
|
|
67 dcTest_initPlatform();
|
|
68
|
|
69 cb = dcbNewCallback("ifsdl)s", &cbHandler, &userdata);
|
139
|
70 printf("about to callback (trampoline for 0x%tx at 0x%tx)...\n", &cbHandler, cb);
|
|
71
|
0
|
72 result = ((short(*)(int, float, short, double, long long))cb)(123, 23.f, 3, 1.82, 9909ull);
|
|
73 dcbFreeCallback(cb);
|
139
|
74
|
0
|
75 printf("successfully returned from callback\n");
|
|
76 printf("return value (should be 1234): %d\n", result);
|
|
77
|
|
78 printf("result: callback_plain: %s\n", (userdata == 6) && (result == 1234) ? "1" : "0");
|
|
79
|
|
80 dcTest_deInitPlatform();
|
|
81
|
|
82 return 0;
|
|
83 }
|
|
84
|