2019-04-12 22:02:49 +03:00
|
|
|
#include "sdk/allocator.h"
|
|
|
|
#include "sdk/logger.h"
|
2019-04-21 22:23:36 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
const char *const greeting = "Hello world! From ";
|
|
|
|
const int RESPONSE_SIZE_BYTES = 4;
|
2019-04-12 22:02:49 +03:00
|
|
|
|
|
|
|
char *invoke(const char *str, int length) {
|
2019-04-21 22:23:36 +03:00
|
|
|
const size_t greeting_length = strlen(greeting);
|
|
|
|
// TODO: add check for overflow
|
|
|
|
const size_t response_length = length + RESPONSE_SIZE_BYTES + greeting_length;
|
|
|
|
|
|
|
|
char *response = (char *)allocate(response_length);
|
|
|
|
|
|
|
|
wasm_log(str, length);
|
|
|
|
|
|
|
|
for(int i = 0; i < RESPONSE_SIZE_BYTES; ++i) {
|
|
|
|
response[i] = (response_length >> 8*i) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(response + RESPONSE_SIZE_BYTES, greeting, greeting_length);
|
|
|
|
memcpy(response + RESPONSE_SIZE_BYTES + greeting_length, str, length);
|
2019-04-12 22:02:49 +03:00
|
|
|
|
2019-04-21 22:23:36 +03:00
|
|
|
return response;
|
2019-04-12 22:02:49 +03:00
|
|
|
}
|