Path: blob/main/core/dylink/test/python-extension/hellomodule.c
1393 views
#include "app.h"1#include <stdio.h>2#include <math.h>3#include <time.h>4#include <assert.h>56extern float mysin(float n);78static PyObject *hello(PyObject *self, PyObject *args) {9float s = 0;10printf("start hello...\n");11printf(12"If this takes more than a second, then the function pointer mechanism "13"is broken:\n");14time_t start, end;15time(&start);16// ((int)self) % n is just to keep this from getting compiled out:17for (int i = 0; i < 1000000 + ((int)self) % 1000; i++) {18s += mysin(i);19}20time(&end);21printf("done hello... s=%f\n", s);22printf("time = %ld seconds\n",23end - start); // this is in *seconds*, so basicaly going to be 024assert(end - start <= 1);25// printf("python-wasm: 'hello there ** FROM HELLO!'\n");26return PyNone;27}2829static struct PyMethodDef module_methods[] = {{"hello", &hello}, {NULL, NULL}};3031struct PyModuleDef _hellomodule = {32.m_name = "hello",33.m_methods = module_methods,34};3536EXPORTED_SYMBOL int PyInit_hello(void) {37printf("PyInit_hello\n");38// initialize the module:39printf("the hello function is at %p\n", &hello);40return PyModuleDef_Init(&_hellomodule);41}4243