Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/wasi/app.c
1393 views
1
#include <stdio.h>
2
#include <assert.h>
3
#include "app.h"
4
5
extern void* dlopen(const char* filename, int flags);
6
extern void* dlsym(void* handle, const char* symbol);
7
8
int x = 5077;
9
int y = 123;
10
11
EXPORTED_SYMBOL
12
PyObject _Py_NoneStruct = {.thingy = 1};
13
14
#ifndef WASM_EXPORT
15
#define WASM_EXPORT(x) __attribute__((visibility("default"))) void* __WASM_EXPORT__##x() { return &(x);}
16
#endif
17
WASM_EXPORT(_Py_NoneStruct)
18
19
EXPORTED_SYMBOL
20
PyObject* pynone_a() { return PyNone; }
21
22
typedef PyObject* (*FUN_PY_PTR)();
23
24
EXPORTED_SYMBOL
25
int pynones_match() {
26
// return 1 if PyNone same here and in so module.
27
void* handle = dlopen("./dynamic-library.so", 2);
28
// Fetch a pointer to the function that adds 10 from the library.
29
FUN_PY_PTR f = (FUN_PY_PTR)dlsym(handle, "pynone_b");
30
if (pynone_a() == (*f)()) {
31
return 1;
32
} else {
33
return 0;
34
}
35
}
36
37
EXPORTED_SYMBOL
38
int add10(int n) {
39
// Open our dynamic library
40
void* handle = dlopen("./dynamic-library.so", 2);
41
// Fetch a pointer to the function that adds 10 from the library.
42
FUN_PTR f = (FUN_PTR)dlsym(handle, "add10");
43
return (*f)(n);
44
}
45
46
typedef void* (*FUN0_PTR)();
47
48
EXPORTED_SYMBOL
49
int add10b(int n) {
50
// Open our dynamic library
51
void* handle = dlopen("./dynamic-library.so", 2);
52
// Fetch a pointer to the function that adds 10 from the library.
53
FUN0_PTR f = (FUN0_PTR)dlsym(handle, "pointer_to_add10");
54
void* ptr = (*f)();
55
FUN_PTR g = (FUN_PTR)ptr;
56
return (*g)(n);
57
}
58
59
EXPORTED_SYMBOL
60
int add389(int n) {
61
// Open our dynamic library
62
void* handle = dlopen("./dynamic-library.so", 2);
63
// Fetch a pointer to the function that adds 389 from the library.
64
FUN_PTR f = (FUN_PTR)dlsym(handle, "add389");
65
return (*f)(n);
66
}
67
68
// This is going to get called by the dynamic library to do something.
69
EXPORTED_SYMBOL
70
int add5077(int n) { return n + 5077; }
71
72
EXPORTED_SYMBOL
73
int add5077_using_lib_using_main(int n) {
74
void* handle = dlopen("./dynamic-library.so", 2);
75
// Fetch a pointer to the function that adds 389 from the library.
76
FUN_PTR f = (FUN_PTR)dlsym(handle, "add5077_using_func_from_main");
77
return (*f)(n);
78
}
79
80
// int _printf(const char* restrict format, ...) { return printf(format, ...); }
81
82
int main() {
83
printf("add10(2022) = %d\n", add10(2022));
84
assert(add10(2022) == 2022 + 10);
85
86
printf("add10b(2022) = %d\n", add10(2022));
87
assert(add10b(2022) == 2022 + 10);
88
89
printf("add389(2022) = %d\n", add389(2022));
90
assert(add389(2022) == 2022 + 389);
91
92
int n = add5077_using_lib_using_main(389);
93
printf("add5077_using_lib_using_main(389) = %d\n", n);
94
assert(n == 5077 + 389);
95
96
printf("pynones_match() = %d\n", pynones_match());
97
assert(pynones_match() == 1);
98
99
printf("All tests passed!\n");
100
}
101
102