Path: blob/main/core/dylink/test/python-extension/app.c
1393 views
#include <math.h>1#include <stdio.h>2#include <assert.h>3#include "app.h"45extern void* dlopen(const char* filename, int flags);6extern void* dlsym(void* handle, const char* symbol);78#ifndef WASM_EXPORT9#define WASM_EXPORT(x) __attribute__((visibility("default"))) void* __WASM_EXPORT__##x() { return &(x);}10#endif1112EXPORTED_SYMBOL13PyObject _Py_NoneStruct = {.thingy = 1};14WASM_EXPORT(_Py_NoneStruct)1516EXPORTED_SYMBOL17float mysin(float n) { return sin(n + 1); }1819// If you comment this out, then no function pointer to mysin is generated,20// and the call to hello will be insanely slow.21WASM_EXPORT(mysin)2223EXPORTED_SYMBOL24int PyModuleDef_Init(struct PyModuleDef* module) {25printf("PyModuleDef_Init, module = %p \n", module);2627PyCFunction f = (module->m_methods)[0].f;28printf("PyModuleDef_Init, hello = %p \n", f);29(*f)(NULL, NULL);30return 0;31}3233typedef int (*INIT_FUNCTION)();3435int main() {36printf("Running Tests...\n");37void* handle = dlopen("./hello.so", 2);38printf("Got handle=%p\n", handle);39assert(handle != NULL);40// Fetch a pointer to the init function:41INIT_FUNCTION init = (INIT_FUNCTION)dlsym(handle, "PyInit_hello");42assert(init != NULL);43printf("Got init=%p\n", init);44printf("PyInit_hello() = %d\n", (*init)());45printf("All tests passed!\n");46}474849