Files
hakorune/tools/hako_check/hako_source_checker.hako
nyash-codex 301b1d212a Phase 21.2 Complete: VM Adapter正規実装 + devブリッジ完全撤去
## 🎉 Phase 21.2完全達成

###  実装完了
- VM static box 永続化(singleton infrastructure)
- devブリッジ完全撤去(adapter_dev.rs削除、by-name dispatch削除)
- .hako正規実装(MirCallV1Handler, AbiAdapterRegistry等)
- text-merge経路完全動作
- 全phase2120 adapter reps PASS(7テスト)

### 🐛 バグ修正
1. strip_local_decl修正
   - トップレベルのみlocal削除、メソッド内は保持
   - src/runner/modes/common_util/hako.rs:29

2. static box フィールド永続化
   - MirInterpreter singleton storage実装
   - me parameter binding修正(1:1マッピング)
   - getField/setField string→singleton解決
   - src/backend/mir_interpreter/{mod,exec,handlers/boxes_object_fields}.rs

3. Map.len alias rc=0修正
   - [map/missing]パターン検出でnull扱い(4箇所)
   - lang/src/vm/boxes/mir_call_v1_handler.hako:91-93,131-133,151-153,199-201

### 📁 主要変更ファイル

#### Rust(VM Runtime)
- src/backend/mir_interpreter/mod.rs - static box singleton storage
- src/backend/mir_interpreter/exec.rs - parameter binding fix
- src/backend/mir_interpreter/handlers/boxes_object_fields.rs - singleton resolution
- src/backend/mir_interpreter/handlers/calls.rs - dev bridge removal
- src/backend/mir_interpreter/utils/mod.rs - adapter_dev module removal
- src/backend/mir_interpreter/utils/adapter_dev.rs - DELETED (7555 bytes)
- src/runner/modes/vm.rs - static box declaration collection
- src/runner/modes/common_util/hako.rs - strip_local_decl fix
- src/instance_v2.rs - Clone implementation

#### Hako (.hako実装)
- lang/src/vm/boxes/mir_call_v1_handler.hako - [map/missing] detection
- lang/src/vm/boxes/abi_adapter_registry.hako - NEW (adapter registry)
- lang/src/vm/helpers/method_alias_policy.hako - method alias support

#### テスト
- tools/smokes/v2/profiles/quick/core/phase2120/s3_vm_adapter_*.sh - 7 new tests

### 🎯 テスト結果
```
 s3_vm_adapter_array_len_canary_vm.sh
 s3_vm_adapter_array_len_per_recv_canary_vm.sh
 s3_vm_adapter_array_length_alias_canary_vm.sh
 s3_vm_adapter_array_size_alias_canary_vm.sh
 s3_vm_adapter_map_len_alias_state_canary_vm.sh
 s3_vm_adapter_map_length_alias_state_canary_vm.sh
 s3_vm_adapter_map_size_struct_canary_vm.sh
```

環境フラグ: HAKO_ABI_ADAPTER=1 HAKO_ABI_ADAPTER_DEV=0

### 🏆 設計品質
-  ハードコード禁止(AGENTS.md 5.1)完全準拠
-  構造的・一般化設計(特定Box名のif分岐なし)
-  後方互換性保持(既存コード破壊ゼロ)
-  text-merge経路(.hako依存関係正しくマージ)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 19:32:44 +09:00

142 lines
5.8 KiB
Plaintext

