typedef struct {
uint32_t abi_tag; // 必須: 0x54594258 ('TYBX')
uint16_t version; // APIバージョン(現在: 1)
uint16_t struct_size; // sizeof(NyashTypeBox)
const char* name; // Box型名(NULL終端)
// 基本操作
void* (*create)(void* args);
void (*destroy)(void* self);
// 高速ディスパッチ
uint32_t (*resolve)(const char* name);
NyResult (*invoke_id)(void* self, uint32_t method_id,
NyValue* args, int argc);
// 互換性
void* (*method)(void* self, const char* name,
void** args, int argc);
// メタ情報
const char* (*get_type_info)(void);
uint64_t capabilities;
// 予約済み
void* reserved[4];
} NyashTypeBox;
void* (*create)(void* args);
args
- 初期化パラメータ(型依存)void (*destroy)(void* self);
self
- 破棄するインスタンスuint32_t (*resolve)(const char* name);
name
- メソッド名NyResult (*invoke_id)(void* self, uint32_t method_id,
NyValue* args, int argc);
self
- 対象インスタンスmethod_id
- resolveで取得したIDargs
- 引数配列argc
- 引数数void* (*method)(void* self, const char* name,
void** args, int argc);
const char* (*get_type_info)(void);
{
"methods": [
{"name": "length", "id": 1, "returns": "int"},
{"name": "concat", "id": 2, "returns": "string"}
]
}
typedef struct __attribute__((aligned(16))) {
uint64_t type_tag; // 型識別子
union {
int64_t i64; // 整数
double f64; // 浮動小数点
void* ptr; // ポインタ
uint64_t bits; // ビットパターン
} payload;
} NyValue;
#define NYVAL_NULL 0x00
#define NYVAL_BOOL 0x01
#define NYVAL_INT 0x02
#define NYVAL_FLOAT 0x03
#define NYVAL_STRING 0x04
#define NYVAL_BOX 0x05
#define NYVAL_ARRAY 0x06
#define NYVAL_MAP 0x07
// 値生成
NyValue ny_value_null(void);
NyValue ny_value_bool(bool val);
NyValue ny_value_int(int64_t val);
NyValue ny_value_float(double val);
NyValue ny_value_string(const char* str);
NyValue ny_value_box(void* box, NyashTypeBox* type);
// 値取得
bool ny_is_null(NyValue val);
bool ny_to_bool(NyValue val);
int64_t ny_to_int(NyValue val);
double ny_to_float(NyValue val);
const char* ny_to_string(NyValue val);
void* ny_to_box(NyValue val);
#define NYASH_CAP_THREAD_SAFE (1 << 0) // スレッドセーフ
#define NYASH_CAP_ASYNC_SAFE (1 << 1) // async/await対応
#define NYASH_CAP_REENTRANT (1 << 2) // 再入可能
#define NYASH_CAP_PARALLELIZABLE (1 << 3) // 並列実行可能
#define NYASH_CAP_PURE (1 << 4) // 副作用なし
#define NYASH_CAP_DETERMINISTIC (1 << 5) // 決定的動作
#define NYASH_CAP_GPU_ACCEL (1 << 8) // GPU実行可能
#define NYASH_CAP_SIMD_OPTIMIZED (1 << 9) // SIMD最適化済み
#define NYASH_CAP_LAZY_EVAL (1 << 10) // 遅延評価対応
typedef struct {
int status; // 0 = 成功、非0 = エラー
NyValue value; // 戻り値(成功時)
const char* error_msg; // エラーメッセージ(エラー時)
} NyResult;
#define NY_OK 0
#define NY_ERROR_GENERIC -1
#define NY_ERROR_NULL_PTR -2
#define NY_ERROR_TYPE -3
#define NY_ERROR_BOUNDS -4
#define NY_ERROR_NOT_FOUND -5
#define NY_ERROR_MEMORY -6
NyResult ny_result_ok(NyValue val);
NyResult ny_result_error(const char* msg);
bool ny_result_is_ok(NyResult res);
bool ny_result_is_error(NyResult res);
NyashTypeBox* ny_host_get_typebox(const char* name);
name
- Box型名// MapBoxがArrayBoxを生成する例
NyResult map_keys(void* self, uint32_t method_id,
NyValue* args, int argc) {
MapBoxImpl* map = (MapBoxImpl*)self;
// ArrayBoxを取得
NyashTypeBox* array_type = ny_host_get_typebox("ArrayBox");
if (!array_type) {
return ny_result_error("ArrayBox not found");
}
// 新しいArrayBoxを生成
void* array = array_type->create(NULL);
// キーを追加
for (int i = 0; i < map->key_count; i++) {
NyValue key = ny_value_string(map->keys[i]);
array_type->invoke_id(array, 1, &key, 1); // push
}
return ny_result_ok(ny_value_box(array, array_type));
}
// 良い例:文字列をコピー
void* string_concat(void* self, NyValue* args, int argc) {
char* str1 = (char*)self;
char* str2 = ny_to_string(args[0]);
// 新しいメモリを確保
size_t len = strlen(str1) + strlen(str2) + 1;
char* result = malloc(len);
snprintf(result, len, "%s%s", str1, str2);
return ny_value_string(result);
}
version
フィールドで確認struct_size >= sizeof(NyashTypeBox)
reserved
配列を使用