Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/malloc/app.c
1393 views
1
#include <math.h>
2
#include <stdio.h>
3
#include <assert.h>
4
#include "app.h"
5
6
extern void* dlopen(const char* filename, int flags);
7
extern void* dlsym(void* handle, const char* symbol);
8
9
#ifndef WASM_EXPORT
10
#define WASM_EXPORT(x) __attribute__((visibility("default"))) void* __WASM_EXPORT__##x() { return &(x);}
11
#endif
12
13
14
15
EXPORTED_SYMBOL
16
PyObject _Py_NoneStruct = {.thingy = 1};
17
WASM_EXPORT(_Py_NoneStruct)
18
19
20
EXPORTED_SYMBOL
21
int vecsum(int* v, int n) {
22
int s = 0;
23
for (int i = 0; i < n; i++) {
24
s += v[i];
25
}
26
return s;
27
}
28
29
EXPORTED_SYMBOL
30
int PyModuleDef_Init(struct PyModuleDef* module) {
31
printf("PyModuleDef_Init, module = %p \n", module);
32
33
PyCFunction f = (module->m_methods)[0].f;
34
printf("PyModuleDef_Init, hello = %p \n", f);
35
(*f)(NULL, NULL);
36
return 0;
37
}
38
39
typedef int (*INIT_FUNCTION)();
40
41
int main() {
42
printf("stdout=%d, &stdout=%p\n", stdout, &stdout);
43
printf("Running Tests...\n");
44
void* handle = dlopen("./hello.so", 2);
45
printf("Got handle=%p\n", handle);
46
assert(handle != NULL);
47
// Fetch a pointer to the init function:
48
INIT_FUNCTION init = (INIT_FUNCTION)dlsym(handle, "PyInit_hello");
49
assert(init != NULL);
50
printf("Got init=%p\n", init);
51
printf("PyInit_hello() = %d\n", (*init)());
52
printf("All tests passed!\n");
53
}
54
55