// hako_source_checker.hako — HakoSourceCheckerBox
// Purpose: Lint/structure checks for .hako sources (Phase 21.3)
// Rules (MVP):
// HC001: Forbid top-level assignment inside static box (before any method)
// HC002: Forbid include "..." lines (using+alias only)
// HC003: Using must be quoted (using "pkg.name" as Alias)
// HC004: Encourage JsonFragBox helpers for JSON scans (warn when substring/indexOf used with seg/inst_json)
using selfhost.shared.common.string_helpers as Str
using selfhost.shared.json.utils.json_frag as JsonFragBox
static box HakoSourceCheckerBox {
// Public: check a file path. Returns 0 on success; >0 on issues.
check_file(path) {
local f = new FileBox()
if f.open(path) == 0 { print("[lint/error] cannot open: " + path); return 2 }
local text = f.read(); f.close()
return me.check_source(text, path)
}
// Public: check raw source
check_source(text, path) {
local issues = new ArrayBox()
me._rule_include_forbidden(text, path, issues)
me._rule_using_quoted(text, path, issues)
me._rule_static_top_assign(text, path, issues)
me._rule_jsonfrag_usage(text, path, issues)
local n = issues.size()
if n > 0 {
do { local i=0; while i<n { print(issues.get(i)); i=i+1 } } while 0
return n
}
return 0
}
// HC002: include is forbidden
_rule_include_forbidden(text, path, out) {
local lines = text.split("\n")
do { local i=0; while i<lines.size() { local ln=lines.get(i); local trimmed=me._ltrim(ln); if trimmed.indexOf("include \"") == 0 { out.push("[HC002] include is forbidden (use using+alias): " + path + ":" + Str.int_to_str(i+1)) }; i=i+1 } } while 0
}
// HC003: using must be quoted
_rule_using_quoted(text, path, out) {
local lines = text.split("\n")
do { local i=0; while i<lines.size() { local ln=lines.get(i); local t=me._ltrim(ln); if t.indexOf("using ") == 0 { if t.indexOf("using \"") != 0 { out.push("[HC003] using must be quoted: " + path + ":" + Str.int_to_str(i+1)) } }; i=i+1 } } while 0
}
// HC001: static box top-level assignment (before any method) is forbidden
_rule_static_top_assign(text, path, out) {
local n = Str.len(text); local line = 1
local in_static = 0; local brace = 0; local in_method = 0
do { local i=0; while i<n { local c = text.substring(i, i+1)
// crude line counting
if c == "\n" { line = line + 1 }
// detect "static box"
if in_static == 0 {
if me._match_kw(text, i, "static box ") { in_static = 1; in_method = 0 }
}
if in_static == 1 {
// method start
if in_method == 0 && me._match_kw(text, i, "method ") { in_method = 1 }
// brace tracking
if c == "{" { brace = brace + 1 }
if c == "}" {
brace = brace - 1
if brace <= 0 { in_static = 0; in_method = 0 }
}
// assignment at column start (rough heuristic): letter at i and next '=' later
if in_method == 0 {
// find line start segment
local lstart = me._line_start(text, i)
local head = text.substring(lstart, i+1)
// only check at the first non-space of the line
if me._is_line_head(text, i) == 1 {
// identifier = ... is suspicious
if me._is_ident_start(c) == 1 {
// scan next few chars for '=' (up to EOL)
local seen_eq = 0
do { local off=0; while off<n { local j = i + 1 + off; if j>=n { break }; local cj=text.substring(j,j+1); if cj=="\n" { break }; if cj=="=" { seen_eq=1; break }; off=off+1 } } while 0
if seen_eq == 1 {
out.push("[HC001] top-level assignment in static box (use lazy init in method): " + path + ":" + Str.int_to_str(line))
}
}
}
}
i=i+1 } } while 0
}
// HC004: encourage JsonFragBox for JSON scans
_rule_jsonfrag_usage(text, path, out) {
// If the file manipulates mir_call/inst_json/seg and uses indexOf/substring heavily, warn.
local suspicious = 0
if text.indexOf("\"mir_call\"") >= 0 || text.indexOf("inst_json") >= 0 || text.indexOf(" seg") >= 0 {
if text.indexOf(".indexOf(") >= 0 || text.indexOf(".substring(") >= 0 { suspicious = 1 }
}
if suspicious == 1 && text.indexOf("JsonFragBox.") < 0 {
out.push("[HC004] JSON scan likely brittle; prefer JsonFragBox helpers: " + path)
}
}
// helpers
_ltrim(s) { return me._ltrim_chars(s, " \t") }
_ltrim_chars(s, cs) {
local n = Str.len(s)
local head = 0
do { local i=0; while i<n { local ch=s.substring(i,i+1); if ch!=" " && ch!="\t" { head=i; break }; if i==n-1 { head=n }; i=i+1 } } while 0
return s.substring(head)
}
_match_kw(s, i, kw) {
local k = Str.len(kw)
if i + k > Str.len(s) { return 0 }
if s.substring(i, i+k) == kw { return 1 }
return 0
}
_is_ident_start(c) {
// ASCII alpha or _
if c >= "A" && c <= "Z" { return 1 }
if c >= "a" && c <= "z" { return 1 }
if c == "_" { return 1 }
return 0
}
_is_line_head(s, i) {
// true if all chars before i on same line are spaces/tabs
do { local r=0; while r<=i { if i==0 { return 1 }; local j=i - 1 - r; local cj=s.substring(j,j+1); if cj=="\n" { return 1 }; if cj!=" " && cj!="\t" { return 0 }; if j==0 { return 1 }; r=r+1 } } while 0
return 1
}
_line_start(s, i) {
do { local r=0; while r<=i { local j=i-r; if j==0 { return 0 }; local cj=s.substring(j-1,j); if cj=="\n" { return j }; r=r+1 } } while 0
return 0
}
}
static box HakoSourceCheckerMain { method main(args) {
if args == null || args.size() < 1 {
print("[lint/error] require at least one path argument")
return 2
}
local fail = 0
do { local i=0; while i<args.size() { local p=args.get(i); local rc=HakoSourceCheckerBox.check_file(p); if rc!=0 { fail=fail+1 }; i=i+1 } } while 0
return fail
} }