File size: 5,702 Bytes
a7ac19e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import json
import os
import tempfile
import multiprocessing
import traceback
from wasmtime import Engine, Store, Module, Linker, WasiConfig, ExitTrap, Config

# 定义一个独立的函数用于在子进程中运行
def _run_wasm_in_process(wasm_path, input_data, return_dict):
    """
    运行在独立子进程中的 WASM 执行逻辑。
    结果写入 return_dict['result'] 或 return_dict['error']
    """
    try:
        # 配置 WASM 引擎 (尝试降低优化等级以规避寄存器分配错误)
        config = Config()
        config.cranelift_opt_level = "none" # 关闭优化,牺牲速度换取稳定性
        
        engine = Engine(config)
        linker = Linker(engine)
        linker.define_wasi()
        
        # 加载模块
        module = Module.from_file(engine, wasm_path)
        store = Store(engine)
        
        input_bytes = json.dumps(input_data).encode("utf-8")

        # 使用临时文件处理 IO
        with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f_in, \
             tempfile.NamedTemporaryFile(mode='rb', delete=False) as f_out, \
             tempfile.NamedTemporaryFile(mode='rb', delete=False) as f_err:
            
            # 记录文件名以便稍后清理 (注意:子进程内 unlink 可能有权限问题,最好由父进程或最后清理)
            # 但为了简单,我们尽量在 finally 清理
            temp_files = [f_in.name, f_out.name, f_err.name]

            try:
                # 1. 写入输入
                f_in.write(input_bytes)
                f_in.flush()
                f_in.close()

                # 2. 配置 WASI
                wasi = WasiConfig()
                wasi.stdin_file = f_in.name
                wasi.stdout_file = f_out.name
                wasi.stderr_file = f_err.name
                store.set_wasi(wasi)

                # 3. 实例化并运行
                instance = linker.instantiate(store, module)
                start = instance.exports(store)["_start"]
                start(store)

                # 4. 读取结果
                output_bytes = f_out.read()
                if not output_bytes:
                    return_dict['error'] = "Empty Output from WASM"
                else:
                    try:
                        return_dict['result'] = json.loads(output_bytes)
                    except json.JSONDecodeError:
                        return_dict['error'] = "Invalid JSON Output from WASM"

            except ExitTrap as e:
                if e.code != 0:
                    f_err.seek(0)
                    log = f_err.read().decode('utf-8', errors='ignore')
                    return_dict['error'] = f"WASM Crashed (Code {e.code}): {log}"
                else:
                    # Exit 0 可能是正常的,尝试读取输出
                    f_out.seek(0)
                    output_bytes = f_out.read()
                    if output_bytes:
                        try:
                            return_dict['result'] = json.loads(output_bytes)
                        except:
                            return_dict['error'] = "Exit 0 but invalid JSON"
                    else:
                        return_dict['error'] = "Exit 0 with no output"
            
            except Exception as e:
                return_dict['error'] = f"Execution Error: {str(e)}"
            
            finally:
                # 清理文件
                try: f_out.close()
                except: pass
                try: f_err.close()
                except: pass
                
                for f in temp_files:
                    if os.path.exists(f):
                        try: os.unlink(f)
                        except: pass

    except Exception as e:
        return_dict['error'] = f"Process Init Error: {str(e)}\n{traceback.format_exc()}"


class TrussSolver:
    def __init__(self, wasm_path="bin/framecalc.wasm"):
        if not os.path.exists(wasm_path):
            raise FileNotFoundError(f"WASM binary not found at: {wasm_path}")
        self.wasm_path = wasm_path

    def solve(self, input_data: dict, timeout=10):
        """
        通过子进程执行计算,确保主进程安全。
        """
        manager = multiprocessing.Manager()
        return_dict = manager.dict()
        
        # 启动子进程
        p = multiprocessing.Process(
            target=_run_wasm_in_process,
            args=(self.wasm_path, input_data, return_dict)
        )
        
        p.start()
        p.join(timeout=timeout)
        
        if p.is_alive():
            p.terminate()
            p.join()
            return None, f"Timeout ({timeout}s) - Solver process killed"
        
        # 检查退出码
        if p.exitcode != 0:
            # 如果退出码不为0,说明底层崩溃了 (例如 Rust Panic)
            error_msg = return_dict.get('error', f"Process Crashed with exit code {p.exitcode}")
            return None, error_msg
        
        # 正常退出,检查结果
        if 'error' in return_dict:
            return None, return_dict['error']
        
        if 'result' in return_dict:
            return self._clean_floats(return_dict['result']), None
            
        return None, "Unknown Error (No result returned)"

    def _clean_floats(self, data, threshold=1e-9):
        """清洗浮点数"""
        if isinstance(data, dict):
            return {k: self._clean_floats(v, threshold) for k, v in data.items()}
        elif isinstance(data, list):
            return [self._clean_floats(v, threshold) for v in data]
        elif isinstance(data, float):
            return 0.0 if abs(data) < threshold else data
        return data