Files
hakorune/docs/予定/native-plan/issues/phase_10_app_migration.md
Moe Charm f7f651d095 📋 Phase 10: Add Classic C Applications Migration Plan - Triple Porting Project
## Summary
Created comprehensive migration plan for 3 classic C applications to Nyash
- Issue #98: Triple porting project specification
- Advanced memory management features testing strategy

## Applications Selected

### 1. 🌐 Tinyproxy - Zero-copy Detection Testing
- **Target**: HTTP proxy server (~5000 lines C)
- **Focus**: Zero-copy optimization validation
- **APIs**: `is_shared_with()`, `share_reference()`
- **Goal**: 90%+ zero-copy rate in HTTP forwarding

### 2. 🎮 Chip-8 Emulator - fini Propagation & Weak References
- **Target**: 8-bit game machine emulator (~1000 lines C)
- **Focus**: Automatic cleanup chains, circular reference handling
- **APIs**: `fini()` propagation, `is_alive()` weak checking
- **Goal**: 60FPS stable emulation + complete fini propagation

### 3. ✏️ kilo Text Editor - "Accidental Full Copy" Detection
- **Target**: Lightweight terminal editor (~1000 lines C)
- **Focus**: Memory efficiency monitoring, copy detection
- **APIs**: `memory_footprint()`, real-time efficiency warnings
- **Goal**: 95%+ memory efficiency for 1MB+ file editing

## Technical Implementation Plan

### New API Requirements
- **Zero-copy APIs**: BufferBox sharing detection
- **fini propagation**: Automatic dependency cleanup
- **Weak references**: Circular reference prevention
- **Memory monitoring**: Real-time efficiency tracking

### Development Timeline
- **Phase 10.1**: Tinyproxy (1 week) - Zero-copy foundation
- **Phase 10.2**: Chip-8 (1 week) - Memory management testing
- **Phase 10.3**: kilo (1 week) - Efficiency monitoring
- **Phase 10.4**: Integration (1 week) - Performance analysis

## Strategic Value
1. **Practical proof**: Real applications can be built with Nyash
2. **Feature validation**: Advanced memory management in production
3. **Developer experience**: Copilot collaboration productivity
4. **Ecosystem expansion**: Concrete Nyash application examples

**Success of this migration establishes Nyash as a practical programming language\!** 🚀

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-15 14:48:28 +09:00

7.2 KiB
Raw Blame History

Phase 10: Classic C Applications Migration to Nyash

🎯 概要

3つの著名なCアプリケーションをNyashに移植し、新実装された高度なメモリ管理機能を実戦テストする。

📦 移植対象アプリケーション(優先順位順)

1. 🌐 Tinyproxy - ゼロコピー判定機能の実証

元実装: https://github.com/tinyproxy/tinyproxy サイズ: ~5000行C、軽量HTTPプロキシサーバー Nyash移植目標: apps/tinyproxy_nyash/

🔍 ゼロコピー判定テストケース

// HTTPリクエスト転送でのメモリ効率検証
static box ProxyServer {
    init { upstream_buffer, downstream_buffer }
    
    relay_data(client_data) {
        // ⭐ ゼロコピー判定:バッファーが共有されているかチェック
        if (me.upstream_buffer.is_shared_with(client_data)) {
            console.log("✅ Zero-copy achieved!")
        } else {
            console.log("❌ Unnecessary copy detected")
        }
        
        // 大量データ転送での最適化確認
        return me.upstream_buffer.share_reference(client_data)
    }
}

📋 実装要件

  • HTTPプロキシの基本機能GET/POST転送
  • SocketBoxでのクライアント・サーバー接続
  • BufferBoxでの効率的なデータ転送
  • ゼロコピー判定APIの実装・テスト

2. 🎮 Chip-8エミュレーター - fini伝播とweak生存チェック

元実装: https://github.com/mattmikolay/chip-8 (参考) サイズ: ~1000行C、8ビットゲーム機エミュレーター Nyash移植目標: apps/chip8_nyash/

🔍 メモリ管理テストケース

// CPU・メモリ・グラフィックスの相互参照関係でのfini伝播テスト
static box Chip8CPU {
    init { memory, graphics, sound }
    
    fini() {
        // ⭐ fini伝播依存オブジェクトの自動クリーンアップ
        console.log("🔄 CPU cleanup triggered")
        me.memory.cleanup()  // メモリバンクの解放
        me.graphics.cleanup()  // VRAM解放
    }
}

static box Chip8Memory {
    init { ram, weak_cpu_ref }  // CPUへの弱参照
    
    read_byte(address) {
        // ⭐ weak生存チェックCPUがまだ生きているか確認
        if (me.weak_cpu_ref.is_alive()) {
            return me.ram.get(address)
        } else {
            console.log("⚠️ CPU destroyed, memory access blocked")
            return null
        }
    }
}

