Last active
October 25, 2024 18:43
-
-
Save maxsei/c9ad140ace2d66fc2a5a15fb2333279d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by `wit-bindgen` 0.33.0. DO NOT EDIT! | |
#include "strings.h" | |
#include <stdlib.h> | |
#include <string.h> | |
// Exported Functions from `x:strings/upper` | |
__attribute__((__weak__, __export_name__("cabi_post_x:strings/upper#upper"))) | |
void __wasm_export_exports_x_strings_upper_upper_post_return(uint8_t * arg0) { | |
switch ((int32_t) (int32_t) *((uint8_t*) (arg0 + 0))) { | |
case 0: { | |
if ((*((size_t*) (arg0 + 8))) > 0) { | |
free(*((uint8_t **) (arg0 + 4))); | |
} | |
break; | |
} | |
case 1: { | |
break; | |
} | |
} | |
} | |
// Canonical ABI intrinsics | |
__attribute__((__weak__, __export_name__("cabi_realloc"))) | |
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { | |
(void) old_size; | |
if (new_size == 0) return (void*) align; | |
void *ret = realloc(ptr, new_size); | |
if (!ret) abort(); | |
return ret; | |
} | |
// Helper Functions | |
void exports_x_strings_upper_result_string_void_free(exports_x_strings_upper_result_string_void_t *ptr) { | |
if (!ptr->is_err) { | |
strings_string_free(&ptr->val.ok); | |
} | |
} | |
void strings_string_set(strings_string_t *ret, const char*s) { | |
ret->ptr = (uint8_t*) s; | |
ret->len = strlen(s); | |
} | |
void strings_string_dup(strings_string_t *ret, const char*s) { | |
ret->len = strlen(s); | |
ret->ptr = (uint8_t*) cabi_realloc(NULL, 0, 1, ret->len * 1); | |
memcpy(ret->ptr, s, ret->len * 1); | |
} | |
void strings_string_free(strings_string_t *ret) { | |
if (ret->len > 0) { | |
free(ret->ptr); | |
} | |
ret->ptr = NULL; | |
ret->len = 0; | |
} | |
// Component Adapters | |
// Static writable memory that is used to for any exported function call to send | |
// non-primitive WASM data structures. XXX: Is this only allocated inside the | |
// WASM blob or dynamically allocated to the stack frames of a wasm export? | |
__attribute__((__aligned__(4))) | |
static uint8_t RET_AREA[12]; | |
__attribute__((__export_name__("x:strings/upper#upper"))) | |
uint8_t * __wasm_export_exports_x_strings_upper_upper(uint8_t * arg, size_t arg0) { | |
// Get input string from host. | |
strings_string_t arg1 = (strings_string_t) { (uint8_t*)(arg), (arg0) }; | |
// Call implementation of upper and get result. | |
// XXX: implementation allcates memory which potentially which only this | |
// module knows how to free? | |
exports_x_strings_upper_result_string_void_t ret; | |
strings_string_t ok; | |
ret.is_err = !exports_x_strings_upper_upper(&arg1, &ok); | |
if (!ret.is_err) { | |
ret.val.ok = ok; | |
} | |
// Encode strings_string_t as tagged union bytes and write them to RET_AREA. | |
// i.e. This is slightly more efficient code than casting | |
// `exports_x_strings_upper_result_string_void_t` as bytes and copying to | |
// RET_AREA. | |
uint8_t *ptr = (uint8_t *) &RET_AREA; | |
if ((ret).is_err) { | |
*((int8_t*)(ptr + 0)) = 1; | |
} else { | |
// XXX: Could just cast `strings_string_t ok` as bytes and memcpy to RET_AREA. | |
const strings_string_t *payload = &(ret).val.ok;*((int8_t*)(ptr + 0)) = 0; | |
*((size_t*)(ptr + 8)) = (*payload).len; | |
*((uint8_t **)(ptr + 4)) = (uint8_t *) (*payload).ptr; | |
} | |
return ptr; | |
} | |
// Ensure that the *_component_type.o object is linked in | |
extern void __component_type_object_force_link_strings(void); | |
void __component_type_object_force_link_strings_public_use_in_this_compilation_unit(void) { | |
__component_type_object_force_link_strings(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by `wit-bindgen` 0.33.0. DO NOT EDIT! | |
#ifndef __BINDINGS_STRINGS_H | |
#define __BINDINGS_STRINGS_H | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
#include <stdint.h> | |
#include <stdbool.h> | |
#include <stddef.h> | |
typedef struct strings_string_t { | |
uint8_t*ptr; | |
size_t len; | |
} strings_string_t; | |
typedef struct { | |
bool is_err; | |
union { | |
strings_string_t ok; | |
} val; | |
} exports_x_strings_upper_result_string_void_t; | |
// Exported Functions from `x:strings/upper` | |
bool exports_x_strings_upper_upper(strings_string_t *s, strings_string_t *ret); | |
// Helper Functions | |
void exports_x_strings_upper_result_string_void_free(exports_x_strings_upper_result_string_void_t *ptr); | |
// Transfers ownership of `s` into the string `ret` | |
void strings_string_set(strings_string_t *ret, const char*s); | |
// Creates a copy of the input nul-terminate string `s` and | |
// stores it into the component model string `ret`. | |
void strings_string_dup(strings_string_t *ret, const char*s); | |
// Deallocates the string pointed to by `ret`, deallocating | |
// the memory behind the string. | |
void strings_string_free(strings_string_t *ret); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "strings.h" | |
#include <stdlib.h> | |
#define OK true | |
bool exports_x_strings_upper_upper(strings_string_t *s, strings_string_t *ret) { | |
ret->len = s->len; | |
ret->ptr = (uint8_t *)malloc(ret->len); | |
if (ret->ptr == NULL) { | |
return !OK; | |
}; | |
const int n = s->len; | |
for (int i = 0; i < n; ++i) { | |
const uint8_t c = s->ptr[i]; | |
if (!(c < 97 || 122 > c)) { | |
continue; | |
} | |
ret->ptr[i] = c - 32; | |
} | |
return OK; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x:strings; | |
interface upper { | |
upper: func(s: string) -> result<string>; | |
} | |
world strings { | |
export upper; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment