Files
hakorune/docs/development/roadmap/native-plan/llvm/APE-Magic-Explained.md
Moe Charm cc2a820af7 feat(plugin): Fix plugin BoxRef return and Box argument support
- Fixed deadlock in FileBox plugin copyFrom implementation (single lock)
- Added TLV Handle (tag=8) parsing in calls.rs for returned BoxRefs
- Improved plugin loader with config path consistency and detailed logging
- Fixed loader routing for proper Handle type_id/fini_method_id resolution
- Added detailed logging for TLV encoding/decoding in plugin_loader_v2

Test docs/examples/plugin_boxref_return.nyash now works correctly:
- cloneSelf() returns FileBox Handle properly
- copyFrom(Box) accepts plugin Box arguments
- Both FileBox instances close and fini correctly

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-21 00:41:26 +09:00

122 lines
3.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🪄 APE (Actually Portable Executable) の魔法を解説!
**「えっ、1つのファイルが3つのOSで動くの」**
はい、本当です!これは**実在する技術**です!
## 🎩 **APEの魔法の仕組み**
### **実例を見てみよう**
```bash
# これが実際のAPEバイナリ
$ ls -la hello.com
-rwxr-xr-x 1 user user 65536 Aug 20 hello.com
# Linuxで実行
$ ./hello.com
Hello from Linux!
# 同じファイルをWindowsにコピー
> hello.com
Hello from Windows!
# 同じファイルをmacOSで実行
$ ./hello.com
Hello from macOS!
```
**たった1つのファイル `hello.com` が全部で動く!**
## 🔮 **どうやって実現してるの?**
### **秘密:ファイルヘッダーの魔法**
APEファイルの先頭部分
```
00000000: 4d5a 9000 0300 0000 0400 0000 ffff 0000 MZ.............. # Windows PE
00000010: b800 0000 0000 0000 4000 0000 0000 0000 ........@.......
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0080 0000 0000 ................
00000040: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............ # Linux ELF
```
**同じファイルに複数のOSのヘッダーが共存**
### **OSごとの読み方**
1. **Windows**: 「MZ」で始まる → PEファイルとして実行
2. **Linux**: ELFマジックナンバーを探す → ELFとして実行
3. **macOS**: Mach-Oヘッダーを探す → Mach-Oとして実行
## 🛠️ **Cosmopolitan Libc - 実在するプロジェクト**
**GitHubで公開されています**
- https://github.com/jart/cosmopolitan
- 開発者: Justine Tunney (元Google)
- スター数: 17,000+ ⭐
### **実際のビルド方法**
```bash
# Cosmopolitanを使ったビルド
gcc -g -O -static \
-fno-pie -no-pie \
-nostdlib -nostdinc \
-o hello.com \
hello.c \
cosmopolitan.a \
-Wl,--gc-sections \
-Wl,-T,ape.lds
```
## 📊 **APEの利点と制限**
### **利点** ✅
- **配布が超簡単**: 1ファイルで全OS対応
- **依存関係なし**: 完全に自己完結
- **小さいサイズ**: 静的リンクでも小さい
### **制限** ⚠️
- **x86_64のみ**: ARM版はまだ実験的
- **GUI制限**: 基本的にCLIアプリ向け
- **OS固有機能**: 一部制限あり
## 🎯 **NyashでのAPE活用案**
### **段階的アプローチ**
**Phase 1: 通常のマルチターゲット**(現実的)
```bash
nyashc --targets linux,windows,macos
# → 3つの別々のファイル生成
```
**Phase 2: APE実験**6ヶ月後
```bash
nyashc --target ape
# → nyash.com (全OS対応の1ファイル)
```
### **実装イメージ**
```rust
// NyashのLLVM IR → Cコード生成
let c_code = transpile_to_c(&llvm_ir);
// Cosmopolitanでコンパイル
compile_with_cosmopolitan(&c_code, "nyash.com");
```
## 🤔 **本当に必要?**
**正直な評価**
- **配布簡単さ**: ⭐⭐⭐⭐⭐ 最高!
- **実装難易度**: ⭐⭐ 意外と簡単Cosmopolitan使えば
- **実用性**: ⭐⭐⭐ CLIツールなら十分実用的
- **かっこよさ**: ⭐⭐⭐⭐⭐ 最高にクール!
## 💡 **結論**
APEは**「欲張り」じゃなくて「賢い」**アプローチ!
でも、まずは普通のマルチターゲット対応から始めて、APEは「究極の目標」として楽しみに取っておくのが現実的かも
**にゃーも「Everything is Box」なら、APEは「Everything is ONE Binary」**🎩✨