fix(plugins): Fix FileBox plugin I/O operations and add TLV debugging

## FileBox Plugin Fixes

### Issue Resolution
- Fixed file write operations not persisting to disk
- Fixed TLV tag mismatch preventing read operations
- Added comprehensive TLV debugging capability

### Changes Made

#### plugins/nyash-filebox-plugin/src/lib.rs
- Added file.flush() after write operations to ensure data persistence
- Modified tlv_parse_bytes to accept both String(6) and Bytes(7) tags
- Resolves compatibility issue with plugin-tester String encoding

#### tools/plugin-tester/src/main.rs
- Added `tlv-debug` subcommand with detailed TLV analysis
- Provides hex dumps, encoding/decoding verification
- Full plugin round-trip testing with file I/O validation
- Detailed error analysis for debugging TLV protocol issues

## Test Results

### Before Fix
```
INFO: WRITE 8 bytes
INFO: READ 0 bytes  ← Problem: no data read
✗: Plugin round-trip failed\!
```

### After Fix
```
INFO: WRITE 8 bytes
INFO: READ 8 bytes  ← Fixed: data successfully read
✓: Plugin round-trip successful\!
```

## Technical Details
- Root cause: Missing file.flush() + TLV tag type mismatch
- Plugin-tester sends String(6), plugin expected Bytes(7) only
- File buffer not flushed to disk before close/reopen
- Solution: Added flush() + dual tag support for compatibility

This completes the core BID-FFI plugin I/O functionality validation.
Nyash integration still needs method dispatch optimization.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-18 11:21:04 +09:00
parent 5caee18764
commit f5ab4910e4
2 changed files with 192 additions and 1 deletions

View File

@ -297,6 +297,10 @@ pub extern "C" fn nyash_plugin_invoke(
if let Some(file) = inst.file.as_mut() {
match file.write(&data) {
Ok(n) => {
// ファイルバッファをフラッシュ(重要!)
if let Err(_) = file.flush() {
return NYB_E_PLUGIN_ERROR;
}
log_info(&format!("WRITE {} bytes", n));
return write_tlv_i32(n as i32, _result, _result_len);
}
@ -433,7 +437,8 @@ fn tlv_parse_bytes(data: &[u8]) -> Result<Vec<u8>, ()> {
if pos + 4 > data.len() { return Err(()); }
let tag = data[pos]; let _res = data[pos+1];
let size = u16::from_le_bytes([data[pos+2], data[pos+3]]) as usize; pos += 4;
if tag != 7 || pos + size > data.len() { return Err(()); }
// StringタグもBytesタグも受け付ける互換性のため
if (tag != 6 && tag != 7) || pos + size > data.len() { return Err(()); }
Ok(data[pos..pos+size].to_vec())
}