-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
57 lines (46 loc) · 1.31 KB
/
test.c
File metadata and controls
57 lines (46 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "callback_system.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// these are used by the test to get/set things in mem
WASM_EXPORT(malloc) void* export_malloc(size_t s) {
return malloc(s);
}
WASM_EXPORT(free) void export_free(void* p) {
free(p);
}
// Function to be implemented by host
WASM_IMPORT(http_get)
int http_get(char* url);
// Global variable to store the return value
static char return_value[256];
// Function to set the return value
void set_return_value(const char* value) {
// In a real implementation, you might need to handle string copying more carefully
int i = 0;
while (value[i] && i < 255) {
return_value[i] = value[i];
i++;
}
return_value[i] = '\0';
}
// Callback function
void mycallback(void* retval) {
char buffer[255];
sprintf(buffer, "Ok, I got the data: %lu", strlen(retval));
set_return_value(buffer);
}
// This will be called by host
WASM_EXPORT(example)
char* example() {
// Initialize the return value
set_return_value("");
// Register the callback for the HTTP request
wasm_promise_callbacks_register(http_get("https://example.com"), mycallback);
// The host will need to detect that this is a promise and await its resolution
return return_value;
}
int main() {
init_callback_system();
return 0;
}