|
| 1 | +// ===================================== |
| 2 | +// WARNING: very unstable API, for internal use only |
| 3 | +// ===================================== |
| 4 | + |
| 5 | +#ifndef moonbit_h_INCLUDED |
| 6 | +#define moonbit_h_INCLUDED |
| 7 | + |
| 8 | +#ifdef MOONBIT_NATIVE_NO_SYS_HEADER |
| 9 | +#include "moonbit-fundamental.h" |
| 10 | +#else |
| 11 | +#include <stddef.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <math.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +#if defined (_WIN32) || defined (_WIN64) |
| 21 | +#ifdef MOONBIT_BUILD_RUNTIME |
| 22 | +#define MOONBIT_EXPORT __declspec(dllexport) |
| 23 | +#else |
| 24 | +#ifdef MOONBIT_USE_SHARED_RUNTIME |
| 25 | +#define MOONBIT_EXPORT __declspec(dllimport) |
| 26 | +#else |
| 27 | +#define MOONBIT_EXPORT |
| 28 | +#endif |
| 29 | +#endif |
| 30 | +#define MOONBIT_FFI_EXPORT __declspec(dllexport) |
| 31 | +#else |
| 32 | +#define MOONBIT_EXPORT // __attribute__ ((visibility("default"))) |
| 33 | +#define MOONBIT_FFI_EXPORT |
| 34 | +#endif |
| 35 | + |
| 36 | +enum moonbit_block_kind { |
| 37 | + // 0 => regular block |
| 38 | + moonbit_BLOCK_KIND_REGULAR = 0, |
| 39 | + // 1 => array of pointers |
| 40 | + moonbit_BLOCK_KIND_REF_ARRAY = 1, |
| 41 | + // 2 => array of immediate value/string/bytes |
| 42 | + moonbit_BLOCK_KIND_VAL_ARRAY = 2, |
| 43 | + // 3 => external object with custom deallocator |
| 44 | + // Note: if we run out of block kind in the future, |
| 45 | + // we may turn [3] into [moonbit_BLOCK_KIND_EXTENDED], |
| 46 | + // and store the actual kind & other meta information in the first field |
| 47 | + moonbit_BLOCK_KIND_EXTERNAL = 3 |
| 48 | +}; |
| 49 | + |
| 50 | +struct moonbit_object { |
| 51 | + int32_t rc; |
| 52 | + /* The layout of 32bit meta data (starting from most significant bit): |
| 53 | +
|
| 54 | + enum moonbit_block_kind kind : 2; |
| 55 | + union { |
| 56 | + // when [kind = BLOCK_KIND_REGULAR] |
| 57 | + struct { |
| 58 | + // The 22 length bits are separated into two 11 bit parts: |
| 59 | + // |
| 60 | + // - [ptr_field_offset] is the offset of the first pointer field, |
| 61 | + // counted by the number of 32-bit words. |
| 62 | + // The object header itself is also included. |
| 63 | + // |
| 64 | + // - [n_ptr_fields] is the number of pointer fields |
| 65 | + // |
| 66 | + // We rearrange the layout of all pointer fields |
| 67 | + // so that all pointer fields are placed at the end. |
| 68 | + // This make it easy for the runtime to enumerate all pointer fields in an object, |
| 69 | + // without any static type information. |
| 70 | + // |
| 71 | + // The total length of the object can be reconstructed via: |
| 72 | + // |
| 73 | + // ptr_field_offset * 4 + n_ptr_fields * sizeof(void*) |
| 74 | + unsigned int ptr_field_offset : 11; |
| 75 | + unsigned int n_ptr_fields : 11; |
| 76 | + // For blocks, we steal 8 bits from the length to represent enum tag |
| 77 | + unsigned int tag : 8; |
| 78 | + }; |
| 79 | + // when [kind = BLOCK_KIND_REF_ARRAY] or [kind = BLOCK_KIND_VAL_ARRAY] |
| 80 | + struct { |
| 81 | + // The size of array element is [2^object_size_shift] bytes |
| 82 | + unsigned int object_size_shift : 2; |
| 83 | + // The number of elements |
| 84 | + unsigned int len : 28; |
| 85 | + } array_header; |
| 86 | + // when [kind = BLOCK_KIND_REF_EXTERN] |
| 87 | + uint32_t size : 30; |
| 88 | + }; |
| 89 | + */ |
| 90 | + uint32_t meta; |
| 91 | +}; |
| 92 | + |
| 93 | +#define Moonbit_object_header(obj) ((struct moonbit_object*)(obj) - 1) |
| 94 | +#define Moonbit_object_kind(obj) (Moonbit_object_header(obj)->meta >> 30) |
| 95 | +#define Moonbit_object_tag(obj) (Moonbit_object_header(obj)->meta & 0xFF) |
| 96 | +#define Moonbit_array_length(obj) (Moonbit_object_header(obj)->meta & (((uint32_t)1 << 28) - 1)) |
| 97 | +#define Moonbit_array_elem_size_shift(obj) ((Moonbit_object_header(obj)->meta >> 28) & 3) |
| 98 | +#define Moonbit_make_array_header(kind, elem_size_shift, length)\ |
| 99 | + (((uint32_t)kind << 30)\ |
| 100 | + | (((uint32_t)(elem_size_shift) & 3) << 28)\ |
| 101 | + | ((length) & (((uint32_t)1 << 28) - 1))) |
| 102 | + |
| 103 | +MOONBIT_EXPORT void *libc_malloc(size_t size); |
| 104 | +MOONBIT_EXPORT void libc_free(void *ptr); |
| 105 | +MOONBIT_EXPORT void *moonbit_malloc(size_t size); |
| 106 | +MOONBIT_EXPORT void moonbit_incref(void *obj); |
| 107 | +MOONBIT_EXPORT void moonbit_decref(void *obj); |
| 108 | + |
| 109 | +typedef uint16_t *moonbit_string_t; |
| 110 | +typedef uint8_t *moonbit_bytes_t; |
| 111 | + |
| 112 | +struct moonbit_view_t { |
| 113 | + int32_t start; |
| 114 | + int32_t len; |
| 115 | + void *buf; |
| 116 | +}; |
| 117 | + |
| 118 | +MOONBIT_EXPORT moonbit_string_t moonbit_make_string(int32_t size, uint16_t value); |
| 119 | +MOONBIT_EXPORT moonbit_bytes_t moonbit_make_bytes(int32_t size, int value); |
| 120 | +MOONBIT_EXPORT int32_t *moonbit_make_int32_array(int32_t len, int32_t value); |
| 121 | +MOONBIT_EXPORT void **moonbit_make_ref_array(int32_t len, void *value); |
| 122 | +MOONBIT_EXPORT int64_t *moonbit_make_int64_array(int32_t len, int64_t value); |
| 123 | +MOONBIT_EXPORT double *moonbit_make_double_array(int32_t len, double value); |
| 124 | +MOONBIT_EXPORT float *moonbit_make_float_array(int32_t len, float value); |
| 125 | +MOONBIT_EXPORT void **moonbit_make_extern_ref_array(int32_t len, void *value); |
| 126 | +MOONBIT_EXPORT void *moonbit_make_view_array(int32_t len, void *ptr); |
| 127 | + |
| 128 | +/* `finalize` should drop the payload of the external object. |
| 129 | + `finalize` MUST NOT drop the [moonbit_external_object] container itself. |
| 130 | +
|
| 131 | + `payload_size` is the size of payload, excluding [drop]. |
| 132 | +
|
| 133 | + The returned pointer points directly to the start of user payload. |
| 134 | + The finalizer pointer would be stored at the end of the object, after user payload. |
| 135 | +*/ |
| 136 | +MOONBIT_EXPORT void *moonbit_make_external_object( |
| 137 | + void (*finalize)(void *self), |
| 138 | + uint32_t payload_size |
| 139 | +); |
| 140 | + |
| 141 | +MOONBIT_EXPORT extern uint8_t* const moonbit_empty_int8_array; |
| 142 | +MOONBIT_EXPORT extern uint16_t* const moonbit_empty_int16_array; |
| 143 | +MOONBIT_EXPORT extern int32_t* const moonbit_empty_int32_array; |
| 144 | +MOONBIT_EXPORT extern int64_t* const moonbit_empty_int64_array; |
| 145 | +MOONBIT_EXPORT extern float* const moonbit_empty_float_array; |
| 146 | +MOONBIT_EXPORT extern double* const moonbit_empty_double_array; |
| 147 | +MOONBIT_EXPORT extern void** const moonbit_empty_ref_array; |
| 148 | +MOONBIT_EXPORT extern void** const moonbit_empty_extern_ref_array; |
| 149 | +MOONBIT_EXPORT extern struct moonbit_view_t* const moonbit_empty_view_array; |
| 150 | + |
| 151 | +#ifdef __cplusplus |
| 152 | +} |
| 153 | +#endif |
| 154 | + |
| 155 | +#endif // moonbit_h_INCLUDED |
0 commit comments