Nyash ABIをC実装TypeBoxで提供する設計案 ======================================== 【核心的アイデア】 Nyash ABIそのものをTypeBox(C ABI)として実装することで、 Rust依存を排除し、セルフホスティングへの道を開く。 【基本構造】 1. Nyash ABIもTypeBoxの一種として定義 2. C言語で完全実装(Rust非依存) 3. 既存のプラグインシステムで配布可能 【実装詳細】 ■ TypeBox定義 ```c // nyash_abi_provider.h typedef struct { // TypeBox標準ヘッダ uint32_t abi_tag; // 'NABI' const char* name; // "NyashABIProvider" void* (*create)(void); // プロバイダインスタンス生成 // Nyash ABI専用拡張 struct { void* (*create_value)(uint64_t type_id, void* data); void* (*invoke_method)(void* obj, const char* method, void* args[], int argc); void (*retain)(void* value); void (*release)(void* value); } nyash_ops; } NyashABITypeBox; ``` ■ NyashValue(C構造体版) ```c typedef struct { uint64_t type_id; // 型識別子 uint64_t box_handle; // ポインタまたはインライン値 uint64_t metadata; // フラグ・追加情報 } NyashValue_C; ``` ■ 実装戦略 Phase 1: 最小C実装 ------------------ - 基本型(Integer/String/Bool)のみサポート - シンプルなメソッド呼び出し - plugins/nyash_abi_c/ として実装 Phase 2: フル機能実装 ------------------- - 全Box型サポート - エラーハンドリング - 非同期サポート Phase 3: Nyashで再実装 -------------------- - C実装をNyashで書き直し - セルフホスティング達成 【利点】 1. Rust完全非依存 2. 既存インフラ(TypeBox/C ABI)活用 3. 段階的移行可能 4. セルフホスティング対応 【実装例】 ```c // nyash_abi_impl.c static NyashValue_C call_method_c( const char* type_name, const char* method, void* instance, NyashValue_C* args, int argc ) { // StringBoxの例 if (strcmp(type_name, "StringBox") == 0) { if (strcmp(method, "length") == 0) { char* str = (char*)instance; int len = strlen(str); return (NyashValue_C){ .type_id = TYPE_INTEGER, .box_handle = (uint64_t)len, .metadata = INLINE_FLAG }; } } // 他のBox型も同様に実装... } ``` 【配布方法】 ```toml # nyash.toml [plugins.nyash_abi_provider] path = "plugins/nyash_abi_provider.so" abi = "c" types = ["NyashABIProvider"] ``` 【使用例(Nyashコード)】 ```nyash // TypeBox経由でNyash ABIプロバイダ取得 local abiType = getTypeBox("NyashABIProvider") local abi = abiType.create() // Nyash ABIで呼び出し! local str = abi.createValue(TYPE_STRING, "Hello") local len = abi.invoke(str, "length", []) print(len) // 5 ``` 【移行パス】 1. 現在: Rust実装のNyash ABI 2. Phase 12.5: C実装のNyash ABIプロバイダ追加 3. Phase 13: プロバイダ経由に移行 4. Phase 14: Nyashでプロバイダ再実装 5. Phase 15: 完全セルフホスティング 【技術的課題と解決】 - メモリ管理 → 参照カウント(Cで実装) - 型安全性 → type_idで厳密チェック - パフォーマンス → インライン値で最適化 - 互換性 → 既存Rust実装と同じインターフェース 【結論】 TypeBoxシステムを使ってNyash ABIをC実装すれば、 Rust依存を排除しつつ、既存のエコシステムを活用して スムーズにセルフホスティングへ移行できる。 「Everything is Box」の究極形:ABIすらBoxとして扱う!