Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/python-extension/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
EXPORTED_SYMBOL
14
PyObject _Py_NoneStruct = {.thingy = 1};
15
WASM_EXPORT(_Py_NoneStruct)
16
17
EXPORTED_SYMBOL
18
float mysin(float n) { return sin(n + 1); }
19
20
// If you comment this out, then no function pointer to mysin is generated,
21
// and the call to hello will be insanely slow.
22
WASM_EXPORT(mysin)
23
24
EXPORTED_SYMBOL
25
int PyModuleDef_Init(struct PyModuleDef* module) {
26
printf("PyModuleDef_Init, module = %p \n", module);
27
28
PyCFunction f = (module->m_methods)[0].f;
29
printf("PyModuleDef_Init, hello = %p \n", f);
30
(*f)(NULL, NULL);
31
return 0;
32
}
33
34
typedef int (*INIT_FUNCTION)();
35
36
int main() {
37
printf("Running Tests...\n");
38
void* handle = dlopen("./hello.so", 2);
39
printf("Got handle=%p\n", handle);
40
assert(handle != NULL);
41
// Fetch a pointer to the init function:
42
INIT_FUNCTION init = (INIT_FUNCTION)dlsym(handle, "PyInit_hello");
43
assert(init != NULL);
44
printf("Got init=%p\n", init);
45
printf("PyInit_hello() = %d\n", (*init)());
46
printf("All tests passed!\n");
47
}
48
49