📋 実装要件

  • Chip-8命令セット実装35命令
  • 64x32ピクセルグラフィックスWebCanvasBox使用)
  • サウンド出力(SoundBox使用)
  • fini伝播システムweak参照の実戦テスト

3. ✏️ kilo テキストエディター - 「うっかり全体コピー」検出

元実装: https://github.com/antirez/kilo サイズ: ~1000行C、軽量ターミナルエディター Nyash移植目標: apps/kilo_nyash/

🔍 メモリ効率テストケース

// 大きなテキストファイル編集での不必要なコピー検出
static box TextBuffer {
    init { lines, undo_stack }
    
    insert_char(row, col, char) {
        local old_lines_size = me.lines.memory_footprint()
        
        // 文字挿入操作
        me.lines.get(row).insert_at(col, char)
        
        local new_lines_size = me.lines.memory_footprint()
        local size_diff = new_lines_size - old_lines_size
        
        // ⭐ 「うっかり全体コピー」検出
        if (size_diff > 1000) {  // 1文字挿入で1KB以上増加
            console.log("🚨 INEFFICIENT COPY DETECTED!")
            console.log("Expected: 1 byte, Actual: " + size_diff + " bytes")
            me.log_memory_leak_warning()
        }
    }
    
    // 大規模な検索・置換での効率性チェック
    search_and_replace(pattern, replacement) {
        local initial_memory = me.lines.memory_footprint()
        
        // 検索・置換実行
        me.lines.replace_all(pattern, replacement)
        
        local final_memory = me.lines.memory_footprint()
        // メモリ使用量が2倍を超えた場合は問題
        if (final_memory > initial_memory * 2) {
            console.log("⚠️ Memory usage doubled during replace operation")
        }
    }
}

📋 実装要件

  • ターミナル操作(ConsoleBoxでの入出力)
  • ファイル読み書き(FileBox使用)
  • 基本的な編集機能(カーソル移動、挿入、削除)
  • メモリ効率監視コピー検出システム

🛠️ 技術的実装指針

共通アーキテクチャ

// 各アプリケーション共通の構造
static box AppName {
    init { core_components }
    
    main() {
        me.initialize_components()
        me.run_main_loop()
        me.cleanup_resources()
    }
    
    // メモリ効率レポート(全アプリ共通)
    memory_report() {
        return new MapBox()
            .set("zero_copy_count", me.zero_copy_operations)
            .set("unnecessary_copies", me.detected_copies)
            .set("memory_leaks", me.fini_failures)
            .set("weak_ref_cleanups", me.weak_cleanup_count)
    }
}

新API要件

  1. ゼロコピー判定API

    • BufferBox.is_shared_with(other) → BoolBox
    • BufferBox.share_reference(data) → 参照共有
  2. fini伝播システム

    • 自動的な依存オブジェクトクリーンアップ
    • クリーンアップチェーンの可視化
  3. weak参照システム

    • WeakBox.is_alive() → BoolBox
    • 循環参照の自動検出・回避
  4. メモリ効率監視

    • Box.memory_footprint() → IntegerBox
    • コピー発生の検出・警告

🎯 期待される成果

パフォーマンス目標

  • Tinyproxy: HTTP転送でのゼロコピー率 90%以上
  • Chip-8: 60FPSエミュレーション + fini伝播の完全動作
  • kilo: 1MB+ファイル編集でのメモリ効率 95%以上

学習効果

  • Copilot: 大規模Nyashアプリケーション開発経験
  • 開発者: 新メモリ管理機能の実用性確認
  • コミュニティ: Nyashでの実用アプリケーション事例

📅 実装計画

Phase 10.1: Tinyproxy実装 (1週間)

  • HTTPプロキシ基本機能
  • ゼロコピー判定API実装・テスト

Phase 10.2: Chip-8実装 (1週間)

  • エミュレーター基本機能
  • fini伝播・weak参照の実戦テスト

Phase 10.3: kilo実装 (1週間)

  • テキストエディター基本機能
  • メモリ効率監視システム

Phase 10.4: 統合テスト・最適化 (1週間)

  • 3アプリケーション同時実行テスト
  • パフォーマンス分析・改善

🚀 この移植プロジェクトの意義

  1. 実用性の実証: Nyashで実際のアプリケーションが作れることを証明
  2. 新機能の検証: ゼロコピー・fini・weakの実戦テスト
  3. 開発体験の向上: Copilotとの協調開発での生産性検証
  4. エコシステム拡充: Nyashアプリケーションの具体例提供

この移植が成功すれば、Nyashは「実用的なプログラミング言語」として確立されます 🎉