chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt
This commit is contained in:
@ -342,3 +342,58 @@ Namespaces / Using(現状)
|
||||
**AOT Quick**
|
||||
- Array literal: `NYASH_SYNTAX_SUGAR_LEVEL=basic ./tools/build_llvm.sh tmp/aot_array_literal_main.nyash -o app && ./app`
|
||||
- Map literal: `NYASH_SYNTAX_SUGAR_LEVEL=basic NYASH_ENABLE_MAP_LITERAL=1 ./tools/build_llvm.sh tmp/aot_map_literal_main.nyash -o app && ./app`
|
||||
|
||||
Refactoring Plan (Phase‑15 follow‑up)
|
||||
- 背景: 巨大ファイル(AST/MIR/LLVM)が保守コスト増の主因。機能非変化の小刻みリファクタで視認性と変更容易性を上げる。
|
||||
|
||||
- 目的と範囲(非機能変更・段階適用)
|
||||
1) AST(src/ast.rs:1): 定義とヘルパの分離、将来のサブenum活用導線の整備(現行API互換)。
|
||||
2) MIR Builder(src/mir/builder.rs:1): build_module の骨格化・責務分離、既存分割(builder/*)の徹底。
|
||||
3) Python LLVM(src/llvm_py/llvm_builder.py:1): lower_function の前処理/本体分離、lower_block の責務拡張。
|
||||
4) MIR 命令(src/mir/instruction.rs:1): 構造体+トレイト導線の導入(enum への委譲を維持した非破壊移行)。
|
||||
|
||||
- 実施順(小PR単位、CI緑維持)
|
||||
PR‑1: AST utils 抽出(非破壊)
|
||||
- 追加: `src/ast/utils.rs` に classify/info/span/to_string などのヘルパを移設。
|
||||
- `src/ast.rs` は ASTNode/StructureNode/ExpressionNode/StatementNode の定義中心に縮退。
|
||||
- 互換維持: `pub use ast::utils::*;` で既存呼び出しを壊さない。
|
||||
- 受入: 全ビルド/スモーク緑、差分はファイル移動のみ。
|
||||
|
||||
PR‑2: MIR Builder build_module 分割(非破壊)
|
||||
- `build_module` を `prepare_module`/`lower_root`/`finalize_module` に3分割。
|
||||
- 型推定(value_types→返り値)は finalize 側へ集約(現行ロジック移設)。
|
||||
- 既存の exprs/stmts などの委譲を明示し、build_module 本体を「骨格のみ」に縮退。
|
||||
- 受入: LLVM/PyVM/Bridge スモーク緑(挙動非変化)。
|
||||
|
||||
PR‑3: Python LLVM lower_function の前処理抽出
|
||||
- 新設: `setup_phi_placeholders()` を導入し、PHI 宣言/CFG 依存前処理をここへ移設。
|
||||
- `lower_block()` に snapshot(block_end_values 収集)までの責務を移動。メインループは薄い周回に。
|
||||
- 受入: `tools/smokes/curated_llvm.sh` / `curated_llvm_stage3.sh` 緑。
|
||||
|
||||
PR‑4: AST ラッパー導入(非破壊導線)
|
||||
- 追加: `src/ast/nodes.rs` に小さな構造体群(Assign/Return/...)。
|
||||
- enum `StatementNode/ExpressionNode` を構造体で保持。`From<T> for ASTNode` / `TryFrom<ASTNode> for T` を提供。
|
||||
- Builder 入口(`builder/stmts.rs`, `builder/exprs.rs`)で ASTNode → TryFrom 変換を 1 行追加し以降はサブ型で match。
|
||||
- 受入: ビルド/スモーク緑(機能非変化)。
|
||||
|
||||
PR‑5: MIR 命令トレイト POC(Const/BinOp)
|
||||
- 追加: `src/mir/instruction_kinds/` に `const_.rs`, `binop.rs`(各命令を struct 化)。
|
||||
- 共通トレイト `InstructionMeta { effects(), dst(), used() }` を定義。
|
||||
- `MirInstruction::{effects,dst_value,used_values}` から一部を構造体 impl に委譲(match 縮退の礎)。
|
||||
- 受入: スモーク緑、`instruction_introspection.rs` の挙動非変化。
|
||||
|
||||
- リスクとガード
|
||||
- 機能非変化を原則(挙動差分は不可)。
|
||||
- CI で LLVM/Bridge を優先確認。Selfhost/E2E は任意ジョブで回す。
|
||||
- PR は 400 行未満/ファイル移動中心を目安に分割。
|
||||
|
||||
- 参考ファイル
|
||||
- AST: `src/ast.rs`, (新規)`src/ast/utils.rs`, (将来)`src/ast/nodes.rs`
|
||||
- Builder: `src/mir/builder.rs`, `src/mir/builder/*`
|
||||
- Python LLVM: `src/llvm_py/llvm_builder.py`
|
||||
- MIR 命令: `src/mir/instruction.rs`, (新規)`src/mir/instruction_kinds/*`
|
||||
|
||||
Acceptance Criteria
|
||||
- すべての変更は機能非変化(スモーク/CI 緑)。
|
||||
- 大型関数・巨大match の見通しが改善し、追従点が局所化。
|
||||
- 新規追加の導線(AST サブ型/命令トレイト)は既存 API と共存し、段階移行を可能にする。
|
||||
|
||||
116
src/ast.rs
116
src/ast.rs
@ -20,9 +20,9 @@ mod utils;
|
||||
/// ASTノードの種類分類
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ASTNodeType {
|
||||
Structure, // 構造定義: box, function, if, loop, try/catch
|
||||
Expression, // 式: リテラル, 変数, 演算, 呼び出し
|
||||
Statement, // 文: 代入, return, break, include
|
||||
Structure, // 構造定義: box, function, if, loop, try/catch
|
||||
Expression, // 式: リテラル, 変数, 演算, 呼び出し
|
||||
Statement, // 文: 代入, return, break, include
|
||||
}
|
||||
|
||||
/// 構造ノード - 言語の基本構造を定義
|
||||
@ -34,9 +34,9 @@ pub enum StructureNode {
|
||||
methods: Vec<ASTNode>,
|
||||
constructors: Vec<ASTNode>,
|
||||
init_fields: Vec<String>,
|
||||
weak_fields: Vec<String>, // 🔗 weak修飾子が付いたフィールドのリスト
|
||||
weak_fields: Vec<String>, // 🔗 weak修飾子が付いたフィールドのリスト
|
||||
is_interface: bool,
|
||||
extends: Vec<String>, // 🚀 Multi-delegation: Changed from Option<String> to Vec<String>
|
||||
extends: Vec<String>, // 🚀 Multi-delegation: Changed from Option<String> to Vec<String>
|
||||
implements: Vec<String>,
|
||||
/// 🔥 ジェネリクス型パラメータ (例: ["T", "U"])
|
||||
type_parameters: Vec<String>,
|
||||
@ -50,8 +50,8 @@ pub enum StructureNode {
|
||||
name: String,
|
||||
params: Vec<String>,
|
||||
body: Vec<ASTNode>,
|
||||
is_static: bool, // 🔥 静的メソッドフラグ
|
||||
is_override: bool, // 🔥 オーバーライドフラグ
|
||||
is_static: bool, // 🔥 静的メソッドフラグ
|
||||
is_override: bool, // 🔥 オーバーライドフラグ
|
||||
span: Span,
|
||||
},
|
||||
IfStructure {
|
||||
@ -178,8 +178,8 @@ pub enum StatementNode {
|
||||
/// Catch節の構造体
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct CatchClause {
|
||||
pub exception_type: Option<String>, // None = catch-all
|
||||
pub variable_name: Option<String>, // 例外を受け取る変数名
|
||||
pub exception_type: Option<String>, // None = catch-all
|
||||
pub variable_name: Option<String>, // 例外を受け取る変数名
|
||||
pub body: Vec<ASTNode>, // catch本体
|
||||
pub span: Span, // ソースコード位置
|
||||
}
|
||||
@ -189,16 +189,16 @@ pub struct CatchClause {
|
||||
pub enum LiteralValue {
|
||||
String(String),
|
||||
Integer(i64),
|
||||
Float(f64), // 浮動小数点数サポート追加
|
||||
Float(f64), // 浮動小数点数サポート追加
|
||||
Bool(bool),
|
||||
Null, // null値
|
||||
Null, // null値
|
||||
Void,
|
||||
}
|
||||
|
||||
impl LiteralValue {
|
||||
/// LiteralValueをNyashBoxに変換
|
||||
pub fn to_nyash_box(&self) -> Box<dyn NyashBox> {
|
||||
use crate::box_trait::{StringBox, IntegerBox, BoolBox, VoidBox};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, StringBox, VoidBox};
|
||||
use crate::boxes::FloatBox;
|
||||
|
||||
match self {
|
||||
@ -213,10 +213,10 @@ impl LiteralValue {
|
||||
|
||||
/// NyashBoxからLiteralValueに変換
|
||||
pub fn from_nyash_box(box_val: &dyn NyashBox) -> Option<LiteralValue> {
|
||||
use crate::box_trait::{BoolBox, IntegerBox, StringBox, VoidBox};
|
||||
use crate::boxes::FloatBox;
|
||||
#[allow(unused_imports)]
|
||||
use std::any::Any;
|
||||
use crate::box_trait::{StringBox, IntegerBox, BoolBox, VoidBox};
|
||||
use crate::boxes::FloatBox;
|
||||
|
||||
if let Some(string_box) = box_val.as_any().downcast_ref::<StringBox>() {
|
||||
Some(LiteralValue::String(string_box.value.clone()))
|
||||
@ -226,7 +226,11 @@ impl LiteralValue {
|
||||
Some(LiteralValue::Float(float_box.value))
|
||||
} else if let Some(bool_box) = box_val.as_any().downcast_ref::<BoolBox>() {
|
||||
Some(LiteralValue::Bool(bool_box.value))
|
||||
} else if box_val.as_any().downcast_ref::<crate::boxes::null_box::NullBox>().is_some() {
|
||||
} else if box_val
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::null_box::NullBox>()
|
||||
.is_some()
|
||||
{
|
||||
Some(LiteralValue::Null)
|
||||
} else if box_val.as_any().downcast_ref::<VoidBox>().is_some() {
|
||||
Some(LiteralValue::Void)
|
||||
@ -252,8 +256,8 @@ impl fmt::Display for LiteralValue {
|
||||
/// 単項演算子の種類
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum UnaryOperator {
|
||||
Minus, // -x
|
||||
Not, // not x
|
||||
Minus, // -x
|
||||
Not, // not x
|
||||
}
|
||||
|
||||
/// 二項演算子の種類
|
||||
@ -267,7 +271,7 @@ pub enum BinaryOperator {
|
||||
BitAnd,
|
||||
BitOr,
|
||||
BitXor,
|
||||
Shl, // << shift-left (Phase 1)
|
||||
Shl, // << shift-left (Phase 1)
|
||||
Shr,
|
||||
Equal,
|
||||
NotEqual,
|
||||
@ -325,7 +329,6 @@ pub enum ASTNode {
|
||||
},
|
||||
|
||||
// ===== 文 (Statements) =====
|
||||
|
||||
/// 代入文: target = value
|
||||
Assignment {
|
||||
target: Box<ASTNode>,
|
||||
@ -361,19 +364,12 @@ pub enum ASTNode {
|
||||
},
|
||||
|
||||
/// break文
|
||||
Break {
|
||||
span: Span,
|
||||
},
|
||||
Break { span: Span },
|
||||
/// continue文
|
||||
Continue {
|
||||
span: Span,
|
||||
},
|
||||
Continue { span: Span },
|
||||
|
||||
/// using文: using namespace_name
|
||||
UsingStatement {
|
||||
namespace_name: String,
|
||||
span: Span,
|
||||
},
|
||||
UsingStatement { namespace_name: String, span: Span },
|
||||
/// import文: import "path" (as Alias)?
|
||||
ImportStatement {
|
||||
path: String,
|
||||
@ -408,10 +404,7 @@ pub enum ASTNode {
|
||||
span: Span,
|
||||
},
|
||||
/// 配列リテラル(糖衣): [e1, e2, ...]
|
||||
ArrayLiteral {
|
||||
elements: Vec<ASTNode>,
|
||||
span: Span,
|
||||
},
|
||||
ArrayLiteral { elements: Vec<ASTNode>, span: Span },
|
||||
/// マップリテラル(糖衣): { "k": v, ... } (Stage‑2: 文字列キー限定)
|
||||
MapLiteral {
|
||||
entries: Vec<(String, ASTNode)>,
|
||||
@ -447,7 +440,6 @@ pub enum ASTNode {
|
||||
},
|
||||
|
||||
// ===== 宣言 (Declarations) =====
|
||||
|
||||
/// box宣言: box Name { fields... methods... }
|
||||
BoxDeclaration {
|
||||
name: String,
|
||||
@ -458,12 +450,12 @@ pub enum ASTNode {
|
||||
private_fields: Vec<String>,
|
||||
methods: HashMap<String, ASTNode>, // method_name -> FunctionDeclaration
|
||||
constructors: HashMap<String, ASTNode>, // constructor_key -> FunctionDeclaration
|
||||
init_fields: Vec<String>, // initブロック内のフィールド定義
|
||||
weak_fields: Vec<String>, // 🔗 weak修飾子が付いたフィールドのリスト
|
||||
is_interface: bool, // interface box かどうか
|
||||
extends: Vec<String>, // 🚀 Multi-delegation: Changed from Option<String> to Vec<String>
|
||||
implements: Vec<String>, // 実装するinterface名のリスト
|
||||
type_parameters: Vec<String>, // 🔥 ジェネリクス型パラメータ (例: ["T", "U"])
|
||||
init_fields: Vec<String>, // initブロック内のフィールド定義
|
||||
weak_fields: Vec<String>, // 🔗 weak修飾子が付いたフィールドのリスト
|
||||
is_interface: bool, // interface box かどうか
|
||||
extends: Vec<String>, // 🚀 Multi-delegation: Changed from Option<String> to Vec<String>
|
||||
implements: Vec<String>, // 実装するinterface名のリスト
|
||||
type_parameters: Vec<String>, // 🔥 ジェネリクス型パラメータ (例: ["T", "U"])
|
||||
/// 🔥 Static boxかどうかのフラグ
|
||||
is_static: bool,
|
||||
/// 🔥 Static初期化ブロック (static { ... })
|
||||
@ -476,8 +468,8 @@ pub enum ASTNode {
|
||||
name: String,
|
||||
params: Vec<String>,
|
||||
body: Vec<ASTNode>,
|
||||
is_static: bool, // 🔥 静的メソッドフラグ
|
||||
is_override: bool, // 🔥 オーバーライドフラグ
|
||||
is_static: bool, // 🔥 静的メソッドフラグ
|
||||
is_override: bool, // 🔥 オーバーライドフラグ
|
||||
span: Span,
|
||||
},
|
||||
|
||||
@ -489,18 +481,11 @@ pub enum ASTNode {
|
||||
},
|
||||
|
||||
// ===== 式 (Expressions) =====
|
||||
|
||||
/// リテラル値: "string", 42, true, etc
|
||||
Literal {
|
||||
value: LiteralValue,
|
||||
span: Span,
|
||||
},
|
||||
Literal { value: LiteralValue, span: Span },
|
||||
|
||||
/// 変数参照: variableName
|
||||
Variable {
|
||||
name: String,
|
||||
span: Span,
|
||||
},
|
||||
Variable { name: String, span: Span },
|
||||
|
||||
/// 単項演算: operator operand
|
||||
UnaryOp {
|
||||
@ -536,45 +521,32 @@ pub enum ASTNode {
|
||||
New {
|
||||
class: String,
|
||||
arguments: Vec<ASTNode>,
|
||||
type_arguments: Vec<String>, // 🔥 ジェネリクス型引数 (例: ["IntegerBox", "StringBox"])
|
||||
type_arguments: Vec<String>, // 🔥 ジェネリクス型引数 (例: ["IntegerBox", "StringBox"])
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// this参照
|
||||
This {
|
||||
span: Span,
|
||||
},
|
||||
This { span: Span },
|
||||
|
||||
/// me参照
|
||||
Me {
|
||||
span: Span,
|
||||
},
|
||||
Me { span: Span },
|
||||
|
||||
/// 🔥 from呼び出し: from Parent.method(arguments) or from Parent.constructor(arguments)
|
||||
FromCall {
|
||||
parent: String, // Parent名
|
||||
method: String, // method名またはconstructor
|
||||
parent: String, // Parent名
|
||||
method: String, // method名またはconstructor
|
||||
arguments: Vec<ASTNode>, // 引数
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// thisフィールドアクセス: this.field
|
||||
ThisField {
|
||||
field: String,
|
||||
span: Span,
|
||||
},
|
||||
ThisField { field: String, span: Span },
|
||||
|
||||
/// meフィールドアクセス: me.field
|
||||
MeField {
|
||||
field: String,
|
||||
span: Span,
|
||||
},
|
||||
MeField { field: String, span: Span },
|
||||
|
||||
/// ファイル読み込み: include "filename.nyash"
|
||||
Include {
|
||||
filename: String,
|
||||
span: Span,
|
||||
},
|
||||
Include { filename: String, span: Span },
|
||||
|
||||
/// ローカル変数宣言: local x, y, z
|
||||
Local {
|
||||
|
||||
@ -3,21 +3,31 @@ use std::fmt;
|
||||
/// ソースコード位置情報 - エラー報告とデバッグの革命
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Span {
|
||||
pub start: usize, // 開始位置(バイトオフセット)
|
||||
pub end: usize, // 終了位置(バイトオフセット)
|
||||
pub line: usize, // 行番号(1から開始)
|
||||
pub column: usize, // 列番号(1から開始)
|
||||
pub start: usize, // 開始位置(バイトオフセット)
|
||||
pub end: usize, // 終了位置(バイトオフセット)
|
||||
pub line: usize, // 行番号(1から開始)
|
||||
pub column: usize, // 列番号(1から開始)
|
||||
}
|
||||
|
||||
impl Span {
|
||||
/// 新しいSpanを作成
|
||||
pub fn new(start: usize, end: usize, line: usize, column: usize) -> Self {
|
||||
Self { start, end, line, column }
|
||||
Self {
|
||||
start,
|
||||
end,
|
||||
line,
|
||||
column,
|
||||
}
|
||||
}
|
||||
|
||||
/// デフォルトのSpan(不明な位置)
|
||||
pub fn unknown() -> Self {
|
||||
Self { start: 0, end: 0, line: 1, column: 1 }
|
||||
Self {
|
||||
start: 0,
|
||||
end: 0,
|
||||
line: 1,
|
||||
column: 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// 2つのSpanを結合(開始位置から終了位置まで)
|
||||
@ -46,11 +56,17 @@ impl Span {
|
||||
// カーソル位置を表示(簡易版)
|
||||
if self.column > 0 && self.column <= line_content.len() + 1 {
|
||||
context.push_str(" | ");
|
||||
for _ in 1..self.column { context.push(' '); }
|
||||
for _ in 1..self.column {
|
||||
context.push(' ');
|
||||
}
|
||||
let span_length = if self.end > self.start {
|
||||
(self.end - self.start).min(line_content.len() - self.column + 1)
|
||||
} else { 1 };
|
||||
for _ in 0..span_length.max(1) { context.push('^'); }
|
||||
} else {
|
||||
1
|
||||
};
|
||||
for _ in 0..span_length.max(1) {
|
||||
context.push('^');
|
||||
}
|
||||
context.push('\n');
|
||||
}
|
||||
|
||||
@ -68,4 +84,3 @@ impl fmt::Display for Span {
|
||||
write!(f, "line {}, column {}", self.line, self.column)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
* Initial scope focuses on value coercions used by the MIR interpreter and JIT.
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, IntegerBox, BoolBox, StringBox, VoidBox};
|
||||
use crate::backend::vm::VMValue;
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox, VoidBox};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Opaque handle type used by JIT/runtime bridges.
|
||||
@ -80,4 +80,3 @@ pub fn handle_of(boxref: Arc<dyn NyashBox>) -> Handle {
|
||||
pub fn handle_get(h: Handle) -> Option<Arc<dyn NyashBox>> {
|
||||
crate::jit::rt::handles::get(h)
|
||||
}
|
||||
|
||||
|
||||
@ -4,11 +4,11 @@
|
||||
* Handles the MIR -> WASM -> Native compilation pipeline
|
||||
*/
|
||||
|
||||
use super::{AotError, AotConfig, AotStats};
|
||||
use crate::mir::MirModule;
|
||||
use super::{AotConfig, AotError, AotStats};
|
||||
use crate::backend::wasm::{WasmBackend, WasmError};
|
||||
use wasmtime::{Engine, Module};
|
||||
use crate::mir::MirModule;
|
||||
use std::time::Instant;
|
||||
use wasmtime::{Engine, Module};
|
||||
|
||||
/// AOT compiler that handles the full compilation pipeline
|
||||
pub struct AotCompiler {
|
||||
@ -21,8 +21,9 @@ impl AotCompiler {
|
||||
/// Create a new AOT compiler with the given configuration
|
||||
pub fn new(config: &AotConfig) -> Result<Self, AotError> {
|
||||
// Create wasmtime engine with optimized configuration
|
||||
let engine = Engine::new(config.wasmtime_config())
|
||||
.map_err(|e| AotError::WasmtimeError(format!("Failed to create wasmtime engine: {}", e)))?;
|
||||
let engine = Engine::new(config.wasmtime_config()).map_err(|e| {
|
||||
AotError::WasmtimeError(format!("Failed to create wasmtime engine: {}", e))
|
||||
})?;
|
||||
|
||||
// Create WASM backend for MIR -> WASM compilation
|
||||
let wasm_backend = WasmBackend::new();
|
||||
@ -46,12 +47,22 @@ impl AotCompiler {
|
||||
let start_time = Instant::now();
|
||||
|
||||
// Use existing WASM backend to compile MIR to WASM
|
||||
let wasm_bytes = self.wasm_backend.compile_module(mir_module)
|
||||
let wasm_bytes = self
|
||||
.wasm_backend
|
||||
.compile_module(mir_module)
|
||||
.map_err(|e| match e {
|
||||
WasmError::CodegenError(msg) => AotError::CompilationError(format!("WASM codegen failed: {}", msg)),
|
||||
WasmError::MemoryError(msg) => AotError::CompilationError(format!("WASM memory error: {}", msg)),
|
||||
WasmError::UnsupportedInstruction(msg) => AotError::CompilationError(format!("Unsupported MIR instruction: {}", msg)),
|
||||
WasmError::WasmValidationError(msg) => AotError::CompilationError(format!("WASM validation failed: {}", msg)),
|
||||
WasmError::CodegenError(msg) => {
|
||||
AotError::CompilationError(format!("WASM codegen failed: {}", msg))
|
||||
}
|
||||
WasmError::MemoryError(msg) => {
|
||||
AotError::CompilationError(format!("WASM memory error: {}", msg))
|
||||
}
|
||||
WasmError::UnsupportedInstruction(msg) => {
|
||||
AotError::CompilationError(format!("Unsupported MIR instruction: {}", msg))
|
||||
}
|
||||
WasmError::WasmValidationError(msg) => {
|
||||
AotError::CompilationError(format!("WASM validation failed: {}", msg))
|
||||
}
|
||||
WasmError::IOError(msg) => AotError::IOError(msg),
|
||||
})?;
|
||||
|
||||
@ -70,8 +81,9 @@ impl AotCompiler {
|
||||
.map_err(|e| AotError::WasmtimeError(format!("Failed to parse WASM module: {}", e)))?;
|
||||
|
||||
// Serialize the precompiled module to bytes
|
||||
let precompiled_bytes = module.serialize()
|
||||
.map_err(|e| AotError::WasmtimeError(format!("Failed to serialize precompiled module: {}", e)))?;
|
||||
let precompiled_bytes = module.serialize().map_err(|e| {
|
||||
AotError::WasmtimeError(format!("Failed to serialize precompiled module: {}", e))
|
||||
})?;
|
||||
|
||||
self.stats.precompiled_size = precompiled_bytes.len();
|
||||
self.stats.compilation_time_ms += start_time.elapsed().as_millis() as u64;
|
||||
@ -89,8 +101,9 @@ impl AotCompiler {
|
||||
pub fn execute_precompiled(&self, precompiled_bytes: &[u8]) -> Result<i32, AotError> {
|
||||
// Deserialize the precompiled module
|
||||
let module = unsafe {
|
||||
Module::deserialize(&self.wasmtime_engine, precompiled_bytes)
|
||||
.map_err(|e| AotError::WasmtimeError(format!("Failed to deserialize module: {}", e)))?
|
||||
Module::deserialize(&self.wasmtime_engine, precompiled_bytes).map_err(|e| {
|
||||
AotError::WasmtimeError(format!("Failed to deserialize module: {}", e))
|
||||
})?
|
||||
};
|
||||
|
||||
// Create instance and execute
|
||||
@ -106,7 +119,8 @@ impl AotCompiler {
|
||||
.map_err(|e| AotError::RuntimeError(format!("No main function found: {}", e)))?;
|
||||
|
||||
// Execute the function
|
||||
let result = main_func.call(&mut store, ())
|
||||
let result = main_func
|
||||
.call(&mut store, ())
|
||||
.map_err(|e| AotError::RuntimeError(format!("Execution failed: {}", e)))?;
|
||||
|
||||
Ok(result)
|
||||
|
||||
@ -49,7 +49,8 @@ impl AotConfig {
|
||||
"x86"
|
||||
} else {
|
||||
"unknown"
|
||||
}.to_string();
|
||||
}
|
||||
.to_string();
|
||||
|
||||
Ok(Self {
|
||||
wasmtime_config: config,
|
||||
@ -98,19 +99,22 @@ impl AotConfig {
|
||||
// Enable all advanced features for x86_64
|
||||
config.enable_simd = true;
|
||||
config.enable_multi_memory = true;
|
||||
},
|
||||
}
|
||||
"aarch64" => {
|
||||
// ARM64 - enable SIMD but be conservative with memory features
|
||||
config.enable_simd = true;
|
||||
config.enable_multi_memory = false;
|
||||
},
|
||||
}
|
||||
"x86" => {
|
||||
// x86 - be conservative
|
||||
config.enable_simd = false;
|
||||
config.enable_multi_memory = false;
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
return Err(AotError::ConfigError(format!("Unsupported target architecture: {}", target)));
|
||||
return Err(AotError::ConfigError(format!(
|
||||
"Unsupported target architecture: {}",
|
||||
target
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,7 +192,9 @@ impl AotConfig {
|
||||
/// Set custom optimization level
|
||||
pub fn set_optimization_level(&mut self, level: u8) -> Result<(), AotError> {
|
||||
if level > 3 {
|
||||
return Err(AotError::ConfigError("Optimization level must be 0-3".to_string()));
|
||||
return Err(AotError::ConfigError(
|
||||
"Optimization level must be 0-3".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
self.optimization_level = level;
|
||||
@ -244,7 +250,9 @@ mod tests {
|
||||
#[test]
|
||||
fn test_optimization_level_setting() {
|
||||
let mut config = AotConfig::new().expect("Failed to create config");
|
||||
config.set_optimization_level(1).expect("Failed to set opt level");
|
||||
config
|
||||
.set_optimization_level(1)
|
||||
.expect("Failed to set opt level");
|
||||
assert_eq!(config.optimization_level(), 1);
|
||||
}
|
||||
|
||||
|
||||
@ -4,9 +4,9 @@
|
||||
* Embeds precompiled WASM modules into self-contained executables
|
||||
*/
|
||||
|
||||
use super::{AotError, AotConfig};
|
||||
use std::path::Path;
|
||||
use super::{AotConfig, AotError};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// Builder for creating standalone executable files
|
||||
pub struct ExecutableBuilder<'a> {
|
||||
@ -33,8 +33,9 @@ impl<'a> ExecutableBuilder<'a> {
|
||||
|
||||
/// Create the standalone executable
|
||||
pub fn create_executable<P: AsRef<Path>>(&self, output_path: P) -> Result<(), AotError> {
|
||||
let module_data = self.precompiled_module.as_ref()
|
||||
.ok_or_else(|| AotError::CompilationError("No precompiled module embedded".to_string()))?;
|
||||
let module_data = self.precompiled_module.as_ref().ok_or_else(|| {
|
||||
AotError::CompilationError("No precompiled module embedded".to_string())
|
||||
})?;
|
||||
|
||||
// Generate the runtime code with embedded module
|
||||
let runtime_code = self.generate_runtime_code(module_data)?;
|
||||
@ -62,10 +63,14 @@ impl<'a> ExecutableBuilder<'a> {
|
||||
let module_bytes = self.format_module_bytes(module_data);
|
||||
let compatibility_key = self.config.compatibility_key();
|
||||
|
||||
let runtime_code = self.runtime_template
|
||||
let runtime_code = self
|
||||
.runtime_template
|
||||
.replace("{{MODULE_BYTES}}", &module_bytes)
|
||||
.replace("{{COMPATIBILITY_KEY}}", &compatibility_key)
|
||||
.replace("{{OPTIMIZATION_LEVEL}}", &self.config.optimization_level().to_string())
|
||||
.replace(
|
||||
"{{OPTIMIZATION_LEVEL}}",
|
||||
&self.config.optimization_level().to_string(),
|
||||
)
|
||||
.replace("{{TARGET_ARCH}}", self.config.target_arch())
|
||||
.replace("{{WASMTIME_VERSION}}", "18.0");
|
||||
|
||||
@ -90,7 +95,8 @@ impl<'a> ExecutableBuilder<'a> {
|
||||
|
||||
/// Generate Cargo.toml for the executable
|
||||
fn generate_cargo_toml(&self) -> String {
|
||||
format!(r#"[package]
|
||||
format!(
|
||||
r#"[package]
|
||||
name = "nyash-aot-executable"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@ -108,25 +114,34 @@ strip = true
|
||||
[[bin]]
|
||||
name = "nyash-aot-executable"
|
||||
path = "nyash_aot_main.rs"
|
||||
"#)
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
/// Compile the Rust executable
|
||||
fn compile_rust_executable<P: AsRef<Path>, Q: AsRef<Path>>(&self, temp_dir: P, output_path: Q) -> Result<(), AotError> {
|
||||
fn compile_rust_executable<P: AsRef<Path>, Q: AsRef<Path>>(
|
||||
&self,
|
||||
temp_dir: P,
|
||||
output_path: Q,
|
||||
) -> Result<(), AotError> {
|
||||
let temp_dir = temp_dir.as_ref();
|
||||
let output_path = output_path.as_ref();
|
||||
|
||||
// Use cargo to compile
|
||||
let mut cmd = std::process::Command::new("cargo");
|
||||
cmd.current_dir(temp_dir)
|
||||
.args(&["build", "--release", "--bin", "nyash-aot-executable"]);
|
||||
.args(&["build", "--release", "--bin", "nyash-aot-executable"]);
|
||||
|
||||
let output = cmd.output()
|
||||
let output = cmd
|
||||
.output()
|
||||
.map_err(|e| AotError::CompilationError(format!("Failed to run cargo: {}", e)))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(AotError::CompilationError(format!("Cargo build failed: {}", stderr)));
|
||||
return Err(AotError::CompilationError(format!(
|
||||
"Cargo build failed: {}",
|
||||
stderr
|
||||
)));
|
||||
}
|
||||
|
||||
// Copy the compiled executable to the desired location
|
||||
@ -138,7 +153,9 @@ path = "nyash_aot_main.rs"
|
||||
};
|
||||
|
||||
if !compiled_exe.exists() {
|
||||
return Err(AotError::CompilationError("Compiled executable not found".to_string()));
|
||||
return Err(AotError::CompilationError(
|
||||
"Compiled executable not found".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
fs::copy(&compiled_exe, output_path)
|
||||
@ -238,7 +255,9 @@ mod tests {
|
||||
let mut builder = ExecutableBuilder::new(&config);
|
||||
let test_data = vec![1, 2, 3, 4, 5];
|
||||
|
||||
builder.embed_precompiled_module(test_data).expect("Failed to embed module");
|
||||
builder
|
||||
.embed_precompiled_module(test_data)
|
||||
.expect("Failed to embed module");
|
||||
assert!(builder.precompiled_module.is_some());
|
||||
}
|
||||
|
||||
@ -272,9 +291,11 @@ mod tests {
|
||||
let builder = ExecutableBuilder::new(&config);
|
||||
let test_data = vec![0x00, 0x61, 0x73, 0x6d];
|
||||
|
||||
let runtime_code = builder.generate_runtime_code(&test_data).expect("Failed to generate runtime");
|
||||
let runtime_code = builder
|
||||
.generate_runtime_code(&test_data)
|
||||
.expect("Failed to generate runtime");
|
||||
assert!(runtime_code.contains("MODULE_DATA"));
|
||||
assert!(runtime_code.contains("0x00"));
|
||||
assert!(runtime_code.contains("18.0")); // Wasmtime version
|
||||
assert!(runtime_code.contains("18.0")); // Wasmtime version
|
||||
}
|
||||
}
|
||||
@ -6,12 +6,12 @@
|
||||
*/
|
||||
|
||||
mod compiler;
|
||||
mod executable;
|
||||
mod config;
|
||||
mod executable;
|
||||
|
||||
pub use compiler::AotCompiler;
|
||||
pub use executable::ExecutableBuilder;
|
||||
pub use config::AotConfig;
|
||||
pub use executable::ExecutableBuilder;
|
||||
|
||||
use crate::mir::MirModule;
|
||||
use std::path::Path;
|
||||
@ -65,27 +65,21 @@ impl AotBackend {
|
||||
let config = AotConfig::new()?;
|
||||
let compiler = AotCompiler::new(&config)?;
|
||||
|
||||
Ok(Self {
|
||||
compiler,
|
||||
config,
|
||||
})
|
||||
Ok(Self { compiler, config })
|
||||
}
|
||||
|
||||
/// Create AOT backend with custom configuration
|
||||
pub fn with_config(config: AotConfig) -> Result<Self, AotError> {
|
||||
let compiler = AotCompiler::new(&config)?;
|
||||
|
||||
Ok(Self {
|
||||
compiler,
|
||||
config,
|
||||
})
|
||||
Ok(Self { compiler, config })
|
||||
}
|
||||
|
||||
/// Compile MIR module to standalone native executable
|
||||
pub fn compile_to_executable<P: AsRef<Path>>(
|
||||
&mut self,
|
||||
mir_module: MirModule,
|
||||
output_path: P
|
||||
output_path: P,
|
||||
) -> Result<(), AotError> {
|
||||
// For now, just create a .cwasm precompiled module
|
||||
// TODO: Implement full standalone executable generation
|
||||
@ -97,7 +91,7 @@ impl AotBackend {
|
||||
pub fn compile_to_precompiled<P: AsRef<Path>>(
|
||||
&mut self,
|
||||
mir_module: MirModule,
|
||||
output_path: P
|
||||
output_path: P,
|
||||
) -> Result<(), AotError> {
|
||||
// Compile MIR to WASM
|
||||
let wasm_bytes = self.compiler.compile_mir_to_wasm(mir_module)?;
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
* Status: Initial skeleton for future extraction from vm.rs
|
||||
*/
|
||||
|
||||
use super::vm::VMError;
|
||||
use crate::mir::BasicBlockId;
|
||||
use super::vm::{VMError};
|
||||
|
||||
/// Result of a block step when evaluating a terminator
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@ -30,4 +30,3 @@ pub fn record_transition(
|
||||
loop_recorder.record_transition(from, to);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@ -7,11 +7,16 @@
|
||||
|
||||
#![cfg(feature = "cranelift-jit")]
|
||||
|
||||
use crate::jit::lower::builder::{IRBuilder, BinOpKind, CmpKind, ParamKind};
|
||||
use crate::jit::lower::builder::{BinOpKind, CmpKind, IRBuilder, ParamKind};
|
||||
use cranelift_codegen::ir::InstBuilder;
|
||||
|
||||
// Minimal recorded opcodes for Const/Add/Return first
|
||||
enum RecOp { ConstI64(i64), ConstF64(f64), BinOp(BinOpKind), Return }
|
||||
enum RecOp {
|
||||
ConstI64(i64),
|
||||
ConstF64(f64),
|
||||
BinOp(BinOpKind),
|
||||
Return,
|
||||
}
|
||||
|
||||
pub struct ClifBuilder {
|
||||
pub consts: usize,
|
||||
@ -23,24 +28,36 @@ pub struct ClifBuilder {
|
||||
}
|
||||
|
||||
impl ClifBuilder {
|
||||
pub fn new() -> Self { Self { consts: 0, binops: 0, cmps: 0, branches: 0, rets: 0, ops: Vec::new() } }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
consts: 0,
|
||||
binops: 0,
|
||||
cmps: 0,
|
||||
branches: 0,
|
||||
rets: 0,
|
||||
ops: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Build and execute the recorded ops as a native function using Cranelift
|
||||
pub fn finish_and_execute(&self) -> Result<i64, String> {
|
||||
use cranelift_codegen::ir::{Signature, AbiParam, types};
|
||||
use cranelift_codegen::ir::{types, AbiParam, Signature};
|
||||
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
||||
use cranelift_module::{Module, Linkage};
|
||||
use cranelift_module::{Linkage, Module};
|
||||
// JIT setup
|
||||
let isa_builder = cranelift_native::builder().map_err(|e| e.to_string())?;
|
||||
let flag_builder = cranelift_codegen::settings::builder();
|
||||
let flags = cranelift_codegen::settings::Flags::new(flag_builder);
|
||||
let isa = isa_builder.finish(flags).map_err(|e| e.to_string())?;
|
||||
let jit_builder = cranelift_jit::JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
||||
let jit_builder =
|
||||
cranelift_jit::JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
||||
let mut module = cranelift_jit::JITModule::new(jit_builder);
|
||||
// Signature ()->i64
|
||||
let mut sig = Signature::new(module.target_config().default_call_conv);
|
||||
sig.returns.push(AbiParam::new(types::I64));
|
||||
let func_id = module.declare_function("ny_lowercore_main", Linkage::Export, &sig).map_err(|e| e.to_string())?;
|
||||
let func_id = module
|
||||
.declare_function("ny_lowercore_main", Linkage::Export, &sig)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut ctx = module.make_context();
|
||||
ctx.func.signature = sig;
|
||||
let mut fbc = FunctionBuilderContext::new();
|
||||
@ -53,16 +70,31 @@ impl ClifBuilder {
|
||||
let mut did_return = false;
|
||||
for op in &self.ops {
|
||||
match *op {
|
||||
RecOp::ConstI64(i) => { vs.push(fb.ins().iconst(types::I64, i)); }
|
||||
RecOp::ConstF64(f) => { let fv = fb.ins().f64const(f); let iv = fb.ins().fcvt_to_sint(types::I64, fv); vs.push(iv); }
|
||||
RecOp::ConstI64(i) => {
|
||||
vs.push(fb.ins().iconst(types::I64, i));
|
||||
}
|
||||
RecOp::ConstF64(f) => {
|
||||
let fv = fb.ins().f64const(f);
|
||||
let iv = fb.ins().fcvt_to_sint(types::I64, fv);
|
||||
vs.push(iv);
|
||||
}
|
||||
RecOp::BinOp(BinOpKind::Add) => {
|
||||
if vs.len() < 2 { vs.clear(); vs.push(fb.ins().iconst(types::I64, 0)); } else {
|
||||
let r = vs.pop().unwrap(); let l = vs.pop().unwrap(); vs.push(fb.ins().iadd(l, r));
|
||||
if vs.len() < 2 {
|
||||
vs.clear();
|
||||
vs.push(fb.ins().iconst(types::I64, 0));
|
||||
} else {
|
||||
let r = vs.pop().unwrap();
|
||||
let l = vs.pop().unwrap();
|
||||
vs.push(fb.ins().iadd(l, r));
|
||||
}
|
||||
}
|
||||
RecOp::BinOp(_) => { /* ignore others for now */ }
|
||||
RecOp::Return => {
|
||||
let retv = if let Some(v) = vs.last().copied() { v } else { fb.ins().iconst(types::I64, 0) };
|
||||
let retv = if let Some(v) = vs.last().copied() {
|
||||
v
|
||||
} else {
|
||||
fb.ins().iconst(types::I64, 0)
|
||||
};
|
||||
fb.ins().return_(&[retv]);
|
||||
did_return = true;
|
||||
}
|
||||
@ -70,12 +102,18 @@ impl ClifBuilder {
|
||||
}
|
||||
// Ensure function ends with return
|
||||
if !did_return {
|
||||
let retv = if let Some(v) = vs.last().copied() { v } else { fb.ins().iconst(types::I64, 0) };
|
||||
let retv = if let Some(v) = vs.last().copied() {
|
||||
v
|
||||
} else {
|
||||
fb.ins().iconst(types::I64, 0)
|
||||
};
|
||||
fb.ins().return_(&[retv]);
|
||||
}
|
||||
fb.seal_block(entry);
|
||||
fb.finalize();
|
||||
module.define_function(func_id, &mut ctx).map_err(|e| e.to_string())?;
|
||||
module
|
||||
.define_function(func_id, &mut ctx)
|
||||
.map_err(|e| e.to_string())?;
|
||||
module.clear_context(&mut ctx);
|
||||
let _ = module.finalize_definitions();
|
||||
let code = module.get_finalized_function(func_id);
|
||||
@ -87,35 +125,77 @@ impl ClifBuilder {
|
||||
impl IRBuilder for ClifBuilder {
|
||||
fn begin_function(&mut self, _name: &str) {}
|
||||
fn end_function(&mut self) {}
|
||||
fn prepare_signature_i64(&mut self, _argc: usize, _has_ret: bool) { }
|
||||
fn prepare_signature_typed(&mut self, _params: &[ParamKind], _ret_is_f64: bool) { }
|
||||
fn emit_param_i64(&mut self, _index: usize) { }
|
||||
fn emit_const_i64(&mut self, val: i64) { self.consts += 1; self.ops.push(RecOp::ConstI64(val)); }
|
||||
fn emit_const_f64(&mut self, val: f64) { self.consts += 1; self.ops.push(RecOp::ConstF64(val)); }
|
||||
fn emit_binop(&mut self, op: BinOpKind) { self.binops += 1; self.ops.push(RecOp::BinOp(op)); }
|
||||
fn emit_compare(&mut self, _op: CmpKind) { self.cmps += 1; }
|
||||
fn prepare_signature_i64(&mut self, _argc: usize, _has_ret: bool) {}
|
||||
fn prepare_signature_typed(&mut self, _params: &[ParamKind], _ret_is_f64: bool) {}
|
||||
fn emit_param_i64(&mut self, _index: usize) {}
|
||||
fn emit_const_i64(&mut self, val: i64) {
|
||||
self.consts += 1;
|
||||
self.ops.push(RecOp::ConstI64(val));
|
||||
}
|
||||
fn emit_const_f64(&mut self, val: f64) {
|
||||
self.consts += 1;
|
||||
self.ops.push(RecOp::ConstF64(val));
|
||||
}
|
||||
fn emit_binop(&mut self, op: BinOpKind) {
|
||||
self.binops += 1;
|
||||
self.ops.push(RecOp::BinOp(op));
|
||||
}
|
||||
fn emit_compare(&mut self, _op: CmpKind) {
|
||||
self.cmps += 1;
|
||||
}
|
||||
fn emit_jump(&mut self) {}
|
||||
fn emit_branch(&mut self) { self.branches += 1; }
|
||||
fn emit_return(&mut self) { self.rets += 1; self.ops.push(RecOp::Return); }
|
||||
fn emit_host_call(&mut self, _symbol: &str, _argc: usize, _has_ret: bool) { }
|
||||
fn emit_host_call_typed(&mut self, _symbol: &str, _params: &[ParamKind], _has_ret: bool, _ret_is_f64: bool) { }
|
||||
fn emit_plugin_invoke(&mut self, _type_id: u32, _method_id: u32, _argc: usize, _has_ret: bool) { }
|
||||
fn emit_plugin_invoke_by_name(&mut self, _method: &str, _argc: usize, _has_ret: bool) { }
|
||||
fn prepare_blocks(&mut self, _count: usize) { }
|
||||
fn switch_to_block(&mut self, _index: usize) { }
|
||||
fn seal_block(&mut self, _index: usize) { }
|
||||
fn br_if_top_is_true(&mut self, _then_index: usize, _else_index: usize) { }
|
||||
fn jump_to(&mut self, _target_index: usize) { }
|
||||
fn ensure_block_params_i64(&mut self, _index: usize, _count: usize) { }
|
||||
fn ensure_block_params_b1(&mut self, index: usize, count: usize) { self.ensure_block_params_i64(index, count); }
|
||||
fn ensure_block_param_i64(&mut self, index: usize) { self.ensure_block_params_i64(index, 1); }
|
||||
fn push_block_param_i64_at(&mut self, _pos: usize) { }
|
||||
fn push_block_param_b1_at(&mut self, pos: usize) { self.push_block_param_i64_at(pos); }
|
||||
fn push_block_param_i64(&mut self) { self.push_block_param_i64_at(0); }
|
||||
fn br_if_with_args(&mut self, _then_index: usize, _else_index: usize, _then_n: usize, _else_n: usize) { self.emit_branch(); }
|
||||
fn jump_with_args(&mut self, _target_index: usize, _n: usize) { self.emit_jump(); }
|
||||
fn hint_ret_bool(&mut self, _is_b1: bool) { }
|
||||
fn ensure_local_i64(&mut self, _index: usize) { }
|
||||
fn store_local_i64(&mut self, _index: usize) { }
|
||||
fn load_local_i64(&mut self, _index: usize) { }
|
||||
fn emit_branch(&mut self) {
|
||||
self.branches += 1;
|
||||
}
|
||||
fn emit_return(&mut self) {
|
||||
self.rets += 1;
|
||||
self.ops.push(RecOp::Return);
|
||||
}
|
||||
fn emit_host_call(&mut self, _symbol: &str, _argc: usize, _has_ret: bool) {}
|
||||
fn emit_host_call_typed(
|
||||
&mut self,
|
||||
_symbol: &str,
|
||||
_params: &[ParamKind],
|
||||
_has_ret: bool,
|
||||
_ret_is_f64: bool,
|
||||
) {
|
||||
}
|
||||
fn emit_plugin_invoke(&mut self, _type_id: u32, _method_id: u32, _argc: usize, _has_ret: bool) {
|
||||
}
|
||||
fn emit_plugin_invoke_by_name(&mut self, _method: &str, _argc: usize, _has_ret: bool) {}
|
||||
fn prepare_blocks(&mut self, _count: usize) {}
|
||||
fn switch_to_block(&mut self, _index: usize) {}
|
||||
fn seal_block(&mut self, _index: usize) {}
|
||||
fn br_if_top_is_true(&mut self, _then_index: usize, _else_index: usize) {}
|
||||
fn jump_to(&mut self, _target_index: usize) {}
|
||||
fn ensure_block_params_i64(&mut self, _index: usize, _count: usize) {}
|
||||
fn ensure_block_params_b1(&mut self, index: usize, count: usize) {
|
||||
self.ensure_block_params_i64(index, count);
|
||||
}
|
||||
fn ensure_block_param_i64(&mut self, index: usize) {
|
||||
self.ensure_block_params_i64(index, 1);
|
||||
}
|
||||
fn push_block_param_i64_at(&mut self, _pos: usize) {}
|
||||
fn push_block_param_b1_at(&mut self, pos: usize) {
|
||||
self.push_block_param_i64_at(pos);
|
||||
}
|
||||
fn push_block_param_i64(&mut self) {
|
||||
self.push_block_param_i64_at(0);
|
||||
}
|
||||
fn br_if_with_args(
|
||||
&mut self,
|
||||
_then_index: usize,
|
||||
_else_index: usize,
|
||||
_then_n: usize,
|
||||
_else_n: usize,
|
||||
) {
|
||||
self.emit_branch();
|
||||
}
|
||||
fn jump_with_args(&mut self, _target_index: usize, _n: usize) {
|
||||
self.emit_jump();
|
||||
}
|
||||
fn hint_ret_bool(&mut self, _is_b1: bool) {}
|
||||
fn ensure_local_i64(&mut self, _index: usize) {}
|
||||
fn store_local_i64(&mut self, _index: usize) {}
|
||||
fn load_local_i64(&mut self, _index: usize) {}
|
||||
}
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use cranelift_codegen::ir::{Block, Signature, AbiParam, types};
|
||||
use cranelift_codegen::ir::{types, AbiParam, Block, Signature};
|
||||
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
||||
use cranelift_jit::{JITBuilder, JITModule};
|
||||
use cranelift_module::{Module, Linkage, FuncId};
|
||||
use cranelift_module::{FuncId, Linkage, Module};
|
||||
|
||||
use crate::mir::{BasicBlockId, MirFunction, ValueId};
|
||||
|
||||
@ -13,9 +13,15 @@ use crate::mir::{BasicBlockId, MirFunction, ValueId};
|
||||
pub struct BlockMap(pub HashMap<BasicBlockId, Block>);
|
||||
|
||||
impl BlockMap {
|
||||
pub fn new() -> Self { Self(HashMap::new()) }
|
||||
pub fn get(&self, bb: &BasicBlockId) -> Option<&Block> { self.0.get(bb) }
|
||||
pub fn insert(&mut self, bb: BasicBlockId, blk: Block) { self.0.insert(bb, blk); }
|
||||
pub fn new() -> Self {
|
||||
Self(HashMap::new())
|
||||
}
|
||||
pub fn get(&self, bb: &BasicBlockId) -> Option<&Block> {
|
||||
self.0.get(bb)
|
||||
}
|
||||
pub fn insert(&mut self, bb: BasicBlockId, blk: Block) {
|
||||
self.0.insert(bb, blk);
|
||||
}
|
||||
/// Create a CLIF block for each MIR block id
|
||||
pub fn create_for_function(func: &MirFunction, builder: &mut FunctionBuilder) -> Self {
|
||||
let mut m = HashMap::new();
|
||||
@ -33,15 +39,31 @@ pub struct ValueEnv {
|
||||
}
|
||||
|
||||
impl ValueEnv {
|
||||
pub fn new() -> Self { Self { vals: HashMap::new(), mem: HashMap::new() } }
|
||||
pub fn get_val(&self, id: &ValueId) -> Result<cranelift_codegen::ir::Value, String> {
|
||||
self.vals.get(id).cloned().ok_or_else(|| format!("undef {:?}", id))
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
vals: HashMap::new(),
|
||||
mem: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn set_val(&mut self, id: ValueId, v: cranelift_codegen::ir::Value) { self.vals.insert(id, v); }
|
||||
pub fn get_mem_or(&self, id: &ValueId, default: cranelift_codegen::ir::Value) -> cranelift_codegen::ir::Value {
|
||||
pub fn get_val(&self, id: &ValueId) -> Result<cranelift_codegen::ir::Value, String> {
|
||||
self.vals
|
||||
.get(id)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("undef {:?}", id))
|
||||
}
|
||||
pub fn set_val(&mut self, id: ValueId, v: cranelift_codegen::ir::Value) {
|
||||
self.vals.insert(id, v);
|
||||
}
|
||||
pub fn get_mem_or(
|
||||
&self,
|
||||
id: &ValueId,
|
||||
default: cranelift_codegen::ir::Value,
|
||||
) -> cranelift_codegen::ir::Value {
|
||||
*self.mem.get(id).unwrap_or(&default)
|
||||
}
|
||||
pub fn set_mem(&mut self, id: ValueId, v: cranelift_codegen::ir::Value) { self.mem.insert(id, v); }
|
||||
pub fn set_mem(&mut self, id: ValueId, v: cranelift_codegen::ir::Value) {
|
||||
self.mem.insert(id, v);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cranelift JIT module wrapper (context)
|
||||
@ -56,24 +78,37 @@ impl ClifContext {
|
||||
let flags = cranelift_codegen::settings::Flags::new(flag_builder);
|
||||
let isa = isa_builder.finish(flags).map_err(|e| e.to_string())?;
|
||||
let jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
||||
Ok(Self { module: JITModule::new(jit_builder) })
|
||||
Ok(Self {
|
||||
module: JITModule::new(jit_builder),
|
||||
})
|
||||
}
|
||||
|
||||
/// Declare an exported i64-return function and return its id and Cranelift context/signature
|
||||
pub fn declare_i64_fn(&mut self, name: &str) -> Result<(FuncId, cranelift_codegen::Context, Signature), String> {
|
||||
pub fn declare_i64_fn(
|
||||
&mut self,
|
||||
name: &str,
|
||||
) -> Result<(FuncId, cranelift_codegen::Context, Signature), String> {
|
||||
let mut sig = Signature::new(self.module.target_config().default_call_conv);
|
||||
sig.returns.push(AbiParam::new(types::I64));
|
||||
let func_id = self.module.declare_function(name, Linkage::Export, &sig).map_err(|e| e.to_string())?;
|
||||
let func_id = self
|
||||
.module
|
||||
.declare_function(name, Linkage::Export, &sig)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut ctx = self.module.make_context();
|
||||
ctx.func.signature = sig.clone();
|
||||
Ok((func_id, ctx, sig))
|
||||
}
|
||||
|
||||
pub fn finalize(&mut self, func_id: FuncId, ctx: &mut cranelift_codegen::Context) -> Result<*const u8, String> {
|
||||
self.module.define_function(func_id, ctx).map_err(|e| e.to_string())?;
|
||||
pub fn finalize(
|
||||
&mut self,
|
||||
func_id: FuncId,
|
||||
ctx: &mut cranelift_codegen::Context,
|
||||
) -> Result<*const u8, String> {
|
||||
self.module
|
||||
.define_function(func_id, ctx)
|
||||
.map_err(|e| e.to_string())?;
|
||||
self.module.clear_context(ctx);
|
||||
let _ = self.module.finalize_definitions();
|
||||
Ok(self.module.get_finalized_function(func_id))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
#![cfg(feature = "cranelift-jit")]
|
||||
|
||||
use cranelift_codegen::ir::{AbiParam, InstBuilder, Signature, types, condcodes::IntCC, StackSlot, StackSlotData, StackSlotKind};
|
||||
use cranelift_codegen::ir::{
|
||||
condcodes::IntCC, types, AbiParam, InstBuilder, Signature, StackSlot, StackSlotData,
|
||||
StackSlotKind,
|
||||
};
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
||||
use cranelift_jit::{JITBuilder, JITModule};
|
||||
use cranelift_module::{Module, Linkage};
|
||||
use cranelift_module::{Linkage, Module};
|
||||
|
||||
use crate::mir::{MirFunction, MirInstruction, ConstValue, ValueId, BasicBlockId, CompareOp};
|
||||
use crate::mir::{BasicBlockId, CompareOp, ConstValue, MirFunction, MirInstruction, ValueId};
|
||||
|
||||
/// Compile a minimal subset of MIR(main) to a native function and execute it.
|
||||
/// Supported: Const(Integer), BinOp(Add for integers), Return(Integer or default 0).
|
||||
@ -22,8 +25,11 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
|
||||
// Signature: () -> i64
|
||||
let mut sig = Signature::new(module.target_config().default_call_conv);
|
||||
sig.returns.push(AbiParam::new(cranelift_codegen::ir::types::I64));
|
||||
let func_id = module.declare_function("ny_main", Linkage::Export, &sig).map_err(|e| e.to_string())?;
|
||||
sig.returns
|
||||
.push(AbiParam::new(cranelift_codegen::ir::types::I64));
|
||||
let func_id = module
|
||||
.declare_function("ny_main", Linkage::Export, &sig)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let mut ctx = module.make_context();
|
||||
ctx.func.signature = sig;
|
||||
@ -36,7 +42,9 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
let mut vals: HashMap<ValueId, cranelift_codegen::ir::Value> = HashMap::new();
|
||||
let mut slots: HashMap<ValueId, StackSlot> = HashMap::new();
|
||||
|
||||
for (bb_id, _) in &main.blocks { clif_blocks.insert(*bb_id, builder.create_block()); }
|
||||
for (bb_id, _) in &main.blocks {
|
||||
clif_blocks.insert(*bb_id, builder.create_block());
|
||||
}
|
||||
// Switch to entry
|
||||
let entry = *clif_blocks.get(&main.entry_block).unwrap();
|
||||
builder.switch_to_block(entry);
|
||||
@ -55,9 +63,16 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
MirInstruction::Const { dst, value } => {
|
||||
let v = match value {
|
||||
ConstValue::Integer(i) => builder.ins().iconst(types::I64, *i),
|
||||
ConstValue::Bool(b) => builder.ins().iconst(types::I64, if *b { 1 } else { 0 }),
|
||||
ConstValue::Float(f) => { let fv = builder.ins().f64const(*f); builder.ins().fcvt_to_sint(types::I64, fv) },
|
||||
ConstValue::String(_) | ConstValue::Null | ConstValue::Void => builder.ins().iconst(types::I64, 0),
|
||||
ConstValue::Bool(b) => {
|
||||
builder.ins().iconst(types::I64, if *b { 1 } else { 0 })
|
||||
}
|
||||
ConstValue::Float(f) => {
|
||||
let fv = builder.ins().f64const(*f);
|
||||
builder.ins().fcvt_to_sint(types::I64, fv)
|
||||
}
|
||||
ConstValue::String(_) | ConstValue::Null | ConstValue::Void => {
|
||||
builder.ins().iconst(types::I64, 0)
|
||||
}
|
||||
};
|
||||
vals.insert(*dst, v);
|
||||
}
|
||||
@ -78,7 +93,14 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
MirInstruction::Compare { dst, op, lhs, rhs } => {
|
||||
let l = *vals.get(lhs).ok_or_else(|| format!("undef {:?}", lhs))?;
|
||||
let r = *vals.get(rhs).ok_or_else(|| format!("undef {:?}", rhs))?;
|
||||
let cc = match op { CompareOp::Eq => IntCC::Equal, CompareOp::Ne => IntCC::NotEqual, CompareOp::Lt => IntCC::SignedLessThan, CompareOp::Le => IntCC::SignedLessThanOrEqual, CompareOp::Gt => IntCC::SignedGreaterThan, CompareOp::Ge => IntCC::SignedGreaterThanOrEqual };
|
||||
let cc = match op {
|
||||
CompareOp::Eq => IntCC::Equal,
|
||||
CompareOp::Ne => IntCC::NotEqual,
|
||||
CompareOp::Lt => IntCC::SignedLessThan,
|
||||
CompareOp::Le => IntCC::SignedLessThanOrEqual,
|
||||
CompareOp::Gt => IntCC::SignedGreaterThan,
|
||||
CompareOp::Ge => IntCC::SignedGreaterThanOrEqual,
|
||||
};
|
||||
let b1 = builder.ins().icmp(cc, l, r);
|
||||
let one = builder.ins().iconst(types::I64, 1);
|
||||
let zero = builder.ins().iconst(types::I64, 0);
|
||||
@ -94,8 +116,15 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
}
|
||||
}
|
||||
MirInstruction::Store { value, ptr } => {
|
||||
let v = *vals.get(value).ok_or_else(|| format!("undef {:?}", value))?;
|
||||
let ss = *slots.entry(*ptr).or_insert_with(|| builder.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 8)));
|
||||
let v = *vals
|
||||
.get(value)
|
||||
.ok_or_else(|| format!("undef {:?}", value))?;
|
||||
let ss = *slots.entry(*ptr).or_insert_with(|| {
|
||||
builder.create_sized_stack_slot(StackSlotData::new(
|
||||
StackSlotKind::ExplicitSlot,
|
||||
8,
|
||||
))
|
||||
});
|
||||
builder.ins().stack_store(v, ss, 0);
|
||||
}
|
||||
MirInstruction::Copy { dst, src } => {
|
||||
@ -108,21 +137,32 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
// Terminator
|
||||
match &bb.terminator {
|
||||
Some(MirInstruction::Return { value }) => {
|
||||
let retv = if let Some(v) = value { *vals.get(v).unwrap_or(&builder.ins().iconst(types::I64, 0)) } else { builder.ins().iconst(types::I64, 0) };
|
||||
let retv = if let Some(v) = value {
|
||||
*vals.get(v).unwrap_or(&builder.ins().iconst(types::I64, 0))
|
||||
} else {
|
||||
builder.ins().iconst(types::I64, 0)
|
||||
};
|
||||
builder.ins().return_(&[retv]);
|
||||
}
|
||||
Some(MirInstruction::Jump { target }) => {
|
||||
let t = *clif_blocks.get(target).unwrap();
|
||||
builder.ins().jump(t, &[]);
|
||||
}
|
||||
Some(MirInstruction::Branch { condition, then_bb, else_bb }) => {
|
||||
let cond_i64 = *vals.get(condition).unwrap_or(&builder.ins().iconst(types::I64, 0));
|
||||
Some(MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
}) => {
|
||||
let cond_i64 = *vals
|
||||
.get(condition)
|
||||
.unwrap_or(&builder.ins().iconst(types::I64, 0));
|
||||
let is_true = builder.ins().icmp_imm(IntCC::NotEqual, cond_i64, 0);
|
||||
let tb = *clif_blocks.get(then_bb).unwrap();
|
||||
let eb = *clif_blocks.get(else_bb).unwrap();
|
||||
builder.ins().brif(is_true, tb, &[], eb, &[]);
|
||||
}
|
||||
_ => { /* fallthrough not allowed: insert return 0 to keep verifier happy */
|
||||
_ => {
|
||||
/* fallthrough not allowed: insert return 0 to keep verifier happy */
|
||||
let z = builder.ins().iconst(types::I64, 0);
|
||||
builder.ins().return_(&[z]);
|
||||
}
|
||||
@ -132,7 +172,9 @@ pub fn compile_and_execute_minimal(main: &MirFunction) -> Result<i64, String> {
|
||||
|
||||
builder.finalize();
|
||||
|
||||
module.define_function(func_id, &mut ctx).map_err(|e| e.to_string())?;
|
||||
module
|
||||
.define_function(func_id, &mut ctx)
|
||||
.map_err(|e| e.to_string())?;
|
||||
module.clear_context(&mut ctx);
|
||||
let _ = module.finalize_definitions();
|
||||
|
||||
|
||||
@ -6,31 +6,43 @@
|
||||
|
||||
#![cfg(feature = "cranelift-jit")]
|
||||
|
||||
use crate::mir::{function::MirModule, MirInstruction, ConstValue, ValueId, BinaryOp};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox, VoidBox};
|
||||
use crate::jit::lower::{
|
||||
builder::{IRBuilder, NoopBuilder},
|
||||
core::LowerCore,
|
||||
};
|
||||
use crate::jit::semantics::clif::ClifSemanticsSkeleton;
|
||||
use crate::mir::{function::MirModule, BinaryOp, ConstValue, MirInstruction, ValueId};
|
||||
#[cfg(feature = "cranelift-jit")]
|
||||
use crate::semantics::Semantics;
|
||||
use crate::box_trait::{NyashBox, IntegerBox, BoolBox, StringBox, VoidBox};
|
||||
use std::collections::HashMap;
|
||||
use crate::jit::lower::{builder::{NoopBuilder, IRBuilder}, core::LowerCore};
|
||||
use crate::jit::semantics::clif::ClifSemanticsSkeleton;
|
||||
|
||||
pub mod context; // Context/Block/Value env wrappers
|
||||
pub mod builder; // Clif IRBuilder implementation (skeleton)
|
||||
pub mod builder;
|
||||
pub mod context; // Context/Block/Value env wrappers // Clif IRBuilder implementation (skeleton)
|
||||
pub mod lower {}
|
||||
pub mod jit; // JIT compile/execute using Cranelift (minimal)
|
||||
pub mod object {}
|
||||
|
||||
/// JIT: compile and execute a MIR module (skeleton)
|
||||
pub fn compile_and_execute(mir_module: &MirModule, _temp_name: &str) -> Result<Box<dyn NyashBox>, String> {
|
||||
pub fn compile_and_execute(
|
||||
mir_module: &MirModule,
|
||||
_temp_name: &str,
|
||||
) -> Result<Box<dyn NyashBox>, String> {
|
||||
// Minimal semantics: Const/Return/Add only (straight-line code)
|
||||
let main = mir_module.functions.get("main").ok_or("missing main function")?;
|
||||
let main = mir_module
|
||||
.functions
|
||||
.get("main")
|
||||
.ok_or("missing main function")?;
|
||||
|
||||
// Minimal ClifSem lowering pass (NoopBuilder): Const/Return/Add カバレッジ確認
|
||||
if std::env::var("NYASH_JIT_DUMP").ok().as_deref() == Some("1") {
|
||||
let mut builder = NoopBuilder::new();
|
||||
let mut lower = LowerCore::new();
|
||||
let _ = lower.lower_function(main, &mut builder);
|
||||
eprintln!("[CLIF-LOWER] covered={} unsupported={}", lower.covered, lower.unsupported);
|
||||
eprintln!(
|
||||
"[CLIF-LOWER] covered={} unsupported={}",
|
||||
lower.covered, lower.unsupported
|
||||
);
|
||||
}
|
||||
|
||||
// まずは新JITエンジン経路を試す(LowerCore -> CraneliftBuilder -> 実行)
|
||||
@ -41,10 +53,13 @@ pub fn compile_and_execute(mir_module: &MirModule, _temp_name: &str) -> Result<B
|
||||
// 実行(引数なし)。戻り値は MIR の型に合わせて変換
|
||||
let out = engine.execute_handle(h, &[]);
|
||||
if let Some(jv) = out {
|
||||
let vmv = crate::jit::boundary::CallBoundaryBox::to_vm(&main.signature.return_type, jv);
|
||||
let vmv =
|
||||
crate::jit::boundary::CallBoundaryBox::to_vm(&main.signature.return_type, jv);
|
||||
let boxed: Box<dyn NyashBox> = match vmv {
|
||||
crate::backend::vm::VMValue::Integer(i) => Box::new(IntegerBox::new(i)),
|
||||
crate::backend::vm::VMValue::Float(f) => Box::new(crate::boxes::FloatBox::new(f)),
|
||||
crate::backend::vm::VMValue::Float(f) => {
|
||||
Box::new(crate::boxes::FloatBox::new(f))
|
||||
}
|
||||
crate::backend::vm::VMValue::Bool(b) => Box::new(BoolBox::new(b)),
|
||||
crate::backend::vm::VMValue::String(s) => Box::new(StringBox::new(&s)),
|
||||
crate::backend::vm::VMValue::BoxRef(b) => b.share_box(),
|
||||
@ -63,19 +78,30 @@ pub fn compile_and_execute(mir_module: &MirModule, _temp_name: &str) -> Result<B
|
||||
let mut cur = main.entry_block;
|
||||
let mut last_pred: Option<crate::mir::BasicBlockId> = None;
|
||||
loop {
|
||||
let bb = main.blocks.get(&cur).ok_or_else(|| format!("invalid bb {:?}", cur))?;
|
||||
let bb = main
|
||||
.blocks
|
||||
.get(&cur)
|
||||
.ok_or_else(|| format!("invalid bb {:?}", cur))?;
|
||||
// PHI (very minimal): choose first input or predecessor match
|
||||
for inst in &bb.instructions {
|
||||
if let MirInstruction::Phi { dst, inputs } = inst {
|
||||
if let Some(pred) = last_pred {
|
||||
if let Some((_, v)) = inputs.iter().find(|(b, _)| *b == pred) { if let Some(val) = regs.get(v).cloned() { regs.insert(*dst, val); } }
|
||||
} else if let Some((_, v)) = inputs.first() { if let Some(val) = regs.get(v).cloned() { regs.insert(*dst, val); } }
|
||||
if let Some((_, v)) = inputs.iter().find(|(b, _)| *b == pred) {
|
||||
if let Some(val) = regs.get(v).cloned() {
|
||||
regs.insert(*dst, val);
|
||||
}
|
||||
}
|
||||
} else if let Some((_, v)) = inputs.first() {
|
||||
if let Some(val) = regs.get(v).cloned() {
|
||||
regs.insert(*dst, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut sem = ClifSemanticsSkeleton::new();
|
||||
for inst in &bb.instructions {
|
||||
match inst {
|
||||
MirInstruction::Const { dst, value } => {
|
||||
let mut sem = ClifSemanticsSkeleton::new();
|
||||
for inst in &bb.instructions {
|
||||
match inst {
|
||||
MirInstruction::Const { dst, value } => {
|
||||
let vv = match value {
|
||||
ConstValue::Integer(i) => sem.const_i64(*i),
|
||||
ConstValue::Float(f) => sem.const_f64(*f),
|
||||
@ -84,39 +110,87 @@ pub fn compile_and_execute(mir_module: &MirModule, _temp_name: &str) -> Result<B
|
||||
ConstValue::Null | ConstValue::Void => sem.const_null(),
|
||||
};
|
||||
regs.insert(*dst, vv);
|
||||
}
|
||||
MirInstruction::BinOp { dst, op, lhs, rhs } if matches!(op, BinaryOp::Add) => {
|
||||
use crate::backend::vm::VMValue as V;
|
||||
let a = regs.get(lhs).cloned().ok_or_else(|| format!("undef {:?}", lhs))?;
|
||||
let b = regs.get(rhs).cloned().ok_or_else(|| format!("undef {:?}", rhs))?;
|
||||
let out = sem.add(a, b);
|
||||
regs.insert(*dst, out);
|
||||
}
|
||||
MirInstruction::Copy { dst, src } => {
|
||||
if let Some(v) = regs.get(src).cloned() { regs.insert(*dst, v); }
|
||||
}
|
||||
MirInstruction::Debug { .. } | MirInstruction::Print { .. } | MirInstruction::Barrier { .. } | MirInstruction::BarrierRead { .. } | MirInstruction::BarrierWrite { .. } | MirInstruction::Safepoint | MirInstruction::Load { .. } | MirInstruction::Store { .. } | MirInstruction::TypeOp { .. } | MirInstruction::Compare { .. } | MirInstruction::NewBox { .. } | MirInstruction::PluginInvoke { .. } | MirInstruction::BoxCall { .. } | MirInstruction::RefGet { .. } | MirInstruction::RefSet { .. } | MirInstruction::WeakRef { .. } | MirInstruction::FutureNew { .. } | MirInstruction::FutureSet { .. } | MirInstruction::Await { .. } | MirInstruction::Throw { .. } | MirInstruction::Catch { .. } => {
|
||||
MirInstruction::BinOp { dst, op, lhs, rhs } if matches!(op, BinaryOp::Add) => {
|
||||
use crate::backend::vm::VMValue as V;
|
||||
let a = regs
|
||||
.get(lhs)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("undef {:?}", lhs))?;
|
||||
let b = regs
|
||||
.get(rhs)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("undef {:?}", rhs))?;
|
||||
let out = sem.add(a, b);
|
||||
regs.insert(*dst, out);
|
||||
}
|
||||
MirInstruction::Copy { dst, src } => {
|
||||
if let Some(v) = regs.get(src).cloned() {
|
||||
regs.insert(*dst, v);
|
||||
}
|
||||
}
|
||||
MirInstruction::Debug { .. }
|
||||
| MirInstruction::Print { .. }
|
||||
| MirInstruction::Barrier { .. }
|
||||
| MirInstruction::BarrierRead { .. }
|
||||
| MirInstruction::BarrierWrite { .. }
|
||||
| MirInstruction::Safepoint
|
||||
| MirInstruction::Load { .. }
|
||||
| MirInstruction::Store { .. }
|
||||
| MirInstruction::TypeOp { .. }
|
||||
| MirInstruction::Compare { .. }
|
||||
| MirInstruction::NewBox { .. }
|
||||
| MirInstruction::PluginInvoke { .. }
|
||||
| MirInstruction::BoxCall { .. }
|
||||
| MirInstruction::RefGet { .. }
|
||||
| MirInstruction::RefSet { .. }
|
||||
| MirInstruction::WeakRef { .. }
|
||||
| MirInstruction::FutureNew { .. }
|
||||
| MirInstruction::FutureSet { .. }
|
||||
| MirInstruction::Await { .. }
|
||||
| MirInstruction::Throw { .. }
|
||||
| MirInstruction::Catch { .. } => {
|
||||
// ignore for minimal path
|
||||
}
|
||||
MirInstruction::ExternCall { dst, iface_name, method_name, args, .. } => {
|
||||
MirInstruction::ExternCall {
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
..
|
||||
} => {
|
||||
use crate::backend::vm::VMValue as V;
|
||||
match (iface_name.as_str(), method_name.as_str()) {
|
||||
("env.local", "get") => {
|
||||
if let Some(d) = dst { if let Some(a0) = args.get(0) { if let Some(v) = regs.get(a0).cloned() { regs.insert(*d, v); } } }
|
||||
if let Some(d) = dst {
|
||||
if let Some(a0) = args.get(0) {
|
||||
if let Some(v) = regs.get(a0).cloned() {
|
||||
regs.insert(*d, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
("env.local", "set") => {
|
||||
if args.len() >= 2 { if let Some(v) = regs.get(&args[1]).cloned() { regs.insert(args[0], v); } }
|
||||
if args.len() >= 2 {
|
||||
if let Some(v) = regs.get(&args[1]).cloned() {
|
||||
regs.insert(args[0], v);
|
||||
}
|
||||
}
|
||||
// dst ignored
|
||||
}
|
||||
("env.box", "new") => {
|
||||
if let Some(d) = dst {
|
||||
if let Some(a0) = args.get(0) {
|
||||
if let Some(V::String(ty)) = regs.get(a0).cloned() {
|
||||
let reg = crate::runtime::box_registry::get_global_registry();
|
||||
let reg =
|
||||
crate::runtime::box_registry::get_global_registry();
|
||||
// Collect args as NyashBox
|
||||
let mut ny_args: Vec<Box<dyn crate::box_trait::NyashBox>> = Vec::new();
|
||||
let mut ny_args: Vec<Box<dyn crate::box_trait::NyashBox>> =
|
||||
Vec::new();
|
||||
for vid in args.iter().skip(1) {
|
||||
if let Some(v) = regs.get(vid).cloned() { ny_args.push(v.to_nyash_box()); }
|
||||
if let Some(v) = regs.get(vid).cloned() {
|
||||
ny_args.push(v.to_nyash_box());
|
||||
}
|
||||
}
|
||||
if let Ok(b) = reg.create_box(&ty, &ny_args) {
|
||||
regs.insert(*d, V::from_nyash_box(b));
|
||||
@ -151,11 +225,25 @@ pub fn compile_and_execute(mir_module: &MirModule, _temp_name: &str) -> Result<B
|
||||
};
|
||||
return Ok(out);
|
||||
}
|
||||
Some(MirInstruction::Jump { target }) => { last_pred = Some(bb.id); cur = *target; }
|
||||
Some(MirInstruction::Branch { condition, then_bb, else_bb }) => {
|
||||
Some(MirInstruction::Jump { target }) => {
|
||||
last_pred = Some(bb.id);
|
||||
cur = *target;
|
||||
}
|
||||
Some(MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
}) => {
|
||||
// Minimal: integer/bool truthiness
|
||||
let c = regs.get(condition).cloned().unwrap_or(crate::backend::vm::VMValue::Void);
|
||||
let t = match c { crate::backend::vm::VMValue::Bool(b) => b, crate::backend::vm::VMValue::Integer(i) => i != 0, _ => false };
|
||||
let c = regs
|
||||
.get(condition)
|
||||
.cloned()
|
||||
.unwrap_or(crate::backend::vm::VMValue::Void);
|
||||
let t = match c {
|
||||
crate::backend::vm::VMValue::Bool(b) => b,
|
||||
crate::backend::vm::VMValue::Integer(i) => i != 0,
|
||||
_ => false,
|
||||
};
|
||||
last_pred = Some(bb.id);
|
||||
cur = if t { *then_bb } else { *else_bb };
|
||||
}
|
||||
|
||||
@ -5,15 +5,19 @@
|
||||
* Status: Initial skeleton; currently unused. Future: build static table for hot-path dispatch.
|
||||
*/
|
||||
|
||||
use crate::mir::MirInstruction;
|
||||
use super::vm::{VM, VMError};
|
||||
use super::vm::ControlFlow;
|
||||
use crate::mir::CompareOp;
|
||||
use super::vm::VMValue;
|
||||
use super::vm::{VMError, VM};
|
||||
use crate::mir::CompareOp;
|
||||
use crate::mir::MirInstruction;
|
||||
|
||||
/// Minimal dispatcher that routes a single instruction to the appropriate handler.
|
||||
/// Keeps behavior identical to the big match in vm.rs but centralized here.
|
||||
pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, debug_global: bool) -> Result<ControlFlow, VMError> {
|
||||
pub(super) fn execute_instruction(
|
||||
vm: &mut VM,
|
||||
instruction: &MirInstruction,
|
||||
debug_global: bool,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
match instruction {
|
||||
// Basic operations
|
||||
MirInstruction::Const { dst, value } => vm.execute_const(*dst, value),
|
||||
@ -23,30 +27,59 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
eprintln!("[VM] execute_instruction -> BinOp({:?})", op);
|
||||
}
|
||||
vm.execute_binop(*dst, op, *lhs, *rhs)
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::UnaryOp { dst, op, operand } => vm.execute_unaryop(*dst, op, *operand),
|
||||
|
||||
MirInstruction::Compare { dst, op, lhs, rhs } => {
|
||||
let debug_cmp = debug_global || std::env::var("NYASH_VM_DEBUG_CMP").ok().as_deref() == Some("1");
|
||||
if debug_cmp { eprintln!("[VM] dispatch Compare op={:?} lhs={:?} rhs={:?}", op, lhs, rhs); }
|
||||
let debug_cmp =
|
||||
debug_global || std::env::var("NYASH_VM_DEBUG_CMP").ok().as_deref() == Some("1");
|
||||
if debug_cmp {
|
||||
eprintln!(
|
||||
"[VM] dispatch Compare op={:?} lhs={:?} rhs={:?}",
|
||||
op, lhs, rhs
|
||||
);
|
||||
}
|
||||
if let (Ok(lv), Ok(rv)) = (vm.get_value(*lhs), vm.get_value(*rhs)) {
|
||||
if debug_cmp { eprintln!("[VM] values before fastpath: left={:?} right={:?}", lv, rv); }
|
||||
if debug_cmp {
|
||||
eprintln!("[VM] values before fastpath: left={:?} right={:?}", lv, rv);
|
||||
}
|
||||
if let (VMValue::BoxRef(lb), VMValue::BoxRef(rb)) = (&lv, &rv) {
|
||||
if debug_cmp { eprintln!("[VM] BoxRef types: lty={} rty={} lstr={} rstr={}", lb.type_name(), rb.type_name(), lb.to_string_box().value, rb.to_string_box().value); }
|
||||
let li = lb.as_any().downcast_ref::<crate::box_trait::IntegerBox>().map(|x| x.value)
|
||||
if debug_cmp {
|
||||
eprintln!(
|
||||
"[VM] BoxRef types: lty={} rty={} lstr={} rstr={}",
|
||||
lb.type_name(),
|
||||
rb.type_name(),
|
||||
lb.to_string_box().value,
|
||||
rb.to_string_box().value
|
||||
);
|
||||
}
|
||||
let li = lb
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::IntegerBox>()
|
||||
.map(|x| x.value)
|
||||
.or_else(|| lb.to_string_box().value.trim().parse::<i64>().ok());
|
||||
let ri = rb.as_any().downcast_ref::<crate::box_trait::IntegerBox>().map(|x| x.value)
|
||||
let ri = rb
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::IntegerBox>()
|
||||
.map(|x| x.value)
|
||||
.or_else(|| rb.to_string_box().value.trim().parse::<i64>().ok());
|
||||
if let (Some(li), Some(ri)) = (li, ri) {
|
||||
let out = match op { CompareOp::Eq => li == ri, CompareOp::Ne => li != ri, CompareOp::Lt => li < ri, CompareOp::Le => li <= ri, CompareOp::Gt => li > ri, CompareOp::Ge => li >= ri };
|
||||
let out = match op {
|
||||
CompareOp::Eq => li == ri,
|
||||
CompareOp::Ne => li != ri,
|
||||
CompareOp::Lt => li < ri,
|
||||
CompareOp::Le => li <= ri,
|
||||
CompareOp::Gt => li > ri,
|
||||
CompareOp::Ge => li >= ri,
|
||||
};
|
||||
vm.set_value(*dst, VMValue::Bool(out));
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
}
|
||||
}
|
||||
vm.execute_compare(*dst, op, *lhs, *rhs)
|
||||
},
|
||||
}
|
||||
|
||||
// I/O operations
|
||||
MirInstruction::Print { value, .. } => vm.execute_print(*value),
|
||||
@ -57,12 +90,20 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
// Control flow
|
||||
MirInstruction::Return { value } => {
|
||||
if crate::config::env::vm_vt_trace() {
|
||||
if let Some(v) = value { eprintln!("[VT] Dispatch Return val_id={}", v.to_usize()); } else { eprintln!("[VT] Dispatch Return void"); }
|
||||
if let Some(v) = value {
|
||||
eprintln!("[VT] Dispatch Return val_id={}", v.to_usize());
|
||||
} else {
|
||||
eprintln!("[VT] Dispatch Return void");
|
||||
}
|
||||
}
|
||||
vm.execute_return(*value)
|
||||
},
|
||||
}
|
||||
MirInstruction::Jump { target } => vm.execute_jump(*target),
|
||||
MirInstruction::Branch { condition, then_bb, else_bb } => vm.execute_branch(*condition, *then_bb, *else_bb),
|
||||
MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
} => vm.execute_branch(*condition, *then_bb, *else_bb),
|
||||
MirInstruction::Phi { dst, inputs } => vm.execute_phi(*dst, inputs),
|
||||
|
||||
// Memory operations
|
||||
@ -71,20 +112,63 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
MirInstruction::Copy { dst, src } => vm.execute_copy(*dst, *src),
|
||||
|
||||
// Complex operations
|
||||
MirInstruction::Call { dst, func, args, effects: _ } => vm.execute_call(*dst, *func, args),
|
||||
MirInstruction::FunctionNew { dst, params, body, captures, me } => vm.execute_function_new(*dst, params, body, captures, me),
|
||||
MirInstruction::BoxCall { dst, box_val, method, method_id, args, effects: _ , .. } => vm.execute_boxcall(*dst, *box_val, method, *method_id, args),
|
||||
MirInstruction::PluginInvoke { dst, box_val, method, args, effects: _ } => vm.execute_plugin_invoke(*dst, *box_val, method, args),
|
||||
MirInstruction::NewBox { dst, box_type, args } => vm.execute_newbox(*dst, box_type, args),
|
||||
MirInstruction::Call {
|
||||
dst,
|
||||
func,
|
||||
args,
|
||||
effects: _,
|
||||
} => vm.execute_call(*dst, *func, args),
|
||||
MirInstruction::FunctionNew {
|
||||
dst,
|
||||
params,
|
||||
body,
|
||||
captures,
|
||||
me,
|
||||
} => vm.execute_function_new(*dst, params, body, captures, me),
|
||||
MirInstruction::BoxCall {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
method_id,
|
||||
args,
|
||||
effects: _,
|
||||
..
|
||||
} => vm.execute_boxcall(*dst, *box_val, method, *method_id, args),
|
||||
MirInstruction::PluginInvoke {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
effects: _,
|
||||
} => vm.execute_plugin_invoke(*dst, *box_val, method, args),
|
||||
MirInstruction::NewBox {
|
||||
dst,
|
||||
box_type,
|
||||
args,
|
||||
} => vm.execute_newbox(*dst, box_type, args),
|
||||
|
||||
// Array operations
|
||||
MirInstruction::ArrayGet { dst, array, index } => vm.execute_array_get(*dst, *array, *index),
|
||||
MirInstruction::ArraySet { array, index, value } => vm.execute_array_set(*array, *index, *value),
|
||||
MirInstruction::ArrayGet { dst, array, index } => {
|
||||
vm.execute_array_get(*dst, *array, *index)
|
||||
}
|
||||
MirInstruction::ArraySet {
|
||||
array,
|
||||
index,
|
||||
value,
|
||||
} => vm.execute_array_set(*array, *index, *value),
|
||||
|
||||
// Reference operations
|
||||
MirInstruction::RefNew { dst, box_val } => vm.execute_ref_new(*dst, *box_val),
|
||||
MirInstruction::RefGet { dst, reference, field } => vm.execute_ref_get(*dst, *reference, field),
|
||||
MirInstruction::RefSet { reference, field, value } => vm.execute_ref_set(*reference, field, *value),
|
||||
MirInstruction::RefGet {
|
||||
dst,
|
||||
reference,
|
||||
field,
|
||||
} => vm.execute_ref_get(*dst, *reference, field),
|
||||
MirInstruction::RefSet {
|
||||
reference,
|
||||
field,
|
||||
value,
|
||||
} => vm.execute_ref_set(*reference, field, *value),
|
||||
|
||||
// Weak references
|
||||
MirInstruction::WeakNew { dst, box_val } => vm.execute_weak_new(*dst, *box_val),
|
||||
@ -108,11 +192,16 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
let (func, bb, pc) = vm.gc_site_info();
|
||||
eprintln!("[GC] barrier: Write @{} bb={} pc={}", func, bb, pc);
|
||||
}
|
||||
vm.runtime.gc.barrier(crate::runtime::gc::BarrierKind::Write);
|
||||
vm.runtime
|
||||
.gc
|
||||
.barrier(crate::runtime::gc::BarrierKind::Write);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
MirInstruction::Barrier { op, .. } => {
|
||||
let k = match op { crate::mir::BarrierOp::Read => crate::runtime::gc::BarrierKind::Read, crate::mir::BarrierOp::Write => crate::runtime::gc::BarrierKind::Write };
|
||||
let k = match op {
|
||||
crate::mir::BarrierOp::Read => crate::runtime::gc::BarrierKind::Read,
|
||||
crate::mir::BarrierOp::Write => crate::runtime::gc::BarrierKind::Write,
|
||||
};
|
||||
if crate::config::env::gc_trace() {
|
||||
let (func, bb, pc) = vm.gc_site_info();
|
||||
eprintln!("[GC] barrier: {:?} @{} bb={} pc={}", k, func, bb, pc);
|
||||
@ -123,7 +212,9 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
|
||||
// Exceptions
|
||||
MirInstruction::Throw { exception, .. } => vm.execute_throw(*exception),
|
||||
MirInstruction::Catch { exception_value, .. } => vm.execute_catch(*exception_value),
|
||||
MirInstruction::Catch {
|
||||
exception_value, ..
|
||||
} => vm.execute_catch(*exception_value),
|
||||
|
||||
// Futures
|
||||
MirInstruction::FutureNew { dst, value } => {
|
||||
@ -141,15 +232,31 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
future_box.set_result(new_value.to_nyash_box());
|
||||
Ok(ControlFlow::Continue)
|
||||
} else {
|
||||
Err(VMError::TypeError(format!("Expected Future, got {:?}", future_val)))
|
||||
Err(VMError::TypeError(format!(
|
||||
"Expected Future, got {:?}",
|
||||
future_val
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
// Special
|
||||
MirInstruction::Await { dst, future } => vm.execute_await(*dst, *future),
|
||||
MirInstruction::ExternCall { dst, iface_name, method_name, args, .. } => vm.execute_extern_call(*dst, iface_name, method_name, args),
|
||||
MirInstruction::TypeCheck { dst, .. } => { vm.set_value(*dst, VMValue::Bool(true)); Ok(ControlFlow::Continue) }
|
||||
MirInstruction::Cast { dst, value, .. } => { let val = vm.get_value(*value)?; vm.set_value(*dst, val); Ok(ControlFlow::Continue) }
|
||||
MirInstruction::ExternCall {
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
..
|
||||
} => vm.execute_extern_call(*dst, iface_name, method_name, args),
|
||||
MirInstruction::TypeCheck { dst, .. } => {
|
||||
vm.set_value(*dst, VMValue::Bool(true));
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
MirInstruction::Cast { dst, value, .. } => {
|
||||
let val = vm.get_value(*value)?;
|
||||
vm.set_value(*dst, val);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
MirInstruction::Debug { .. } => Ok(ControlFlow::Continue),
|
||||
MirInstruction::Nop => Ok(ControlFlow::Continue),
|
||||
MirInstruction::Safepoint => {
|
||||
@ -159,9 +266,11 @@ pub(super) fn execute_instruction(vm: &mut VM, instruction: &MirInstruction, deb
|
||||
}
|
||||
vm.runtime.gc.safepoint();
|
||||
// Cooperative scheduling: poll single-thread scheduler
|
||||
if let Some(s) = &vm.runtime.scheduler { s.poll(); }
|
||||
if let Some(s) = &vm.runtime.scheduler {
|
||||
s.poll();
|
||||
}
|
||||
Ok(ControlFlow::Continue)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,11 +281,17 @@ pub struct DispatchEntry;
|
||||
pub struct DispatchTable;
|
||||
|
||||
impl DispatchTable {
|
||||
pub fn new() -> Self { Self }
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
/// Example API for future use: resolve a handler for an instruction
|
||||
pub fn resolve(&self, _instr: &MirInstruction) -> Option<DispatchEntry> { None }
|
||||
pub fn resolve(&self, _instr: &MirInstruction) -> Option<DispatchEntry> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Example execution of a dispatch entry
|
||||
pub fn execute_entry(_entry: &DispatchEntry) -> Result<(), VMError> { Ok(()) }
|
||||
pub fn execute_entry(_entry: &DispatchEntry) -> Result<(), VMError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -15,7 +15,16 @@ pub struct ExecutionFrame {
|
||||
}
|
||||
|
||||
impl ExecutionFrame {
|
||||
pub fn new() -> Self { Self { current_block: None, pc: 0, last_result: None } }
|
||||
pub fn reset(&mut self) { self.current_block = None; self.pc = 0; self.last_result = None; }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current_block: None,
|
||||
pc: 0,
|
||||
last_result: None,
|
||||
}
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.current_block = None;
|
||||
self.pc = 0;
|
||||
self.last_result = None;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -12,10 +12,16 @@ pub fn is_mutating_builtin_call(recv: &VMValue, method: &str) -> bool {
|
||||
|
||||
match recv {
|
||||
VMValue::BoxRef(b) => {
|
||||
if b.as_any().downcast_ref::<crate::boxes::array::ArrayBox>().is_some() {
|
||||
if b.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
.is_some()
|
||||
{
|
||||
return ARRAY_METHODS.iter().any(|m| *m == method);
|
||||
}
|
||||
if b.as_any().downcast_ref::<crate::boxes::map_box::MapBox>().is_some() {
|
||||
if b.as_any()
|
||||
.downcast_ref::<crate::boxes::map_box::MapBox>()
|
||||
.is_some()
|
||||
{
|
||||
return MAP_METHODS.iter().any(|m| *m == method);
|
||||
}
|
||||
false
|
||||
@ -28,7 +34,11 @@ pub fn is_mutating_builtin_call(recv: &VMValue, method: &str) -> bool {
|
||||
pub fn gc_write_barrier_site(runtime: &crate::runtime::NyashRuntime, site: &str) {
|
||||
let trace = crate::config::env::gc_trace();
|
||||
let strict = crate::config::env::gc_barrier_strict();
|
||||
let before = if strict { runtime.gc.snapshot_counters() } else { None };
|
||||
let before = if strict {
|
||||
runtime.gc.snapshot_counters()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if trace {
|
||||
eprintln!("[GC] barrier: Write @{}", site);
|
||||
}
|
||||
@ -38,10 +48,16 @@ pub fn gc_write_barrier_site(runtime: &crate::runtime::NyashRuntime, site: &str)
|
||||
match (before, after) {
|
||||
(Some((_, _, bw)), Some((_, _, aw))) if aw > bw => {}
|
||||
(Some(_), Some(_)) => {
|
||||
panic!("[GC][STRICT] write barrier did not increment at site='{}'", site);
|
||||
panic!(
|
||||
"[GC][STRICT] write barrier did not increment at site='{}'",
|
||||
site
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
panic!("[GC][STRICT] CountingGc required for strict verification at site='{}'", site);
|
||||
panic!(
|
||||
"[GC][STRICT] CountingGc required for strict verification at site='{}'",
|
||||
site
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,4 +13,3 @@ pub mod compiler {
|
||||
pub mod box_types {
|
||||
pub use crate::backend::llvm_legacy::box_types::*;
|
||||
}
|
||||
|
||||
|
||||
@ -3,4 +3,3 @@
|
||||
pub fn load_box_type_ids() -> std::collections::HashMap<String, u32> {
|
||||
std::collections::HashMap::new()
|
||||
}
|
||||
|
||||
|
||||
@ -1,2 +1 @@
|
||||
// legacy aot placeholder; full implementation retained in archived branch or prior history
|
||||
|
||||
|
||||
10
src/backend/llvm_legacy/compiler/codegen.rs
Normal file
10
src/backend/llvm_legacy/compiler/codegen.rs
Normal file
@ -0,0 +1,10 @@
|
||||
//! Legacy LLVM codegen placeholder.
|
||||
//!
|
||||
//! The original inkwell-based implementation was removed during the Phase-15
|
||||
//! refactor. We keep a stub module so tools like `cargo fmt` can resolve the
|
||||
//! module tree even when the legacy feature is gated off.
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn lower_module() {
|
||||
// Intentionally left blank.
|
||||
}
|
||||
@ -1,2 +1 @@
|
||||
// legacy helpers placeholder; kept to satisfy module structure after move
|
||||
|
||||
|
||||
6
src/backend/llvm_legacy/compiler/interpreter.rs
Normal file
6
src/backend/llvm_legacy/compiler/interpreter.rs
Normal file
@ -0,0 +1,6 @@
|
||||
//! Legacy LLVM interpreter placeholder.
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn execute() {
|
||||
// Stub implementation.
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::box_trait::{IntegerBox, NyashBox};
|
||||
use crate::mir::function::MirModule;
|
||||
use crate::box_trait::{NyashBox, IntegerBox};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct LLVMCompiler {
|
||||
@ -8,7 +8,9 @@ pub struct LLVMCompiler {
|
||||
|
||||
impl LLVMCompiler {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
Ok(Self { values: HashMap::new() })
|
||||
Ok(Self {
|
||||
values: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn compile_module(&self, _mir: &MirModule, _out: &str) -> Result<(), String> {
|
||||
@ -16,8 +18,11 @@ impl LLVMCompiler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compile_and_execute(&mut self, _mir: &MirModule, _out: &str) -> Result<Box<dyn NyashBox>, String> {
|
||||
pub fn compile_and_execute(
|
||||
&mut self,
|
||||
_mir: &MirModule,
|
||||
_out: &str,
|
||||
) -> Result<Box<dyn NyashBox>, String> {
|
||||
Ok(Box::new(IntegerBox::new(0)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -31,4 +31,3 @@ mod tests {
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -11,19 +11,21 @@ pub struct CodegenContext {
|
||||
#[cfg(not(feature = "llvm-inkwell-legacy"))]
|
||||
impl CodegenContext {
|
||||
pub fn new(_module_name: &str) -> Result<Self, String> {
|
||||
Ok(Self { _phantom: std::marker::PhantomData })
|
||||
Ok(Self {
|
||||
_phantom: std::marker::PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Real implementation (compiled only when feature "llvm-inkwell-legacy" is enabled)
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
use inkwell::builder::Builder;
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
use inkwell::context::Context;
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
use inkwell::module::Module;
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
use inkwell::builder::Builder;
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
use inkwell::targets::{Target, TargetMachine, InitializationConfig};
|
||||
use inkwell::targets::{InitializationConfig, Target, TargetMachine};
|
||||
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub struct CodegenContext<'ctx> {
|
||||
@ -40,8 +42,8 @@ impl<'ctx> CodegenContext<'ctx> {
|
||||
.map_err(|e| format!("Failed to initialize native target: {}", e))?;
|
||||
let module = context.create_module(module_name);
|
||||
let triple = TargetMachine::get_default_triple();
|
||||
let target = Target::from_triple(&triple)
|
||||
.map_err(|e| format!("Failed to get target: {}", e))?;
|
||||
let target =
|
||||
Target::from_triple(&triple).map_err(|e| format!("Failed to get target: {}", e))?;
|
||||
let target_machine = target
|
||||
.create_target_machine(
|
||||
&triple,
|
||||
@ -53,7 +55,11 @@ impl<'ctx> CodegenContext<'ctx> {
|
||||
)
|
||||
.ok_or_else(|| "Failed to create target machine".to_string())?;
|
||||
let builder = context.create_builder();
|
||||
Ok(Self { context, module, builder, target_machine })
|
||||
Ok(Self {
|
||||
context,
|
||||
module,
|
||||
builder,
|
||||
target_machine,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -34,4 +34,3 @@ mod tests {
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,10 +8,12 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::vm::{VMValue, VMError};
|
||||
use crate::backend::abi_util::{eq_vm, to_bool_vm};
|
||||
use crate::backend::vm::{VMError, VMValue};
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::mir::{MirModule, MirFunction, MirInstruction, ConstValue, BinaryOp, CompareOp, ValueId, BasicBlockId};
|
||||
use crate::backend::abi_util::{to_bool_vm, eq_vm};
|
||||
use crate::mir::{
|
||||
BasicBlockId, BinaryOp, CompareOp, ConstValue, MirFunction, MirInstruction, MirModule, ValueId,
|
||||
};
|
||||
|
||||
pub struct MirInterpreter {
|
||||
// SSA value table
|
||||
@ -23,11 +25,20 @@ pub struct MirInterpreter {
|
||||
}
|
||||
|
||||
impl MirInterpreter {
|
||||
pub fn new() -> Self { Self { regs: HashMap::new(), mem: HashMap::new(), obj_fields: HashMap::new() } }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
regs: HashMap::new(),
|
||||
mem: HashMap::new(),
|
||||
obj_fields: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute module entry (main) and return boxed result
|
||||
pub fn execute_module(&mut self, module: &MirModule) -> Result<Box<dyn NyashBox>, VMError> {
|
||||
let func = module.functions.get("main").ok_or_else(|| VMError::InvalidInstruction("missing main".into()))?;
|
||||
let func = module
|
||||
.functions
|
||||
.get("main")
|
||||
.ok_or_else(|| VMError::InvalidInstruction("missing main".into()))?;
|
||||
let ret = self.execute_function(func)?;
|
||||
Ok(ret.to_nyash_box())
|
||||
}
|
||||
@ -36,7 +47,10 @@ impl MirInterpreter {
|
||||
let mut cur = func.entry_block;
|
||||
let mut last_pred: Option<BasicBlockId> = None;
|
||||
loop {
|
||||
let block = func.blocks.get(&cur).ok_or_else(|| VMError::InvalidBasicBlock(format!("bb {:?} not found", cur)))?;
|
||||
let block = func
|
||||
.blocks
|
||||
.get(&cur)
|
||||
.ok_or_else(|| VMError::InvalidBasicBlock(format!("bb {:?} not found", cur)))?;
|
||||
// Resolve incoming phi nodes using predecessor
|
||||
for inst in &block.instructions {
|
||||
if let MirInstruction::Phi { dst, inputs } = inst {
|
||||
@ -67,17 +81,34 @@ impl MirInterpreter {
|
||||
};
|
||||
self.regs.insert(*dst, v);
|
||||
}
|
||||
MirInstruction::NewBox { dst, box_type, args } => {
|
||||
MirInstruction::NewBox {
|
||||
dst,
|
||||
box_type,
|
||||
args,
|
||||
} => {
|
||||
// Build arg boxes
|
||||
let mut a: Vec<Box<dyn crate::box_trait::NyashBox>> = Vec::new();
|
||||
for vid in args { a.push(self.reg_load(*vid)?.to_nyash_box()); }
|
||||
for vid in args {
|
||||
a.push(self.reg_load(*vid)?.to_nyash_box());
|
||||
}
|
||||
// Use unified global registry (plugins already initialized by runner)
|
||||
let reg = crate::runtime::unified_registry::get_global_unified_registry();
|
||||
let created = reg.lock().unwrap().create_box(box_type, &a)
|
||||
.map_err(|e| VMError::InvalidInstruction(format!("NewBox {} failed: {}", box_type, e)))?;
|
||||
let created =
|
||||
reg.lock().unwrap().create_box(box_type, &a).map_err(|e| {
|
||||
VMError::InvalidInstruction(format!(
|
||||
"NewBox {} failed: {}",
|
||||
box_type, e
|
||||
))
|
||||
})?;
|
||||
self.regs.insert(*dst, VMValue::from_nyash_box(created));
|
||||
}
|
||||
MirInstruction::PluginInvoke { dst, box_val, method, args, .. } => {
|
||||
MirInstruction::PluginInvoke {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
..
|
||||
} => {
|
||||
// Resolve receiver
|
||||
let recv = self.reg_load(*box_val)?;
|
||||
let recv_box: Box<dyn crate::box_trait::NyashBox> = match recv.clone() {
|
||||
@ -85,40 +116,101 @@ impl MirInterpreter {
|
||||
other => other.to_nyash_box(),
|
||||
};
|
||||
// If PluginBoxV2 → invoke via unified plugin host
|
||||
if let Some(p) = recv_box.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
let host = crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
||||
if let Some(p) = recv_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>(
|
||||
) {
|
||||
let host =
|
||||
crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
let mut argv: Vec<Box<dyn crate::box_trait::NyashBox>> = Vec::new();
|
||||
for a in args { argv.push(self.reg_load(*a)?.to_nyash_box()); }
|
||||
match host.invoke_instance_method(&p.box_type, method, p.inner.instance_id, &argv) {
|
||||
for a in args {
|
||||
argv.push(self.reg_load(*a)?.to_nyash_box());
|
||||
}
|
||||
match host.invoke_instance_method(
|
||||
&p.box_type,
|
||||
method,
|
||||
p.inner.instance_id,
|
||||
&argv,
|
||||
) {
|
||||
Ok(Some(ret)) => {
|
||||
if let Some(d) = dst { self.regs.insert(*d, VMValue::from_nyash_box(ret)); }
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::from_nyash_box(ret));
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::Void);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"PluginInvoke {}.{} failed: {:?}",
|
||||
p.box_type, method, e
|
||||
)));
|
||||
}
|
||||
Ok(None) => { if let Some(d) = dst { self.regs.insert(*d, VMValue::Void); } }
|
||||
Err(e) => { return Err(VMError::InvalidInstruction(format!("PluginInvoke {}.{} failed: {:?}", p.box_type, method, e))); }
|
||||
}
|
||||
} else {
|
||||
// Minimal fallback: toString
|
||||
if method == "toString" {
|
||||
if let Some(d) = dst { self.regs.insert(*d, VMValue::String(recv_box.to_string_box().value)); }
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(
|
||||
*d,
|
||||
VMValue::String(recv_box.to_string_box().value),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Err(VMError::InvalidInstruction(format!("PluginInvoke unsupported on {} for method {}", recv_box.type_name(), method)));
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"PluginInvoke unsupported on {} for method {}",
|
||||
recv_box.type_name(),
|
||||
method
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
MirInstruction::BoxCall { dst, box_val, method, args, .. } => {
|
||||
MirInstruction::BoxCall {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
..
|
||||
} => {
|
||||
// Support getField/setField for normalized RefGet/RefSet
|
||||
if method == "getField" {
|
||||
if args.len() != 1 { return Err(VMError::InvalidInstruction("getField expects 1 arg".into())); }
|
||||
let fname = match self.reg_load(args[0].clone())? { VMValue::String(s) => s, v => v.to_string() };
|
||||
let v = self.obj_fields.get(box_val).and_then(|m| m.get(&fname)).cloned().unwrap_or(VMValue::Void);
|
||||
if let Some(d) = dst { self.regs.insert(*d, v); }
|
||||
if args.len() != 1 {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"getField expects 1 arg".into(),
|
||||
));
|
||||
}
|
||||
let fname = match self.reg_load(args[0].clone())? {
|
||||
VMValue::String(s) => s,
|
||||
v => v.to_string(),
|
||||
};
|
||||
let v = self
|
||||
.obj_fields
|
||||
.get(box_val)
|
||||
.and_then(|m| m.get(&fname))
|
||||
.cloned()
|
||||
.unwrap_or(VMValue::Void);
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, v);
|
||||
}
|
||||
continue;
|
||||
} else if method == "setField" {
|
||||
if args.len() != 2 { return Err(VMError::InvalidInstruction("setField expects 2 args".into())); }
|
||||
let fname = match self.reg_load(args[0].clone())? { VMValue::String(s) => s, v => v.to_string() };
|
||||
if args.len() != 2 {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"setField expects 2 args".into(),
|
||||
));
|
||||
}
|
||||
let fname = match self.reg_load(args[0].clone())? {
|
||||
VMValue::String(s) => s,
|
||||
v => v.to_string(),
|
||||
};
|
||||
let valv = self.reg_load(args[1].clone())?;
|
||||
self.obj_fields.entry(*box_val).or_default().insert(fname, valv);
|
||||
self.obj_fields
|
||||
.entry(*box_val)
|
||||
.or_default()
|
||||
.insert(fname, valv);
|
||||
continue;
|
||||
}
|
||||
// Fallback: treat like PluginInvoke for plugin-backed boxes
|
||||
@ -127,7 +219,10 @@ impl MirInterpreter {
|
||||
VMValue::BoxRef(b) => b.share_box(),
|
||||
other => other.to_nyash_box(),
|
||||
};
|
||||
if let Some(p) = recv_box.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
if let Some(p) = recv_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>(
|
||||
) {
|
||||
// Special-case: ConsoleBox.readLine → stdin fallback if not provided by plugin
|
||||
if p.box_type == "ConsoleBox" && method == "readLine" {
|
||||
use std::io::{self, Read};
|
||||
@ -136,64 +231,145 @@ impl MirInterpreter {
|
||||
// Read a single line (blocking)
|
||||
let mut buf = [0u8; 1];
|
||||
while let Ok(n) = stdin.read(&mut buf) {
|
||||
if n == 0 { break; }
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
let ch = buf[0] as char;
|
||||
if ch == '\n' { break; }
|
||||
if ch == '\n' {
|
||||
break;
|
||||
}
|
||||
s.push(ch);
|
||||
if s.len() > 1_000_000 { break; }
|
||||
if s.len() > 1_000_000 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::String(s));
|
||||
}
|
||||
if let Some(d) = dst { self.regs.insert(*d, VMValue::String(s)); }
|
||||
continue;
|
||||
}
|
||||
let host = crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
||||
let host =
|
||||
crate::runtime::plugin_loader_unified::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
let mut argv: Vec<Box<dyn crate::box_trait::NyashBox>> = Vec::new();
|
||||
for a in args { argv.push(self.reg_load(*a)?.to_nyash_box()); }
|
||||
match host.invoke_instance_method(&p.box_type, method, p.inner.instance_id, &argv) {
|
||||
Ok(Some(ret)) => { if let Some(d) = dst { self.regs.insert(*d, VMValue::from_nyash_box(ret)); } }
|
||||
Ok(None) => { if let Some(d) = dst { self.regs.insert(*d, VMValue::Void); } }
|
||||
Err(e) => { return Err(VMError::InvalidInstruction(format!("BoxCall {}.{} failed: {:?}", p.box_type, method, e))); }
|
||||
for a in args {
|
||||
argv.push(self.reg_load(*a)?.to_nyash_box());
|
||||
}
|
||||
match host.invoke_instance_method(
|
||||
&p.box_type,
|
||||
method,
|
||||
p.inner.instance_id,
|
||||
&argv,
|
||||
) {
|
||||
Ok(Some(ret)) => {
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::from_nyash_box(ret));
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::Void);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"BoxCall {}.{} failed: {:?}",
|
||||
p.box_type, method, e
|
||||
)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(VMError::InvalidInstruction(format!("BoxCall unsupported on {}.{}", recv_box.type_name(), method)));
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"BoxCall unsupported on {}.{}",
|
||||
recv_box.type_name(),
|
||||
method
|
||||
)));
|
||||
}
|
||||
}
|
||||
MirInstruction::ExternCall { dst, iface_name, method_name, args, .. } => {
|
||||
MirInstruction::ExternCall {
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
..
|
||||
} => {
|
||||
// Minimal env.console.log bridge
|
||||
if iface_name == "env.console" && method_name == "log" {
|
||||
if let Some(a0) = args.get(0) {
|
||||
let v = self.reg_load(*a0)?;
|
||||
println!("{}", v.to_string());
|
||||
}
|
||||
if let Some(d) = dst { self.regs.insert(*d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.regs.insert(*d, VMValue::Void);
|
||||
}
|
||||
} else {
|
||||
return Err(VMError::InvalidInstruction(format!("ExternCall {}.{} not supported", iface_name, method_name)));
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"ExternCall {}.{} not supported",
|
||||
iface_name, method_name
|
||||
)));
|
||||
}
|
||||
}
|
||||
MirInstruction::RefSet { reference, field, value } => {
|
||||
MirInstruction::RefSet {
|
||||
reference,
|
||||
field,
|
||||
value,
|
||||
} => {
|
||||
let v = self.reg_load(*value)?;
|
||||
self.obj_fields.entry(*reference).or_default().insert(field.clone(), v);
|
||||
self.obj_fields
|
||||
.entry(*reference)
|
||||
.or_default()
|
||||
.insert(field.clone(), v);
|
||||
}
|
||||
MirInstruction::RefGet { dst, reference, field } => {
|
||||
let v = self.obj_fields.get(reference).and_then(|m| m.get(field)).cloned().unwrap_or(VMValue::Void);
|
||||
MirInstruction::RefGet {
|
||||
dst,
|
||||
reference,
|
||||
field,
|
||||
} => {
|
||||
let v = self
|
||||
.obj_fields
|
||||
.get(reference)
|
||||
.and_then(|m| m.get(field))
|
||||
.cloned()
|
||||
.unwrap_or(VMValue::Void);
|
||||
self.regs.insert(*dst, v);
|
||||
}
|
||||
MirInstruction::BinOp { dst, op, lhs, rhs } => {
|
||||
let a = self.reg_load(*lhs)?; let b = self.reg_load(*rhs)?;
|
||||
let a = self.reg_load(*lhs)?;
|
||||
let b = self.reg_load(*rhs)?;
|
||||
let v = self.eval_binop(*op, a, b)?;
|
||||
self.regs.insert(*dst, v);
|
||||
}
|
||||
MirInstruction::UnaryOp { dst, op, operand } => {
|
||||
let x = self.reg_load(*operand)?;
|
||||
let v = match op {
|
||||
crate::mir::UnaryOp::Neg => match x { VMValue::Integer(i) => VMValue::Integer(-i), VMValue::Float(f)=>VMValue::Float(-f), _=> return Err(VMError::TypeError(format!("neg expects number, got {:?}", x))) },
|
||||
crate::mir::UnaryOp::Not => VMValue::Bool(!to_bool_vm(&x).map_err(|e| VMError::TypeError(e))?),
|
||||
crate::mir::UnaryOp::BitNot => match x { VMValue::Integer(i) => VMValue::Integer(!i), _=> return Err(VMError::TypeError(format!("bitnot expects integer, got {:?}", x))) },
|
||||
crate::mir::UnaryOp::Neg => match x {
|
||||
VMValue::Integer(i) => VMValue::Integer(-i),
|
||||
VMValue::Float(f) => VMValue::Float(-f),
|
||||
_ => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"neg expects number, got {:?}",
|
||||
x
|
||||
)))
|
||||
}
|
||||
},
|
||||
crate::mir::UnaryOp::Not => {
|
||||
VMValue::Bool(!to_bool_vm(&x).map_err(|e| VMError::TypeError(e))?)
|
||||
}
|
||||
crate::mir::UnaryOp::BitNot => match x {
|
||||
VMValue::Integer(i) => VMValue::Integer(!i),
|
||||
_ => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"bitnot expects integer, got {:?}",
|
||||
x
|
||||
)))
|
||||
}
|
||||
},
|
||||
};
|
||||
self.regs.insert(*dst, v);
|
||||
}
|
||||
MirInstruction::Compare { dst, op, lhs, rhs } => {
|
||||
let a = self.reg_load(*lhs)?; let b = self.reg_load(*rhs)?;
|
||||
let a = self.reg_load(*lhs)?;
|
||||
let b = self.reg_load(*rhs)?;
|
||||
let v = self.eval_cmp(*op, a, b)?;
|
||||
self.regs.insert(*dst, VMValue::Bool(v));
|
||||
}
|
||||
@ -206,7 +382,8 @@ impl MirInterpreter {
|
||||
self.mem.insert(*ptr, v);
|
||||
}
|
||||
MirInstruction::Copy { dst, src } => {
|
||||
let v = self.reg_load(*src)?; self.regs.insert(*dst, v);
|
||||
let v = self.reg_load(*src)?;
|
||||
self.regs.insert(*dst, v);
|
||||
}
|
||||
MirInstruction::Debug { value, message } => {
|
||||
let v = self.reg_load(*value)?;
|
||||
@ -215,38 +392,75 @@ impl MirInterpreter {
|
||||
}
|
||||
}
|
||||
MirInstruction::Print { value, .. } => {
|
||||
let v = self.reg_load(*value)?; println!("{}", v.to_string());
|
||||
let v = self.reg_load(*value)?;
|
||||
println!("{}", v.to_string());
|
||||
}
|
||||
// No-ops in the interpreter for now
|
||||
MirInstruction::BarrierRead { .. } | MirInstruction::BarrierWrite { .. } | MirInstruction::Barrier { .. } | MirInstruction::Safepoint | MirInstruction::Nop => {}
|
||||
MirInstruction::BarrierRead { .. }
|
||||
| MirInstruction::BarrierWrite { .. }
|
||||
| MirInstruction::Barrier { .. }
|
||||
| MirInstruction::Safepoint
|
||||
| MirInstruction::Nop => {}
|
||||
// Unimplemented but recognized — return clear error for visibility
|
||||
other => return Err(VMError::InvalidInstruction(format!("MIR interp: unimplemented instruction: {:?}", other))),
|
||||
other => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"MIR interp: unimplemented instruction: {:?}",
|
||||
other
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Handle terminator
|
||||
match &block.terminator {
|
||||
Some(MirInstruction::Return { value }) => {
|
||||
if let Some(v) = value { return self.reg_load(*v); } else { return Ok(VMValue::Void); }
|
||||
if let Some(v) = value {
|
||||
return self.reg_load(*v);
|
||||
} else {
|
||||
return Ok(VMValue::Void);
|
||||
}
|
||||
}
|
||||
Some(MirInstruction::Jump { target }) => {
|
||||
last_pred = Some(block.id); cur = *target; continue;
|
||||
last_pred = Some(block.id);
|
||||
cur = *target;
|
||||
continue;
|
||||
}
|
||||
Some(MirInstruction::Branch { condition, then_bb, else_bb }) => {
|
||||
let c = self.reg_load(*condition)?; let t = to_bool_vm(&c).map_err(|e| VMError::TypeError(e))?;
|
||||
last_pred = Some(block.id); cur = if t { *then_bb } else { *else_bb }; continue;
|
||||
Some(MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
}) => {
|
||||
let c = self.reg_load(*condition)?;
|
||||
let t = to_bool_vm(&c).map_err(|e| VMError::TypeError(e))?;
|
||||
last_pred = Some(block.id);
|
||||
cur = if t { *then_bb } else { *else_bb };
|
||||
continue;
|
||||
}
|
||||
None => {
|
||||
return Err(VMError::InvalidBasicBlock(format!(
|
||||
"unterminated block {:?}",
|
||||
block.id
|
||||
)))
|
||||
}
|
||||
Some(other) => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"invalid terminator in MIR interp: {:?}",
|
||||
other
|
||||
)))
|
||||
}
|
||||
None => return Err(VMError::InvalidBasicBlock(format!("unterminated block {:?}", block.id))),
|
||||
Some(other) => return Err(VMError::InvalidInstruction(format!("invalid terminator in MIR interp: {:?}", other))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reg_load(&self, id: ValueId) -> Result<VMValue, VMError> {
|
||||
self.regs.get(&id).cloned().ok_or_else(|| VMError::InvalidValue(format!("use of undefined value {:?}", id)))
|
||||
self.regs
|
||||
.get(&id)
|
||||
.cloned()
|
||||
.ok_or_else(|| VMError::InvalidValue(format!("use of undefined value {:?}", id)))
|
||||
}
|
||||
|
||||
fn eval_binop(&self, op: BinaryOp, a: VMValue, b: VMValue) -> Result<VMValue, VMError> {
|
||||
use BinaryOp::*; use VMValue::*;
|
||||
use BinaryOp::*;
|
||||
use VMValue::*;
|
||||
Ok(match (op, a, b) {
|
||||
(Add, Integer(x), Integer(y)) => Integer(x + y),
|
||||
// String concat: ifいずれかがStringなら文字列連結
|
||||
@ -277,12 +491,18 @@ impl MirInterpreter {
|
||||
(Shl, Integer(x), Integer(y)) => Integer(x.wrapping_shl(y as u32)),
|
||||
(Shr, Integer(x), Integer(y)) => Integer(x.wrapping_shr(y as u32)),
|
||||
// Fallbacks not yet supported
|
||||
(opk, va, vb) => return Err(VMError::TypeError(format!("unsupported binop {:?} on {:?} and {:?}", opk, va, vb))),
|
||||
(opk, va, vb) => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"unsupported binop {:?} on {:?} and {:?}",
|
||||
opk, va, vb
|
||||
)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn eval_cmp(&self, op: CompareOp, a: VMValue, b: VMValue) -> Result<bool, VMError> {
|
||||
use CompareOp::*; use VMValue::*;
|
||||
use CompareOp::*;
|
||||
use VMValue::*;
|
||||
Ok(match (op, &a, &b) {
|
||||
(Eq, _, _) => eq_vm(&a, &b),
|
||||
(Ne, _, _) => !eq_vm(&a, &b),
|
||||
@ -294,7 +514,12 @@ impl MirInterpreter {
|
||||
(Le, Float(x), Float(y)) => x <= y,
|
||||
(Gt, Float(x), Float(y)) => x > y,
|
||||
(Ge, Float(x), Float(y)) => x >= y,
|
||||
(opk, va, vb) => return Err(VMError::TypeError(format!("unsupported compare {:?} on {:?} and {:?}", opk, va, vb))),
|
||||
(opk, va, vb) => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"unsupported compare {:?} on {:?} and {:?}",
|
||||
opk, va, vb
|
||||
)))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,49 +3,54 @@
|
||||
*/
|
||||
|
||||
pub mod vm;
|
||||
pub mod vm_phi;
|
||||
pub mod vm_instructions;
|
||||
pub mod vm_values;
|
||||
pub mod vm_types;
|
||||
pub mod vm_boxcall;
|
||||
pub mod vm_instructions;
|
||||
pub mod vm_phi;
|
||||
pub mod vm_stats;
|
||||
pub mod vm_types;
|
||||
pub mod vm_values;
|
||||
// Phase 9.78h: VM split scaffolding (control_flow/dispatch/frame)
|
||||
pub mod abi_util; // Shared ABI/utility helpers
|
||||
pub mod control_flow;
|
||||
pub mod dispatch;
|
||||
pub mod frame;
|
||||
pub mod gc_helpers;
|
||||
pub mod mir_interpreter;
|
||||
pub mod vm_control_flow;
|
||||
mod vm_gc; // A3: GC roots & diagnostics extracted
|
||||
mod vm_exec; // A3: execution loop extracted
|
||||
mod vm_state; // A3: state & basic helpers extracted
|
||||
mod vm_gc; // A3: GC roots & diagnostics extracted
|
||||
mod vm_methods; // A3-S1: method dispatch wrappers extracted
|
||||
pub mod abi_util; // Shared ABI/utility helpers
|
||||
pub mod mir_interpreter; // Lightweight MIR interpreter
|
||||
mod vm_state; // A3: state & basic helpers extracted // Lightweight MIR interpreter
|
||||
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub mod wasm;
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub mod aot;
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub mod wasm;
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub mod wasm_v2;
|
||||
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub mod llvm_legacy;
|
||||
// Back-compat shim so existing paths crate::backend::llvm::* keep working
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub mod llvm;
|
||||
#[cfg(feature = "cranelift-jit")]
|
||||
pub mod cranelift;
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub mod llvm;
|
||||
|
||||
pub use vm::{VM, VMError, VMValue};
|
||||
pub use mir_interpreter::MirInterpreter;
|
||||
pub use vm::{VMError, VMValue, VM};
|
||||
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub use aot::{AotBackend, AotConfig, AotError, AotStats};
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub use wasm::{WasmBackend, WasmError};
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
pub use aot::{AotBackend, AotError, AotConfig, AotStats};
|
||||
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub use llvm_legacy::{compile_and_execute as llvm_compile_and_execute, compile_to_object as llvm_compile_to_object};
|
||||
#[cfg(feature = "cranelift-jit")]
|
||||
pub use cranelift::{compile_and_execute as cranelift_compile_and_execute, compile_to_object as cranelift_compile_to_object};
|
||||
pub use cranelift::{
|
||||
compile_and_execute as cranelift_compile_and_execute,
|
||||
compile_to_object as cranelift_compile_to_object,
|
||||
};
|
||||
#[cfg(feature = "llvm-inkwell-legacy")]
|
||||
pub use llvm_legacy::{
|
||||
compile_and_execute as llvm_compile_and_execute, compile_to_object as llvm_compile_to_object,
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -7,60 +7,109 @@
|
||||
* Typical Callers: vm_instructions::execute_boxcall / VM::call_unified_method
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, VoidBox};
|
||||
use super::vm::{VM, VMError, VMValue};
|
||||
use super::vm::{VMError, VMValue, VM};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox, VoidBox};
|
||||
|
||||
impl VM {
|
||||
/// Call a method on a Box - simplified version of interpreter method dispatch
|
||||
pub(super) fn call_box_method_impl(&self, box_value: Box<dyn NyashBox>, method: &str, _args: Vec<Box<dyn NyashBox>>) -> Result<Box<dyn NyashBox>, VMError> {
|
||||
pub(super) fn call_box_method_impl(
|
||||
&self,
|
||||
box_value: Box<dyn NyashBox>,
|
||||
method: &str,
|
||||
_args: Vec<Box<dyn NyashBox>>,
|
||||
) -> Result<Box<dyn NyashBox>, VMError> {
|
||||
// PluginBoxV2: delegate to unified plugin host (BID-FFI v1)
|
||||
if let Some(pbox) = box_value.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
if let Some(pbox) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
{
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM][BoxCall→PluginInvoke] {}.{} inst_id={}", pbox.box_type, method, pbox.inner.instance_id);
|
||||
eprintln!(
|
||||
"[VM][BoxCall→PluginInvoke] {}.{} inst_id={}",
|
||||
pbox.box_type, method, pbox.inner.instance_id
|
||||
);
|
||||
}
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
match host.invoke_instance_method(&pbox.box_type, method, pbox.inner.instance_id, &_args) {
|
||||
Ok(Some(val)) => { return Ok(val); }
|
||||
Ok(None) => { return Ok(Box::new(crate::box_trait::VoidBox::new())); }
|
||||
match host.invoke_instance_method(
|
||||
&pbox.box_type,
|
||||
method,
|
||||
pbox.inner.instance_id,
|
||||
&_args,
|
||||
) {
|
||||
Ok(Some(val)) => {
|
||||
return Ok(val);
|
||||
}
|
||||
Ok(None) => {
|
||||
return Ok(Box::new(crate::box_trait::VoidBox::new()));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(VMError::InvalidInstruction(format!("PluginInvoke failed via BoxCall: {}.{} ({})", pbox.box_type, method, e.message())));
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"PluginInvoke failed via BoxCall: {}.{} ({})",
|
||||
pbox.box_type,
|
||||
method,
|
||||
e.message()
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MathBox methods (minimal set used in 10.9)
|
||||
if let Some(math) = box_value.as_any().downcast_ref::<crate::boxes::math_box::MathBox>() {
|
||||
if let Some(math) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::MathBox>()
|
||||
{
|
||||
match method {
|
||||
"min" => {
|
||||
if _args.len() >= 2 { return Ok(math.min(_args[0].clone_or_share(), _args[1].clone_or_share())); }
|
||||
if _args.len() >= 2 {
|
||||
return Ok(math.min(_args[0].clone_or_share(), _args[1].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: min(a, b) requires 2 args")));
|
||||
}
|
||||
"max" => {
|
||||
if _args.len() >= 2 { return Ok(math.max(_args[0].clone_or_share(), _args[1].clone_or_share())); }
|
||||
if _args.len() >= 2 {
|
||||
return Ok(math.max(_args[0].clone_or_share(), _args[1].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: max(a, b) requires 2 args")));
|
||||
}
|
||||
"abs" => {
|
||||
if let Some(v) = _args.get(0) { return Ok(math.abs(v.clone_or_share())); }
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(math.abs(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: abs(x) requires 1 arg")));
|
||||
}
|
||||
"sin" => {
|
||||
if let Some(v) = _args.get(0) { return Ok(math.sin(v.clone_or_share())); }
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(math.sin(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: sin(x) requires 1 arg")));
|
||||
}
|
||||
"cos" => {
|
||||
if let Some(v) = _args.get(0) { return Ok(math.cos(v.clone_or_share())); }
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(math.cos(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: cos(x) requires 1 arg")));
|
||||
}
|
||||
_ => { return Ok(Box::new(VoidBox::new())); }
|
||||
_ => {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// ResultBox (NyashResultBox - new)
|
||||
if let Some(result_box) = box_value.as_any().downcast_ref::<crate::boxes::result::NyashResultBox>() {
|
||||
if let Some(result_box) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::result::NyashResultBox>()
|
||||
{
|
||||
match method {
|
||||
"is_ok" | "isOk" => { return Ok(result_box.is_ok()); }
|
||||
"get_value" | "getValue" => { return Ok(result_box.get_value()); }
|
||||
"get_error" | "getError" => { return Ok(result_box.get_error()); }
|
||||
"is_ok" | "isOk" => {
|
||||
return Ok(result_box.is_ok());
|
||||
}
|
||||
"get_value" | "getValue" => {
|
||||
return Ok(result_box.get_value());
|
||||
}
|
||||
"get_error" | "getError" => {
|
||||
return Ok(result_box.get_error());
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
@ -73,10 +122,17 @@ impl VM {
|
||||
}
|
||||
|
||||
// JitConfigBox methods
|
||||
if let Some(jcb) = box_value.as_any().downcast_ref::<crate::boxes::jit_config_box::JitConfigBox>() {
|
||||
if let Some(jcb) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::jit_config_box::JitConfigBox>()
|
||||
{
|
||||
match method {
|
||||
"get" => {
|
||||
if let Some(k) = _args.get(0) { return Ok(jcb.get_flag(&k.to_string_box().value).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string())))); }
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(jcb
|
||||
.get_flag(&k.to_string_box().value)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("get(name) requires 1 arg")));
|
||||
}
|
||||
"set" => {
|
||||
@ -84,42 +140,73 @@ impl VM {
|
||||
let k = _args[0].to_string_box().value;
|
||||
let v = _args[1].to_string_box().value;
|
||||
let on = matches!(v.as_str(), "1" | "true" | "True" | "on" | "ON");
|
||||
return Ok(jcb.set_flag(&k, on).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
return Ok(jcb
|
||||
.set_flag(&k, on)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("set(name, bool) requires 2 args")));
|
||||
}
|
||||
"getThreshold" => { return Ok(jcb.get_threshold()); }
|
||||
"getThreshold" => {
|
||||
return Ok(jcb.get_threshold());
|
||||
}
|
||||
"setThreshold" => {
|
||||
if let Some(v) = _args.get(0) {
|
||||
let iv = v.to_string_box().value.parse::<i64>().unwrap_or(0);
|
||||
return Ok(jcb.set_threshold(iv).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
return Ok(jcb
|
||||
.set_threshold(iv)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("setThreshold(n) requires 1 arg")));
|
||||
}
|
||||
"apply" => { return Ok(jcb.apply()); }
|
||||
"toJson" => { return Ok(jcb.to_json()); }
|
||||
"apply" => {
|
||||
return Ok(jcb.apply());
|
||||
}
|
||||
"toJson" => {
|
||||
return Ok(jcb.to_json());
|
||||
}
|
||||
"fromJson" => {
|
||||
if let Some(s) = _args.get(0) { return Ok(jcb.from_json(&s.to_string_box().value).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string())))); }
|
||||
if let Some(s) = _args.get(0) {
|
||||
return Ok(jcb
|
||||
.from_json(&s.to_string_box().value)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("fromJson(json) requires 1 arg")));
|
||||
}
|
||||
"enable" => {
|
||||
if let Some(k) = _args.get(0) { return Ok(jcb.set_flag(&k.to_string_box().value, true).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string())))); }
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(jcb
|
||||
.set_flag(&k.to_string_box().value, true)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("enable(name) requires 1 arg")));
|
||||
}
|
||||
"disable" => {
|
||||
if let Some(k) = _args.get(0) { return Ok(jcb.set_flag(&k.to_string_box().value, false).unwrap_or_else(|e| Box::new(StringBox::new(e.to_string())))); }
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(jcb
|
||||
.set_flag(&k.to_string_box().value, false)
|
||||
.unwrap_or_else(|e| Box::new(StringBox::new(e.to_string()))));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("disable(name) requires 1 arg")));
|
||||
}
|
||||
"summary" => { return Ok(jcb.summary()); }
|
||||
_ => { return Ok(Box::new(VoidBox::new())); }
|
||||
"summary" => {
|
||||
return Ok(jcb.summary());
|
||||
}
|
||||
_ => {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JitPolicyBox methods
|
||||
if let Some(jpb) = box_value.as_any().downcast_ref::<crate::boxes::jit_policy_box::JitPolicyBox>() {
|
||||
if let Some(jpb) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::jit_policy_box::JitPolicyBox>()
|
||||
{
|
||||
match method {
|
||||
"get" => {
|
||||
if let Some(k) = _args.get(0) { return Ok(jpb.get_flag(&k.to_string_box().value)); }
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(jpb.get_flag(&k.to_string_box().value));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("get(name) requires 1 arg")));
|
||||
}
|
||||
"set" => {
|
||||
@ -132,37 +219,61 @@ impl VM {
|
||||
return Ok(Box::new(StringBox::new("set(name, bool) requires 2 args")));
|
||||
}
|
||||
"setWhitelistCsv" | "set_whitelist_csv" => {
|
||||
if let Some(s) = _args.get(0) { return Ok(jpb.set_whitelist_csv(&s.to_string_box().value)); }
|
||||
return Ok(Box::new(StringBox::new("setWhitelistCsv(csv) requires 1 arg")));
|
||||
if let Some(s) = _args.get(0) {
|
||||
return Ok(jpb.set_whitelist_csv(&s.to_string_box().value));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"setWhitelistCsv(csv) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"addWhitelist" | "add_whitelist" => {
|
||||
if let Some(s) = _args.get(0) { return Ok(jpb.add_whitelist(&s.to_string_box().value)); }
|
||||
return Ok(Box::new(StringBox::new("addWhitelist(name) requires 1 arg")));
|
||||
if let Some(s) = _args.get(0) {
|
||||
return Ok(jpb.add_whitelist(&s.to_string_box().value));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"addWhitelist(name) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"clearWhitelist" | "clear_whitelist" => {
|
||||
return Ok(jpb.clear_whitelist());
|
||||
}
|
||||
"clearWhitelist" | "clear_whitelist" => { return Ok(jpb.clear_whitelist()); }
|
||||
"enablePreset" | "enable_preset" => {
|
||||
if let Some(s) = _args.get(0) { return Ok(jpb.enable_preset(&s.to_string_box().value)); }
|
||||
return Ok(Box::new(StringBox::new("enablePreset(name) requires 1 arg")));
|
||||
if let Some(s) = _args.get(0) {
|
||||
return Ok(jpb.enable_preset(&s.to_string_box().value));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"enablePreset(name) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
_ => {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
_ => { return Ok(Box::new(VoidBox::new())); }
|
||||
}
|
||||
}
|
||||
|
||||
// JitStatsBox methods
|
||||
if let Some(jsb) = box_value.as_any().downcast_ref::<crate::boxes::jit_stats_box::JitStatsBox>() {
|
||||
if let Some(jsb) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::jit_stats_box::JitStatsBox>()
|
||||
{
|
||||
match method {
|
||||
"toJson" => { return Ok(jsb.to_json()); }
|
||||
"toJson" => {
|
||||
return Ok(jsb.to_json());
|
||||
}
|
||||
"top5" => {
|
||||
if let Some(jm) = &self.jit_manager {
|
||||
let v = jm.top_hits(5);
|
||||
let arr: Vec<serde_json::Value> = v.into_iter().map(|(name, hits, compiled, handle)| {
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"hits": hits,
|
||||
"compiled": compiled,
|
||||
"handle": handle
|
||||
let arr: Vec<serde_json::Value> = v
|
||||
.into_iter()
|
||||
.map(|(name, hits, compiled, handle)| {
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"hits": hits,
|
||||
"compiled": compiled,
|
||||
"handle": handle
|
||||
})
|
||||
})
|
||||
}).collect();
|
||||
.collect();
|
||||
let s = serde_json::to_string(&arr).unwrap_or_else(|_| "[]".to_string());
|
||||
return Ok(Box::new(StringBox::new(s)));
|
||||
}
|
||||
@ -171,7 +282,11 @@ impl VM {
|
||||
"summary" => {
|
||||
let cfg = crate::jit::config::current();
|
||||
let caps = crate::jit::config::probe_capabilities();
|
||||
let abi_mode = if cfg.native_bool_abi && caps.supports_b1_sig { "b1_bool" } else { "i64_bool" };
|
||||
let abi_mode = if cfg.native_bool_abi && caps.supports_b1_sig {
|
||||
"b1_bool"
|
||||
} else {
|
||||
"i64_bool"
|
||||
};
|
||||
let b1_norm = crate::jit::rt::b1_norm_get();
|
||||
let ret_b1 = crate::jit::rt::ret_bool_hint_get();
|
||||
let mut payload = serde_json::json!({
|
||||
@ -192,41 +307,66 @@ impl VM {
|
||||
let per_arr: Vec<serde_json::Value> = perf.into_iter().map(|(name, phi_t, phi_b1, rb, hits, compiled, handle)| serde_json::json!({
|
||||
"name": name, "phi_total": phi_t, "phi_b1": phi_b1, "ret_bool_hint": rb, "hits": hits, "compiled": compiled, "handle": handle
|
||||
})).collect();
|
||||
if let Some(obj) = payload.as_object_mut() { obj.insert("top5".to_string(), serde_json::Value::Array(top5)); obj.insert("perFunction".to_string(), serde_json::Value::Array(per_arr)); }
|
||||
if let Some(obj) = payload.as_object_mut() {
|
||||
obj.insert("top5".to_string(), serde_json::Value::Array(top5));
|
||||
obj.insert(
|
||||
"perFunction".to_string(),
|
||||
serde_json::Value::Array(per_arr),
|
||||
);
|
||||
}
|
||||
}
|
||||
let s = serde_json::to_string_pretty(&payload).unwrap_or_else(|_| "{}".to_string());
|
||||
let s =
|
||||
serde_json::to_string_pretty(&payload).unwrap_or_else(|_| "{}".to_string());
|
||||
return Ok(Box::new(StringBox::new(s)));
|
||||
}
|
||||
"perFunction" | "per_function" => {
|
||||
if let Some(jm) = &self.jit_manager {
|
||||
let v = jm.per_function_stats();
|
||||
let arr: Vec<serde_json::Value> = v.into_iter().map(|(name, phi_t, phi_b1, rb, hits, compiled, handle)| serde_json::json!({
|
||||
"name": name,
|
||||
"phi_total": phi_t,
|
||||
"phi_b1": phi_b1,
|
||||
"ret_bool_hint": rb,
|
||||
"hits": hits,
|
||||
"compiled": compiled,
|
||||
"handle": handle,
|
||||
})).collect();
|
||||
let s = serde_json::to_string_pretty(&arr).unwrap_or_else(|_| "[]".to_string());
|
||||
let arr: Vec<serde_json::Value> = v
|
||||
.into_iter()
|
||||
.map(|(name, phi_t, phi_b1, rb, hits, compiled, handle)| {
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"phi_total": phi_t,
|
||||
"phi_b1": phi_b1,
|
||||
"ret_bool_hint": rb,
|
||||
"hits": hits,
|
||||
"compiled": compiled,
|
||||
"handle": handle,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let s =
|
||||
serde_json::to_string_pretty(&arr).unwrap_or_else(|_| "[]".to_string());
|
||||
return Ok(Box::new(StringBox::new(s)));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("[]")));
|
||||
}
|
||||
_ => { return Ok(Box::new(VoidBox::new())); }
|
||||
_ => {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// StringBox methods
|
||||
if let Some(string_box) = box_value.as_any().downcast_ref::<StringBox>() {
|
||||
match method {
|
||||
"length" | "len" => { return Ok(Box::new(IntegerBox::new(string_box.value.len() as i64))); }
|
||||
"toString" => { return Ok(Box::new(StringBox::new(string_box.value.clone()))); }
|
||||
"length" | "len" => {
|
||||
return Ok(Box::new(IntegerBox::new(string_box.value.len() as i64)));
|
||||
}
|
||||
"toString" => {
|
||||
return Ok(Box::new(StringBox::new(string_box.value.clone())));
|
||||
}
|
||||
"substring" => {
|
||||
if _args.len() >= 2 {
|
||||
let s = match _args[0].to_string_box().value.parse::<i64>() { Ok(v) => v.max(0) as usize, Err(_) => 0 };
|
||||
let e = match _args[1].to_string_box().value.parse::<i64>() { Ok(v) => v.max(0) as usize, Err(_) => string_box.value.chars().count() };
|
||||
let s = match _args[0].to_string_box().value.parse::<i64>() {
|
||||
Ok(v) => v.max(0) as usize,
|
||||
Err(_) => 0,
|
||||
};
|
||||
let e = match _args[1].to_string_box().value.parse::<i64>() {
|
||||
Ok(v) => v.max(0) as usize,
|
||||
Err(_) => string_box.value.chars().count(),
|
||||
};
|
||||
return Ok(string_box.substring(s, e));
|
||||
}
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
@ -243,115 +383,345 @@ impl VM {
|
||||
}
|
||||
|
||||
// ArrayBox methods (minimal set)
|
||||
if let Some(array_box) = box_value.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(array_box) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
match method {
|
||||
"push" => { if let Some(v) = _args.get(0) { return Ok(array_box.push(v.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: push(value) requires 1 arg"))); }
|
||||
"pop" => { return Ok(array_box.pop()); },
|
||||
"length" | "len" => { return Ok(array_box.length()); },
|
||||
"get" => { if let Some(i) = _args.get(0) { return Ok(array_box.get(i.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: get(index) requires 1 arg"))); }
|
||||
"set" => { if _args.len() >= 2 { return Ok(array_box.set(_args[0].clone_or_share(), _args[1].clone_or_share())); } return Ok(Box::new(StringBox::new("Error: set(index, value) requires 2 args"))); }
|
||||
"remove" => { if let Some(i) = _args.get(0) { return Ok(array_box.remove(i.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: remove(index) requires 1 arg"))); }
|
||||
"contains" => { if let Some(v) = _args.get(0) { return Ok(array_box.contains(v.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: contains(value) requires 1 arg"))); }
|
||||
"indexOf" => { if let Some(v) = _args.get(0) { return Ok(array_box.indexOf(v.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: indexOf(value) requires 1 arg"))); }
|
||||
"clear" => { return Ok(array_box.clear()); },
|
||||
"join" => { if let Some(sep) = _args.get(0) { return Ok(array_box.join(sep.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: join(sep) requires 1 arg"))); }
|
||||
"sort" => { return Ok(array_box.sort()); },
|
||||
"reverse" => { return Ok(array_box.reverse()); },
|
||||
"slice" => { if _args.len() >= 2 { return Ok(array_box.slice(_args[0].clone_or_share(), _args[1].clone_or_share())); } return Ok(Box::new(StringBox::new("Error: slice(start, end) requires 2 args"))); }
|
||||
"push" => {
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(array_box.push(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: push(value) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"pop" => {
|
||||
return Ok(array_box.pop());
|
||||
}
|
||||
"length" | "len" => {
|
||||
return Ok(array_box.length());
|
||||
}
|
||||
"get" => {
|
||||
if let Some(i) = _args.get(0) {
|
||||
return Ok(array_box.get(i.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: get(index) requires 1 arg")));
|
||||
}
|
||||
"set" => {
|
||||
if _args.len() >= 2 {
|
||||
return Ok(
|
||||
array_box.set(_args[0].clone_or_share(), _args[1].clone_or_share())
|
||||
);
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: set(index, value) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"remove" => {
|
||||
if let Some(i) = _args.get(0) {
|
||||
return Ok(array_box.remove(i.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: remove(index) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"contains" => {
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(array_box.contains(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: contains(value) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"indexOf" => {
|
||||
if let Some(v) = _args.get(0) {
|
||||
return Ok(array_box.indexOf(v.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: indexOf(value) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"clear" => {
|
||||
return Ok(array_box.clear());
|
||||
}
|
||||
"join" => {
|
||||
if let Some(sep) = _args.get(0) {
|
||||
return Ok(array_box.join(sep.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: join(sep) requires 1 arg")));
|
||||
}
|
||||
"sort" => {
|
||||
return Ok(array_box.sort());
|
||||
}
|
||||
"reverse" => {
|
||||
return Ok(array_box.reverse());
|
||||
}
|
||||
"slice" => {
|
||||
if _args.len() >= 2 {
|
||||
return Ok(
|
||||
array_box.slice(_args[0].clone_or_share(), _args[1].clone_or_share())
|
||||
);
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: slice(start, end) requires 2 args",
|
||||
)));
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// MapBox methods (minimal set)
|
||||
if let Some(map_box) = box_value.as_any().downcast_ref::<crate::boxes::map_box::MapBox>() {
|
||||
if let Some(map_box) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::map_box::MapBox>()
|
||||
{
|
||||
match method {
|
||||
"set" => { if _args.len() >= 2 { return Ok(map_box.set(_args[0].clone_or_share(), _args[1].clone_or_share())); } return Ok(Box::new(StringBox::new("Error: set(key, value) requires 2 args"))); }
|
||||
"get" => { if let Some(k) = _args.get(0) { return Ok(map_box.get(k.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: get(key) requires 1 arg"))); }
|
||||
"has" => { if let Some(k) = _args.get(0) { return Ok(map_box.has(k.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: has(key) requires 1 arg"))); }
|
||||
"delete" | "remove" => { if let Some(k) = _args.get(0) { return Ok(map_box.delete(k.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: delete(key) requires 1 arg"))); }
|
||||
"keys" => { return Ok(map_box.keys()); },
|
||||
"values" => { return Ok(map_box.values()); },
|
||||
"size" => { return Ok(map_box.size()); },
|
||||
"clear" => { return Ok(map_box.clear()); },
|
||||
"set" => {
|
||||
if _args.len() >= 2 {
|
||||
return Ok(
|
||||
map_box.set(_args[0].clone_or_share(), _args[1].clone_or_share())
|
||||
);
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: set(key, value) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"get" => {
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(map_box.get(k.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: get(key) requires 1 arg")));
|
||||
}
|
||||
"has" => {
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(map_box.has(k.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("Error: has(key) requires 1 arg")));
|
||||
}
|
||||
"delete" | "remove" => {
|
||||
if let Some(k) = _args.get(0) {
|
||||
return Ok(map_box.delete(k.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: delete(key) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"keys" => {
|
||||
return Ok(map_box.keys());
|
||||
}
|
||||
"values" => {
|
||||
return Ok(map_box.values());
|
||||
}
|
||||
"size" => {
|
||||
return Ok(map_box.size());
|
||||
}
|
||||
"clear" => {
|
||||
return Ok(map_box.clear());
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// TaskGroupBox methods (scaffold → instance内の所有Futureに対して実行)
|
||||
if box_value.as_any().downcast_ref::<crate::boxes::task_group_box::TaskGroupBox>().is_some() {
|
||||
if box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::task_group_box::TaskGroupBox>()
|
||||
.is_some()
|
||||
{
|
||||
let mut owned = box_value;
|
||||
if let Some(tg) = (&mut *owned).as_any_mut().downcast_mut::<crate::boxes::task_group_box::TaskGroupBox>() {
|
||||
if let Some(tg) = (&mut *owned)
|
||||
.as_any_mut()
|
||||
.downcast_mut::<crate::boxes::task_group_box::TaskGroupBox>()
|
||||
{
|
||||
match method {
|
||||
"cancelAll" | "cancel_all" => { return Ok(tg.cancelAll()); }
|
||||
"cancelAll" | "cancel_all" => {
|
||||
return Ok(tg.cancelAll());
|
||||
}
|
||||
"joinAll" | "join_all" => {
|
||||
let ms = _args.get(0).map(|a| a.to_string_box().value.parse::<i64>().unwrap_or(2000));
|
||||
let ms = _args
|
||||
.get(0)
|
||||
.map(|a| a.to_string_box().value.parse::<i64>().unwrap_or(2000));
|
||||
return Ok(tg.joinAll(ms));
|
||||
}
|
||||
_ => { return Ok(Box::new(VoidBox::new())); }
|
||||
_ => {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
}
|
||||
|
||||
// P2PBox methods (minimal)
|
||||
if let Some(p2p) = box_value.as_any().downcast_ref::<crate::boxes::p2p_box::P2PBox>() {
|
||||
if let Some(p2p) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::p2p_box::P2PBox>()
|
||||
{
|
||||
match method {
|
||||
"send" => {
|
||||
if _args.len() >= 2 { return Ok(p2p.send(_args[0].clone_or_share(), _args[1].clone_or_share())); }
|
||||
return Ok(Box::new(StringBox::new("Error: send(to, intent) requires 2 args")));
|
||||
if _args.len() >= 2 {
|
||||
return Ok(p2p.send(_args[0].clone_or_share(), _args[1].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: send(to, intent) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"on" => {
|
||||
if _args.len() >= 2 { return Ok(p2p.on(_args[0].clone_or_share(), _args[1].clone_or_share())); }
|
||||
return Ok(Box::new(StringBox::new("Error: on(intent, handler) requires 2 args")));
|
||||
if _args.len() >= 2 {
|
||||
return Ok(p2p.on(_args[0].clone_or_share(), _args[1].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: on(intent, handler) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"onOnce" | "on_once" => {
|
||||
if _args.len() >= 2 { return Ok(p2p.on_once(_args[0].clone_or_share(), _args[1].clone_or_share())); }
|
||||
return Ok(Box::new(StringBox::new("Error: onOnce(intent, handler) requires 2 args")));
|
||||
if _args.len() >= 2 {
|
||||
return Ok(
|
||||
p2p.on_once(_args[0].clone_or_share(), _args[1].clone_or_share())
|
||||
);
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: onOnce(intent, handler) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"off" => {
|
||||
if _args.len() >= 1 { return Ok(p2p.off(_args[0].clone_or_share())); }
|
||||
return Ok(Box::new(StringBox::new("Error: off(intent) requires 1 arg")));
|
||||
if _args.len() >= 1 {
|
||||
return Ok(p2p.off(_args[0].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: off(intent) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"getLastFrom" => {
|
||||
return Ok(p2p.get_last_from());
|
||||
}
|
||||
"getLastIntentName" => {
|
||||
return Ok(p2p.get_last_intent_name());
|
||||
}
|
||||
"debug_nodes" | "debugNodes" => {
|
||||
return Ok(p2p.debug_nodes());
|
||||
}
|
||||
"getNodeId" | "getId" => {
|
||||
return Ok(p2p.get_node_id());
|
||||
}
|
||||
"getTransportType" | "transport" => {
|
||||
return Ok(p2p.get_transport_type());
|
||||
}
|
||||
"isReachable" => {
|
||||
if let Some(n) = _args.get(0) {
|
||||
return Ok(p2p.is_reachable(n.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(BoolBox::new(false)));
|
||||
}
|
||||
"getLastFrom" => { return Ok(p2p.get_last_from()); }
|
||||
"getLastIntentName" => { return Ok(p2p.get_last_intent_name()); }
|
||||
"debug_nodes" | "debugNodes" => { return Ok(p2p.debug_nodes()); }
|
||||
"getNodeId" | "getId" => { return Ok(p2p.get_node_id()); }
|
||||
"getTransportType" | "transport" => { return Ok(p2p.get_transport_type()); }
|
||||
"isReachable" => { if let Some(n) = _args.get(0) { return Ok(p2p.is_reachable(n.clone_or_share())); } return Ok(Box::new(BoolBox::new(false))); }
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// SocketBox methods (minimal + timeouts)
|
||||
if let Some(sock) = box_value.as_any().downcast_ref::<crate::boxes::socket_box::SocketBox>() {
|
||||
if let Some(sock) = box_value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::socket_box::SocketBox>()
|
||||
{
|
||||
match method {
|
||||
"bind" => { if _args.len() >= 2 { return Ok(sock.bind(_args[0].clone_or_share(), _args[1].clone_or_share())); } return Ok(Box::new(StringBox::new("Error: bind(address, port) requires 2 args"))); }
|
||||
"listen" => { if let Some(b) = _args.get(0) { return Ok(sock.listen(b.clone_or_share())); } return Ok(Box::new(StringBox::new("Error: listen(backlog) requires 1 arg"))); }
|
||||
"accept" => { return Ok(sock.accept()); },
|
||||
"acceptTimeout" | "accept_timeout" => { if let Some(ms) = _args.get(0) { return Ok(sock.accept_timeout(ms.clone_or_share())); } return Ok(Box::new(crate::box_trait::VoidBox::new())); }
|
||||
"connect" => { if _args.len() >= 2 { return Ok(sock.connect(_args[0].clone_or_share(), _args[1].clone_or_share())); } return Ok(Box::new(StringBox::new("Error: connect(address, port) requires 2 args"))); }
|
||||
"read" => { return Ok(sock.read()); },
|
||||
"recvTimeout" | "recv_timeout" => { if let Some(ms) = _args.get(0) { return Ok(sock.recv_timeout(ms.clone_or_share())); } return Ok(Box::new(StringBox::new(""))); }
|
||||
"write" => { if let Some(d) = _args.get(0) { return Ok(sock.write(d.clone_or_share())); } return Ok(Box::new(crate::box_trait::BoolBox::new(false))); }
|
||||
"close" => { return Ok(sock.close()); },
|
||||
"isServer" | "is_server" => { return Ok(sock.is_server()); },
|
||||
"isConnected" | "is_connected" => { return Ok(sock.is_connected()); },
|
||||
"bind" => {
|
||||
if _args.len() >= 2 {
|
||||
return Ok(sock.bind(_args[0].clone_or_share(), _args[1].clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: bind(address, port) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"listen" => {
|
||||
if let Some(b) = _args.get(0) {
|
||||
return Ok(sock.listen(b.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: listen(backlog) requires 1 arg",
|
||||
)));
|
||||
}
|
||||
"accept" => {
|
||||
return Ok(sock.accept());
|
||||
}
|
||||
"acceptTimeout" | "accept_timeout" => {
|
||||
if let Some(ms) = _args.get(0) {
|
||||
return Ok(sock.accept_timeout(ms.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(crate::box_trait::VoidBox::new()));
|
||||
}
|
||||
"connect" => {
|
||||
if _args.len() >= 2 {
|
||||
return Ok(
|
||||
sock.connect(_args[0].clone_or_share(), _args[1].clone_or_share())
|
||||
);
|
||||
}
|
||||
return Ok(Box::new(StringBox::new(
|
||||
"Error: connect(address, port) requires 2 args",
|
||||
)));
|
||||
}
|
||||
"read" => {
|
||||
return Ok(sock.read());
|
||||
}
|
||||
"recvTimeout" | "recv_timeout" => {
|
||||
if let Some(ms) = _args.get(0) {
|
||||
return Ok(sock.recv_timeout(ms.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(StringBox::new("")));
|
||||
}
|
||||
"write" => {
|
||||
if let Some(d) = _args.get(0) {
|
||||
return Ok(sock.write(d.clone_or_share()));
|
||||
}
|
||||
return Ok(Box::new(crate::box_trait::BoolBox::new(false)));
|
||||
}
|
||||
"close" => {
|
||||
return Ok(sock.close());
|
||||
}
|
||||
"isServer" | "is_server" => {
|
||||
return Ok(sock.is_server());
|
||||
}
|
||||
"isConnected" | "is_connected" => {
|
||||
return Ok(sock.is_connected());
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// IntegerBox methods
|
||||
if let Some(integer_box) = box_value.as_any().downcast_ref::<IntegerBox>() { match method { "toString" => { return Ok(Box::new(StringBox::new(integer_box.value.to_string()))); }, "abs" => { return Ok(Box::new(IntegerBox::new(integer_box.value.abs()))); }, _ => return Ok(Box::new(VoidBox::new())), } }
|
||||
if let Some(integer_box) = box_value.as_any().downcast_ref::<IntegerBox>() {
|
||||
match method {
|
||||
"toString" => {
|
||||
return Ok(Box::new(StringBox::new(integer_box.value.to_string())));
|
||||
}
|
||||
"abs" => {
|
||||
return Ok(Box::new(IntegerBox::new(integer_box.value.abs())));
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// BoolBox methods
|
||||
if let Some(bool_box) = box_value.as_any().downcast_ref::<BoolBox>() { match method { "toString" => { return Ok(Box::new(StringBox::new(bool_box.value.to_string()))); }, _ => return Ok(Box::new(VoidBox::new())), } }
|
||||
if let Some(bool_box) = box_value.as_any().downcast_ref::<BoolBox>() {
|
||||
match method {
|
||||
"toString" => {
|
||||
return Ok(Box::new(StringBox::new(bool_box.value.to_string())));
|
||||
}
|
||||
_ => return Ok(Box::new(VoidBox::new())),
|
||||
}
|
||||
}
|
||||
|
||||
// Default: return void for any unrecognized box type or method
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
|
||||
/// Debug helper for BoxCall tracing (enabled via NYASH_VM_DEBUG_BOXCALL=1)
|
||||
pub(super) fn debug_log_boxcall(&self, recv: &VMValue, method: &str, args: &[Box<dyn NyashBox>], stage: &str, result: Option<&VMValue>) {
|
||||
pub(super) fn debug_log_boxcall(
|
||||
&self,
|
||||
recv: &VMValue,
|
||||
method: &str,
|
||||
args: &[Box<dyn NyashBox>],
|
||||
stage: &str,
|
||||
result: Option<&VMValue>,
|
||||
) {
|
||||
if std::env::var("NYASH_VM_DEBUG_BOXCALL").ok().as_deref() == Some("1") {
|
||||
let recv_ty = match recv {
|
||||
VMValue::BoxRef(arc) => arc.type_name().to_string(),
|
||||
@ -373,9 +743,24 @@ impl VM {
|
||||
VMValue::Future(_) => "Future".to_string(),
|
||||
VMValue::Void => "Void".to_string(),
|
||||
};
|
||||
eprintln!("[VM-BOXCALL][{}] recv_ty={} method={} argc={} args={:?} => result_ty={}", stage, recv_ty, method, args.len(), args_desc, res_ty);
|
||||
eprintln!(
|
||||
"[VM-BOXCALL][{}] recv_ty={} method={} argc={} args={:?} => result_ty={}",
|
||||
stage,
|
||||
recv_ty,
|
||||
method,
|
||||
args.len(),
|
||||
args_desc,
|
||||
res_ty
|
||||
);
|
||||
} else {
|
||||
eprintln!("[VM-BOXCALL][{}] recv_ty={} method={} argc={} args={:?}", stage, recv_ty, method, args.len(), args_desc);
|
||||
eprintln!(
|
||||
"[VM-BOXCALL][{}] recv_ty={} method={} argc={} args={:?}",
|
||||
stage,
|
||||
recv_ty,
|
||||
method,
|
||||
args.len(),
|
||||
args_desc
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::mir::BasicBlockId;
|
||||
use crate::backend::vm::VMValue;
|
||||
use crate::mir::BasicBlockId;
|
||||
|
||||
/// Control flow result from instruction execution
|
||||
pub(crate) enum ControlFlow {
|
||||
|
||||
@ -11,10 +11,10 @@
|
||||
* Behavior and public APIs are preserved. This is a pure move/refactor.
|
||||
*/
|
||||
|
||||
use crate::mir::{MirModule, MirFunction, MirInstruction, BasicBlockId};
|
||||
use crate::box_trait::NyashBox;
|
||||
use super::{vm::VM, vm::VMError, vm::VMValue};
|
||||
use super::{vm::VMError, vm::VMValue, vm::VM};
|
||||
use crate::backend::vm_control_flow::ControlFlow;
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::mir::{BasicBlockId, MirFunction, MirInstruction, MirModule};
|
||||
|
||||
impl VM {
|
||||
/// Execute a MIR module
|
||||
@ -34,12 +34,17 @@ impl VM {
|
||||
if crate::config::env::vm_pic_stats() {
|
||||
self.print_cache_stats_summary();
|
||||
}
|
||||
if let Some(jm) = &self.jit_manager { jm.print_summary(); }
|
||||
if let Some(jm) = &self.jit_manager {
|
||||
jm.print_summary();
|
||||
}
|
||||
{
|
||||
let lvl = crate::config::env::gc_trace_level();
|
||||
if lvl > 0 {
|
||||
if let Some((sp, rd, wr)) = self.runtime.gc.snapshot_counters() {
|
||||
eprintln!("[GC] counters: safepoints={} read_barriers={} write_barriers={}", sp, rd, wr);
|
||||
eprintln!(
|
||||
"[GC] counters: safepoints={} read_barriers={} write_barriers={}",
|
||||
sp, rd, wr
|
||||
);
|
||||
}
|
||||
let roots_total = self.scope_tracker.root_count_total();
|
||||
let root_regions = self.scope_tracker.root_regions();
|
||||
@ -48,8 +53,12 @@ impl VM {
|
||||
"[GC] mock_mark: roots_total={} regions={} object_field_slots={}",
|
||||
roots_total, root_regions, field_slots
|
||||
);
|
||||
if lvl >= 2 { self.gc_print_roots_breakdown(); }
|
||||
if lvl >= 3 { self.gc_print_reachability_depth2(); }
|
||||
if lvl >= 2 {
|
||||
self.gc_print_roots_breakdown();
|
||||
}
|
||||
if lvl >= 3 {
|
||||
self.gc_print_reachability_depth2();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(result.to_nyash_box())
|
||||
@ -97,9 +106,9 @@ impl VM {
|
||||
.module
|
||||
.as_ref()
|
||||
.ok_or_else(|| VMError::InvalidInstruction("No active module".to_string()))?;
|
||||
let function_ref = module_ref
|
||||
.get_function(func_name)
|
||||
.ok_or_else(|| VMError::InvalidInstruction(format!("Function '{}' not found", func_name)))?;
|
||||
let function_ref = module_ref.get_function(func_name).ok_or_else(|| {
|
||||
VMError::InvalidInstruction(format!("Function '{}' not found", func_name))
|
||||
})?;
|
||||
let function = function_ref.clone();
|
||||
|
||||
let saved_values = std::mem::take(&mut self.values);
|
||||
@ -143,7 +152,11 @@ impl VM {
|
||||
self.current_function = Some(function.signature.name.clone());
|
||||
if let Some(jm) = &mut self.jit_manager {
|
||||
if let Ok(s) = std::env::var("NYASH_JIT_THRESHOLD") {
|
||||
if let Ok(t) = s.parse::<u32>() { if t > 0 { jm.set_threshold(t); } }
|
||||
if let Ok(t) = s.parse::<u32>() {
|
||||
if t > 0 {
|
||||
jm.set_threshold(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
jm.record_entry(&function.signature.name);
|
||||
let _ = jm.maybe_compile(&function.signature.name, function);
|
||||
@ -197,7 +210,10 @@ impl VM {
|
||||
} else if std::env::var("NYASH_JIT_STATS").ok().as_deref() == Some("1")
|
||||
|| std::env::var("NYASH_JIT_TRAP_LOG").ok().as_deref() == Some("1")
|
||||
{
|
||||
eprintln!("[JIT] fallback: VM path taken for {}", function.signature.name);
|
||||
eprintln!(
|
||||
"[JIT] fallback: VM path taken for {}",
|
||||
function.signature.name
|
||||
);
|
||||
if jit_only {
|
||||
self.leave_root_region();
|
||||
self.scope_tracker.pop_scope();
|
||||
@ -285,9 +301,13 @@ impl VM {
|
||||
if should_return.is_none() && next_block.is_none() {
|
||||
if let Some(term) = &block.terminator {
|
||||
match self.execute_instruction(term)? {
|
||||
ControlFlow::Continue => {},
|
||||
ControlFlow::Jump(target) => { next_block = Some(target); },
|
||||
ControlFlow::Return(value) => { should_return = Some(value); },
|
||||
ControlFlow::Continue => {}
|
||||
ControlFlow::Jump(target) => {
|
||||
next_block = Some(target);
|
||||
}
|
||||
ControlFlow::Return(value) => {
|
||||
should_return = Some(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -320,10 +340,16 @@ impl VM {
|
||||
}
|
||||
|
||||
/// Execute a single instruction
|
||||
pub(super) fn execute_instruction(&mut self, instruction: &MirInstruction) -> Result<ControlFlow, VMError> {
|
||||
pub(super) fn execute_instruction(
|
||||
&mut self,
|
||||
instruction: &MirInstruction,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let debug_global = std::env::var("NYASH_VM_DEBUG").ok().as_deref() == Some("1");
|
||||
let debug_exec = debug_global || std::env::var("NYASH_VM_DEBUG_EXEC").ok().as_deref() == Some("1");
|
||||
if debug_exec { eprintln!("[VM] execute_instruction: {:?}", instruction); }
|
||||
let debug_exec =
|
||||
debug_global || std::env::var("NYASH_VM_DEBUG_EXEC").ok().as_deref() == Some("1");
|
||||
if debug_exec {
|
||||
eprintln!("[VM] execute_instruction: {:?}", instruction);
|
||||
}
|
||||
self.record_instruction(instruction);
|
||||
super::dispatch::execute_instruction(self, instruction, debug_global)
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
* - Debug prints for roots snapshot and shallow reachability
|
||||
*/
|
||||
|
||||
use super::vm::{VM, VMValue};
|
||||
use super::vm::{VMValue, VM};
|
||||
|
||||
impl VM {
|
||||
/// Enter a GC root region and return a guard that leaves on drop
|
||||
@ -22,11 +22,17 @@ impl VM {
|
||||
}
|
||||
|
||||
/// Leave current GC root region
|
||||
pub(super) fn leave_root_region(&mut self) { self.scope_tracker.leave_root_region(); }
|
||||
pub(super) fn leave_root_region(&mut self) {
|
||||
self.scope_tracker.leave_root_region();
|
||||
}
|
||||
|
||||
/// Site info for GC logs: (func, bb, pc)
|
||||
pub(super) fn gc_site_info(&self) -> (String, i64, i64) {
|
||||
let func = self.current_function.as_deref().unwrap_or("<none>").to_string();
|
||||
let func = self
|
||||
.current_function
|
||||
.as_deref()
|
||||
.unwrap_or("<none>")
|
||||
.to_string();
|
||||
let bb = self.frame.current_block.map(|b| b.0 as i64).unwrap_or(-1);
|
||||
let pc = self.frame.pc as i64;
|
||||
(func, bb, pc)
|
||||
@ -78,7 +84,10 @@ impl VM {
|
||||
}
|
||||
if let Some(map) = b.as_any().downcast_ref::<crate::boxes::map_box::MapBox>() {
|
||||
let vals = map.values();
|
||||
if let Some(arr2) = vals.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(arr2) = vals
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
if let Ok(items) = arr2.items.read() {
|
||||
for item in items.iter() {
|
||||
let tn = item.type_name().to_string();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,41 +1,66 @@
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::mir::ValueId;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
|
||||
impl VM {
|
||||
/// Execute Call instruction (supports function name or FunctionBox value)
|
||||
pub(crate) fn execute_call(&mut self, dst: Option<ValueId>, func: ValueId, args: &[ValueId]) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
func: ValueId,
|
||||
args: &[ValueId],
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
// Evaluate function value
|
||||
let fval = self.get_value(func)?;
|
||||
match fval {
|
||||
VMValue::String(func_name) => {
|
||||
// Legacy: call function by name
|
||||
let arg_values: Vec<VMValue> = args.iter().map(|arg| self.get_value(*arg)).collect::<Result<Vec<_>, _>>()?;
|
||||
let arg_values: Vec<VMValue> = args
|
||||
.iter()
|
||||
.map(|arg| self.get_value(*arg))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let result = self.call_function_by_name(&func_name, arg_values)?;
|
||||
if let Some(dst_id) = dst { self.set_value(dst_id, result); }
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, result);
|
||||
}
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
VMValue::BoxRef(arc_box) => {
|
||||
// FunctionBox call path
|
||||
if let Some(fun) = arc_box.as_any().downcast_ref::<crate::boxes::function_box::FunctionBox>() {
|
||||
if let Some(fun) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::function_box::FunctionBox>()
|
||||
{
|
||||
// Convert args to NyashBox for interpreter helper
|
||||
let nyash_args: Vec<Box<dyn NyashBox>> = args.iter()
|
||||
let nyash_args: Vec<Box<dyn NyashBox>> = args
|
||||
.iter()
|
||||
.map(|a| self.get_value(*a).map(|v| v.to_nyash_box()))
|
||||
.collect::<Result<Vec<_>, VMError>>()?;
|
||||
// Execute via interpreter helper
|
||||
match crate::interpreter::run_function_box(fun, nyash_args) {
|
||||
Ok(out) => {
|
||||
if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::from_nyash_box(out)); }
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::from_nyash_box(out));
|
||||
}
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
Err(e) => Err(VMError::InvalidInstruction(format!("FunctionBox call failed: {:?}", e)))
|
||||
Err(e) => Err(VMError::InvalidInstruction(format!(
|
||||
"FunctionBox call failed: {:?}",
|
||||
e
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
Err(VMError::TypeError(format!("Call target not callable: {}", arc_box.type_name())))
|
||||
Err(VMError::TypeError(format!(
|
||||
"Call target not callable: {}",
|
||||
arc_box.type_name()
|
||||
)))
|
||||
}
|
||||
}
|
||||
other => Err(VMError::TypeError(format!("Call target must be function name or FunctionBox, got {:?}", other))),
|
||||
other => Err(VMError::TypeError(format!(
|
||||
"Call target must be function name or FunctionBox, got {:?}",
|
||||
other
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,27 @@
|
||||
use crate::mir::{ConstValue, BinaryOp, CompareOp, UnaryOp, ValueId, BasicBlockId, TypeOpKind, MirType};
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::box_trait::{BoolBox, VoidBox};
|
||||
use crate::boxes::ArrayBox;
|
||||
use crate::mir::{
|
||||
BasicBlockId, BinaryOp, CompareOp, ConstValue, MirType, TypeOpKind, UnaryOp, ValueId,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
|
||||
impl VM {
|
||||
// ---- Helpers (PIC/VTable bookkeeping) ----
|
||||
pub(super) fn build_pic_key(&self, recv: &VMValue, method: &str, method_id: Option<u16>) -> String {
|
||||
pub(super) fn build_pic_key(
|
||||
&self,
|
||||
recv: &VMValue,
|
||||
method: &str,
|
||||
method_id: Option<u16>,
|
||||
) -> String {
|
||||
let label = self.cache_label_for_recv(recv);
|
||||
let ver = self.cache_version_for_label(&label);
|
||||
if let Some(mid) = method_id { format!("v{}:{}#{}", ver, label, mid) } else { format!("v{}:{}#{}", ver, label, method) }
|
||||
if let Some(mid) = method_id {
|
||||
format!("v{}:{}#{}", ver, label, mid)
|
||||
} else {
|
||||
format!("v{}:{}#{}", ver, label, method)
|
||||
}
|
||||
}
|
||||
pub(super) fn pic_record_hit(&mut self, key: &str) {
|
||||
use std::collections::hash_map::Entry;
|
||||
@ -19,23 +30,43 @@ impl VM {
|
||||
let v = e.get_mut();
|
||||
*v = v.saturating_add(1);
|
||||
if std::env::var("NYASH_VM_PIC_DEBUG").ok().as_deref() == Some("1") {
|
||||
if *v == 8 || *v == 32 { eprintln!("[PIC] Hot BoxCall site '{}' hits={} (skeleton)", key, v); }
|
||||
if *v == 8 || *v == 32 {
|
||||
eprintln!("[PIC] Hot BoxCall site '{}' hits={} (skeleton)", key, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Entry::Vacant(v) => { v.insert(1); }
|
||||
Entry::Vacant(v) => {
|
||||
v.insert(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) fn pic_hits(&self, key: &str) -> u32 { *self.boxcall_pic_hits.get(key).unwrap_or(&0) }
|
||||
pub(super) fn build_vtable_key(&self, class_name: &str, method_id: u16, arity: usize) -> String {
|
||||
pub(super) fn pic_hits(&self, key: &str) -> u32 {
|
||||
*self.boxcall_pic_hits.get(key).unwrap_or(&0)
|
||||
}
|
||||
pub(super) fn build_vtable_key(
|
||||
&self,
|
||||
class_name: &str,
|
||||
method_id: u16,
|
||||
arity: usize,
|
||||
) -> String {
|
||||
let label = format!("BoxRef:{}", class_name);
|
||||
let ver = self.cache_version_for_label(&label);
|
||||
format!("VT@v{}:{}#{}{}", ver, class_name, method_id, format!("/{}", arity))
|
||||
format!(
|
||||
"VT@v{}:{}#{}{}",
|
||||
ver,
|
||||
class_name,
|
||||
method_id,
|
||||
format!("/{}", arity)
|
||||
)
|
||||
}
|
||||
pub(super) fn try_poly_pic(&mut self, pic_site_key: &str, recv: &VMValue) -> Option<String> {
|
||||
let label = self.cache_label_for_recv(recv);
|
||||
let ver = self.cache_version_for_label(&label);
|
||||
if let Some(entries) = self.boxcall_poly_pic.get_mut(pic_site_key) {
|
||||
if let Some(idx) = entries.iter().position(|(l, v, _)| *l == label && *v == ver) {
|
||||
if let Some(idx) = entries
|
||||
.iter()
|
||||
.position(|(l, v, _)| *l == label && *v == ver)
|
||||
{
|
||||
let entry = entries.remove(idx);
|
||||
entries.push(entry.clone());
|
||||
return Some(entry.2);
|
||||
@ -50,15 +81,28 @@ impl VM {
|
||||
match self.boxcall_poly_pic.entry(pic_site_key.to_string()) {
|
||||
Entry::Occupied(mut e) => {
|
||||
let v = e.get_mut();
|
||||
if let Some(idx) = v.iter().position(|(l, vv, _)| *l == label && *vv == ver) { v.remove(idx); }
|
||||
if v.len() >= 4 { v.remove(0); }
|
||||
if let Some(idx) = v.iter().position(|(l, vv, _)| *l == label && *vv == ver) {
|
||||
v.remove(idx);
|
||||
}
|
||||
if v.len() >= 4 {
|
||||
v.remove(0);
|
||||
}
|
||||
v.push((label.clone(), ver, func_name.to_string()));
|
||||
}
|
||||
Entry::Vacant(v) => { v.insert(vec![(label.clone(), ver, func_name.to_string())]); }
|
||||
Entry::Vacant(v) => {
|
||||
v.insert(vec![(label.clone(), ver, func_name.to_string())]);
|
||||
}
|
||||
}
|
||||
if crate::config::env::vm_pic_stats() {
|
||||
if let Some(v) = self.boxcall_poly_pic.get(pic_site_key) {
|
||||
eprintln!("[PIC] site={} size={} last=({}, v{}) -> {}", pic_site_key, v.len(), label, ver, func_name);
|
||||
eprintln!(
|
||||
"[PIC] site={} size={} last=({}, v{}) -> {}",
|
||||
pic_site_key,
|
||||
v.len(),
|
||||
label,
|
||||
ver,
|
||||
func_name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -73,25 +117,45 @@ impl VM {
|
||||
VMValue::BoxRef(b) => format!("BoxRef:{}", b.type_name()),
|
||||
}
|
||||
}
|
||||
pub(super) fn cache_version_for_label(&self, label: &str) -> u32 { crate::runtime::cache_versions::get_version(label) }
|
||||
pub(super) fn cache_version_for_label(&self, label: &str) -> u32 {
|
||||
crate::runtime::cache_versions::get_version(label)
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn bump_cache_version(&mut self, label: &str) { crate::runtime::cache_versions::bump_version(label) }
|
||||
pub fn bump_cache_version(&mut self, label: &str) {
|
||||
crate::runtime::cache_versions::bump_version(label)
|
||||
}
|
||||
|
||||
// ---- Basics ----
|
||||
pub(crate) fn execute_const(&mut self, dst: ValueId, value: &ConstValue) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_const(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
value: &ConstValue,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let vm_value = VMValue::from(value);
|
||||
self.set_value(dst, vm_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_binop(&mut self, dst: ValueId, op: &BinaryOp, lhs: ValueId, rhs: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_binop(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
op: &BinaryOp,
|
||||
lhs: ValueId,
|
||||
rhs: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
match *op {
|
||||
BinaryOp::And | BinaryOp::Or => {
|
||||
if std::env::var("NYASH_VM_DEBUG_ANDOR").ok().as_deref() == Some("1") { eprintln!("[VM] And/Or short-circuit path"); }
|
||||
if std::env::var("NYASH_VM_DEBUG_ANDOR").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM] And/Or short-circuit path");
|
||||
}
|
||||
let left = self.get_value(lhs)?;
|
||||
let right = self.get_value(rhs)?;
|
||||
let lb = left.as_bool()?;
|
||||
let rb = right.as_bool()?;
|
||||
let out = match *op { BinaryOp::And => lb && rb, BinaryOp::Or => lb || rb, _ => unreachable!() };
|
||||
let out = match *op {
|
||||
BinaryOp::And => lb && rb,
|
||||
BinaryOp::Or => lb || rb,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.set_value(dst, VMValue::Bool(out));
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
@ -104,29 +168,63 @@ impl VM {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn execute_unaryop(&mut self, dst: ValueId, op: &UnaryOp, operand: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_unaryop(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
op: &UnaryOp,
|
||||
operand: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let operand_val = self.get_value(operand)?;
|
||||
let result = self.execute_unary_op(op, &operand_val)?;
|
||||
self.set_value(dst, result);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_compare(&mut self, dst: ValueId, op: &CompareOp, lhs: ValueId, rhs: ValueId) -> Result<ControlFlow, VMError> {
|
||||
let debug_cmp = std::env::var("NYASH_VM_DEBUG").ok().as_deref() == Some("1") || std::env::var("NYASH_VM_DEBUG_CMP").ok().as_deref() == Some("1");
|
||||
if debug_cmp { eprintln!("[VM] execute_compare enter op={:?} lhs={:?} rhs={:?}", op, lhs, rhs); }
|
||||
pub(crate) fn execute_compare(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
op: &CompareOp,
|
||||
lhs: ValueId,
|
||||
rhs: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let debug_cmp = std::env::var("NYASH_VM_DEBUG").ok().as_deref() == Some("1")
|
||||
|| std::env::var("NYASH_VM_DEBUG_CMP").ok().as_deref() == Some("1");
|
||||
if debug_cmp {
|
||||
eprintln!(
|
||||
"[VM] execute_compare enter op={:?} lhs={:?} rhs={:?}",
|
||||
op, lhs, rhs
|
||||
);
|
||||
}
|
||||
let mut left = self.get_value(lhs)?;
|
||||
let mut right = self.get_value(rhs)?;
|
||||
if debug_cmp { eprintln!("[VM] execute_compare values: left={:?} right={:?}", left, right); }
|
||||
if debug_cmp {
|
||||
eprintln!(
|
||||
"[VM] execute_compare values: left={:?} right={:?}",
|
||||
left, right
|
||||
);
|
||||
}
|
||||
left = match left {
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(ib) = b.as_any().downcast_ref::<crate::box_trait::IntegerBox>() { VMValue::Integer(ib.value) }
|
||||
else { match b.to_string_box().value.trim().parse::<i64>() { Ok(n) => VMValue::Integer(n), Err(_) => VMValue::BoxRef(b) } }
|
||||
if let Some(ib) = b.as_any().downcast_ref::<crate::box_trait::IntegerBox>() {
|
||||
VMValue::Integer(ib.value)
|
||||
} else {
|
||||
match b.to_string_box().value.trim().parse::<i64>() {
|
||||
Ok(n) => VMValue::Integer(n),
|
||||
Err(_) => VMValue::BoxRef(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
right = match right {
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(ib) = b.as_any().downcast_ref::<crate::box_trait::IntegerBox>() { VMValue::Integer(ib.value) }
|
||||
else { match b.to_string_box().value.trim().parse::<i64>() { Ok(n) => VMValue::Integer(n), Err(_) => VMValue::BoxRef(b) } }
|
||||
if let Some(ib) = b.as_any().downcast_ref::<crate::box_trait::IntegerBox>() {
|
||||
VMValue::Integer(ib.value)
|
||||
} else {
|
||||
match b.to_string_box().value.trim().parse::<i64>() {
|
||||
Ok(n) => VMValue::Integer(n),
|
||||
Err(_) => VMValue::BoxRef(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
@ -139,33 +237,70 @@ impl VM {
|
||||
println!("{}", val.to_string());
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_jump(&self, target: BasicBlockId) -> Result<ControlFlow, VMError> { Ok(ControlFlow::Jump(target)) }
|
||||
pub(crate) fn execute_branch(&self, condition: ValueId, then_bb: BasicBlockId, else_bb: BasicBlockId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_jump(&self, target: BasicBlockId) -> Result<ControlFlow, VMError> {
|
||||
Ok(ControlFlow::Jump(target))
|
||||
}
|
||||
pub(crate) fn execute_branch(
|
||||
&self,
|
||||
condition: ValueId,
|
||||
then_bb: BasicBlockId,
|
||||
else_bb: BasicBlockId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let cond_val = self.get_value(condition)?;
|
||||
let should_branch = match &cond_val {
|
||||
VMValue::Bool(b) => *b,
|
||||
VMValue::Void => false,
|
||||
VMValue::Integer(i) => *i != 0,
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(bool_box) = b.as_any().downcast_ref::<BoolBox>() { bool_box.value }
|
||||
else if b.as_any().downcast_ref::<VoidBox>().is_some() { false }
|
||||
else { return Err(VMError::TypeError(format!("Branch condition must be bool, void, or integer, got BoxRef({})", b.type_name()))); }
|
||||
if let Some(bool_box) = b.as_any().downcast_ref::<BoolBox>() {
|
||||
bool_box.value
|
||||
} else if b.as_any().downcast_ref::<VoidBox>().is_some() {
|
||||
false
|
||||
} else {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"Branch condition must be bool, void, or integer, got BoxRef({})",
|
||||
b.type_name()
|
||||
)));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"Branch condition must be bool, void, or integer, got {:?}",
|
||||
cond_val
|
||||
)))
|
||||
}
|
||||
_ => return Err(VMError::TypeError(format!("Branch condition must be bool, void, or integer, got {:?}", cond_val))),
|
||||
};
|
||||
Ok(ControlFlow::Jump(if should_branch { then_bb } else { else_bb }))
|
||||
Ok(ControlFlow::Jump(if should_branch {
|
||||
then_bb
|
||||
} else {
|
||||
else_bb
|
||||
}))
|
||||
}
|
||||
pub(crate) fn execute_return(&self, value: Option<ValueId>) -> Result<ControlFlow, VMError> {
|
||||
if let Some(val_id) = value {
|
||||
let return_val = self.get_value(val_id)?;
|
||||
if crate::config::env::vm_vt_trace() { eprintln!("[VT] Return id={} val={}", val_id.to_usize(), return_val.to_string()); }
|
||||
if crate::config::env::vm_vt_trace() {
|
||||
eprintln!(
|
||||
"[VT] Return id={} val={}",
|
||||
val_id.to_usize(),
|
||||
return_val.to_string()
|
||||
);
|
||||
}
|
||||
Ok(ControlFlow::Return(return_val))
|
||||
} else {
|
||||
if crate::config::env::vm_vt_trace() { eprintln!("[VT] Return void"); }
|
||||
if crate::config::env::vm_vt_trace() {
|
||||
eprintln!("[VT] Return void");
|
||||
}
|
||||
Ok(ControlFlow::Return(VMValue::Void))
|
||||
}
|
||||
}
|
||||
pub(crate) fn execute_typeop(&mut self, dst: ValueId, op: &TypeOpKind, value: ValueId, ty: &MirType) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_typeop(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
op: &TypeOpKind,
|
||||
value: ValueId,
|
||||
ty: &MirType,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let val = self.get_value(value)?;
|
||||
match op {
|
||||
TypeOpKind::Check => {
|
||||
@ -175,7 +310,9 @@ impl VM {
|
||||
(VMValue::Bool(_), MirType::Bool) => true,
|
||||
(VMValue::String(_), MirType::String) => true,
|
||||
(VMValue::Void, MirType::Void) => true,
|
||||
(VMValue::BoxRef(arc_box), MirType::Box(box_name)) => arc_box.type_name() == box_name,
|
||||
(VMValue::BoxRef(arc_box), MirType::Box(box_name)) => {
|
||||
arc_box.type_name() == box_name
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
self.set_value(dst, VMValue::Bool(is_type));
|
||||
@ -185,34 +322,65 @@ impl VM {
|
||||
let result = match (&val, ty) {
|
||||
(VMValue::Integer(i), MirType::Float) => VMValue::Float(*i as f64),
|
||||
(VMValue::Float(f), MirType::Integer) => VMValue::Integer(*f as i64),
|
||||
(VMValue::Integer(_), MirType::Integer) | (VMValue::Float(_), MirType::Float) | (VMValue::Bool(_), MirType::Bool) | (VMValue::String(_), MirType::String) => val.clone(),
|
||||
(VMValue::BoxRef(arc_box), MirType::Box(box_name)) if arc_box.type_name() == box_name => val.clone(),
|
||||
_ => { return Err(VMError::TypeError(format!("Cannot cast {:?} to {:?}", val, ty))); }
|
||||
(VMValue::Integer(_), MirType::Integer)
|
||||
| (VMValue::Float(_), MirType::Float)
|
||||
| (VMValue::Bool(_), MirType::Bool)
|
||||
| (VMValue::String(_), MirType::String) => val.clone(),
|
||||
(VMValue::BoxRef(arc_box), MirType::Box(box_name))
|
||||
if arc_box.type_name() == box_name =>
|
||||
{
|
||||
val.clone()
|
||||
}
|
||||
_ => {
|
||||
return Err(VMError::TypeError(format!(
|
||||
"Cannot cast {:?} to {:?}",
|
||||
val, ty
|
||||
)));
|
||||
}
|
||||
};
|
||||
self.set_value(dst, result);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn execute_phi(&mut self, dst: ValueId, inputs: &[(BasicBlockId, ValueId)]) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_phi(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
inputs: &[(BasicBlockId, ValueId)],
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let selected = self.loop_execute_phi(dst, inputs)?;
|
||||
self.set_value(dst, selected);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_load(&mut self, dst: ValueId, ptr: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_load(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
ptr: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let loaded_value = self.get_value(ptr)?;
|
||||
self.set_value(dst, loaded_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_store(&mut self, value: ValueId, ptr: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_store(
|
||||
&mut self,
|
||||
value: ValueId,
|
||||
ptr: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let val = self.get_value(value)?;
|
||||
self.set_value(ptr, val);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_copy(&mut self, dst: ValueId, src: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_copy(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
src: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let value = self.get_value(src)?;
|
||||
let cloned = match &value {
|
||||
VMValue::BoxRef(arc_box) => { let cloned_box = arc_box.clone_or_share(); VMValue::BoxRef(Arc::from(cloned_box)) }
|
||||
VMValue::BoxRef(arc_box) => {
|
||||
let cloned_box = arc_box.clone_or_share();
|
||||
VMValue::BoxRef(Arc::from(cloned_box))
|
||||
}
|
||||
other => other.clone(),
|
||||
};
|
||||
self.set_value(dst, cloned);
|
||||
@ -220,7 +388,12 @@ impl VM {
|
||||
}
|
||||
|
||||
// ---- Arrays ----
|
||||
pub(crate) fn execute_array_get(&mut self, dst: ValueId, array: ValueId, index: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_array_get(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
array: ValueId,
|
||||
index: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let array_val = self.get_value(array)?;
|
||||
let index_val = self.get_value(index)?;
|
||||
if let VMValue::BoxRef(array_box) = &array_val {
|
||||
@ -229,10 +402,23 @@ impl VM {
|
||||
let result = array.get(index_box);
|
||||
self.set_value(dst, VMValue::BoxRef(Arc::from(result)));
|
||||
Ok(ControlFlow::Continue)
|
||||
} else { Err(VMError::TypeError("ArrayGet requires an ArrayBox".to_string())) }
|
||||
} else { Err(VMError::TypeError("ArrayGet requires array and integer index".to_string())) }
|
||||
} else {
|
||||
Err(VMError::TypeError(
|
||||
"ArrayGet requires an ArrayBox".to_string(),
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Err(VMError::TypeError(
|
||||
"ArrayGet requires array and integer index".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
pub(crate) fn execute_array_set(&mut self, array: ValueId, index: ValueId, value: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_array_set(
|
||||
&mut self,
|
||||
array: ValueId,
|
||||
index: ValueId,
|
||||
value: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let array_val = self.get_value(array)?;
|
||||
let index_val = self.get_value(index)?;
|
||||
let value_val = self.get_value(value)?;
|
||||
@ -243,27 +429,50 @@ impl VM {
|
||||
let box_value = value_val.to_nyash_box();
|
||||
array.set(index_box, box_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
} else { Err(VMError::TypeError("ArraySet requires an ArrayBox".to_string())) }
|
||||
} else { Err(VMError::TypeError("ArraySet requires array and integer index".to_string())) }
|
||||
} else {
|
||||
Err(VMError::TypeError(
|
||||
"ArraySet requires an ArrayBox".to_string(),
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Err(VMError::TypeError(
|
||||
"ArraySet requires array and integer index".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Refs/Weak/Barriers ----
|
||||
pub(crate) fn execute_ref_new(&mut self, dst: ValueId, box_val: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_ref_new(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
box_val: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let box_value = self.get_value(box_val)?;
|
||||
self.set_value(dst, box_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_ref_get(&mut self, dst: ValueId, reference: ValueId, field: &str) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_ref_get(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
reference: ValueId,
|
||||
field: &str,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let debug_ref = std::env::var("NYASH_VM_DEBUG_REF").ok().as_deref() == Some("1");
|
||||
if debug_ref { eprintln!("[VM] RefGet ref={:?} field={}", reference, field); }
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefGet ref={:?} field={}", reference, field);
|
||||
}
|
||||
let is_internal = self.object_internal.contains(&reference);
|
||||
if !is_internal {
|
||||
if let Some(class_name) = self.object_class.get(&reference) {
|
||||
if let Ok(decls) = self.runtime.box_declarations.read() {
|
||||
if let Some(decl) = decls.get(class_name) {
|
||||
let has_vis = !decl.public_fields.is_empty() || !decl.private_fields.is_empty();
|
||||
let has_vis =
|
||||
!decl.public_fields.is_empty() || !decl.private_fields.is_empty();
|
||||
if has_vis && !decl.public_fields.iter().any(|f| f == field) {
|
||||
return Err(VMError::TypeError(format!("Field '{}' is private in {}", field, class_name)));
|
||||
return Err(VMError::TypeError(format!(
|
||||
"Field '{}' is private in {}",
|
||||
field, class_name
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -271,78 +480,140 @@ impl VM {
|
||||
}
|
||||
let mut field_value = if let Some(fields) = self.object_fields.get(&reference) {
|
||||
if let Some(value) = fields.get(field) {
|
||||
if debug_ref { eprintln!("[VM] RefGet hit: {} -> {:?}", field, value); }
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefGet hit: {} -> {:?}", field, value);
|
||||
}
|
||||
value.clone()
|
||||
} else {
|
||||
if debug_ref { eprintln!("[VM] RefGet miss: {} -> default 0", field); }
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefGet miss: {} -> default 0", field);
|
||||
}
|
||||
VMValue::Integer(0)
|
||||
}
|
||||
} else {
|
||||
if debug_ref { eprintln!("[VM] RefGet no fields: -> default 0"); }
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefGet no fields: -> default 0");
|
||||
}
|
||||
VMValue::Integer(0)
|
||||
};
|
||||
if matches!(field_value, VMValue::Integer(0)) && field == "console" {
|
||||
if debug_ref { eprintln!("[VM] RefGet special binding: console -> Plugin ConsoleBox"); }
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefGet special binding: console -> Plugin ConsoleBox");
|
||||
}
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
if let Ok(pbox) = host.create_box("ConsoleBox", &[]) {
|
||||
field_value = VMValue::from_nyash_box(pbox);
|
||||
if !self.object_fields.contains_key(&reference) { self.object_fields.insert(reference, std::collections::HashMap::new()); }
|
||||
if let Some(fields) = self.object_fields.get_mut(&reference) { fields.insert(field.to_string(), field_value.clone()); }
|
||||
if !self.object_fields.contains_key(&reference) {
|
||||
self.object_fields
|
||||
.insert(reference, std::collections::HashMap::new());
|
||||
}
|
||||
if let Some(fields) = self.object_fields.get_mut(&reference) {
|
||||
fields.insert(field.to_string(), field_value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.set_value(dst, field_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_ref_set(&mut self, reference: ValueId, field: &str, value: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_ref_set(
|
||||
&mut self,
|
||||
reference: ValueId,
|
||||
field: &str,
|
||||
value: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let debug_ref = std::env::var("NYASH_VM_DEBUG_REF").ok().as_deref() == Some("1");
|
||||
let new_value = self.get_value(value)?;
|
||||
if debug_ref { eprintln!("[VM] RefSet ref={:?} field={} value={:?}", reference, field, new_value); }
|
||||
if debug_ref {
|
||||
eprintln!(
|
||||
"[VM] RefSet ref={:?} field={} value={:?}",
|
||||
reference, field, new_value
|
||||
);
|
||||
}
|
||||
let is_internal = self.object_internal.contains(&reference);
|
||||
if !is_internal {
|
||||
if let Some(class_name) = self.object_class.get(&reference) {
|
||||
if let Ok(decls) = self.runtime.box_declarations.read() {
|
||||
if let Some(decl) = decls.get(class_name) {
|
||||
let has_vis = !decl.public_fields.is_empty() || !decl.private_fields.is_empty();
|
||||
let has_vis =
|
||||
!decl.public_fields.is_empty() || !decl.private_fields.is_empty();
|
||||
if has_vis && !decl.public_fields.iter().any(|f| f == field) {
|
||||
return Err(VMError::TypeError(format!("Field '{}' is private in {}", field, class_name)));
|
||||
return Err(VMError::TypeError(format!(
|
||||
"Field '{}' is private in {}",
|
||||
field, class_name
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !self.object_fields.contains_key(&reference) { self.object_fields.insert(reference, std::collections::HashMap::new()); }
|
||||
if !self.object_fields.contains_key(&reference) {
|
||||
self.object_fields
|
||||
.insert(reference, std::collections::HashMap::new());
|
||||
}
|
||||
crate::backend::gc_helpers::gc_write_barrier_site(&self.runtime, "RefSet");
|
||||
if let Some(fields) = self.object_fields.get_mut(&reference) { fields.insert(field.to_string(), new_value); if debug_ref { eprintln!("[VM] RefSet stored: {}", field); } }
|
||||
if let Some(fields) = self.object_fields.get_mut(&reference) {
|
||||
fields.insert(field.to_string(), new_value);
|
||||
if debug_ref {
|
||||
eprintln!("[VM] RefSet stored: {}", field);
|
||||
}
|
||||
}
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_weak_new(&mut self, dst: ValueId, box_val: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_weak_new(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
box_val: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let box_value = self.get_value(box_val)?;
|
||||
self.set_value(dst, box_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_weak_load(&mut self, dst: ValueId, weak_ref: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_weak_load(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
weak_ref: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let weak_value = self.get_value(weak_ref)?;
|
||||
self.set_value(dst, weak_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_barrier_read(&mut self, dst: ValueId, value: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_barrier_read(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
value: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let val = self.get_value(value)?;
|
||||
self.set_value(dst, val);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_barrier_write(&mut self, _value: ValueId) -> Result<ControlFlow, VMError> { Ok(ControlFlow::Continue) }
|
||||
pub(crate) fn execute_barrier_write(
|
||||
&mut self,
|
||||
_value: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
pub(crate) fn execute_throw(&mut self, exception: ValueId) -> Result<ControlFlow, VMError> {
|
||||
let exc_value = self.get_value(exception)?;
|
||||
Err(VMError::InvalidInstruction(format!("Exception thrown: {:?}", exc_value)))
|
||||
Err(VMError::InvalidInstruction(format!(
|
||||
"Exception thrown: {:?}",
|
||||
exc_value
|
||||
)))
|
||||
}
|
||||
pub(crate) fn execute_catch(&mut self, exception_value: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_catch(
|
||||
&mut self,
|
||||
exception_value: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
self.set_value(exception_value, VMValue::Void);
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
|
||||
// ---- Futures ----
|
||||
pub(crate) fn execute_await(&mut self, dst: ValueId, future: ValueId) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_await(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
future: ValueId,
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
let future_val = self.get_value(future)?;
|
||||
if let VMValue::Future(ref future_box) = future_val {
|
||||
let max_ms: u64 = crate::config::env::await_max_ms();
|
||||
@ -350,10 +621,14 @@ impl VM {
|
||||
let mut spins = 0usize;
|
||||
while !future_box.ready() {
|
||||
self.runtime.gc.safepoint();
|
||||
if let Some(s) = &self.runtime.scheduler { s.poll(); }
|
||||
if let Some(s) = &self.runtime.scheduler {
|
||||
s.poll();
|
||||
}
|
||||
std::thread::yield_now();
|
||||
spins += 1;
|
||||
if spins % 1024 == 0 { std::thread::sleep(std::time::Duration::from_millis(1)); }
|
||||
if spins % 1024 == 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
|
||||
let err = Box::new(crate::box_trait::StringBox::new("Timeout"));
|
||||
let rb = crate::boxes::result::NyashResultBox::new_err(err);
|
||||
@ -367,6 +642,11 @@ impl VM {
|
||||
let vm_value = VMValue::from_nyash_box(Box::new(ok));
|
||||
self.set_value(dst, vm_value);
|
||||
Ok(ControlFlow::Continue)
|
||||
} else { Err(VMError::TypeError(format!("Expected Future, got {:?}", future_val))) }
|
||||
} else {
|
||||
Err(VMError::TypeError(format!(
|
||||
"Expected Future, got {:?}",
|
||||
future_val
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,33 +1,60 @@
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::mir::ValueId;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
|
||||
impl VM {
|
||||
/// Execute ExternCall instruction
|
||||
pub(crate) fn execute_extern_call(&mut self, dst: Option<ValueId>, iface_name: &str, method_name: &str, args: &[ValueId]) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_extern_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
iface_name: &str,
|
||||
method_name: &str,
|
||||
args: &[ValueId],
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
// Core-13 pure shims: env.local.{get,set}, env.box.new
|
||||
match (iface_name, method_name) {
|
||||
("env.local", "get") => {
|
||||
if args.len() != 1 { return Err(VMError::InvalidInstruction("env.local.get arity".into())); }
|
||||
if args.len() != 1 {
|
||||
return Err(VMError::InvalidInstruction("env.local.get arity".into()));
|
||||
}
|
||||
let ptr = args[0];
|
||||
let v = self.get_value(ptr).unwrap_or(crate::backend::vm::VMValue::Void);
|
||||
if let Some(d) = dst { self.set_value(d, v); }
|
||||
let v = self
|
||||
.get_value(ptr)
|
||||
.unwrap_or(crate::backend::vm::VMValue::Void);
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, v);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.local", "set") => {
|
||||
if args.len() != 2 { return Err(VMError::InvalidInstruction("env.local.set arity".into())); }
|
||||
if args.len() != 2 {
|
||||
return Err(VMError::InvalidInstruction("env.local.set arity".into()));
|
||||
}
|
||||
let ptr = args[0];
|
||||
let val = self.get_value(args[1])?;
|
||||
self.set_value(ptr, val);
|
||||
if let Some(d) = dst { self.set_value(d, crate::backend::vm::VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, crate::backend::vm::VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.box", "new") => {
|
||||
if args.is_empty() { return Err(VMError::InvalidInstruction("env.box.new requires type name".into())); }
|
||||
if args.is_empty() {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"env.box.new requires type name".into(),
|
||||
));
|
||||
}
|
||||
// first arg must be Const String type name
|
||||
let ty = self.get_value(args[0])?;
|
||||
let ty_name = match ty { crate::backend::vm::VMValue::String(s) => s, _ => return Err(VMError::InvalidInstruction("env.box.new first arg must be string".into())) };
|
||||
let ty_name = match ty {
|
||||
crate::backend::vm::VMValue::String(s) => s,
|
||||
_ => {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"env.box.new first arg must be string".into(),
|
||||
))
|
||||
}
|
||||
};
|
||||
// remaining args as NyashBox
|
||||
let mut ny_args: Vec<Box<dyn NyashBox>> = Vec::new();
|
||||
for id in args.iter().skip(1) {
|
||||
@ -36,8 +63,17 @@ impl VM {
|
||||
}
|
||||
let reg = crate::runtime::box_registry::get_global_registry();
|
||||
match reg.create_box(&ty_name, &ny_args) {
|
||||
Ok(b) => { if let Some(d) = dst { self.set_value(d, crate::backend::vm::VMValue::from_nyash_box(b)); } }
|
||||
Err(e) => { return Err(VMError::InvalidInstruction(format!("env.box.new failed for {}: {}", ty_name, e))); }
|
||||
Ok(b) => {
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, crate::backend::vm::VMValue::from_nyash_box(b));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"env.box.new failed for {}: {}",
|
||||
ty_name, e
|
||||
)));
|
||||
}
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
@ -45,41 +81,90 @@ impl VM {
|
||||
}
|
||||
// Optional routing to name→slot handlers for stability and diagnostics
|
||||
if crate::config::env::extern_route_slots() {
|
||||
if let Some(slot) = crate::runtime::extern_registry::resolve_slot(iface_name, method_name) {
|
||||
if let Some(slot) =
|
||||
crate::runtime::extern_registry::resolve_slot(iface_name, method_name)
|
||||
{
|
||||
// Decode args to VMValue as needed by handlers below
|
||||
let vm_args: Vec<VMValue> = args.iter().filter_map(|a| self.get_value(*a).ok()).collect();
|
||||
let vm_args: Vec<VMValue> = args
|
||||
.iter()
|
||||
.filter_map(|a| self.get_value(*a).ok())
|
||||
.collect();
|
||||
match (iface_name, method_name, slot) {
|
||||
("env.local", "get", 40) => {
|
||||
if let Some(d) = dst { if let Some(a0) = args.get(0) { let v = self.get_value(*a0).unwrap_or(VMValue::Void); self.set_value(d, v); } }
|
||||
if let Some(d) = dst {
|
||||
if let Some(a0) = args.get(0) {
|
||||
let v = self.get_value(*a0).unwrap_or(VMValue::Void);
|
||||
self.set_value(d, v);
|
||||
}
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.local", "set", 41) => {
|
||||
if args.len() >= 2 { let ptr = args[0]; let val = vm_args.get(1).cloned().unwrap_or(VMValue::Void); self.set_value(ptr, val); }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if args.len() >= 2 {
|
||||
let ptr = args[0];
|
||||
let val = vm_args.get(1).cloned().unwrap_or(VMValue::Void);
|
||||
self.set_value(ptr, val);
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.box", "new", 50) => {
|
||||
if vm_args.is_empty() { return Err(VMError::InvalidInstruction("env.box.new requires type".into())); }
|
||||
let ty = &vm_args[0]; let ty_name = match ty { VMValue::String(s) => s.clone(), _ => return Err(VMError::InvalidInstruction("env.box.new first arg must be string".into())) };
|
||||
if vm_args.is_empty() {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"env.box.new requires type".into(),
|
||||
));
|
||||
}
|
||||
let ty = &vm_args[0];
|
||||
let ty_name = match ty {
|
||||
VMValue::String(s) => s.clone(),
|
||||
_ => {
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"env.box.new first arg must be string".into(),
|
||||
))
|
||||
}
|
||||
};
|
||||
let mut ny_args: Vec<Box<dyn NyashBox>> = Vec::new();
|
||||
for v in vm_args.iter().skip(1) { ny_args.push(v.to_nyash_box()); }
|
||||
for v in vm_args.iter().skip(1) {
|
||||
ny_args.push(v.to_nyash_box());
|
||||
}
|
||||
let reg = crate::runtime::box_registry::get_global_registry();
|
||||
match reg.create_box(&ty_name, &ny_args) {
|
||||
Ok(b) => { if let Some(d) = dst { self.set_value(d, VMValue::from_nyash_box(b)); } }
|
||||
Err(e) => { return Err(VMError::InvalidInstruction(format!("env.box.new failed for {}: {}", ty_name, e))); }
|
||||
Ok(b) => {
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::from_nyash_box(b));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(VMError::InvalidInstruction(format!(
|
||||
"env.box.new failed for {}: {}",
|
||||
ty_name, e
|
||||
)));
|
||||
}
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.console", m @ ("log" | "warn" | "error" | "println"), 10) => {
|
||||
if let Some(a0) = vm_args.get(0) {
|
||||
match m { "warn" => eprintln!("[warn] {}", a0.to_string()), "error" => eprintln!("[error] {}", a0.to_string()), _ => println!("{}", a0.to_string()), }
|
||||
match m {
|
||||
"warn" => eprintln!("[warn] {}", a0.to_string()),
|
||||
"error" => eprintln!("[error] {}", a0.to_string()),
|
||||
_ => println!("{}", a0.to_string()),
|
||||
}
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.debug", "trace", 11) => {
|
||||
if let Some(a0) = vm_args.get(0) { eprintln!("[trace] {}", a0.to_string()); }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(a0) = vm_args.get(0) {
|
||||
eprintln!("[trace] {}", a0.to_string());
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.runtime", "checkpoint", 12) => {
|
||||
@ -88,21 +173,35 @@ impl VM {
|
||||
eprintln!("[rt] checkpoint @{} bb={} pc={}", func, bb, pc);
|
||||
}
|
||||
self.runtime.gc.safepoint();
|
||||
if let Some(s) = &self.runtime.scheduler { s.poll(); }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(s) = &self.runtime.scheduler {
|
||||
s.poll();
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.future", "new", 20) | ("env.future", "birth", 20) => {
|
||||
// Create a new Future and optionally set initial value from arg0
|
||||
let fut = crate::boxes::future::FutureBox::new();
|
||||
if let Some(a0) = vm_args.get(0) { fut.set_result(a0.to_nyash_box()); }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Future(fut)); }
|
||||
if let Some(a0) = vm_args.get(0) {
|
||||
fut.set_result(a0.to_nyash_box());
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Future(fut));
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.future", "set", 21) => {
|
||||
// set(future, value)
|
||||
if vm_args.len() >= 2 { if let VMValue::Future(f) = &vm_args[0] { f.set_result(vm_args[1].to_nyash_box()); } }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if vm_args.len() >= 2 {
|
||||
if let VMValue::Future(f) = &vm_args[0] {
|
||||
f.set_result(vm_args[1].to_nyash_box());
|
||||
}
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.future", "await", 22) => {
|
||||
@ -112,21 +211,57 @@ impl VM {
|
||||
let max_ms = crate::config::env::await_max_ms();
|
||||
while !fb.ready() {
|
||||
std::thread::yield_now();
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) { break; }
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let result = if fb.ready() { fb.get() } else { Box::new(crate::box_trait::StringBox::new("Timeout")) };
|
||||
let result = if fb.ready() {
|
||||
fb.get()
|
||||
} else {
|
||||
Box::new(crate::box_trait::StringBox::new("Timeout"))
|
||||
};
|
||||
let ok = crate::boxes::result::NyashResultBox::new_ok(result);
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::from_nyash_box(Box::new(ok))); }
|
||||
} else if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::from_nyash_box(Box::new(ok)));
|
||||
}
|
||||
} else if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.task", "cancelCurrent", 30) => {
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.task", "currentToken", 31) => {
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Integer(0));
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.task", "yieldNow", 32) => {
|
||||
std::thread::yield_now();
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.task", "cancelCurrent", 30) => { if let Some(d) = dst { self.set_value(d, VMValue::Void); } return Ok(ControlFlow::Continue); }
|
||||
("env.task", "currentToken", 31) => { if let Some(d) = dst { self.set_value(d, VMValue::Integer(0)); } return Ok(ControlFlow::Continue); }
|
||||
("env.task", "yieldNow", 32) => { std::thread::yield_now(); if let Some(d) = dst { self.set_value(d, VMValue::Void); } return Ok(ControlFlow::Continue); }
|
||||
("env.task", "sleepMs", 33) => {
|
||||
let ms = vm_args.get(0).map(|v| match v { VMValue::Integer(i) => *i, _ => 0 }).unwrap_or(0);
|
||||
if ms > 0 { std::thread::sleep(std::time::Duration::from_millis(ms as u64)); }
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
let ms = vm_args
|
||||
.get(0)
|
||||
.map(|v| match v {
|
||||
VMValue::Integer(i) => *i,
|
||||
_ => 0,
|
||||
})
|
||||
.unwrap_or(0);
|
||||
if ms > 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(ms as u64));
|
||||
}
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
_ => { /* fallthrough to host */ }
|
||||
@ -137,24 +272,38 @@ impl VM {
|
||||
match (iface_name, method_name) {
|
||||
("env.modules", "set") => {
|
||||
// Expect two args
|
||||
let vm_args: Vec<VMValue> = args.iter().filter_map(|a| self.get_value(*a).ok()).collect();
|
||||
let vm_args: Vec<VMValue> = args
|
||||
.iter()
|
||||
.filter_map(|a| self.get_value(*a).ok())
|
||||
.collect();
|
||||
if vm_args.len() >= 2 {
|
||||
let key = vm_args[0].to_string();
|
||||
let val_box = vm_args[1].to_nyash_box();
|
||||
crate::runtime::modules_registry::set(key, val_box);
|
||||
}
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
("env.modules", "get") => {
|
||||
let vm_args: Vec<VMValue> = args.iter().filter_map(|a| self.get_value(*a).ok()).collect();
|
||||
let vm_args: Vec<VMValue> = args
|
||||
.iter()
|
||||
.filter_map(|a| self.get_value(*a).ok())
|
||||
.collect();
|
||||
if let Some(k) = vm_args.get(0) {
|
||||
let key = k.to_string();
|
||||
if let Some(v) = crate::runtime::modules_registry::get(&key) {
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::from_nyash_box(v)); }
|
||||
else { /* no dst */ }
|
||||
} else if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
} else if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::from_nyash_box(v));
|
||||
} else { /* no dst */
|
||||
}
|
||||
} else if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
} else if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
_ => {}
|
||||
@ -164,7 +313,10 @@ impl VM {
|
||||
// Name-route minimal registry even when slot routing is disabled
|
||||
if iface_name == "env.modules" {
|
||||
// Decode args as VMValue for convenience
|
||||
let vm_args: Vec<VMValue> = args.iter().filter_map(|a| self.get_value(*a).ok()).collect();
|
||||
let vm_args: Vec<VMValue> = args
|
||||
.iter()
|
||||
.filter_map(|a| self.get_value(*a).ok())
|
||||
.collect();
|
||||
match method_name {
|
||||
"set" => {
|
||||
if vm_args.len() >= 2 {
|
||||
@ -172,16 +324,24 @@ impl VM {
|
||||
let val_box = vm_args[1].to_nyash_box();
|
||||
crate::runtime::modules_registry::set(key, val_box);
|
||||
}
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
"get" => {
|
||||
if let Some(k) = vm_args.get(0) {
|
||||
let key = k.to_string();
|
||||
if let Some(v) = crate::runtime::modules_registry::get(&key) {
|
||||
if let Some(d) = dst { self.set_value(d, VMValue::from_nyash_box(v)); }
|
||||
} else if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
} else if let Some(d) = dst { self.set_value(d, VMValue::Void); }
|
||||
if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::from_nyash_box(v));
|
||||
}
|
||||
} else if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
} else if let Some(d) = dst {
|
||||
self.set_value(d, VMValue::Void);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
_ => {}
|
||||
@ -190,30 +350,78 @@ impl VM {
|
||||
|
||||
// Evaluate arguments as NyashBox for loader
|
||||
let mut nyash_args: Vec<Box<dyn NyashBox>> = Vec::new();
|
||||
for arg_id in args { let arg_value = self.get_value(*arg_id)?; nyash_args.push(arg_value.to_nyash_box()); }
|
||||
for arg_id in args {
|
||||
let arg_value = self.get_value(*arg_id)?;
|
||||
nyash_args.push(arg_value.to_nyash_box());
|
||||
}
|
||||
|
||||
if crate::config::env::extern_trace() {
|
||||
if let Some(slot) = crate::runtime::extern_registry::resolve_slot(iface_name, method_name) {
|
||||
eprintln!("[EXT] call {}.{} slot={} argc={}", iface_name, method_name, slot, nyash_args.len());
|
||||
} else { eprintln!("[EXT] call {}.{} argc={}", iface_name, method_name, nyash_args.len()); }
|
||||
if let Some(slot) =
|
||||
crate::runtime::extern_registry::resolve_slot(iface_name, method_name)
|
||||
{
|
||||
eprintln!(
|
||||
"[EXT] call {}.{} slot={} argc={}",
|
||||
iface_name,
|
||||
method_name,
|
||||
slot,
|
||||
nyash_args.len()
|
||||
);
|
||||
} else {
|
||||
eprintln!(
|
||||
"[EXT] call {}.{} argc={}",
|
||||
iface_name,
|
||||
method_name,
|
||||
nyash_args.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().map_err(|_| VMError::InvalidInstruction("Plugin host lock poisoned".into()))?;
|
||||
let host = host
|
||||
.read()
|
||||
.map_err(|_| VMError::InvalidInstruction("Plugin host lock poisoned".into()))?;
|
||||
match host.extern_call(iface_name, method_name, &nyash_args) {
|
||||
Ok(Some(result_box)) => { if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::from_nyash_box(result_box)); } }
|
||||
Ok(None) => { if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::Void); } }
|
||||
Ok(Some(result_box)) => {
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::from_nyash_box(result_box));
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::Void);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let strict = crate::config::env::extern_strict() || crate::config::env::abi_strict();
|
||||
let strict =
|
||||
crate::config::env::extern_strict() || crate::config::env::abi_strict();
|
||||
let mut msg = String::new();
|
||||
if strict { msg.push_str("ExternCall STRICT: unregistered or unsupported call "); } else { msg.push_str("ExternCall failed: "); }
|
||||
if strict {
|
||||
msg.push_str("ExternCall STRICT: unregistered or unsupported call ");
|
||||
} else {
|
||||
msg.push_str("ExternCall failed: ");
|
||||
}
|
||||
msg.push_str(&format!("{}.{}", iface_name, method_name));
|
||||
if let Err(detail) = crate::runtime::extern_registry::check_arity(iface_name, method_name, nyash_args.len()) { msg.push_str(&format!(" ({})", detail)); }
|
||||
if let Some(spec) = crate::runtime::extern_registry::resolve(iface_name, method_name) {
|
||||
msg.push_str(&format!(" (expected arity {}..{})", spec.min_arity, spec.max_arity));
|
||||
if let Err(detail) = crate::runtime::extern_registry::check_arity(
|
||||
iface_name,
|
||||
method_name,
|
||||
nyash_args.len(),
|
||||
) {
|
||||
msg.push_str(&format!(" ({})", detail));
|
||||
}
|
||||
if let Some(spec) =
|
||||
crate::runtime::extern_registry::resolve(iface_name, method_name)
|
||||
{
|
||||
msg.push_str(&format!(
|
||||
" (expected arity {}..{})",
|
||||
spec.min_arity, spec.max_arity
|
||||
));
|
||||
} else {
|
||||
let known = crate::runtime::extern_registry::known_for_iface(iface_name);
|
||||
if !known.is_empty() { msg.push_str(&format!("; known methods: {}", known.join(", "))); }
|
||||
else { let ifaces = crate::runtime::extern_registry::all_ifaces(); msg.push_str(&format!("; known interfaces: {}", ifaces.join(", "))); }
|
||||
if !known.is_empty() {
|
||||
msg.push_str(&format!("; known methods: {}", known.join(", ")));
|
||||
} else {
|
||||
let ifaces = crate::runtime::extern_registry::all_ifaces();
|
||||
msg.push_str(&format!("; known interfaces: {}", ifaces.join(", ")));
|
||||
}
|
||||
}
|
||||
return Err(VMError::InvalidInstruction(msg));
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::mir::ValueId;
|
||||
use std::sync::Arc;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
|
||||
impl VM {
|
||||
/// Execute FunctionNew instruction (construct a FunctionBox value)
|
||||
@ -23,11 +23,14 @@ impl VM {
|
||||
// Capture 'me' weakly if provided and is a BoxRef
|
||||
if let Some(m) = me {
|
||||
match self.get_value(*m)? {
|
||||
VMValue::BoxRef(b) => { env.me_value = Some(Arc::downgrade(&b)); }
|
||||
VMValue::BoxRef(b) => {
|
||||
env.me_value = Some(Arc::downgrade(&b));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let fun = crate::boxes::function_box::FunctionBox::with_env(params.to_vec(), body.to_vec(), env);
|
||||
let fun =
|
||||
crate::boxes::function_box::FunctionBox::with_env(params.to_vec(), body.to_vec(), env);
|
||||
self.set_value(dst, VMValue::BoxRef(Arc::new(fun)));
|
||||
Ok(ControlFlow::Continue)
|
||||
}
|
||||
|
||||
@ -5,11 +5,10 @@
|
||||
* behavior and public APIs. Methods remain as `impl VM` across submodules.
|
||||
*/
|
||||
|
||||
pub mod core; // const/binop/unary/compare/print/ctrl/type/phi/mem/array/refs/weak/barriers/exn/await
|
||||
pub mod call; // execute_call (Function name or FunctionBox)
|
||||
pub mod newbox; // execute_newbox
|
||||
pub mod function_new; // execute_function_new
|
||||
pub mod extern_call; // execute_extern_call
|
||||
pub mod boxcall; // execute_boxcall + vtable stub
|
||||
pub mod plugin_invoke; // execute_plugin_invoke + helpers
|
||||
|
||||
pub mod boxcall; // execute_boxcall + vtable stub
|
||||
pub mod call; // execute_call (Function name or FunctionBox)
|
||||
pub mod core; // const/binop/unary/compare/print/ctrl/type/phi/mem/array/refs/weak/barriers/exn/await
|
||||
pub mod extern_call; // execute_extern_call
|
||||
pub mod function_new; // execute_function_new
|
||||
pub mod newbox; // execute_newbox
|
||||
pub mod plugin_invoke; // execute_plugin_invoke + helpers
|
||||
|
||||
@ -1,14 +1,20 @@
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::mir::ValueId;
|
||||
use std::sync::Arc;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
|
||||
impl VM {
|
||||
/// Execute NewBox instruction
|
||||
pub(crate) fn execute_newbox(&mut self, dst: ValueId, box_type: &str, args: &[ValueId]) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_newbox(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
box_type: &str,
|
||||
args: &[ValueId],
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
// Convert args to NyashBox values
|
||||
let arg_values: Vec<Box<dyn NyashBox>> = args.iter()
|
||||
let arg_values: Vec<Box<dyn NyashBox>> = args
|
||||
.iter()
|
||||
.map(|arg| {
|
||||
let val = self.get_value(*arg)?;
|
||||
Ok(val.to_nyash_box())
|
||||
@ -17,15 +23,20 @@ impl VM {
|
||||
|
||||
// Create new box using runtime's registry
|
||||
let new_box = {
|
||||
let registry = self.runtime.box_registry.lock()
|
||||
.map_err(|_| VMError::InvalidInstruction("Failed to lock box registry".to_string()))?;
|
||||
registry.create_box(box_type, &arg_values)
|
||||
.map_err(|e| VMError::InvalidInstruction(format!("Failed to create {}: {}", box_type, e)))?
|
||||
let registry = self.runtime.box_registry.lock().map_err(|_| {
|
||||
VMError::InvalidInstruction("Failed to lock box registry".to_string())
|
||||
})?;
|
||||
registry.create_box(box_type, &arg_values).map_err(|e| {
|
||||
VMError::InvalidInstruction(format!("Failed to create {}: {}", box_type, e))
|
||||
})?
|
||||
};
|
||||
|
||||
// 80/20: Basic boxes are stored as primitives in VMValue for simpler ops
|
||||
if box_type == "IntegerBox" {
|
||||
if let Some(ib) = new_box.as_any().downcast_ref::<crate::box_trait::IntegerBox>() {
|
||||
if let Some(ib) = new_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::IntegerBox>()
|
||||
{
|
||||
self.set_value(dst, VMValue::Integer(ib.value));
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
@ -35,7 +46,10 @@ impl VM {
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
} else if box_type == "StringBox" {
|
||||
if let Some(sb) = new_box.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
if let Some(sb) = new_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
self.set_value(dst, VMValue::String(sb.value.clone()));
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
|
||||
@ -1,79 +1,186 @@
|
||||
use crate::mir::ValueId;
|
||||
use crate::backend::vm::ControlFlow;
|
||||
use crate::backend::{VM, VMError, VMValue};
|
||||
use crate::backend::{VMError, VMValue, VM};
|
||||
use crate::mir::ValueId;
|
||||
|
||||
impl VM {
|
||||
/// Execute a forced plugin invocation (no builtin fallback)
|
||||
pub(crate) fn execute_plugin_invoke(&mut self, dst: Option<ValueId>, box_val: ValueId, method: &str, args: &[ValueId]) -> Result<ControlFlow, VMError> {
|
||||
pub(crate) fn execute_plugin_invoke(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
method: &str,
|
||||
args: &[ValueId],
|
||||
) -> Result<ControlFlow, VMError> {
|
||||
// Helper: extract UTF-8 string from internal StringBox, Result.Ok(String-like), or plugin StringBox via toUtf8
|
||||
fn extract_string_from_box(bx: &dyn crate::box_trait::NyashBox) -> Option<String> {
|
||||
if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() { return Some(sb.value.clone()); }
|
||||
if let Some(res) = bx.as_any().downcast_ref::<crate::boxes::result::NyashResultBox>() {
|
||||
if let crate::boxes::result::NyashResultBox::Ok(inner) = res { return extract_string_from_box(inner.as_ref()); }
|
||||
if let Some(sb) = bx.as_any().downcast_ref::<crate::box_trait::StringBox>() {
|
||||
return Some(sb.value.clone());
|
||||
}
|
||||
if let Some(p) = bx.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
if let Some(res) = bx
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::result::NyashResultBox>()
|
||||
{
|
||||
if let crate::boxes::result::NyashResultBox::Ok(inner) = res {
|
||||
return extract_string_from_box(inner.as_ref());
|
||||
}
|
||||
}
|
||||
if let Some(p) = bx
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
{
|
||||
if p.box_type == "StringBox" {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let tmp: Option<String> = if let Ok(ro) = host.read() {
|
||||
if let Ok(val_opt) = ro.invoke_instance_method("StringBox", "toUtf8", p.inner.instance_id, &[]) {
|
||||
if let Some(vb) = val_opt { if let Some(sb2) = vb.as_any().downcast_ref::<crate::box_trait::StringBox>() { Some(sb2.value.clone()) } else { None } } else { None }
|
||||
} else { None }
|
||||
} else { None };
|
||||
if tmp.is_some() { return tmp; }
|
||||
if let Ok(val_opt) = ro.invoke_instance_method(
|
||||
"StringBox",
|
||||
"toUtf8",
|
||||
p.inner.instance_id,
|
||||
&[],
|
||||
) {
|
||||
if let Some(vb) = val_opt {
|
||||
if let Some(sb2) =
|
||||
vb.as_any().downcast_ref::<crate::box_trait::StringBox>()
|
||||
{
|
||||
Some(sb2.value.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if tmp.is_some() {
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
let recv = self.get_value(box_val)?;
|
||||
if method == "birth" && !matches!(recv, VMValue::BoxRef(ref b) if b.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some()) {
|
||||
if method == "birth"
|
||||
&& !matches!(recv, VMValue::BoxRef(ref b) if b.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some())
|
||||
{
|
||||
eprintln!("[VM PluginInvoke] static birth fallback recv={:?}", recv);
|
||||
let mut created: Option<VMValue> = None;
|
||||
match &recv {
|
||||
VMValue::String(s) => {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
let sb: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::box_trait::StringBox::new(s.clone()));
|
||||
if let Ok(b) = host.create_box("StringBox", &[sb]) { created = Some(VMValue::from_nyash_box(b)); }
|
||||
let sb: Box<dyn crate::box_trait::NyashBox> =
|
||||
Box::new(crate::box_trait::StringBox::new(s.clone()));
|
||||
if let Ok(b) = host.create_box("StringBox", &[sb]) {
|
||||
created = Some(VMValue::from_nyash_box(b));
|
||||
}
|
||||
}
|
||||
VMValue::Integer(_n) => {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
if let Ok(b) = host.create_box("IntegerBox", &[]) { created = Some(VMValue::from_nyash_box(b)); }
|
||||
if let Ok(b) = host.create_box("IntegerBox", &[]) {
|
||||
created = Some(VMValue::from_nyash_box(b));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(val) = created { if let Some(dst_id) = dst { self.set_value(dst_id, val); } return Ok(ControlFlow::Continue); }
|
||||
if let Some(val) = created {
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, val);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
}
|
||||
|
||||
if let VMValue::BoxRef(pbox) = &recv {
|
||||
if let Some(p) = pbox.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
if let Some(p) = pbox
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
{
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
let mh = host.resolve_method(&p.box_type, method).map_err(|_| VMError::InvalidInstruction(format!("Plugin method not found: {}.{}", p.box_type, method)))?;
|
||||
let mut tlv = crate::runtime::plugin_ffi_common::encode_tlv_header(args.len() as u16);
|
||||
let mh = host.resolve_method(&p.box_type, method).map_err(|_| {
|
||||
VMError::InvalidInstruction(format!(
|
||||
"Plugin method not found: {}.{}",
|
||||
p.box_type, method
|
||||
))
|
||||
})?;
|
||||
let mut tlv =
|
||||
crate::runtime::plugin_ffi_common::encode_tlv_header(args.len() as u16);
|
||||
for (idx, a) in args.iter().enumerate() {
|
||||
let v = self.get_value(*a)?;
|
||||
match v {
|
||||
VMValue::Integer(n) => { if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") { eprintln!("[VM→Plugin][vm] arg[{}] encode I64 {}", idx, n); } crate::runtime::plugin_ffi_common::encode::i64(&mut tlv, n) }
|
||||
VMValue::Float(x) => { if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") { eprintln!("[VM→Plugin][vm] arg[{}] encode F64 {}", idx, x); } crate::runtime::plugin_ffi_common::encode::f64(&mut tlv, x) }
|
||||
VMValue::Bool(b) => crate::runtime::plugin_ffi_common::encode::bool(&mut tlv, b),
|
||||
VMValue::String(ref s) => crate::runtime::plugin_ffi_common::encode::string(&mut tlv, s),
|
||||
VMValue::Integer(n) => {
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM→Plugin][vm] arg[{}] encode I64 {}", idx, n);
|
||||
}
|
||||
crate::runtime::plugin_ffi_common::encode::i64(&mut tlv, n)
|
||||
}
|
||||
VMValue::Float(x) => {
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM→Plugin][vm] arg[{}] encode F64 {}", idx, x);
|
||||
}
|
||||
crate::runtime::plugin_ffi_common::encode::f64(&mut tlv, x)
|
||||
}
|
||||
VMValue::Bool(b) => {
|
||||
crate::runtime::plugin_ffi_common::encode::bool(&mut tlv, b)
|
||||
}
|
||||
VMValue::String(ref s) => {
|
||||
crate::runtime::plugin_ffi_common::encode::string(&mut tlv, s)
|
||||
}
|
||||
VMValue::BoxRef(ref b) => {
|
||||
if let Some(h) = b.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>() {
|
||||
if let Some(h) = b
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
{
|
||||
if h.box_type == "StringBox" {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
if let Ok(val_opt) = host.invoke_instance_method("StringBox", "toUtf8", h.inner.instance_id, &[]) {
|
||||
if let Some(sb) = val_opt.and_then(|bx| bx.as_any().downcast_ref::<crate::box_trait::StringBox>().map(|s| s.value.clone())) { crate::runtime::plugin_ffi_common::encode::string(&mut tlv, &sb); continue; }
|
||||
if let Ok(val_opt) = host.invoke_instance_method(
|
||||
"StringBox",
|
||||
"toUtf8",
|
||||
h.inner.instance_id,
|
||||
&[],
|
||||
) {
|
||||
if let Some(sb) = val_opt.and_then(|bx| {
|
||||
bx.as_any()
|
||||
.downcast_ref::<crate::box_trait::StringBox>()
|
||||
.map(|s| s.value.clone())
|
||||
}) {
|
||||
crate::runtime::plugin_ffi_common::encode::string(
|
||||
&mut tlv, &sb,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else if h.box_type == "IntegerBox" {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
let host = host.read().unwrap();
|
||||
if let Ok(val_opt) = host.invoke_instance_method("IntegerBox", "get", h.inner.instance_id, &[]) {
|
||||
if let Some(ib) = val_opt.and_then(|bx| bx.as_any().downcast_ref::<crate::box_trait::IntegerBox>().map(|i| i.value)) { crate::runtime::plugin_ffi_common::encode::i64(&mut tlv, ib); continue; }
|
||||
if let Ok(val_opt) = host.invoke_instance_method(
|
||||
"IntegerBox",
|
||||
"get",
|
||||
h.inner.instance_id,
|
||||
&[],
|
||||
) {
|
||||
if let Some(ib) = val_opt.and_then(|bx| {
|
||||
bx.as_any()
|
||||
.downcast_ref::<crate::box_trait::IntegerBox>()
|
||||
.map(|i| i.value)
|
||||
}) {
|
||||
crate::runtime::plugin_ffi_common::encode::i64(
|
||||
&mut tlv, ib,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::runtime::plugin_ffi_common::encode::plugin_handle(&mut tlv, h.inner.type_id, h.inner.instance_id);
|
||||
crate::runtime::plugin_ffi_common::encode::plugin_handle(
|
||||
&mut tlv,
|
||||
h.inner.type_id,
|
||||
h.inner.instance_id,
|
||||
);
|
||||
} else {
|
||||
let h = crate::runtime::host_handles::to_handle_arc(b.clone());
|
||||
crate::runtime::plugin_ffi_common::encode::host_handle(&mut tlv, h);
|
||||
@ -84,28 +191,63 @@ impl VM {
|
||||
}
|
||||
let mut out = vec![0u8; 32768];
|
||||
let mut out_len: usize = out.len();
|
||||
unsafe { (p.inner.invoke_fn)(p.inner.type_id, mh.method_id as u32, p.inner.instance_id, tlv.as_ptr(), tlv.len(), out.as_mut_ptr(), &mut out_len) };
|
||||
let vm_out_raw: VMValue = if let Some((tag, _sz, payload)) = crate::runtime::plugin_ffi_common::decode::tlv_first(&out[..out_len]) {
|
||||
unsafe {
|
||||
(p.inner.invoke_fn)(
|
||||
p.inner.type_id,
|
||||
mh.method_id as u32,
|
||||
p.inner.instance_id,
|
||||
tlv.as_ptr(),
|
||||
tlv.len(),
|
||||
out.as_mut_ptr(),
|
||||
&mut out_len,
|
||||
)
|
||||
};
|
||||
let vm_out_raw: VMValue = if let Some((tag, _sz, payload)) =
|
||||
crate::runtime::plugin_ffi_common::decode::tlv_first(&out[..out_len])
|
||||
{
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM←Plugin] tag={} size={} bytes={}", tag, _sz, payload.len());
|
||||
eprintln!(
|
||||
"[VM←Plugin] tag={} size={} bytes={}",
|
||||
tag,
|
||||
_sz,
|
||||
payload.len()
|
||||
);
|
||||
}
|
||||
match tag {
|
||||
1 => crate::runtime::plugin_ffi_common::decode::bool(payload).map(VMValue::Bool).unwrap_or(VMValue::Void),
|
||||
1 => crate::runtime::plugin_ffi_common::decode::bool(payload)
|
||||
.map(VMValue::Bool)
|
||||
.unwrap_or(VMValue::Void),
|
||||
2 => {
|
||||
let v = crate::runtime::plugin_ffi_common::decode::i32(payload).unwrap_or_default();
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") { eprintln!("[VM←Plugin] decode i32={}", v); }
|
||||
let v = crate::runtime::plugin_ffi_common::decode::i32(payload)
|
||||
.unwrap_or_default();
|
||||
if std::env::var("NYASH_DEBUG_PLUGIN").ok().as_deref() == Some("1") {
|
||||
eprintln!("[VM←Plugin] decode i32={}", v);
|
||||
}
|
||||
VMValue::Integer(v as i64)
|
||||
},
|
||||
5 => crate::runtime::plugin_ffi_common::decode::f64(payload).map(VMValue::Float).unwrap_or(VMValue::Void),
|
||||
6 | 7 => VMValue::String(crate::runtime::plugin_ffi_common::decode::string(payload)),
|
||||
}
|
||||
5 => crate::runtime::plugin_ffi_common::decode::f64(payload)
|
||||
.map(VMValue::Float)
|
||||
.unwrap_or(VMValue::Void),
|
||||
6 | 7 => VMValue::String(
|
||||
crate::runtime::plugin_ffi_common::decode::string(payload),
|
||||
),
|
||||
8 => {
|
||||
if let Some(u) = crate::runtime::plugin_ffi_common::decode::u64(payload) {
|
||||
if let Some(arc) = crate::runtime::host_handles::get(u) { VMValue::BoxRef(arc) } else { VMValue::Void }
|
||||
} else { VMValue::Void }
|
||||
if let Some(u) = crate::runtime::plugin_ffi_common::decode::u64(payload)
|
||||
{
|
||||
if let Some(arc) = crate::runtime::host_handles::get(u) {
|
||||
VMValue::BoxRef(arc)
|
||||
} else {
|
||||
VMValue::Void
|
||||
}
|
||||
} else {
|
||||
VMValue::Void
|
||||
}
|
||||
}
|
||||
_ => VMValue::Void,
|
||||
}
|
||||
} else { VMValue::Void };
|
||||
} else {
|
||||
VMValue::Void
|
||||
};
|
||||
// Wrap into Result.Ok when method is declared returns_result
|
||||
let vm_out = {
|
||||
let host = crate::runtime::get_global_plugin_host();
|
||||
@ -119,13 +261,19 @@ impl VM {
|
||||
VMValue::String(s) => Box::new(crate::box_trait::StringBox::new(s)),
|
||||
VMValue::BoxRef(b) => b.share_box(),
|
||||
VMValue::Void => Box::new(crate::box_trait::VoidBox::new()),
|
||||
_ => Box::new(crate::box_trait::StringBox::new(vm_out_raw.to_string()))
|
||||
_ => Box::new(crate::box_trait::StringBox::new(vm_out_raw.to_string())),
|
||||
};
|
||||
let res = crate::boxes::result::NyashResultBox::new_ok(boxed);
|
||||
VMValue::BoxRef(std::sync::Arc::from(Box::new(res) as Box<dyn crate::box_trait::NyashBox>))
|
||||
} else { vm_out_raw }
|
||||
VMValue::BoxRef(std::sync::Arc::from(
|
||||
Box::new(res) as Box<dyn crate::box_trait::NyashBox>
|
||||
))
|
||||
} else {
|
||||
vm_out_raw
|
||||
}
|
||||
};
|
||||
if let Some(dst_id) = dst { self.set_value(dst_id, vm_out); }
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, vm_out);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
}
|
||||
@ -133,28 +281,66 @@ impl VM {
|
||||
if let VMValue::BoxRef(ref bx) = recv {
|
||||
if let Some(s) = extract_string_from_box(bx.as_ref()) {
|
||||
match method {
|
||||
"length" => { if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::Integer(s.len() as i64)); } return Ok(ControlFlow::Continue); }
|
||||
"is_empty" | "isEmpty" => { if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::Bool(s.is_empty())); } return Ok(ControlFlow::Continue); }
|
||||
"length" => {
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::Integer(s.len() as i64));
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
"is_empty" | "isEmpty" => {
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::Bool(s.is_empty()));
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
"charCodeAt" => {
|
||||
let idx_v = if let Some(a0) = args.get(0) { self.get_value(*a0)? } else { VMValue::Integer(0) };
|
||||
let idx = match idx_v { VMValue::Integer(i) => i.max(0) as usize, _ => 0 };
|
||||
let idx_v = if let Some(a0) = args.get(0) {
|
||||
self.get_value(*a0)?
|
||||
} else {
|
||||
VMValue::Integer(0)
|
||||
};
|
||||
let idx = match idx_v {
|
||||
VMValue::Integer(i) => i.max(0) as usize,
|
||||
_ => 0,
|
||||
};
|
||||
let code = s.chars().nth(idx).map(|c| c as u32 as i64).unwrap_or(0);
|
||||
if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::Integer(code)); }
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(dst_id, VMValue::Integer(code));
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
"concat" => {
|
||||
let rhs_v = if let Some(a0) = args.get(0) { self.get_value(*a0)? } else { VMValue::String(String::new()) };
|
||||
let rhs_s = match rhs_v { VMValue::String(ss) => ss, VMValue::BoxRef(br) => extract_string_from_box(br.as_ref()).unwrap_or_else(|| br.to_string_box().value), _ => rhs_v.to_string(), };
|
||||
let rhs_v = if let Some(a0) = args.get(0) {
|
||||
self.get_value(*a0)?
|
||||
} else {
|
||||
VMValue::String(String::new())
|
||||
};
|
||||
let rhs_s = match rhs_v {
|
||||
VMValue::String(ss) => ss,
|
||||
VMValue::BoxRef(br) => extract_string_from_box(br.as_ref())
|
||||
.unwrap_or_else(|| br.to_string_box().value),
|
||||
_ => rhs_v.to_string(),
|
||||
};
|
||||
let mut new_s = s.clone();
|
||||
new_s.push_str(&rhs_s);
|
||||
let out = Box::new(crate::box_trait::StringBox::new(new_s));
|
||||
if let Some(dst_id) = dst { self.set_value(dst_id, VMValue::BoxRef(std::sync::Arc::from(out as Box<dyn crate::box_trait::NyashBox>))); }
|
||||
if let Some(dst_id) = dst {
|
||||
self.set_value(
|
||||
dst_id,
|
||||
VMValue::BoxRef(std::sync::Arc::from(
|
||||
out as Box<dyn crate::box_trait::NyashBox>,
|
||||
)),
|
||||
);
|
||||
}
|
||||
return Ok(ControlFlow::Continue);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(VMError::InvalidInstruction(format!("PluginInvoke requires PluginBox receiver; method={} got {:?}", method, recv)))
|
||||
Err(VMError::InvalidInstruction(format!(
|
||||
"PluginInvoke requires PluginBox receiver; method={} got {:?}",
|
||||
method, recv
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
* the existing VM API surface.
|
||||
*/
|
||||
|
||||
use super::vm::{VMError, VM};
|
||||
use crate::box_trait::NyashBox;
|
||||
use super::vm::{VM, VMError};
|
||||
|
||||
impl VM {
|
||||
/// Unified method dispatch entry. Currently delegates to `call_box_method_impl`.
|
||||
@ -31,4 +31,3 @@ impl VM {
|
||||
self.call_box_method_impl(box_value, method, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
* Typical Callers: VM 実行ループ(ブロック遷移/phi評価)
|
||||
*/
|
||||
|
||||
use super::vm::{VMValue, VMError};
|
||||
use super::vm::{VMError, VMValue};
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -72,7 +72,9 @@ impl PhiHandler {
|
||||
get_value_fn: impl Fn(ValueId) -> Result<VMValue, VMError>,
|
||||
) -> Result<VMValue, VMError> {
|
||||
if inputs.is_empty() {
|
||||
return Err(VMError::InvalidInstruction("Phi node has no inputs".to_string()));
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"Phi node has no inputs".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// previous_blockに基づいて入力を選択
|
||||
@ -125,7 +127,8 @@ impl LoopExecutor {
|
||||
self.phi_handler.record_block_transition(from, to);
|
||||
|
||||
// ループイテレーション数を更新(デバッグ用)
|
||||
if from > to { // 単純なバックエッジ検出
|
||||
if from > to {
|
||||
// 単純なバックエッジ検出
|
||||
*self.iteration_count.entry(to).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
@ -178,39 +181,31 @@ mod tests {
|
||||
|
||||
// テスト用の値
|
||||
let inputs = vec![
|
||||
(BasicBlockId::new(0), ValueId::new(1)), // エントリブロックからの初期値
|
||||
(BasicBlockId::new(2), ValueId::new(2)), // ループボディからの更新値
|
||||
(BasicBlockId::new(0), ValueId::new(1)), // エントリブロックからの初期値
|
||||
(BasicBlockId::new(2), ValueId::new(2)), // ループボディからの更新値
|
||||
];
|
||||
|
||||
// エントリポイントからの実行
|
||||
handler.record_entry();
|
||||
let result = handler.execute_phi(
|
||||
ValueId::new(3),
|
||||
&inputs,
|
||||
|id| {
|
||||
if id == ValueId::new(1) {
|
||||
Ok(VMValue::Integer(0))
|
||||
} else {
|
||||
Ok(VMValue::Integer(10))
|
||||
}
|
||||
let result = handler.execute_phi(ValueId::new(3), &inputs, |id| {
|
||||
if id == ValueId::new(1) {
|
||||
Ok(VMValue::Integer(0))
|
||||
} else {
|
||||
Ok(VMValue::Integer(10))
|
||||
}
|
||||
);
|
||||
});
|
||||
assert_eq!(result.unwrap(), VMValue::Integer(0));
|
||||
|
||||
// ループボディからの実行
|
||||
handler.record_block_transition(BasicBlockId::new(2), BasicBlockId::new(1));
|
||||
handler.phi_cache.clear(); // テスト用にキャッシュクリア
|
||||
let result = handler.execute_phi(
|
||||
ValueId::new(3),
|
||||
&inputs,
|
||||
|id| {
|
||||
if id == ValueId::new(1) {
|
||||
Ok(VMValue::Integer(0))
|
||||
} else {
|
||||
Ok(VMValue::Integer(10))
|
||||
}
|
||||
handler.phi_cache.clear(); // テスト用にキャッシュクリア
|
||||
let result = handler.execute_phi(ValueId::new(3), &inputs, |id| {
|
||||
if id == ValueId::new(1) {
|
||||
Ok(VMValue::Integer(0))
|
||||
} else {
|
||||
Ok(VMValue::Integer(10))
|
||||
}
|
||||
);
|
||||
});
|
||||
assert_eq!(result.unwrap(), VMValue::Integer(10));
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,9 +5,9 @@
|
||||
* phi selection delegation, and small utilities that support the exec loop.
|
||||
*/
|
||||
|
||||
use super::vm::{VM, VMError, VMValue};
|
||||
use super::vm_phi::LoopExecutor;
|
||||
use super::frame::ExecutionFrame;
|
||||
use super::vm::{VMError, VMValue, VM};
|
||||
use super::vm_phi::LoopExecutor;
|
||||
use crate::mir::{BasicBlockId, ValueId};
|
||||
use crate::runtime::NyashRuntime;
|
||||
use crate::scope_tracker::ScopeTracker;
|
||||
@ -30,7 +30,9 @@ impl VM {
|
||||
inputs: &[(BasicBlockId, ValueId)],
|
||||
) -> Result<VMValue, VMError> {
|
||||
if inputs.is_empty() {
|
||||
return Err(VMError::InvalidInstruction("Phi node has no inputs".to_string()));
|
||||
return Err(VMError::InvalidInstruction(
|
||||
"Phi node has no inputs".to_string(),
|
||||
));
|
||||
}
|
||||
let debug_phi = std::env::var("NYASH_VM_DEBUG").ok().as_deref() == Some("1")
|
||||
|| std::env::var("NYASH_VM_DEBUG_PHI").ok().as_deref() == Some("1");
|
||||
@ -50,7 +52,10 @@ impl VM {
|
||||
Err(VMError::InvalidValue(format!("Value {} not set", val_id)))
|
||||
}
|
||||
} else {
|
||||
Err(VMError::InvalidValue(format!("Value {} out of bounds", val_id)))
|
||||
Err(VMError::InvalidValue(format!(
|
||||
"Value {} out of bounds",
|
||||
val_id
|
||||
)))
|
||||
}
|
||||
});
|
||||
if debug_phi {
|
||||
@ -88,7 +93,9 @@ impl VM {
|
||||
boxcall_vtable_funcname: std::collections::HashMap::new(),
|
||||
type_versions: std::collections::HashMap::new(),
|
||||
#[cfg(not(feature = "jit-direct-only"))]
|
||||
jit_manager: Some(crate::jit::manager::JitManager::new(Self::jit_threshold_from_env())),
|
||||
jit_manager: Some(crate::jit::manager::JitManager::new(
|
||||
Self::jit_threshold_from_env(),
|
||||
)),
|
||||
#[cfg(feature = "jit-direct-only")]
|
||||
jit_manager: None,
|
||||
}
|
||||
@ -120,7 +127,9 @@ impl VM {
|
||||
boxcall_vtable_funcname: std::collections::HashMap::new(),
|
||||
type_versions: std::collections::HashMap::new(),
|
||||
#[cfg(not(feature = "jit-direct-only"))]
|
||||
jit_manager: Some(crate::jit::manager::JitManager::new(Self::jit_threshold_from_env())),
|
||||
jit_manager: Some(crate::jit::manager::JitManager::new(
|
||||
Self::jit_threshold_from_env(),
|
||||
)),
|
||||
#[cfg(feature = "jit-direct-only")]
|
||||
jit_manager: None,
|
||||
}
|
||||
@ -136,7 +145,10 @@ impl VM {
|
||||
Err(VMError::InvalidValue(format!("Value {} not set", value_id)))
|
||||
}
|
||||
} else {
|
||||
Err(VMError::InvalidValue(format!("Value {} out of bounds", value_id)))
|
||||
Err(VMError::InvalidValue(format!(
|
||||
"Value {} out of bounds",
|
||||
value_id
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -12,25 +12,45 @@ use super::vm::VM;
|
||||
impl VM {
|
||||
/// Print simple VM execution statistics when enabled via env var
|
||||
pub(super) fn maybe_print_stats(&mut self) {
|
||||
let enabled = std::env::var("NYASH_VM_STATS").ok().map(|v| v != "0").unwrap_or(false);
|
||||
if !enabled { return; }
|
||||
let enabled = std::env::var("NYASH_VM_STATS")
|
||||
.ok()
|
||||
.map(|v| v != "0")
|
||||
.unwrap_or(false);
|
||||
if !enabled {
|
||||
return;
|
||||
}
|
||||
|
||||
let elapsed_ms = self.exec_start.map(|t| t.elapsed().as_secs_f64() * 1000.0).unwrap_or(0.0);
|
||||
let mut items: Vec<(&str, usize)> = self.instr_counter.iter().map(|(k,v)| (*k, *v)).collect();
|
||||
items.sort_by(|a,b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
|
||||
let total: usize = items.iter().map(|(_,v)| *v).sum();
|
||||
let elapsed_ms = self
|
||||
.exec_start
|
||||
.map(|t| t.elapsed().as_secs_f64() * 1000.0)
|
||||
.unwrap_or(0.0);
|
||||
let mut items: Vec<(&str, usize)> =
|
||||
self.instr_counter.iter().map(|(k, v)| (*k, *v)).collect();
|
||||
items.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
|
||||
let total: usize = items.iter().map(|(_, v)| *v).sum();
|
||||
|
||||
let json_enabled = std::env::var("NYASH_VM_STATS_JSON").ok().map(|v| v != "0").unwrap_or(false)
|
||||
|| std::env::var("NYASH_VM_STATS_FORMAT").map(|v| v == "json").unwrap_or(false);
|
||||
let json_enabled = std::env::var("NYASH_VM_STATS_JSON")
|
||||
.ok()
|
||||
.map(|v| v != "0")
|
||||
.unwrap_or(false)
|
||||
|| std::env::var("NYASH_VM_STATS_FORMAT")
|
||||
.map(|v| v == "json")
|
||||
.unwrap_or(false);
|
||||
|
||||
if json_enabled {
|
||||
let counts_obj: std::collections::BTreeMap<&str, usize> = self.instr_counter.iter().map(|(k,v)| (*k, *v)).collect();
|
||||
let top20: Vec<_> = items.iter().take(20).map(|(op,cnt)| {
|
||||
serde_json::json!({ "op": op, "count": cnt })
|
||||
}).collect();
|
||||
let counts_obj: std::collections::BTreeMap<&str, usize> =
|
||||
self.instr_counter.iter().map(|(k, v)| (*k, *v)).collect();
|
||||
let top20: Vec<_> = items
|
||||
.iter()
|
||||
.take(20)
|
||||
.map(|(op, cnt)| serde_json::json!({ "op": op, "count": cnt }))
|
||||
.collect();
|
||||
let now_ms = {
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as u64)
|
||||
.unwrap_or(0)
|
||||
};
|
||||
let payload = serde_json::json!({
|
||||
"total": total,
|
||||
@ -44,7 +64,10 @@ impl VM {
|
||||
Err(_) => println!("{{\"total\":{},\"elapsed_ms\":{:.3}}}", total, elapsed_ms),
|
||||
}
|
||||
} else {
|
||||
println!("\n🧮 VM Stats: {} instructions in {:.3} ms", total, elapsed_ms);
|
||||
println!(
|
||||
"\n🧮 VM Stats: {} instructions in {:.3} ms",
|
||||
total, elapsed_ms
|
||||
);
|
||||
for (k, v) in items.into_iter().take(20) {
|
||||
println!(" {:>10}: {:>8}", k, v);
|
||||
}
|
||||
@ -55,9 +78,14 @@ impl VM {
|
||||
pub(super) fn maybe_print_jit_unified_stats(&self) {
|
||||
// Show when either JIT stats requested or VM stats are on
|
||||
let jit_enabled = std::env::var("NYASH_JIT_STATS").ok().as_deref() == Some("1");
|
||||
let vm_enabled = std::env::var("NYASH_VM_STATS").ok().map(|v| v != "0").unwrap_or(false);
|
||||
let vm_enabled = std::env::var("NYASH_VM_STATS")
|
||||
.ok()
|
||||
.map(|v| v != "0")
|
||||
.unwrap_or(false);
|
||||
let jit_json = std::env::var("NYASH_JIT_STATS_JSON").ok().as_deref() == Some("1");
|
||||
if !jit_enabled && !vm_enabled { return; }
|
||||
if !jit_enabled && !vm_enabled {
|
||||
return;
|
||||
}
|
||||
if let Some(jm) = &self.jit_manager {
|
||||
// Gather basic counters
|
||||
let sites = jm.sites();
|
||||
@ -66,11 +94,19 @@ impl VM {
|
||||
let ok = jm.exec_ok_count();
|
||||
let tr = jm.exec_trap_count();
|
||||
let total_exec = ok + tr;
|
||||
let fb_rate = if total_exec > 0 { (tr as f64) / (total_exec as f64) } else { 0.0 };
|
||||
let fb_rate = if total_exec > 0 {
|
||||
(tr as f64) / (total_exec as f64)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let handles = crate::jit::rt::handles::len();
|
||||
let cfg = crate::jit::config::current();
|
||||
let caps = crate::jit::config::probe_capabilities();
|
||||
let abi_mode = if cfg.native_bool_abi && caps.supports_b1_sig { "b1_bool" } else { "i64_bool" };
|
||||
let abi_mode = if cfg.native_bool_abi && caps.supports_b1_sig {
|
||||
"b1_bool"
|
||||
} else {
|
||||
"i64_bool"
|
||||
};
|
||||
let b1_norm = crate::jit::rt::b1_norm_get();
|
||||
let ret_b1_hints = crate::jit::rt::ret_bool_hint_get();
|
||||
if jit_json {
|
||||
@ -99,11 +135,17 @@ impl VM {
|
||||
})
|
||||
}).collect::<Vec<_>>()
|
||||
});
|
||||
println!("{}", serde_json::to_string_pretty(&payload).unwrap_or_else(|_| String::from("{}")));
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&payload).unwrap_or_else(|_| String::from("{}"))
|
||||
);
|
||||
} else {
|
||||
eprintln!("[JIT] summary: sites={} compiled={} hits={} exec_ok={} trap={} fallback_rate={:.2} handles={}",
|
||||
sites, compiled, total_hits, ok, tr, fb_rate, handles);
|
||||
eprintln!(" abi_mode={} b1_norm_count={} ret_bool_hint_count={}", abi_mode, b1_norm, ret_b1_hints);
|
||||
eprintln!(
|
||||
" abi_mode={} b1_norm_count={} ret_bool_hint_count={}",
|
||||
abi_mode, b1_norm, ret_b1_hints
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
* Kept separate to thin vm.rs and allow reuse across helpers.
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, VoidBox};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox, VoidBox};
|
||||
use crate::mir::ConstValue;
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -94,7 +94,10 @@ impl VMValue {
|
||||
pub fn as_integer(&self) -> Result<i64, VMError> {
|
||||
match self {
|
||||
VMValue::Integer(i) => Ok(*i),
|
||||
_ => Err(VMError::TypeError(format!("Expected integer, got {:?}", self))),
|
||||
_ => Err(VMError::TypeError(format!(
|
||||
"Expected integer, got {:?}",
|
||||
self
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,11 +108,25 @@ impl VMValue {
|
||||
VMValue::Integer(i) => Ok(*i != 0),
|
||||
// Pragmatic coercions for dynamic boxes (preserve legacy semantics)
|
||||
VMValue::BoxRef(b) => {
|
||||
if let Some(bb) = b.as_any().downcast_ref::<BoolBox>() { return Ok(bb.value); }
|
||||
if let Some(ib) = b.as_any().downcast_ref::<IntegerBox>() { return Ok(ib.value != 0); }
|
||||
if let Some(ib) = b.as_any().downcast_ref::<crate::boxes::integer_box::IntegerBox>() { return Ok(ib.value != 0); }
|
||||
if b.as_any().downcast_ref::<VoidBox>().is_some() { return Ok(false); }
|
||||
Err(VMError::TypeError(format!("Expected bool, got BoxRef({})", b.type_name())))
|
||||
if let Some(bb) = b.as_any().downcast_ref::<BoolBox>() {
|
||||
return Ok(bb.value);
|
||||
}
|
||||
if let Some(ib) = b.as_any().downcast_ref::<IntegerBox>() {
|
||||
return Ok(ib.value != 0);
|
||||
}
|
||||
if let Some(ib) = b
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::integer_box::IntegerBox>()
|
||||
{
|
||||
return Ok(ib.value != 0);
|
||||
}
|
||||
if b.as_any().downcast_ref::<VoidBox>().is_some() {
|
||||
return Ok(false);
|
||||
}
|
||||
Err(VMError::TypeError(format!(
|
||||
"Expected bool, got BoxRef({})",
|
||||
b.type_name()
|
||||
)))
|
||||
}
|
||||
VMValue::Void => Ok(false),
|
||||
VMValue::Float(f) => Ok(*f != 0.0),
|
||||
@ -120,7 +137,11 @@ impl VMValue {
|
||||
|
||||
/// Convert from NyashBox to VMValue
|
||||
pub fn from_nyash_box(nyash_box: Box<dyn crate::box_trait::NyashBox>) -> VMValue {
|
||||
if nyash_box.as_any().downcast_ref::<crate::boxes::null_box::NullBox>().is_some() {
|
||||
if nyash_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::null_box::NullBox>()
|
||||
.is_some()
|
||||
{
|
||||
// Treat NullBox as Void in VMValue to align with `null` literal semantics
|
||||
VMValue::Void
|
||||
} else if let Some(int_box) = nyash_box.as_any().downcast_ref::<IntegerBox>() {
|
||||
@ -129,7 +150,10 @@ impl VMValue {
|
||||
VMValue::Bool(bool_box.value)
|
||||
} else if let Some(string_box) = nyash_box.as_any().downcast_ref::<StringBox>() {
|
||||
VMValue::String(string_box.value.clone())
|
||||
} else if let Some(future_box) = nyash_box.as_any().downcast_ref::<crate::boxes::future::FutureBox>() {
|
||||
} else if let Some(future_box) = nyash_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::future::FutureBox>()
|
||||
{
|
||||
VMValue::Future(future_box.clone())
|
||||
} else {
|
||||
VMValue::BoxRef(Arc::from(nyash_box))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -5,8 +5,10 @@
|
||||
* Phase 8.3 PoC2: Reference operations (RefNew/RefGet/RefSet)
|
||||
*/
|
||||
|
||||
use crate::mir::{MirModule, MirFunction, MirInstruction, ConstValue, BinaryOp, CompareOp, ValueId, BasicBlockId};
|
||||
use super::{WasmError, MemoryManager, RuntimeImports};
|
||||
use super::{MemoryManager, RuntimeImports, WasmError};
|
||||
use crate::mir::{
|
||||
BasicBlockId, BinaryOp, CompareOp, ConstValue, MirFunction, MirInstruction, MirModule, ValueId,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// WASM module representation for WAT generation
|
||||
@ -96,7 +98,7 @@ impl WasmCodegen {
|
||||
&mut self,
|
||||
mir_module: MirModule,
|
||||
memory_manager: &MemoryManager,
|
||||
runtime: &RuntimeImports
|
||||
runtime: &RuntimeImports,
|
||||
) -> Result<WasmModule, WasmError> {
|
||||
let mut wasm_module = WasmModule::new();
|
||||
|
||||
@ -110,8 +112,12 @@ impl WasmCodegen {
|
||||
wasm_module.globals.extend(memory_manager.get_globals());
|
||||
|
||||
// Add memory management functions
|
||||
wasm_module.functions.push(memory_manager.get_malloc_function());
|
||||
wasm_module.functions.push(memory_manager.get_generic_box_alloc_function());
|
||||
wasm_module
|
||||
.functions
|
||||
.push(memory_manager.get_malloc_function());
|
||||
wasm_module
|
||||
.functions
|
||||
.push(memory_manager.get_generic_box_alloc_function());
|
||||
|
||||
// Add Box-specific allocation functions for known types
|
||||
for box_type in ["StringBox", "IntegerBox", "BoolBox", "DataBox"] {
|
||||
@ -127,18 +133,26 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
// Add string literal data segments
|
||||
wasm_module.data_segments.extend(self.generate_data_segments());
|
||||
wasm_module
|
||||
.data_segments
|
||||
.extend(self.generate_data_segments());
|
||||
|
||||
// Add main function export if it exists
|
||||
if mir_module.functions.contains_key("main") {
|
||||
wasm_module.exports.push("(export \"main\" (func $main))".to_string());
|
||||
wasm_module
|
||||
.exports
|
||||
.push("(export \"main\" (func $main))".to_string());
|
||||
}
|
||||
|
||||
Ok(wasm_module)
|
||||
}
|
||||
|
||||
/// Generate WASM function from MIR function
|
||||
fn generate_function(&mut self, name: &str, mir_function: MirFunction) -> Result<String, WasmError> {
|
||||
fn generate_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
mir_function: MirFunction,
|
||||
) -> Result<String, WasmError> {
|
||||
// Reset local variable tracking for this function
|
||||
self.current_locals.clear();
|
||||
self.next_local_index = 0;
|
||||
@ -150,10 +164,13 @@ impl WasmCodegen {
|
||||
match mir_function.signature.return_type {
|
||||
crate::mir::MirType::Integer => function_body.push_str(" (result i32)"),
|
||||
crate::mir::MirType::Bool => function_body.push_str(" (result i32)"),
|
||||
crate::mir::MirType::Void => {}, // No return type
|
||||
_ => return Err(WasmError::UnsupportedInstruction(
|
||||
format!("Unsupported return type: {:?}", mir_function.signature.return_type)
|
||||
)),
|
||||
crate::mir::MirType::Void => {} // No return type
|
||||
_ => {
|
||||
return Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Unsupported return type: {:?}",
|
||||
mir_function.signature.return_type
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all local variables needed
|
||||
@ -168,7 +185,8 @@ impl WasmCodegen {
|
||||
function_body.push('\n');
|
||||
|
||||
// Generate body from entry block
|
||||
let entry_instructions = self.generate_basic_block(&mir_function, mir_function.entry_block)?;
|
||||
let entry_instructions =
|
||||
self.generate_basic_block(&mir_function, mir_function.entry_block)?;
|
||||
for instruction in entry_instructions {
|
||||
function_body.push_str(&format!(" {}\n", instruction));
|
||||
}
|
||||
@ -203,9 +221,14 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate WASM instructions for a basic block
|
||||
fn generate_basic_block(&mut self, mir_function: &MirFunction, block_id: BasicBlockId) -> Result<Vec<String>, WasmError> {
|
||||
let block = mir_function.blocks.get(&block_id)
|
||||
.ok_or_else(|| WasmError::CodegenError(format!("Basic block {:?} not found", block_id)))?;
|
||||
fn generate_basic_block(
|
||||
&mut self,
|
||||
mir_function: &MirFunction,
|
||||
block_id: BasicBlockId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let block = mir_function.blocks.get(&block_id).ok_or_else(|| {
|
||||
WasmError::CodegenError(format!("Basic block {:?} not found", block_id))
|
||||
})?;
|
||||
|
||||
let mut instructions = Vec::new();
|
||||
|
||||
@ -225,28 +248,25 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate WASM instructions for a single MIR instruction
|
||||
fn generate_instruction(&mut self, instruction: &MirInstruction) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_instruction(
|
||||
&mut self,
|
||||
instruction: &MirInstruction,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
match instruction {
|
||||
// Phase 8.2 PoC1: Basic operations
|
||||
MirInstruction::Const { dst, value } => {
|
||||
self.generate_const(*dst, value)
|
||||
},
|
||||
MirInstruction::Const { dst, value } => self.generate_const(*dst, value),
|
||||
|
||||
MirInstruction::BinOp { dst, op, lhs, rhs } => {
|
||||
self.generate_binop(*dst, *op, *lhs, *rhs)
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::Compare { dst, op, lhs, rhs } => {
|
||||
self.generate_compare(*dst, *op, *lhs, *rhs)
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::Return { value } => {
|
||||
self.generate_return(value.as_ref())
|
||||
},
|
||||
MirInstruction::Return { value } => self.generate_return(value.as_ref()),
|
||||
|
||||
MirInstruction::Print { value, .. } => {
|
||||
self.generate_print(*value)
|
||||
},
|
||||
MirInstruction::Print { value, .. } => self.generate_print(*value),
|
||||
|
||||
// Phase 8.3 PoC2: Reference operations
|
||||
MirInstruction::RefNew { dst, box_val } => {
|
||||
@ -256,9 +276,13 @@ impl WasmCodegen {
|
||||
format!("local.get ${}", self.get_local_index(*box_val)?),
|
||||
format!("local.set ${}", self.get_local_index(*dst)?),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::RefGet { dst, reference, field: _ } => {
|
||||
MirInstruction::RefGet {
|
||||
dst,
|
||||
reference,
|
||||
field: _,
|
||||
} => {
|
||||
// Load field value from Box through reference
|
||||
// reference contains Box pointer, field is the field name
|
||||
// For now, assume all fields are at offset 12 (first field after header)
|
||||
@ -270,9 +294,13 @@ impl WasmCodegen {
|
||||
"i32.load".to_string(),
|
||||
format!("local.set ${}", self.get_local_index(*dst)?),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::RefSet { reference, field: _, value } => {
|
||||
MirInstruction::RefSet {
|
||||
reference,
|
||||
field: _,
|
||||
value,
|
||||
} => {
|
||||
// Store field value to Box through reference
|
||||
// reference contains Box pointer, field is the field name, value is new value
|
||||
// For now, assume all fields are at offset 12 (first field after header)
|
||||
@ -284,9 +312,13 @@ impl WasmCodegen {
|
||||
format!("local.get ${}", self.get_local_index(*value)?),
|
||||
"i32.store".to_string(),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::NewBox { dst, box_type, args } => {
|
||||
MirInstruction::NewBox {
|
||||
dst,
|
||||
box_type,
|
||||
args,
|
||||
} => {
|
||||
// Create a new Box using the generic allocator
|
||||
match box_type.as_str() {
|
||||
"DataBox" => {
|
||||
@ -308,7 +340,7 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
Ok(instructions)
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
// Use generic allocator for unknown types
|
||||
// This is a fallback - in a real implementation, all Box types should be known
|
||||
@ -320,45 +352,53 @@ impl WasmCodegen {
|
||||
])
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Phase 8.4 PoC3: Extension stubs
|
||||
MirInstruction::WeakNew { dst, box_val } |
|
||||
MirInstruction::FutureNew { dst, value: box_val } => {
|
||||
MirInstruction::WeakNew { dst, box_val }
|
||||
| MirInstruction::FutureNew {
|
||||
dst,
|
||||
value: box_val,
|
||||
} => {
|
||||
// Treat as regular reference for now
|
||||
Ok(vec![
|
||||
format!("local.get ${}", self.get_local_index(*box_val)?),
|
||||
format!("local.set ${}", self.get_local_index(*dst)?),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::WeakLoad { dst, weak_ref } |
|
||||
MirInstruction::Await { dst, future: weak_ref } => {
|
||||
MirInstruction::WeakLoad { dst, weak_ref }
|
||||
| MirInstruction::Await {
|
||||
dst,
|
||||
future: weak_ref,
|
||||
} => {
|
||||
// Always succeed for now
|
||||
Ok(vec![
|
||||
format!("local.get ${}", self.get_local_index(*weak_ref)?),
|
||||
format!("local.set ${}", self.get_local_index(*dst)?),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
MirInstruction::BarrierRead { .. } |
|
||||
MirInstruction::BarrierWrite { .. } |
|
||||
MirInstruction::FutureSet { .. } |
|
||||
MirInstruction::Safepoint => {
|
||||
MirInstruction::BarrierRead { .. }
|
||||
| MirInstruction::BarrierWrite { .. }
|
||||
| MirInstruction::FutureSet { .. }
|
||||
| MirInstruction::Safepoint => {
|
||||
// No-op for now
|
||||
Ok(vec!["nop".to_string()])
|
||||
},
|
||||
}
|
||||
|
||||
// Control Flow Instructions (Critical for loops and conditions)
|
||||
MirInstruction::Jump { target } => {
|
||||
// Unconditional jump to target basic block
|
||||
// Use WASM br instruction to break to the target block
|
||||
Ok(vec![
|
||||
format!("br $block_{}", target.as_u32()),
|
||||
])
|
||||
},
|
||||
Ok(vec![format!("br $block_{}", target.as_u32())])
|
||||
}
|
||||
|
||||
MirInstruction::Branch { condition, then_bb, else_bb } => {
|
||||
MirInstruction::Branch {
|
||||
condition,
|
||||
then_bb,
|
||||
else_bb,
|
||||
} => {
|
||||
// Conditional branch based on condition value
|
||||
// Load condition value and branch accordingly
|
||||
Ok(vec![
|
||||
@ -369,18 +409,27 @@ impl WasmCodegen {
|
||||
// Otherwise, fall through to else_bb
|
||||
format!("br $block_{}", else_bb.as_u32()),
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
// Phase 9.7: External Function Calls
|
||||
MirInstruction::ExternCall { dst, iface_name, method_name, args, effects: _ } => {
|
||||
MirInstruction::ExternCall {
|
||||
dst,
|
||||
iface_name,
|
||||
method_name,
|
||||
args,
|
||||
effects: _,
|
||||
} => {
|
||||
// Generate call to external function import
|
||||
let call_target = match (iface_name.as_str(), method_name.as_str()) {
|
||||
("env.console", "log") => "console_log",
|
||||
("env.canvas", "fillRect") => "canvas_fillRect",
|
||||
("env.canvas", "fillText") => "canvas_fillText",
|
||||
_ => return Err(WasmError::UnsupportedInstruction(
|
||||
format!("Unsupported extern call: {}.{}", iface_name, method_name)
|
||||
)),
|
||||
_ => {
|
||||
return Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Unsupported extern call: {}.{}",
|
||||
iface_name, method_name
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
let mut instructions = Vec::new();
|
||||
@ -401,22 +450,32 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
Ok(instructions)
|
||||
},
|
||||
}
|
||||
|
||||
// BoxCall codegen - critical Box method calls
|
||||
MirInstruction::BoxCall { dst, box_val, method, args, effects: _ , .. } => {
|
||||
self.generate_box_call(*dst, *box_val, method, args)
|
||||
},
|
||||
MirInstruction::BoxCall {
|
||||
dst,
|
||||
box_val,
|
||||
method,
|
||||
args,
|
||||
effects: _,
|
||||
..
|
||||
} => self.generate_box_call(*dst, *box_val, method, args),
|
||||
|
||||
// Unsupported instructions
|
||||
_ => Err(WasmError::UnsupportedInstruction(
|
||||
format!("Instruction not yet supported: {:?}", instruction)
|
||||
)),
|
||||
_ => Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Instruction not yet supported: {:?}",
|
||||
instruction
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate constant loading
|
||||
fn generate_const(&mut self, dst: ValueId, value: &ConstValue) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_const(
|
||||
&mut self,
|
||||
dst: ValueId,
|
||||
value: &ConstValue,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let const_instruction = match value {
|
||||
ConstValue::Integer(n) => format!("i32.const {}", n),
|
||||
ConstValue::Bool(b) => format!("i32.const {}", if *b { 1 } else { 0 }),
|
||||
@ -429,10 +488,13 @@ impl WasmCodegen {
|
||||
// Generate code to allocate a StringBox and return its pointer
|
||||
// This is more complex and will need StringBox allocation
|
||||
return self.generate_string_box_const(dst, data_offset, string_len);
|
||||
},
|
||||
_ => return Err(WasmError::UnsupportedInstruction(
|
||||
format!("Unsupported constant type: {:?}", value)
|
||||
)),
|
||||
}
|
||||
_ => {
|
||||
return Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Unsupported constant type: {:?}",
|
||||
value
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(vec![
|
||||
@ -442,7 +504,13 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate binary operation
|
||||
fn generate_binop(&self, dst: ValueId, op: BinaryOp, lhs: ValueId, rhs: ValueId) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_binop(
|
||||
&self,
|
||||
dst: ValueId,
|
||||
op: BinaryOp,
|
||||
lhs: ValueId,
|
||||
rhs: ValueId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let wasm_op = match op {
|
||||
BinaryOp::Add => "i32.add",
|
||||
BinaryOp::Sub => "i32.sub",
|
||||
@ -450,9 +518,12 @@ impl WasmCodegen {
|
||||
BinaryOp::Div => "i32.div_s",
|
||||
BinaryOp::And => "i32.and",
|
||||
BinaryOp::Or => "i32.or",
|
||||
_ => return Err(WasmError::UnsupportedInstruction(
|
||||
format!("Unsupported binary operation: {:?}", op)
|
||||
)),
|
||||
_ => {
|
||||
return Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Unsupported binary operation: {:?}",
|
||||
op
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(vec![
|
||||
@ -464,7 +535,13 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate comparison operation
|
||||
fn generate_compare(&self, dst: ValueId, op: CompareOp, lhs: ValueId, rhs: ValueId) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_compare(
|
||||
&self,
|
||||
dst: ValueId,
|
||||
op: CompareOp,
|
||||
lhs: ValueId,
|
||||
rhs: ValueId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let wasm_op = match op {
|
||||
CompareOp::Eq => "i32.eq",
|
||||
CompareOp::Ne => "i32.ne",
|
||||
@ -495,7 +572,12 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate StringBox allocation for a string constant
|
||||
fn generate_string_box_const(&self, dst: ValueId, data_offset: u32, string_len: u32) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_string_box_const(
|
||||
&self,
|
||||
dst: ValueId,
|
||||
data_offset: u32,
|
||||
string_len: u32,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
// Allocate a StringBox using the StringBox allocator
|
||||
// StringBox layout: [type_id:0x1001][ref_count:1][field_count:2][data_ptr:offset][length:len]
|
||||
Ok(vec![
|
||||
@ -503,7 +585,6 @@ impl WasmCodegen {
|
||||
"call $alloc_stringbox".to_string(),
|
||||
// Store the result (StringBox pointer) in local variable
|
||||
format!("local.set ${}", self.get_local_index(dst)?),
|
||||
|
||||
// Initialize StringBox fields
|
||||
// Get StringBox pointer back
|
||||
format!("local.get ${}", self.get_local_index(dst)?),
|
||||
@ -512,7 +593,6 @@ impl WasmCodegen {
|
||||
"i32.add".to_string(),
|
||||
format!("i32.const {}", data_offset),
|
||||
"i32.store".to_string(),
|
||||
|
||||
// Get StringBox pointer again
|
||||
format!("local.get ${}", self.get_local_index(dst)?),
|
||||
// Set length field (offset 16 from StringBox pointer)
|
||||
@ -553,15 +633,12 @@ impl WasmCodegen {
|
||||
let string_bytes = string.as_bytes();
|
||||
|
||||
// Convert to hex-escaped string for WAT
|
||||
let byte_string = string_bytes.iter()
|
||||
let byte_string = string_bytes
|
||||
.iter()
|
||||
.map(|b| format!("\\{:02x}", b))
|
||||
.collect::<String>();
|
||||
|
||||
let data_segment = format!(
|
||||
"(data (i32.const {}) \"{}\")",
|
||||
offset,
|
||||
byte_string
|
||||
);
|
||||
let data_segment = format!("(data (i32.const {}) \"{}\")", offset, byte_string);
|
||||
|
||||
segments.push(data_segment);
|
||||
}
|
||||
@ -571,34 +648,53 @@ impl WasmCodegen {
|
||||
|
||||
/// Get WASM local variable index for ValueId
|
||||
fn get_local_index(&self, value_id: ValueId) -> Result<u32, WasmError> {
|
||||
self.current_locals.get(&value_id)
|
||||
.copied()
|
||||
.ok_or_else(|| WasmError::CodegenError(format!("Local variable not found for ValueId: {:?}", value_id)))
|
||||
self.current_locals.get(&value_id).copied().ok_or_else(|| {
|
||||
WasmError::CodegenError(format!(
|
||||
"Local variable not found for ValueId: {:?}",
|
||||
value_id
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate BoxCall method invocation
|
||||
/// Implements critical Box methods: toString, print, equals, clone
|
||||
fn generate_box_call(&mut self, dst: Option<ValueId>, box_val: ValueId, method: &str, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_box_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
method: &str,
|
||||
args: &[ValueId],
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
match method {
|
||||
"toString" => self.generate_to_string_call(dst, box_val),
|
||||
"print" => self.generate_print_call(dst, box_val),
|
||||
"equals" => self.generate_equals_call(dst, box_val, args),
|
||||
"clone" => self.generate_clone_call(dst, box_val),
|
||||
"log" => self.generate_log_call(dst, box_val, args),
|
||||
_ => Err(WasmError::UnsupportedInstruction(
|
||||
format!("Unsupported BoxCall method: {}", method)
|
||||
))
|
||||
_ => Err(WasmError::UnsupportedInstruction(format!(
|
||||
"Unsupported BoxCall method: {}",
|
||||
method
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate toString() method call - Box → String conversion
|
||||
fn generate_to_string_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_to_string_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let Some(dst) = dst else {
|
||||
return Err(WasmError::CodegenError("toString() requires destination".to_string()));
|
||||
return Err(WasmError::CodegenError(
|
||||
"toString() requires destination".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
Ok(vec![
|
||||
format!(";; toString() implementation for ValueId({})", box_val.as_u32()),
|
||||
format!(
|
||||
";; toString() implementation for ValueId({})",
|
||||
box_val.as_u32()
|
||||
),
|
||||
format!("local.get ${}", self.get_local_index(box_val)?),
|
||||
"call $box_to_string".to_string(),
|
||||
format!("local.set ${}", self.get_local_index(dst)?),
|
||||
@ -606,9 +702,16 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate print() method call - Basic output
|
||||
fn generate_print_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_print_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let mut instructions = vec![
|
||||
format!(";; print() implementation for ValueId({})", box_val.as_u32()),
|
||||
format!(
|
||||
";; print() implementation for ValueId({})",
|
||||
box_val.as_u32()
|
||||
),
|
||||
format!("local.get ${}", self.get_local_index(box_val)?),
|
||||
"call $box_print".to_string(),
|
||||
];
|
||||
@ -625,19 +728,31 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate equals() method call - Box comparison
|
||||
fn generate_equals_call(&mut self, dst: Option<ValueId>, box_val: ValueId, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_equals_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
args: &[ValueId],
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let Some(dst) = dst else {
|
||||
return Err(WasmError::CodegenError("equals() requires destination".to_string()));
|
||||
return Err(WasmError::CodegenError(
|
||||
"equals() requires destination".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
if args.len() != 1 {
|
||||
return Err(WasmError::CodegenError(
|
||||
format!("equals() expects 1 argument, got {}", args.len())
|
||||
));
|
||||
return Err(WasmError::CodegenError(format!(
|
||||
"equals() expects 1 argument, got {}",
|
||||
args.len()
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(vec![
|
||||
format!(";; equals() implementation for ValueId({}) == ValueId({})", box_val.as_u32(), args[0].as_u32()),
|
||||
format!(
|
||||
";; equals() implementation for ValueId({}) == ValueId({})",
|
||||
box_val.as_u32(),
|
||||
args[0].as_u32()
|
||||
),
|
||||
format!("local.get ${}", self.get_local_index(box_val)?),
|
||||
format!("local.get ${}", self.get_local_index(args[0])?),
|
||||
"call $box_equals".to_string(),
|
||||
@ -646,13 +761,22 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate clone() method call - Box duplication
|
||||
fn generate_clone_call(&mut self, dst: Option<ValueId>, box_val: ValueId) -> Result<Vec<String>, WasmError> {
|
||||
fn generate_clone_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let Some(dst) = dst else {
|
||||
return Err(WasmError::CodegenError("clone() requires destination".to_string()));
|
||||
return Err(WasmError::CodegenError(
|
||||
"clone() requires destination".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
Ok(vec![
|
||||
format!(";; clone() implementation for ValueId({})", box_val.as_u32()),
|
||||
format!(
|
||||
";; clone() implementation for ValueId({})",
|
||||
box_val.as_u32()
|
||||
),
|
||||
format!("local.get ${}", self.get_local_index(box_val)?),
|
||||
"call $box_clone".to_string(),
|
||||
format!("local.set ${}", self.get_local_index(dst)?),
|
||||
@ -660,10 +784,16 @@ impl WasmCodegen {
|
||||
}
|
||||
|
||||
/// Generate log() method call - Console logging (ConsoleBox.log)
|
||||
fn generate_log_call(&mut self, dst: Option<ValueId>, box_val: ValueId, args: &[ValueId]) -> Result<Vec<String>, WasmError> {
|
||||
let mut instructions = vec![
|
||||
format!(";; log() implementation for ValueId({})", box_val.as_u32()),
|
||||
];
|
||||
fn generate_log_call(
|
||||
&mut self,
|
||||
dst: Option<ValueId>,
|
||||
box_val: ValueId,
|
||||
args: &[ValueId],
|
||||
) -> Result<Vec<String>, WasmError> {
|
||||
let mut instructions = vec![format!(
|
||||
";; log() implementation for ValueId({})",
|
||||
box_val.as_u32()
|
||||
)];
|
||||
|
||||
// Load box_val (ConsoleBox instance)
|
||||
instructions.push(format!("local.get ${}", self.get_local_index(box_val)?));
|
||||
@ -691,13 +821,18 @@ impl WasmCodegen {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mir::{MirModule, MirFunction, FunctionSignature, MirType, EffectMask, BasicBlock, BasicBlockId, ValueId};
|
||||
use crate::mir::{
|
||||
BasicBlock, BasicBlockId, EffectMask, FunctionSignature, MirFunction, MirModule, MirType,
|
||||
ValueId,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_wasm_module_wat_generation() {
|
||||
let mut module = WasmModule::new();
|
||||
module.memory = "(memory (export \"memory\") 1)".to_string();
|
||||
module.imports.push("(import \"env\" \"print\" (func $print (param i32)))".to_string());
|
||||
module
|
||||
.imports
|
||||
.push("(import \"env\" \"print\" (func $print (param i32)))".to_string());
|
||||
|
||||
let wat = module.to_wat();
|
||||
assert!(wat.contains("(module"));
|
||||
|
||||
@ -24,7 +24,7 @@ impl BoxLayout {
|
||||
"IntegerBox" => 0x1002,
|
||||
"BoolBox" => 0x1003,
|
||||
"ArrayBox" => 0x1004,
|
||||
"DataBox" => 0x1005, // For testing
|
||||
"DataBox" => 0x1005, // For testing
|
||||
_ => {
|
||||
// Generate ID from hash for custom types
|
||||
type_name.chars().map(|c| c as u32).sum::<u32>() % 65536 + 0x2000
|
||||
@ -72,7 +72,10 @@ impl MemoryManager {
|
||||
/// Register standard built-in Box types
|
||||
fn register_standard_box_types(&mut self) {
|
||||
// StringBox: [type_id][ref_count][field_count][ptr_to_chars][length]
|
||||
self.register_box_type("StringBox".to_string(), vec!["data_ptr".to_string(), "length".to_string()]);
|
||||
self.register_box_type(
|
||||
"StringBox".to_string(),
|
||||
vec!["data_ptr".to_string(), "length".to_string()],
|
||||
);
|
||||
|
||||
// IntegerBox: [type_id][ref_count][field_count][value]
|
||||
self.register_box_type("IntegerBox".to_string(), vec!["value".to_string()]);
|
||||
@ -102,9 +105,10 @@ impl MemoryManager {
|
||||
|
||||
/// Generate WASM globals for heap management
|
||||
pub fn get_globals(&self) -> Vec<String> {
|
||||
vec![
|
||||
format!("(global $heap_ptr (mut i32) (i32.const {}))", self.heap_start),
|
||||
]
|
||||
vec![format!(
|
||||
"(global $heap_ptr (mut i32) (i32.const {}))",
|
||||
self.heap_start
|
||||
)]
|
||||
}
|
||||
|
||||
/// Generate heap allocation function with 4-byte alignment
|
||||
@ -140,7 +144,8 @@ impl MemoryManager {
|
||||
|
||||
/// Generate Box allocation function for specific type
|
||||
pub fn get_box_alloc_function(&self, type_name: &str) -> Result<String, WasmError> {
|
||||
let layout = self.get_box_layout(type_name)
|
||||
let layout = self
|
||||
.get_box_layout(type_name)
|
||||
.ok_or_else(|| WasmError::MemoryError(format!("Unknown box type: {}", type_name)))?;
|
||||
|
||||
Ok(format!(
|
||||
@ -182,12 +187,18 @@ impl MemoryManager {
|
||||
}
|
||||
|
||||
/// Generate field getter function
|
||||
pub fn get_field_get_function(&self, type_name: &str, field_name: &str) -> Result<String, WasmError> {
|
||||
let layout = self.get_box_layout(type_name)
|
||||
pub fn get_field_get_function(
|
||||
&self,
|
||||
type_name: &str,
|
||||
field_name: &str,
|
||||
) -> Result<String, WasmError> {
|
||||
let layout = self
|
||||
.get_box_layout(type_name)
|
||||
.ok_or_else(|| WasmError::MemoryError(format!("Unknown box type: {}", type_name)))?;
|
||||
|
||||
let offset = layout.get_field_offset(field_name)
|
||||
.ok_or_else(|| WasmError::MemoryError(format!("Unknown field: {}.{}", type_name, field_name)))?;
|
||||
let offset = layout.get_field_offset(field_name).ok_or_else(|| {
|
||||
WasmError::MemoryError(format!("Unknown field: {}.{}", type_name, field_name))
|
||||
})?;
|
||||
|
||||
Ok(format!(
|
||||
r#"(func $get_{}_{} (param $box_ptr i32) (result i32)
|
||||
@ -215,12 +226,18 @@ impl MemoryManager {
|
||||
}
|
||||
|
||||
/// Generate field setter function
|
||||
pub fn get_field_set_function(&self, type_name: &str, field_name: &str) -> Result<String, WasmError> {
|
||||
let layout = self.get_box_layout(type_name)
|
||||
pub fn get_field_set_function(
|
||||
&self,
|
||||
type_name: &str,
|
||||
field_name: &str,
|
||||
) -> Result<String, WasmError> {
|
||||
let layout = self
|
||||
.get_box_layout(type_name)
|
||||
.ok_or_else(|| WasmError::MemoryError(format!("Unknown box type: {}", type_name)))?;
|
||||
|
||||
let offset = layout.get_field_offset(field_name)
|
||||
.ok_or_else(|| WasmError::MemoryError(format!("Unknown field: {}.{}", type_name, field_name)))?;
|
||||
let offset = layout.get_field_offset(field_name).ok_or_else(|| {
|
||||
WasmError::MemoryError(format!("Unknown field: {}.{}", type_name, field_name))
|
||||
})?;
|
||||
|
||||
Ok(format!(
|
||||
r#"(func $set_{}_{} (param $box_ptr i32) (param $value i32)
|
||||
@ -362,7 +379,10 @@ mod tests {
|
||||
#[test]
|
||||
fn test_memory_manager_registration() {
|
||||
let mut manager = MemoryManager::new();
|
||||
manager.register_box_type("CustomBox".to_string(), vec!["x".to_string(), "y".to_string()]);
|
||||
manager.register_box_type(
|
||||
"CustomBox".to_string(),
|
||||
vec!["x".to_string(), "y".to_string()],
|
||||
);
|
||||
|
||||
let layout = manager.get_box_layout("CustomBox").unwrap();
|
||||
assert_eq!(layout.field_offsets.len(), 2);
|
||||
|
||||
@ -11,7 +11,7 @@ mod runtime;
|
||||
// mod executor; // TODO: Fix WASM executor build errors
|
||||
|
||||
pub use codegen::{WasmCodegen, WasmModule};
|
||||
pub use memory::{MemoryManager, BoxLayout};
|
||||
pub use memory::{BoxLayout, MemoryManager};
|
||||
pub use runtime::RuntimeImports;
|
||||
// pub use executor::WasmExecutor; // TODO: Fix WASM executor build errors
|
||||
|
||||
@ -77,7 +77,7 @@ impl WasmBackend {
|
||||
if !wat_source.is_ascii() {
|
||||
eprintln!("❌ WAT source contains non-ASCII characters");
|
||||
return Err(WasmError::WasmValidationError(
|
||||
"WAT source contains non-ASCII characters".to_string()
|
||||
"WAT source contains non-ASCII characters".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@ -85,27 +85,32 @@ impl WasmBackend {
|
||||
|
||||
// Convert to bytes as required by wabt::wat2wasm
|
||||
eprintln!("🔄 Converting WAT to WASM bytes...");
|
||||
let wasm_bytes = wabt::wat2wasm(wat_source.as_bytes())
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ wabt::wat2wasm failed: {}", e);
|
||||
WasmError::WasmValidationError(format!("WAT to WASM conversion failed: {}", e))
|
||||
})?;
|
||||
let wasm_bytes = wabt::wat2wasm(wat_source.as_bytes()).map_err(|e| {
|
||||
eprintln!("❌ wabt::wat2wasm failed: {}", e);
|
||||
WasmError::WasmValidationError(format!("WAT to WASM conversion failed: {}", e))
|
||||
})?;
|
||||
|
||||
eprintln!("✅ WASM conversion successful, {} bytes generated", wasm_bytes.len());
|
||||
eprintln!(
|
||||
"✅ WASM conversion successful, {} bytes generated",
|
||||
wasm_bytes.len()
|
||||
);
|
||||
Ok(wasm_bytes)
|
||||
}
|
||||
|
||||
/// Compile MIR module to WAT text format (for debugging)
|
||||
pub fn compile_to_wat(&mut self, mir_module: MirModule) -> Result<String, WasmError> {
|
||||
let wasm_module = self.codegen.generate_module(mir_module, &self.memory_manager, &self.runtime)?;
|
||||
let wasm_module =
|
||||
self.codegen
|
||||
.generate_module(mir_module, &self.memory_manager, &self.runtime)?;
|
||||
Ok(wasm_module.to_wat())
|
||||
}
|
||||
|
||||
/// Execute WASM bytes using wasmtime (for testing)
|
||||
pub fn execute_wasm(&self, wasm_bytes: &[u8]) -> Result<i32, WasmError> {
|
||||
let engine = wasmtime::Engine::default();
|
||||
let module = wasmtime::Module::new(&engine, wasm_bytes)
|
||||
.map_err(|e| WasmError::WasmValidationError(format!("Module creation failed: {}", e)))?;
|
||||
let module = wasmtime::Module::new(&engine, wasm_bytes).map_err(|e| {
|
||||
WasmError::WasmValidationError(format!("Module creation failed: {}", e))
|
||||
})?;
|
||||
|
||||
let mut store = wasmtime::Store::new(&engine, ());
|
||||
|
||||
@ -115,38 +120,55 @@ impl WasmBackend {
|
||||
});
|
||||
|
||||
// Create print_str function import for string debugging
|
||||
let print_str_func = wasmtime::Func::wrap(&mut store, |mut caller: wasmtime::Caller<'_, ()>, ptr: i32, len: i32| -> Result<(), wasmtime::Error> {
|
||||
let memory = caller.get_export("memory")
|
||||
.and_then(|export| export.into_memory())
|
||||
.ok_or_else(|| wasmtime::Error::msg("Memory export not found"))?;
|
||||
let print_str_func = wasmtime::Func::wrap(
|
||||
&mut store,
|
||||
|mut caller: wasmtime::Caller<'_, ()>,
|
||||
ptr: i32,
|
||||
len: i32|
|
||||
-> Result<(), wasmtime::Error> {
|
||||
let memory = caller
|
||||
.get_export("memory")
|
||||
.and_then(|export| export.into_memory())
|
||||
.ok_or_else(|| wasmtime::Error::msg("Memory export not found"))?;
|
||||
|
||||
let data = memory.data(&caller);
|
||||
let start = ptr as usize;
|
||||
let end = start + len as usize;
|
||||
let data = memory.data(&caller);
|
||||
let start = ptr as usize;
|
||||
let end = start + len as usize;
|
||||
|
||||
if end <= data.len() {
|
||||
let bytes = &data[start..end];
|
||||
if let Ok(s) = std::str::from_utf8(bytes) {
|
||||
println!("String: {}", s);
|
||||
if end <= data.len() {
|
||||
let bytes = &data[start..end];
|
||||
if let Ok(s) = std::str::from_utf8(bytes) {
|
||||
println!("String: {}", s);
|
||||
} else {
|
||||
println!("Invalid UTF-8 bytes: {:?}", bytes);
|
||||
}
|
||||
} else {
|
||||
println!("Invalid UTF-8 bytes: {:?}", bytes);
|
||||
println!(
|
||||
"String out of bounds: ptr={}, len={}, memory_size={}",
|
||||
ptr,
|
||||
len,
|
||||
data.len()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
println!("String out of bounds: ptr={}, len={}, memory_size={}", ptr, len, data.len());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
||||
let imports = [print_func.into(), print_str_func.into()];
|
||||
let instance = wasmtime::Instance::new(&mut store, &module, &imports)
|
||||
.map_err(|e| WasmError::WasmValidationError(format!("Instance creation failed: {}", e)))?;
|
||||
let instance = wasmtime::Instance::new(&mut store, &module, &imports).map_err(|e| {
|
||||
WasmError::WasmValidationError(format!("Instance creation failed: {}", e))
|
||||
})?;
|
||||
|
||||
// Call main function
|
||||
let main_func = instance.get_typed_func::<(), i32>(&mut store, "main")
|
||||
.map_err(|e| WasmError::WasmValidationError(format!("Main function not found: {}", e)))?;
|
||||
let main_func = instance
|
||||
.get_typed_func::<(), i32>(&mut store, "main")
|
||||
.map_err(|e| {
|
||||
WasmError::WasmValidationError(format!("Main function not found: {}", e))
|
||||
})?;
|
||||
|
||||
let result = main_func.call(&mut store, ())
|
||||
let result = main_func
|
||||
.call(&mut store, ())
|
||||
.map_err(|e| WasmError::WasmValidationError(format!("Execution failed: {}", e)))?;
|
||||
|
||||
Ok(result)
|
||||
|
||||
@ -67,9 +67,14 @@ impl RuntimeImports {
|
||||
module: "env".to_string(),
|
||||
name: "canvas_fillRect".to_string(),
|
||||
params: vec![
|
||||
"i32".to_string(), "i32".to_string(), // canvas_id (ptr, len)
|
||||
"i32".to_string(), "i32".to_string(), "i32".to_string(), "i32".to_string(), // x, y, w, h
|
||||
"i32".to_string(), "i32".to_string(), // color (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // canvas_id (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(),
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // x, y, w, h
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // color (ptr, len)
|
||||
],
|
||||
result: None,
|
||||
});
|
||||
@ -80,11 +85,16 @@ impl RuntimeImports {
|
||||
module: "env".to_string(),
|
||||
name: "canvas_fillText".to_string(),
|
||||
params: vec![
|
||||
"i32".to_string(), "i32".to_string(), // canvas_id (ptr, len)
|
||||
"i32".to_string(), "i32".to_string(), // text (ptr, len)
|
||||
"i32".to_string(), "i32".to_string(), // x, y
|
||||
"i32".to_string(), "i32".to_string(), // font (ptr, len)
|
||||
"i32".to_string(), "i32".to_string(), // color (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // canvas_id (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // text (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // x, y
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // font (ptr, len)
|
||||
"i32".to_string(),
|
||||
"i32".to_string(), // color (ptr, len)
|
||||
],
|
||||
result: None,
|
||||
});
|
||||
@ -112,7 +122,7 @@ impl RuntimeImports {
|
||||
module: "env".to_string(),
|
||||
name: "box_equals".to_string(),
|
||||
params: vec!["i32".to_string(), "i32".to_string()], // box1_ptr, box2_ptr
|
||||
result: Some("i32".to_string()), // bool result
|
||||
result: Some("i32".to_string()), // bool result
|
||||
});
|
||||
|
||||
// box_clone - Clone a Box
|
||||
@ -129,32 +139,37 @@ impl RuntimeImports {
|
||||
|
||||
/// Get all import declarations in WAT format
|
||||
pub fn get_imports(&self) -> Vec<String> {
|
||||
self.imports.iter().map(|import| {
|
||||
let params = if import.params.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("(param {})", import.params.join(" "))
|
||||
};
|
||||
self.imports
|
||||
.iter()
|
||||
.map(|import| {
|
||||
let params = if import.params.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("(param {})", import.params.join(" "))
|
||||
};
|
||||
|
||||
let result = if let Some(ref result_type) = import.result {
|
||||
format!("(result {})", result_type)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let result = if let Some(ref result_type) = import.result {
|
||||
format!("(result {})", result_type)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
format!(
|
||||
"(import \"{}\" \"{}\" (func ${} {} {}))",
|
||||
import.module,
|
||||
import.name,
|
||||
import.name,
|
||||
params,
|
||||
result
|
||||
)
|
||||
}).collect()
|
||||
format!(
|
||||
"(import \"{}\" \"{}\" (func ${} {} {}))",
|
||||
import.module, import.name, import.name, params, result
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Add custom import function
|
||||
pub fn add_import(&mut self, module: String, name: String, params: Vec<String>, result: Option<String>) {
|
||||
pub fn add_import(
|
||||
&mut self,
|
||||
module: String,
|
||||
name: String,
|
||||
params: Vec<String>,
|
||||
result: Option<String>,
|
||||
) {
|
||||
self.imports.push(ImportFunction {
|
||||
module,
|
||||
name,
|
||||
@ -179,9 +194,13 @@ impl RuntimeImports {
|
||||
js.push_str("const importObject = {\n");
|
||||
|
||||
// Group by module
|
||||
let mut modules: std::collections::HashMap<String, Vec<&ImportFunction>> = std::collections::HashMap::new();
|
||||
let mut modules: std::collections::HashMap<String, Vec<&ImportFunction>> =
|
||||
std::collections::HashMap::new();
|
||||
for import in &self.imports {
|
||||
modules.entry(import.module.clone()).or_default().push(import);
|
||||
modules
|
||||
.entry(import.module.clone())
|
||||
.or_default()
|
||||
.push(import);
|
||||
}
|
||||
|
||||
for (module_name, functions) in modules {
|
||||
@ -191,21 +210,21 @@ impl RuntimeImports {
|
||||
match function.name.as_str() {
|
||||
"print" => {
|
||||
js.push_str(" print: (value) => console.log(value),\n");
|
||||
},
|
||||
}
|
||||
"print_str" => {
|
||||
js.push_str(" print_str: (ptr, len) => {\n");
|
||||
js.push_str(" const memory = instance.exports.memory;\n");
|
||||
js.push_str(" const str = new TextDecoder().decode(new Uint8Array(memory.buffer, ptr, len));\n");
|
||||
js.push_str(" console.log(str);\n");
|
||||
js.push_str(" },\n");
|
||||
},
|
||||
}
|
||||
"console_log" => {
|
||||
js.push_str(" console_log: (ptr, len) => {\n");
|
||||
js.push_str(" const memory = instance.exports.memory;\n");
|
||||
js.push_str(" const str = new TextDecoder().decode(new Uint8Array(memory.buffer, ptr, len));\n");
|
||||
js.push_str(" console.log(str);\n");
|
||||
js.push_str(" },\n");
|
||||
},
|
||||
}
|
||||
"canvas_fillRect" => {
|
||||
js.push_str(" canvas_fillRect: (canvasIdPtr, canvasIdLen, x, y, w, h, colorPtr, colorLen) => {\n");
|
||||
js.push_str(" const memory = instance.exports.memory;\n");
|
||||
@ -218,7 +237,7 @@ impl RuntimeImports {
|
||||
js.push_str(" ctx.fillRect(x, y, w, h);\n");
|
||||
js.push_str(" }\n");
|
||||
js.push_str(" },\n");
|
||||
},
|
||||
}
|
||||
"canvas_fillText" => {
|
||||
js.push_str(" canvas_fillText: (canvasIdPtr, canvasIdLen, textPtr, textLen, x, y, fontPtr, fontLen, colorPtr, colorLen) => {\n");
|
||||
js.push_str(" const memory = instance.exports.memory;\n");
|
||||
@ -234,10 +253,12 @@ impl RuntimeImports {
|
||||
js.push_str(" ctx.fillText(text, x, y);\n");
|
||||
js.push_str(" }\n");
|
||||
js.push_str(" },\n");
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
js.push_str(&format!(" {}: () => {{ throw new Error('Not implemented: {}'); }},\n",
|
||||
function.name, function.name));
|
||||
js.push_str(&format!(
|
||||
" {}: () => {{ throw new Error('Not implemented: {}'); }},\n",
|
||||
function.name, function.name
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -258,21 +279,26 @@ impl RuntimeImports {
|
||||
for import in &self.imports {
|
||||
match import.name.as_str() {
|
||||
"print" => {
|
||||
rust_code.push_str(r#"
|
||||
rust_code.push_str(
|
||||
r#"
|
||||
let print_func = wasmtime::Func::wrap(&mut store, |value: i32| {
|
||||
println!("{}", value);
|
||||
});
|
||||
imports.push(print_func.into());
|
||||
"#);
|
||||
},
|
||||
"#,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
rust_code.push_str(&format!(r#"
|
||||
rust_code.push_str(&format!(
|
||||
r#"
|
||||
// TODO: Implement {} import
|
||||
let {}_func = wasmtime::Func::wrap(&mut store, || {{
|
||||
panic!("Not implemented: {}")
|
||||
}});
|
||||
imports.push({}_func.into());
|
||||
"#, import.name, import.name, import.name, import.name));
|
||||
"#,
|
||||
import.name, import.name, import.name, import.name
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -310,7 +336,7 @@ mod tests {
|
||||
"custom".to_string(),
|
||||
"test_func".to_string(),
|
||||
vec!["i32".to_string(), "i32".to_string()],
|
||||
Some("i32".to_string())
|
||||
Some("i32".to_string()),
|
||||
);
|
||||
|
||||
assert!(runtime.has_import("test_func"));
|
||||
|
||||
@ -13,14 +13,20 @@ use crate::box_trait::{NyashBox, StringBox};
|
||||
use crate::boxes::ConsoleBox;
|
||||
|
||||
/// WASM v2エントリポイント: 統一vtableディスパッチの最小テスト
|
||||
pub fn compile_and_execute_v2(_module: &crate::mir::MirModule, _temp_name: &str) -> Result<Box<dyn crate::box_trait::NyashBox>, String> {
|
||||
pub fn compile_and_execute_v2(
|
||||
_module: &crate::mir::MirModule,
|
||||
_temp_name: &str,
|
||||
) -> Result<Box<dyn crate::box_trait::NyashBox>, String> {
|
||||
// 1) ConsoleBoxを生成(WASM環境ではブラウザコンソールに委譲)
|
||||
let console = Box::new(ConsoleBox::new());
|
||||
// 2) slot解決→dispatchでlogを呼ぶ(最小疎通)
|
||||
if let Some(slot_id) = unified_dispatch::resolve_slot(console.as_ref(), "log", 1) {
|
||||
let args = vec![Box::new(StringBox::new("🎉 WASM v2 console.log working!")) as Box<dyn NyashBox>];
|
||||
let args =
|
||||
vec![Box::new(StringBox::new("🎉 WASM v2 console.log working!")) as Box<dyn NyashBox>];
|
||||
let _ = unified_dispatch::dispatch_by_slot(slot_id, console.as_ref(), &args);
|
||||
}
|
||||
// 3) 結果を返す
|
||||
Ok(Box::new(StringBox::new("WASM v2 unified dispatch test completed")))
|
||||
Ok(Box::new(StringBox::new(
|
||||
"WASM v2 unified dispatch test completed",
|
||||
)))
|
||||
}
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
|
||||
#![cfg(feature = "wasm-backend")]
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, VoidBox, BoolBox};
|
||||
use crate::boxes::{ConsoleBox, ArrayBox, MapBox};
|
||||
use crate::box_trait::{BoolBox, NyashBox, StringBox, VoidBox};
|
||||
use crate::boxes::{ArrayBox, ConsoleBox, MapBox};
|
||||
|
||||
/// 受信ボックス/メソッド名/アリティからスロットを解決し、識別子を返す。
|
||||
pub fn resolve_slot(recv: &dyn NyashBox, method: &str, arity: usize) -> Option<u16> {
|
||||
@ -114,6 +114,6 @@ pub fn dispatch_by_slot(
|
||||
None
|
||||
}
|
||||
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,8 @@ pub struct GeneratedVTableInfo {
|
||||
|
||||
pub fn generate_tables() -> GeneratedVTableInfo {
|
||||
// 未実装: TypeRegistry::resolve_typebox_by_name()/methods を走査して集計
|
||||
GeneratedVTableInfo { types: 0, methods: 0 }
|
||||
GeneratedVTableInfo {
|
||||
types: 0,
|
||||
methods: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,14 +7,14 @@
|
||||
* - WASM (MIR -> WASM execution)
|
||||
*/
|
||||
|
||||
use std::time::Instant;
|
||||
use std::fs;
|
||||
use crate::parser::NyashParser;
|
||||
use crate::interpreter::NyashInterpreter;
|
||||
use crate::mir::MirCompiler;
|
||||
use crate::backend::VM;
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
use crate::backend::WasmBackend;
|
||||
use crate::backend::VM;
|
||||
use crate::interpreter::NyashInterpreter;
|
||||
use crate::mir::MirCompiler;
|
||||
use crate::parser::NyashParser;
|
||||
use std::fs;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BenchmarkResult {
|
||||
@ -71,7 +71,11 @@ impl BenchmarkSuite {
|
||||
}
|
||||
|
||||
/// Run benchmark on interpreter backend
|
||||
fn run_interpreter_benchmark(&self, name: &str, source: &str) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
fn run_interpreter_benchmark(
|
||||
&self,
|
||||
name: &str,
|
||||
source: &str,
|
||||
) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
let mut total_duration = 0.0;
|
||||
|
||||
for i in 0..self.iterations {
|
||||
@ -100,7 +104,11 @@ impl BenchmarkSuite {
|
||||
}
|
||||
|
||||
/// Run benchmark on VM backend
|
||||
fn run_vm_benchmark(&self, name: &str, source: &str) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
fn run_vm_benchmark(
|
||||
&self,
|
||||
name: &str,
|
||||
source: &str,
|
||||
) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
let mut total_duration = 0.0;
|
||||
|
||||
for i in 0..self.iterations {
|
||||
@ -132,7 +140,11 @@ impl BenchmarkSuite {
|
||||
|
||||
/// Run benchmark on WASM backend
|
||||
#[cfg(feature = "wasm-backend")]
|
||||
fn run_wasm_benchmark(&self, name: &str, source: &str) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
fn run_wasm_benchmark(
|
||||
&self,
|
||||
name: &str,
|
||||
source: &str,
|
||||
) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
|
||||
let mut total_duration = 0.0;
|
||||
|
||||
for i in 0..self.iterations {
|
||||
@ -174,9 +186,13 @@ impl BenchmarkSuite {
|
||||
println!();
|
||||
|
||||
// Group by benchmark name
|
||||
let mut benchmarks: std::collections::HashMap<String, Vec<&BenchmarkResult>> = std::collections::HashMap::new();
|
||||
let mut benchmarks: std::collections::HashMap<String, Vec<&BenchmarkResult>> =
|
||||
std::collections::HashMap::new();
|
||||
for result in results {
|
||||
benchmarks.entry(result.name.clone()).or_insert_with(Vec::new).push(result);
|
||||
benchmarks
|
||||
.entry(result.name.clone())
|
||||
.or_insert_with(Vec::new)
|
||||
.push(result);
|
||||
}
|
||||
|
||||
for (bench_name, bench_results) in benchmarks {
|
||||
@ -185,15 +201,16 @@ impl BenchmarkSuite {
|
||||
println!(" --------------|---------------|-----------------|------------");
|
||||
|
||||
// Find fastest for ratio calculation
|
||||
let fastest = bench_results.iter().min_by(|a, b| a.avg_duration_ms.partial_cmp(&b.avg_duration_ms).unwrap()).unwrap();
|
||||
let fastest = bench_results
|
||||
.iter()
|
||||
.min_by(|a, b| a.avg_duration_ms.partial_cmp(&b.avg_duration_ms).unwrap())
|
||||
.unwrap();
|
||||
|
||||
for result in &bench_results {
|
||||
let ratio = result.avg_duration_ms / fastest.avg_duration_ms;
|
||||
println!(" {:12} | {:11.3} | {:13.1} | {:8.2}x",
|
||||
result.backend,
|
||||
result.avg_duration_ms,
|
||||
result.duration_ms,
|
||||
ratio
|
||||
println!(
|
||||
" {:12} | {:11.3} | {:13.1} | {:8.2}x",
|
||||
result.backend, result.avg_duration_ms, result.duration_ms, ratio
|
||||
);
|
||||
}
|
||||
println!();
|
||||
@ -201,25 +218,42 @@ impl BenchmarkSuite {
|
||||
|
||||
// Summary
|
||||
println!("💡 Performance Summary:");
|
||||
let interpreter_avg: f64 = results.iter()
|
||||
let interpreter_avg: f64 = results
|
||||
.iter()
|
||||
.filter(|r| r.backend == "Interpreter")
|
||||
.map(|r| r.avg_duration_ms)
|
||||
.sum::<f64>() / results.iter().filter(|r| r.backend == "Interpreter").count() as f64;
|
||||
.sum::<f64>()
|
||||
/ results
|
||||
.iter()
|
||||
.filter(|r| r.backend == "Interpreter")
|
||||
.count() as f64;
|
||||
|
||||
let vm_avg: f64 = results.iter()
|
||||
let vm_avg: f64 = results
|
||||
.iter()
|
||||
.filter(|r| r.backend == "VM")
|
||||
.map(|r| r.avg_duration_ms)
|
||||
.sum::<f64>() / results.iter().filter(|r| r.backend == "VM").count() as f64;
|
||||
.sum::<f64>()
|
||||
/ results.iter().filter(|r| r.backend == "VM").count() as f64;
|
||||
|
||||
let wasm_avg: f64 = results.iter()
|
||||
let wasm_avg: f64 = results
|
||||
.iter()
|
||||
.filter(|r| r.backend == "WASM")
|
||||
.map(|r| r.avg_duration_ms)
|
||||
.sum::<f64>() / results.iter().filter(|r| r.backend == "WASM").count() as f64;
|
||||
.sum::<f64>()
|
||||
/ results.iter().filter(|r| r.backend == "WASM").count() as f64;
|
||||
|
||||
println!(" 📈 Average across all benchmarks:");
|
||||
println!(" Interpreter: {:.2} ms", interpreter_avg);
|
||||
println!(" VM: {:.2} ms ({:.1}x faster than interpreter)", vm_avg, interpreter_avg / vm_avg);
|
||||
println!(" WASM: {:.2} ms ({:.1}x faster than interpreter)", wasm_avg, interpreter_avg / wasm_avg);
|
||||
println!(
|
||||
" VM: {:.2} ms ({:.1}x faster than interpreter)",
|
||||
vm_avg,
|
||||
interpreter_avg / vm_avg
|
||||
);
|
||||
println!(
|
||||
" WASM: {:.2} ms ({:.1}x faster than interpreter)",
|
||||
wasm_avg,
|
||||
interpreter_avg / wasm_avg
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use super::{BidError, BidHandle, BidType};
|
||||
use crate::box_trait::NyashBox;
|
||||
use super::{BidHandle, BidType, BidError};
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// BID-FFI Bridge for Nyash Box types
|
||||
/// Provides conversion between Nyash runtime values and BID handles
|
||||
@ -78,24 +78,51 @@ pub fn box_to_bid_handle(
|
||||
registry: &mut BoxRegistry,
|
||||
) -> Result<(BidType, BidHandle), BidError> {
|
||||
// Downcast to specific box types
|
||||
if let Some(_string_box) = arc_box.as_any().downcast_ref::<crate::boxes::string_box::StringBox>() {
|
||||
if let Some(_string_box) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::string_box::StringBox>()
|
||||
{
|
||||
let handle = registry.register_box(
|
||||
crate::bid::types::BoxTypeId::StringBox as u32,
|
||||
arc_box.clone()
|
||||
arc_box.clone(),
|
||||
);
|
||||
Ok((BidType::Handle { type_id: 1, instance_id: handle.instance_id }, handle))
|
||||
} else if let Some(_integer_box) = arc_box.as_any().downcast_ref::<crate::boxes::integer_box::IntegerBox>() {
|
||||
Ok((
|
||||
BidType::Handle {
|
||||
type_id: 1,
|
||||
instance_id: handle.instance_id,
|
||||
},
|
||||
handle,
|
||||
))
|
||||
} else if let Some(_integer_box) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::integer_box::IntegerBox>()
|
||||
{
|
||||
let handle = registry.register_box(
|
||||
crate::bid::types::BoxTypeId::IntegerBox as u32,
|
||||
arc_box.clone()
|
||||
arc_box.clone(),
|
||||
);
|
||||
Ok((BidType::Handle { type_id: 2, instance_id: handle.instance_id }, handle))
|
||||
} else if let Some(_future_box) = arc_box.as_any().downcast_ref::<crate::boxes::future::NyashFutureBox>() {
|
||||
Ok((
|
||||
BidType::Handle {
|
||||
type_id: 2,
|
||||
instance_id: handle.instance_id,
|
||||
},
|
||||
handle,
|
||||
))
|
||||
} else if let Some(_future_box) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::future::NyashFutureBox>()
|
||||
{
|
||||
let handle = registry.register_box(
|
||||
crate::bid::types::BoxTypeId::FutureBox as u32,
|
||||
arc_box.clone()
|
||||
arc_box.clone(),
|
||||
);
|
||||
Ok((BidType::Handle { type_id: 7, instance_id: handle.instance_id }, handle))
|
||||
Ok((
|
||||
BidType::Handle {
|
||||
type_id: 7,
|
||||
instance_id: handle.instance_id,
|
||||
},
|
||||
handle,
|
||||
))
|
||||
} else {
|
||||
Err(BidError::InvalidType)
|
||||
}
|
||||
@ -106,13 +133,15 @@ pub fn bid_handle_to_box(
|
||||
handle: BidHandle,
|
||||
registry: &BoxRegistry,
|
||||
) -> Result<Arc<dyn NyashBox>, BidError> {
|
||||
registry.get_box(handle)
|
||||
.ok_or(BidError::InvalidHandle)
|
||||
registry.get_box(handle).ok_or(BidError::InvalidHandle)
|
||||
}
|
||||
|
||||
/// Extract string value from a Box for TLV encoding
|
||||
pub fn extract_string_value(arc_box: &Arc<dyn NyashBox>) -> Result<String, BidError> {
|
||||
if let Some(string_box) = arc_box.as_any().downcast_ref::<crate::boxes::string_box::StringBox>() {
|
||||
if let Some(string_box) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::string_box::StringBox>()
|
||||
{
|
||||
Ok(string_box.value.clone())
|
||||
} else {
|
||||
Err(BidError::InvalidType)
|
||||
@ -121,7 +150,10 @@ pub fn extract_string_value(arc_box: &Arc<dyn NyashBox>) -> Result<String, BidEr
|
||||
|
||||
/// Extract integer value from a Box for TLV encoding
|
||||
pub fn extract_integer_value(arc_box: &Arc<dyn NyashBox>) -> Result<i64, BidError> {
|
||||
if let Some(integer_box) = arc_box.as_any().downcast_ref::<crate::boxes::integer_box::IntegerBox>() {
|
||||
if let Some(integer_box) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::integer_box::IntegerBox>()
|
||||
{
|
||||
Ok(integer_box.value)
|
||||
} else {
|
||||
Err(BidError::InvalidType)
|
||||
@ -226,10 +258,16 @@ mod tests {
|
||||
let retrieved = bid_handle_to_box(handle, ®istry).unwrap();
|
||||
|
||||
// Verify it's still a FutureBox
|
||||
assert!(retrieved.as_any().downcast_ref::<crate::boxes::future::NyashFutureBox>().is_some());
|
||||
assert!(retrieved
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::future::NyashFutureBox>()
|
||||
.is_some());
|
||||
|
||||
// Test with result set
|
||||
if let Some(future) = arc_box.as_any().downcast_ref::<crate::boxes::future::NyashFutureBox>() {
|
||||
if let Some(future) = arc_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::future::NyashFutureBox>()
|
||||
{
|
||||
let string_result = crate::boxes::string_box::StringBox::new("Future Result");
|
||||
future.set_result(Box::new(string_result));
|
||||
|
||||
|
||||
@ -1,137 +1,151 @@
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
mod plugin_impl {
|
||||
|
||||
use crate::bid::{BidError, BidResult, LoadedPlugin};
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
use crate::bid::{BidError, BidResult, LoadedPlugin};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
/// 汎用プラグインBoxインスタンス
|
||||
/// 任意のプラグインBoxを統一的に扱える
|
||||
pub struct GenericPluginBox {
|
||||
base: BoxBase,
|
||||
plugin: &'static LoadedPlugin,
|
||||
instance_id: u32,
|
||||
box_name: String,
|
||||
}
|
||||
|
||||
impl GenericPluginBox {
|
||||
/// 汎用的なプラグインBoxを作成(birth呼び出し)
|
||||
pub fn birth(plugin: &'static LoadedPlugin, box_name: String) -> BidResult<Self> {
|
||||
eprintln!("🔍 GenericPluginBox::birth for '{}'", box_name);
|
||||
|
||||
// birthメソッド(method_id = 0)を呼び出し
|
||||
let mut out = Vec::new();
|
||||
plugin.handle.invoke(plugin.type_id, 0, 0, &[], &mut out)?;
|
||||
|
||||
// インスタンスIDを取得
|
||||
let instance_id = if out.len() == 4 {
|
||||
u32::from_le_bytes([out[0], out[1], out[2], out[3]])
|
||||
} else {
|
||||
return Err(BidError::InvalidArgs);
|
||||
};
|
||||
|
||||
eprintln!("✅ Created {} instance with ID: {}", box_name, instance_id);
|
||||
|
||||
Ok(Self {
|
||||
base: BoxBase::new(),
|
||||
plugin,
|
||||
instance_id,
|
||||
box_name,
|
||||
})
|
||||
/// 汎用プラグインBoxインスタンス
|
||||
/// 任意のプラグインBoxを統一的に扱える
|
||||
pub struct GenericPluginBox {
|
||||
base: BoxBase,
|
||||
plugin: &'static LoadedPlugin,
|
||||
instance_id: u32,
|
||||
box_name: String,
|
||||
}
|
||||
|
||||
/// 汎用メソッド呼び出し
|
||||
pub fn call_method(&self, method_name: &str, args: &[u8]) -> BidResult<Vec<u8>> {
|
||||
eprintln!("🔍 GenericPluginBox::call_method '{}' on {}", method_name, self.box_name);
|
||||
impl GenericPluginBox {
|
||||
/// 汎用的なプラグインBoxを作成(birth呼び出し)
|
||||
pub fn birth(plugin: &'static LoadedPlugin, box_name: String) -> BidResult<Self> {
|
||||
eprintln!("🔍 GenericPluginBox::birth for '{}'", box_name);
|
||||
|
||||
// プラグインからメソッドIDを動的取得
|
||||
match self.plugin.find_method(method_name) {
|
||||
Ok(Some((method_id, _signature))) => {
|
||||
eprintln!("✅ Found method '{}' with ID: {}", method_name, method_id);
|
||||
// birthメソッド(method_id = 0)を呼び出し
|
||||
let mut out = Vec::new();
|
||||
plugin.handle.invoke(plugin.type_id, 0, 0, &[], &mut out)?;
|
||||
|
||||
let mut out = Vec::new();
|
||||
self.plugin.handle.invoke(
|
||||
self.plugin.type_id,
|
||||
method_id,
|
||||
self.instance_id,
|
||||
args,
|
||||
&mut out
|
||||
)?;
|
||||
// インスタンスIDを取得
|
||||
let instance_id = if out.len() == 4 {
|
||||
u32::from_le_bytes([out[0], out[1], out[2], out[3]])
|
||||
} else {
|
||||
return Err(BidError::InvalidArgs);
|
||||
};
|
||||
|
||||
Ok(out)
|
||||
eprintln!("✅ Created {} instance with ID: {}", box_name, instance_id);
|
||||
|
||||
Ok(Self {
|
||||
base: BoxBase::new(),
|
||||
plugin,
|
||||
instance_id,
|
||||
box_name,
|
||||
})
|
||||
}
|
||||
|
||||
/// 汎用メソッド呼び出し
|
||||
pub fn call_method(&self, method_name: &str, args: &[u8]) -> BidResult<Vec<u8>> {
|
||||
eprintln!(
|
||||
"🔍 GenericPluginBox::call_method '{}' on {}",
|
||||
method_name, self.box_name
|
||||
);
|
||||
|
||||
// プラグインからメソッドIDを動的取得
|
||||
match self.plugin.find_method(method_name) {
|
||||
Ok(Some((method_id, _signature))) => {
|
||||
eprintln!("✅ Found method '{}' with ID: {}", method_name, method_id);
|
||||
|
||||
let mut out = Vec::new();
|
||||
self.plugin.handle.invoke(
|
||||
self.plugin.type_id,
|
||||
method_id,
|
||||
self.instance_id,
|
||||
args,
|
||||
&mut out,
|
||||
)?;
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
Ok(None) => {
|
||||
eprintln!("❌ Method '{}' not found in {}", method_name, self.box_name);
|
||||
Err(BidError::InvalidArgs)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
Ok(None) => {
|
||||
eprintln!("❌ Method '{}' not found in {}", method_name, self.box_name);
|
||||
Err(BidError::InvalidArgs)
|
||||
}
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for GenericPluginBox {
|
||||
fn drop(&mut self) {
|
||||
// finiメソッド(method_id = u32::MAX)を呼び出し
|
||||
let _ = self.plugin.handle.invoke(
|
||||
self.plugin.type_id,
|
||||
u32::MAX,
|
||||
self.instance_id,
|
||||
&[],
|
||||
&mut Vec::new(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for GenericPluginBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}(plugin)", self.box_name)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
}
|
||||
|
||||
impl NyashBox for GenericPluginBox {
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("{}(plugin)", self.box_name))
|
||||
impl Drop for GenericPluginBox {
|
||||
fn drop(&mut self) {
|
||||
// finiメソッド(method_id = u32::MAX)を呼び出し
|
||||
let _ = self.plugin.handle.invoke(
|
||||
self.plugin.type_id,
|
||||
u32::MAX,
|
||||
self.instance_id,
|
||||
&[],
|
||||
&mut Vec::new(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_plugin) = other.as_any().downcast_ref::<GenericPluginBox>() {
|
||||
BoolBox::new(
|
||||
self.box_name == other_plugin.box_name &&
|
||||
self.instance_id == other_plugin.instance_id
|
||||
impl BoxCore for GenericPluginBox {
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}(plugin)", self.box_name)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for GenericPluginBox {
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("{}(plugin)", self.box_name))
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_plugin) = other.as_any().downcast_ref::<GenericPluginBox>() {
|
||||
BoolBox::new(
|
||||
self.box_name == other_plugin.box_name
|
||||
&& self.instance_id == other_plugin.instance_id,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
// v2 plugin system migration: simplified clone for now
|
||||
// TODO: Implement proper cloning through v2 plugin loader
|
||||
Box::new(StringBox::new(format!("{}(plugin-clone)", self.box_name)))
|
||||
}
|
||||
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for GenericPluginBox {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"GenericPluginBox({}, instance={})",
|
||||
self.box_name, self.instance_id
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
// v2 plugin system migration: simplified clone for now
|
||||
// TODO: Implement proper cloning through v2 plugin loader
|
||||
Box::new(StringBox::new(format!("{}(plugin-clone)", self.box_name)))
|
||||
impl fmt::Display for GenericPluginBox {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.fmt_box(f)
|
||||
}
|
||||
}
|
||||
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for GenericPluginBox {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "GenericPluginBox({}, instance={})", self.box_name, self.instance_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for GenericPluginBox {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.fmt_box(f)
|
||||
}
|
||||
}
|
||||
|
||||
} // mod plugin_impl
|
||||
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
use crate::bid::{BidError, BidResult, NyashHostVtable, NyashPluginInfo, PluginHandle, PLUGIN_ABI_SYMBOL, PLUGIN_INIT_SYMBOL, PLUGIN_INVOKE_SYMBOL, PLUGIN_SHUTDOWN_SYMBOL};
|
||||
use crate::bid::{
|
||||
BidError, BidResult, NyashHostVtable, NyashPluginInfo, PluginHandle, PLUGIN_ABI_SYMBOL,
|
||||
PLUGIN_INIT_SYMBOL, PLUGIN_INVOKE_SYMBOL, PLUGIN_SHUTDOWN_SYMBOL,
|
||||
};
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
use libloading::{Library, Symbol};
|
||||
use std::path::{Path, PathBuf};
|
||||
@ -15,18 +18,21 @@ impl LoadedPlugin {
|
||||
/// Load a plugin dynamic library from file path and initialize it
|
||||
pub fn load_from_file(path: &Path) -> BidResult<Self> {
|
||||
// Load library
|
||||
let library = unsafe { Library::new(path) }
|
||||
.map_err(|_| BidError::PluginError)?;
|
||||
let library = unsafe { Library::new(path) }.map_err(|_| BidError::PluginError)?;
|
||||
|
||||
// Resolve symbols
|
||||
unsafe {
|
||||
let abi: Symbol<unsafe extern "C" fn() -> u32> = library
|
||||
.get(PLUGIN_ABI_SYMBOL.as_bytes())
|
||||
.map_err(|_| BidError::PluginError)?;
|
||||
let init: Symbol<unsafe extern "C" fn(*const NyashHostVtable, *mut NyashPluginInfo) -> i32> = library
|
||||
let init: Symbol<
|
||||
unsafe extern "C" fn(*const NyashHostVtable, *mut NyashPluginInfo) -> i32,
|
||||
> = library
|
||||
.get(PLUGIN_INIT_SYMBOL.as_bytes())
|
||||
.map_err(|_| BidError::PluginError)?;
|
||||
let invoke: Symbol<unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32> = library
|
||||
let invoke: Symbol<
|
||||
unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32,
|
||||
> = library
|
||||
.get(PLUGIN_INVOKE_SYMBOL.as_bytes())
|
||||
.map_err(|_| BidError::PluginError)?;
|
||||
let shutdown: Symbol<unsafe extern "C" fn()> = library
|
||||
@ -49,7 +55,12 @@ impl LoadedPlugin {
|
||||
handle.initialize(&host, &mut info)?;
|
||||
let type_id = info.type_id;
|
||||
|
||||
Ok(Self { library, handle, type_id, plugin_info: info })
|
||||
Ok(Self {
|
||||
library,
|
||||
handle,
|
||||
type_id,
|
||||
plugin_info: info,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,7 +122,12 @@ static HOST_VTABLE_STORAGE: std::sync::LazyLock<NyashHostVtable> = std::sync::La
|
||||
}
|
||||
}
|
||||
|
||||
NyashHostVtable { alloc: host_alloc, free: host_free, wake: host_wake, log: host_log }
|
||||
NyashHostVtable {
|
||||
alloc: host_alloc,
|
||||
free: host_free,
|
||||
wake: host_wake,
|
||||
log: host_log,
|
||||
}
|
||||
});
|
||||
|
||||
/// Build a minimal host vtable for plugins
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use super::{BidError, BidResult};
|
||||
use std::os::raw::{c_char};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
/// Host function table provided to plugins
|
||||
#[repr(C)]
|
||||
@ -14,11 +14,18 @@ pub struct NyashHostVtable {
|
||||
impl NyashHostVtable {
|
||||
/// Create a vtable with no-op stubs (for tests)
|
||||
pub fn empty() -> Self {
|
||||
unsafe extern "C" fn a(_size: usize) -> *mut u8 { std::ptr::null_mut() }
|
||||
unsafe extern "C" fn a(_size: usize) -> *mut u8 {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
unsafe extern "C" fn f(_ptr: *mut u8) {}
|
||||
unsafe extern "C" fn w(_h: u64) {}
|
||||
unsafe extern "C" fn l(_level: i32, _m: *const c_char) {}
|
||||
Self { alloc: a, free: f, wake: w, log: l }
|
||||
Self {
|
||||
alloc: a,
|
||||
free: f,
|
||||
wake: w,
|
||||
log: l,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,9 +49,12 @@ unsafe impl Sync for NyashMethodInfo {}
|
||||
|
||||
impl NyashMethodInfo {
|
||||
/// Create method info with safe string handling
|
||||
pub fn new(method_id: u32, method_name: &str, signature_hash: u32) -> BidResult<(Self, CString)> {
|
||||
let c_name = CString::new(method_name)
|
||||
.map_err(|_| BidError::InvalidUtf8)?;
|
||||
pub fn new(
|
||||
method_id: u32,
|
||||
method_name: &str,
|
||||
signature_hash: u32,
|
||||
) -> BidResult<(Self, CString)> {
|
||||
let c_name = CString::new(method_name).map_err(|_| BidError::InvalidUtf8)?;
|
||||
|
||||
let info = Self {
|
||||
method_id,
|
||||
@ -159,8 +169,7 @@ impl PluginMetadata {
|
||||
methods: Vec<(u32, &str, u32)>, // (id, name, hash)
|
||||
) -> BidResult<Self> {
|
||||
// Create type name
|
||||
let type_name_holder = CString::new(type_name)
|
||||
.map_err(|_| BidError::InvalidUtf8)?;
|
||||
let type_name_holder = CString::new(type_name).map_err(|_| BidError::InvalidUtf8)?;
|
||||
|
||||
// Create method infos
|
||||
let mut method_holders = Vec::new();
|
||||
@ -221,10 +230,17 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_host_vtable() {
|
||||
unsafe extern "C" fn a(_size: usize) -> *mut u8 { std::ptr::null_mut() }
|
||||
unsafe extern "C" fn a(_size: usize) -> *mut u8 {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
unsafe extern "C" fn f(_p: *mut u8) {}
|
||||
unsafe extern "C" fn w(_h: u64) {}
|
||||
unsafe extern "C" fn l(_level: i32, _m: *const c_char) {}
|
||||
let _v = NyashHostVtable { alloc: a, free: f, wake: w, log: l };
|
||||
let _v = NyashHostVtable {
|
||||
alloc: a,
|
||||
free: f,
|
||||
wake: w,
|
||||
log: l,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +1,27 @@
|
||||
// BID-FFI: Box Interface Definition with Foreign Function Interface
|
||||
// Everything is Box philosophy meets practical FFI/ABI design!
|
||||
|
||||
pub mod types;
|
||||
pub mod tlv;
|
||||
pub mod error;
|
||||
pub mod metadata;
|
||||
pub mod plugin_api;
|
||||
pub mod bridge;
|
||||
pub mod plugins;
|
||||
pub mod error;
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
pub mod loader;
|
||||
pub mod metadata;
|
||||
pub mod plugin_api;
|
||||
pub mod plugins;
|
||||
pub mod tlv;
|
||||
pub mod types;
|
||||
// pub mod registry; // legacy - v2 plugin system uses BoxFactoryRegistry instead
|
||||
// pub mod plugin_box; // legacy - FileBox専用実装
|
||||
pub mod generic_plugin_box;
|
||||
|
||||
pub use types::*;
|
||||
pub use tlv::*;
|
||||
pub use error::*;
|
||||
pub use metadata::*;
|
||||
pub use plugin_api::*;
|
||||
pub use bridge::*;
|
||||
pub use error::*;
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
pub use loader::*;
|
||||
pub use metadata::*;
|
||||
pub use plugin_api::*;
|
||||
pub use tlv::*;
|
||||
pub use types::*;
|
||||
// pub use registry::*; // legacy - v2 plugin system uses BoxFactoryRegistry instead
|
||||
// pub use plugin_box::*; // legacy
|
||||
pub use generic_plugin_box::*;
|
||||
|
||||
@ -14,10 +14,8 @@ pub type PluginAbiFn = unsafe extern "C" fn() -> u32;
|
||||
/// - host: Host function table for plugin to use
|
||||
/// - info: Plugin information to be filled by plugin
|
||||
/// Returns: 0 on success, negative error code on failure
|
||||
pub type PluginInitFn = unsafe extern "C" fn(
|
||||
host: *const NyashHostVtable,
|
||||
info: *mut NyashPluginInfo,
|
||||
) -> i32;
|
||||
pub type PluginInitFn =
|
||||
unsafe extern "C" fn(host: *const NyashHostVtable, info: *mut NyashPluginInfo) -> i32;
|
||||
|
||||
/// Invoke a plugin method
|
||||
/// Parameters:
|
||||
@ -67,17 +65,9 @@ impl PluginHandle {
|
||||
}
|
||||
|
||||
/// Initialize plugin with host vtable
|
||||
pub fn initialize(
|
||||
&self,
|
||||
host: &NyashHostVtable,
|
||||
info: &mut NyashPluginInfo,
|
||||
) -> BidResult<()> {
|
||||
let result = unsafe {
|
||||
(self.init)(
|
||||
host as *const NyashHostVtable,
|
||||
info as *mut NyashPluginInfo,
|
||||
)
|
||||
};
|
||||
pub fn initialize(&self, host: &NyashHostVtable, info: &mut NyashPluginInfo) -> BidResult<()> {
|
||||
let result =
|
||||
unsafe { (self.init)(host as *const NyashHostVtable, info as *mut NyashPluginInfo) };
|
||||
|
||||
if result != 0 {
|
||||
Err(BidError::from_raw(result))
|
||||
|
||||
@ -44,9 +44,17 @@ impl FileBoxRegistry {
|
||||
pub fn open(&mut self, path: &str, mode: FileMode) -> Result<BidHandle, std::io::Error> {
|
||||
let file = match mode {
|
||||
FileMode::Read => OpenOptions::new().read(true).open(path)?,
|
||||
FileMode::Write => OpenOptions::new().write(true).create(true).truncate(true).open(path)?,
|
||||
FileMode::Write => OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(path)?,
|
||||
FileMode::Append => OpenOptions::new().append(true).create(true).open(path)?,
|
||||
FileMode::ReadWrite => OpenOptions::new().read(true).write(true).create(true).open(path)?,
|
||||
FileMode::ReadWrite => OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(path)?,
|
||||
};
|
||||
|
||||
let handle_id = self.next_handle;
|
||||
@ -65,8 +73,9 @@ impl FileBoxRegistry {
|
||||
|
||||
pub fn read(&self, handle: BidHandle, size: usize) -> Result<Vec<u8>, std::io::Error> {
|
||||
let handle_id = handle.instance_id;
|
||||
let file_state = self.files.get(&handle_id)
|
||||
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle"))?;
|
||||
let file_state = self.files.get(&handle_id).ok_or_else(|| {
|
||||
std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle")
|
||||
})?;
|
||||
|
||||
let mut state = file_state.lock().unwrap();
|
||||
let mut buffer = vec![0u8; size];
|
||||
@ -78,8 +87,9 @@ impl FileBoxRegistry {
|
||||
|
||||
pub fn write(&self, handle: BidHandle, data: &[u8]) -> Result<usize, std::io::Error> {
|
||||
let handle_id = handle.instance_id;
|
||||
let file_state = self.files.get(&handle_id)
|
||||
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle"))?;
|
||||
let file_state = self.files.get(&handle_id).ok_or_else(|| {
|
||||
std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle")
|
||||
})?;
|
||||
|
||||
let mut state = file_state.lock().unwrap();
|
||||
state.file.write(data)
|
||||
@ -87,8 +97,9 @@ impl FileBoxRegistry {
|
||||
|
||||
pub fn close(&mut self, handle: BidHandle) -> Result<(), std::io::Error> {
|
||||
let handle_id = handle.instance_id;
|
||||
self.files.remove(&handle_id)
|
||||
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle"))?;
|
||||
self.files.remove(&handle_id).ok_or_else(|| {
|
||||
std::io::Error::new(std::io::ErrorKind::NotFound, "Invalid file handle")
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -125,28 +136,32 @@ impl FileBoxPlugin {
|
||||
};
|
||||
|
||||
let mut registry = self.registry.lock().unwrap();
|
||||
registry.open(path, file_mode)
|
||||
registry
|
||||
.open(path, file_mode)
|
||||
.map_err(|e| format!("Failed to open file: {}", e))
|
||||
}
|
||||
|
||||
/// Read data from file
|
||||
pub fn read(&self, handle: BidHandle, size: usize) -> Result<Vec<u8>, String> {
|
||||
let registry = self.registry.lock().unwrap();
|
||||
registry.read(handle, size)
|
||||
registry
|
||||
.read(handle, size)
|
||||
.map_err(|e| format!("Failed to read file: {}", e))
|
||||
}
|
||||
|
||||
/// Write data to file
|
||||
pub fn write(&self, handle: BidHandle, data: &[u8]) -> Result<usize, String> {
|
||||
let registry = self.registry.lock().unwrap();
|
||||
registry.write(handle, data)
|
||||
registry
|
||||
.write(handle, data)
|
||||
.map_err(|e| format!("Failed to write file: {}", e))
|
||||
}
|
||||
|
||||
/// Close file
|
||||
pub fn close(&self, handle: BidHandle) -> Result<(), String> {
|
||||
let mut registry = self.registry.lock().unwrap();
|
||||
registry.close(handle)
|
||||
registry
|
||||
.close(handle)
|
||||
.map_err(|e| format!("Failed to close file: {}", e))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
use super::{BidError, BidResult, BidHandle, BidTag, BID_VERSION};
|
||||
use super::{BidError, BidHandle, BidResult, BidTag, BID_VERSION};
|
||||
use std::mem;
|
||||
|
||||
/// BID-1 TLV Header
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BidTlvHeader {
|
||||
pub version: u16, // BID version (1)
|
||||
pub argc: u16, // Argument count
|
||||
pub version: u16, // BID version (1)
|
||||
pub argc: u16, // Argument count
|
||||
}
|
||||
|
||||
/// TLV Entry structure
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TlvEntry {
|
||||
pub tag: u8, // Type tag
|
||||
pub reserved: u8, // Reserved for future use (0)
|
||||
pub size: u16, // Payload size
|
||||
// Payload follows immediately after
|
||||
pub tag: u8, // Type tag
|
||||
pub reserved: u8, // Reserved for future use (0)
|
||||
pub size: u16, // Payload size
|
||||
// Payload follows immediately after
|
||||
}
|
||||
|
||||
/// TLV encoder for BID-1 format
|
||||
@ -34,7 +34,9 @@ impl TlvEncoder {
|
||||
};
|
||||
|
||||
// Reserve space for header
|
||||
encoder.buffer.extend_from_slice(&[0; mem::size_of::<BidTlvHeader>()]);
|
||||
encoder
|
||||
.buffer
|
||||
.extend_from_slice(&[0; mem::size_of::<BidTlvHeader>()]);
|
||||
encoder
|
||||
}
|
||||
|
||||
@ -174,11 +176,12 @@ impl<'a> TlvDecoder<'a> {
|
||||
// Read entry safely
|
||||
let tag = self.data[self.position];
|
||||
let reserved = self.data[self.position + 1];
|
||||
let size = u16::from_le_bytes([
|
||||
self.data[self.position + 2],
|
||||
self.data[self.position + 3],
|
||||
]);
|
||||
let entry = TlvEntry { tag, reserved, size };
|
||||
let size = u16::from_le_bytes([self.data[self.position + 2], self.data[self.position + 3]]);
|
||||
let entry = TlvEntry {
|
||||
tag,
|
||||
reserved,
|
||||
size,
|
||||
};
|
||||
self.position += mem::size_of::<TlvEntry>();
|
||||
|
||||
// Read payload
|
||||
@ -223,7 +226,9 @@ impl<'a> TlvDecoder<'a> {
|
||||
if payload.len() != 4 {
|
||||
return Err(BidError::InvalidArgs);
|
||||
}
|
||||
Ok(i32::from_le_bytes([payload[0], payload[1], payload[2], payload[3]]))
|
||||
Ok(i32::from_le_bytes([
|
||||
payload[0], payload[1], payload[2], payload[3],
|
||||
]))
|
||||
}
|
||||
|
||||
/// Decode an i64 from payload
|
||||
@ -251,7 +256,9 @@ impl<'a> TlvDecoder<'a> {
|
||||
if payload.len() != 4 {
|
||||
return Err(BidError::InvalidArgs);
|
||||
}
|
||||
Ok(f32::from_le_bytes([payload[0], payload[1], payload[2], payload[3]]))
|
||||
Ok(f32::from_le_bytes([
|
||||
payload[0], payload[1], payload[2], payload[3],
|
||||
]))
|
||||
}
|
||||
|
||||
/// Decode an f64 from payload
|
||||
|
||||
@ -2,32 +2,32 @@
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum BidType {
|
||||
// === Primitives (pass by value across FFI) ===
|
||||
Bool, // i32 (0=false, 1=true)
|
||||
I32, // 32-bit signed integer
|
||||
I64, // 64-bit signed integer
|
||||
F32, // 32-bit floating point
|
||||
F64, // 64-bit floating point
|
||||
Bool, // i32 (0=false, 1=true)
|
||||
I32, // 32-bit signed integer
|
||||
I64, // 64-bit signed integer
|
||||
F32, // 32-bit floating point
|
||||
F64, // 64-bit floating point
|
||||
|
||||
// === Composite types (pass as ptr+len) ===
|
||||
String, // UTF-8 string (ptr: usize, len: usize)
|
||||
Bytes, // Binary data (ptr: usize, len: usize)
|
||||
String, // UTF-8 string (ptr: usize, len: usize)
|
||||
Bytes, // Binary data (ptr: usize, len: usize)
|
||||
|
||||
// === Handle design (ChatGPT recommendation) ===
|
||||
Handle {
|
||||
type_id: u32, // Box type ID (1=StringBox, 6=FileBox, etc.)
|
||||
instance_id: u32, // Instance identifier
|
||||
type_id: u32, // Box type ID (1=StringBox, 6=FileBox, etc.)
|
||||
instance_id: u32, // Instance identifier
|
||||
},
|
||||
|
||||
// === Meta types ===
|
||||
Void, // No return value
|
||||
Void, // No return value
|
||||
|
||||
// === Phase 2 reserved (TLV tags reserved) ===
|
||||
#[allow(dead_code)]
|
||||
Option(Box<BidType>), // TLV tag=21
|
||||
Option(Box<BidType>), // TLV tag=21
|
||||
#[allow(dead_code)]
|
||||
Result(Box<BidType>, Box<BidType>), // TLV tag=20
|
||||
#[allow(dead_code)]
|
||||
Array(Box<BidType>), // TLV tag=22
|
||||
Array(Box<BidType>), // TLV tag=22
|
||||
}
|
||||
|
||||
/// Handle representation for efficient Box references
|
||||
@ -41,7 +41,10 @@ pub struct BidHandle {
|
||||
impl BidHandle {
|
||||
/// Create a new handle
|
||||
pub fn new(type_id: u32, instance_id: u32) -> Self {
|
||||
Self { type_id, instance_id }
|
||||
Self {
|
||||
type_id,
|
||||
instance_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pack into single u64 (type_id << 32 | instance_id)
|
||||
@ -62,15 +65,15 @@ impl BidHandle {
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BidTag {
|
||||
Bool = 1, // payload: 1 byte (0/1)
|
||||
I32 = 2, // payload: 4 bytes (little-endian)
|
||||
I64 = 3, // payload: 8 bytes (little-endian)
|
||||
F32 = 4, // payload: 4 bytes (IEEE 754)
|
||||
F64 = 5, // payload: 8 bytes (IEEE 754)
|
||||
String = 6, // payload: UTF-8 bytes
|
||||
Bytes = 7, // payload: binary data
|
||||
Handle = 8, // payload: 8 bytes (type_id + instance_id)
|
||||
Void = 9, // payload: 0 bytes
|
||||
Bool = 1, // payload: 1 byte (0/1)
|
||||
I32 = 2, // payload: 4 bytes (little-endian)
|
||||
I64 = 3, // payload: 8 bytes (little-endian)
|
||||
F32 = 4, // payload: 4 bytes (IEEE 754)
|
||||
F64 = 5, // payload: 8 bytes (IEEE 754)
|
||||
String = 6, // payload: UTF-8 bytes
|
||||
Bytes = 7, // payload: binary data
|
||||
Handle = 8, // payload: 8 bytes (type_id + instance_id)
|
||||
Void = 9, // payload: 0 bytes
|
||||
|
||||
// Phase 2 reserved
|
||||
Result = 20,
|
||||
@ -120,10 +123,10 @@ pub enum BoxTypeId {
|
||||
BoolBox = 3,
|
||||
FloatBox = 4,
|
||||
ArrayBox = 5,
|
||||
FileBox = 6, // Plugin example
|
||||
FutureBox = 7, // Existing async support
|
||||
P2PBox = 8, // Existing P2P support
|
||||
// ... more box types
|
||||
FileBox = 6, // Plugin example
|
||||
FutureBox = 7, // Existing async support
|
||||
P2PBox = 8, // Existing P2P support
|
||||
// ... more box types
|
||||
}
|
||||
|
||||
// ========== Type Information Management ==========
|
||||
@ -215,7 +218,14 @@ mod tests {
|
||||
fn test_type_tags() {
|
||||
assert_eq!(BidType::Bool.tag(), BidTag::Bool);
|
||||
assert_eq!(BidType::String.tag(), BidTag::String);
|
||||
assert_eq!(BidType::Handle { type_id: 6, instance_id: 0 }.tag(), BidTag::Handle);
|
||||
assert_eq!(
|
||||
BidType::Handle {
|
||||
type_id: 6,
|
||||
instance_id: 0
|
||||
}
|
||||
.tag(),
|
||||
BidTag::Handle
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -232,7 +242,7 @@ mod tests {
|
||||
let mapping = ArgTypeMapping::with_name(
|
||||
"content".to_string(),
|
||||
"string".to_string(),
|
||||
"string".to_string()
|
||||
"string".to_string(),
|
||||
);
|
||||
assert_eq!(mapping.name, Some("content".to_string()));
|
||||
assert_eq!(mapping.determine_bid_tag(), Some(BidTag::String));
|
||||
|
||||
@ -5,9 +5,9 @@
|
||||
* arithmetic, logical, and comparison operations between different Box types.
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, BoxCore, StringBox, IntegerBox, BoolBox, BoxBase};
|
||||
use std::fmt::{Debug, Display};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
// ===== Binary Operation Boxes =====
|
||||
|
||||
@ -34,7 +34,7 @@ impl AddBox {
|
||||
// 1. Integer + Integer
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
let result = left_int.value + right_int.value;
|
||||
return Box::new(IntegerBox::new(result));
|
||||
@ -43,7 +43,7 @@ impl AddBox {
|
||||
// 2. Float + Float (or mixed with Integer)
|
||||
if let (Some(left_float), Some(right_float)) = (
|
||||
self.left.as_any().downcast_ref::<FloatBox>(),
|
||||
self.right.as_any().downcast_ref::<FloatBox>()
|
||||
self.right.as_any().downcast_ref::<FloatBox>(),
|
||||
) {
|
||||
let result = left_float.value + right_float.value;
|
||||
return Box::new(FloatBox::new(result));
|
||||
@ -52,7 +52,7 @@ impl AddBox {
|
||||
// 3. Integer + Float
|
||||
if let (Some(left_int), Some(right_float)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<FloatBox>()
|
||||
self.right.as_any().downcast_ref::<FloatBox>(),
|
||||
) {
|
||||
let result = left_int.value as f64 + right_float.value;
|
||||
return Box::new(FloatBox::new(result));
|
||||
@ -61,7 +61,7 @@ impl AddBox {
|
||||
// 4. Float + Integer
|
||||
if let (Some(left_float), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<FloatBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
let result = left_float.value + right_int.value as f64;
|
||||
return Box::new(FloatBox::new(result));
|
||||
@ -94,8 +94,8 @@ impl NyashBox for AddBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_add) = other.as_any().downcast_ref::<AddBox>() {
|
||||
BoolBox::new(
|
||||
self.left.equals(other_add.left.as_ref()).value &&
|
||||
self.right.equals(other_add.right.as_ref()).value
|
||||
self.left.equals(other_add.left.as_ref()).value
|
||||
&& self.right.equals(other_add.right.as_ref()).value,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
@ -107,10 +107,7 @@ impl NyashBox for AddBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(AddBox::new(
|
||||
self.left.clone_box(),
|
||||
self.right.clone_box()
|
||||
))
|
||||
Box::new(AddBox::new(self.left.clone_box(), self.right.clone_box()))
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
@ -168,7 +165,7 @@ impl SubtractBox {
|
||||
// For now, only handle integer subtraction
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
let result = left_int.value - right_int.value;
|
||||
Box::new(IntegerBox::new(result))
|
||||
@ -180,7 +177,8 @@ impl SubtractBox {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>() {
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
{
|
||||
int_box.value
|
||||
} else {
|
||||
0
|
||||
@ -210,17 +208,22 @@ impl NyashBox for SubtractBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_sub) = other.as_any().downcast_ref::<SubtractBox>() {
|
||||
BoolBox::new(
|
||||
self.left.equals(other_sub.left.as_ref()).value &&
|
||||
self.right.equals(other_sub.right.as_ref()).value
|
||||
self.left.equals(other_sub.left.as_ref()).value
|
||||
&& self.right.equals(other_sub.right.as_ref()).value,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str { "SubtractBox" }
|
||||
fn type_name(&self) -> &'static str {
|
||||
"SubtractBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(SubtractBox::new(self.left.clone_box(), self.right.clone_box()))
|
||||
Box::new(SubtractBox::new(
|
||||
self.left.clone_box(),
|
||||
self.right.clone_box(),
|
||||
))
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
@ -230,13 +233,21 @@ impl NyashBox for SubtractBox {
|
||||
}
|
||||
|
||||
impl BoxCore for SubtractBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", self.to_string_box().value)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for SubtractBox {
|
||||
@ -266,7 +277,7 @@ impl MultiplyBox {
|
||||
// For now, only handle integer multiplication
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
let result = left_int.value * right_int.value;
|
||||
Box::new(IntegerBox::new(result))
|
||||
@ -277,7 +288,8 @@ impl MultiplyBox {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>() {
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
{
|
||||
int_box.value
|
||||
} else {
|
||||
0
|
||||
@ -307,17 +319,22 @@ impl NyashBox for MultiplyBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_mul) = other.as_any().downcast_ref::<MultiplyBox>() {
|
||||
BoolBox::new(
|
||||
self.left.equals(other_mul.left.as_ref()).value &&
|
||||
self.right.equals(other_mul.right.as_ref()).value
|
||||
self.left.equals(other_mul.left.as_ref()).value
|
||||
&& self.right.equals(other_mul.right.as_ref()).value,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str { "MultiplyBox" }
|
||||
fn type_name(&self) -> &'static str {
|
||||
"MultiplyBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(MultiplyBox::new(self.left.clone_box(), self.right.clone_box()))
|
||||
Box::new(MultiplyBox::new(
|
||||
self.left.clone_box(),
|
||||
self.right.clone_box(),
|
||||
))
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
@ -327,13 +344,21 @@ impl NyashBox for MultiplyBox {
|
||||
}
|
||||
|
||||
impl BoxCore for MultiplyBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", self.to_string_box().value)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for MultiplyBox {
|
||||
@ -365,7 +390,7 @@ impl DivideBox {
|
||||
// Handle integer division, but return float result
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
if right_int.value == 0 {
|
||||
// Return error for division by zero
|
||||
@ -380,7 +405,8 @@ impl DivideBox {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>() {
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
{
|
||||
int_box.value
|
||||
} else {
|
||||
1 // Avoid division by zero
|
||||
@ -413,17 +439,22 @@ impl NyashBox for DivideBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_div) = other.as_any().downcast_ref::<DivideBox>() {
|
||||
BoolBox::new(
|
||||
self.left.equals(other_div.left.as_ref()).value &&
|
||||
self.right.equals(other_div.right.as_ref()).value
|
||||
self.left.equals(other_div.left.as_ref()).value
|
||||
&& self.right.equals(other_div.right.as_ref()).value,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str { "DivideBox" }
|
||||
fn type_name(&self) -> &'static str {
|
||||
"DivideBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(DivideBox::new(self.left.clone_box(), self.right.clone_box()))
|
||||
Box::new(DivideBox::new(
|
||||
self.left.clone_box(),
|
||||
self.right.clone_box(),
|
||||
))
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
@ -433,13 +464,21 @@ impl NyashBox for DivideBox {
|
||||
}
|
||||
|
||||
impl BoxCore for DivideBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", self.to_string_box().value)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for DivideBox {
|
||||
@ -469,7 +508,7 @@ impl ModuloBox {
|
||||
// Handle integer modulo operation
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
self.left.as_any().downcast_ref::<IntegerBox>(),
|
||||
self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
self.right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
if right_int.value == 0 {
|
||||
// Return error for modulo by zero
|
||||
@ -484,7 +523,8 @@ impl ModuloBox {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>() {
|
||||
let right_val = if let Some(int_box) = self.right.as_any().downcast_ref::<IntegerBox>()
|
||||
{
|
||||
int_box.value
|
||||
} else {
|
||||
1 // Avoid modulo by zero
|
||||
@ -508,8 +548,12 @@ impl Debug for ModuloBox {
|
||||
}
|
||||
|
||||
impl BoxCore for ModuloBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "ModuloBox[{}]", self.box_id())
|
||||
}
|
||||
@ -532,8 +576,8 @@ impl NyashBox for ModuloBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_modulo) = other.as_any().downcast_ref::<ModuloBox>() {
|
||||
BoolBox::new(
|
||||
self.left.equals(other_modulo.left.as_ref()).value &&
|
||||
self.right.equals(other_modulo.right.as_ref()).value
|
||||
self.left.equals(other_modulo.left.as_ref()).value
|
||||
&& self.right.equals(other_modulo.right.as_ref()).value,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
@ -545,7 +589,10 @@ impl NyashBox for ModuloBox {
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(ModuloBox::new(self.left.clone_box(), self.right.clone_box()))
|
||||
Box::new(ModuloBox::new(
|
||||
self.left.clone_box(),
|
||||
self.right.clone_box(),
|
||||
))
|
||||
}
|
||||
|
||||
/// 仮実装: clone_boxと同じ(後で修正)
|
||||
@ -574,7 +621,7 @@ impl CompareBox {
|
||||
// Try integer comparison first
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
left.as_any().downcast_ref::<IntegerBox>(),
|
||||
right.as_any().downcast_ref::<IntegerBox>()
|
||||
right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return BoolBox::new(left_int.value < right_int.value);
|
||||
}
|
||||
@ -590,7 +637,7 @@ impl CompareBox {
|
||||
// Try integer comparison first
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
left.as_any().downcast_ref::<IntegerBox>(),
|
||||
right.as_any().downcast_ref::<IntegerBox>()
|
||||
right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return BoolBox::new(left_int.value > right_int.value);
|
||||
}
|
||||
@ -606,7 +653,7 @@ impl CompareBox {
|
||||
// Try integer comparison first
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
left.as_any().downcast_ref::<IntegerBox>(),
|
||||
right.as_any().downcast_ref::<IntegerBox>()
|
||||
right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return BoolBox::new(left_int.value <= right_int.value);
|
||||
}
|
||||
@ -622,7 +669,7 @@ impl CompareBox {
|
||||
// Try integer comparison first
|
||||
if let (Some(left_int), Some(right_int)) = (
|
||||
left.as_any().downcast_ref::<IntegerBox>(),
|
||||
right.as_any().downcast_ref::<IntegerBox>()
|
||||
right.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return BoolBox::new(left_int.value >= right_int.value);
|
||||
}
|
||||
@ -722,12 +769,12 @@ mod tests {
|
||||
#[test]
|
||||
fn test_modulo_chip8_pattern() {
|
||||
// Test Chip-8 style bit operations using modulo
|
||||
let left = Box::new(IntegerBox::new(4096)) as Box<dyn NyashBox>; // 0x1000
|
||||
let left = Box::new(IntegerBox::new(4096)) as Box<dyn NyashBox>; // 0x1000
|
||||
let right = Box::new(IntegerBox::new(4096)) as Box<dyn NyashBox>; // 0x1000
|
||||
let mod_box = ModuloBox::new(left, right);
|
||||
let result = mod_box.execute();
|
||||
|
||||
assert_eq!(result.to_string_box().value, "0"); // 4096 % 4096 = 0
|
||||
assert_eq!(result.to_string_box().value, "0"); // 4096 % 4096 = 0
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@ -7,14 +7,16 @@
|
||||
*/
|
||||
|
||||
use super::BoxFactory;
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox};
|
||||
use crate::interpreter::RuntimeError;
|
||||
|
||||
/// Factory for builtin Box types
|
||||
pub struct BuiltinBoxFactory;
|
||||
|
||||
impl BuiltinBoxFactory {
|
||||
pub fn new() -> Self { Self }
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxFactory for BuiltinBoxFactory {
|
||||
@ -57,18 +59,27 @@ impl BoxFactory for BuiltinBoxFactory {
|
||||
"NullBox" => Ok(Box::new(crate::boxes::null_box::NullBox::new())),
|
||||
|
||||
// Leave other types to other factories (user/plugin)
|
||||
_ => Err(RuntimeError::InvalidOperation { message: format!("Unknown Box type: {}", name) }),
|
||||
_ => Err(RuntimeError::InvalidOperation {
|
||||
message: format!("Unknown Box type: {}", name),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn box_types(&self) -> Vec<&str> {
|
||||
vec![
|
||||
// Primitive wrappers
|
||||
"StringBox", "IntegerBox", "BoolBox",
|
||||
"StringBox",
|
||||
"IntegerBox",
|
||||
"BoolBox",
|
||||
// Collections/common
|
||||
"ArrayBox", "MapBox", "ConsoleBox", "NullBox",
|
||||
"ArrayBox",
|
||||
"MapBox",
|
||||
"ConsoleBox",
|
||||
"NullBox",
|
||||
]
|
||||
}
|
||||
|
||||
fn is_builtin_factory(&self) -> bool { true }
|
||||
fn is_builtin_factory(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,8 +119,12 @@ impl UnifiedBoxRegistry {
|
||||
if std::env::var("NYASH_USE_PLUGIN_BUILTINS").ok().as_deref() == Some("1") {
|
||||
use crate::runtime::{get_global_registry, BoxProvider};
|
||||
// Allowlist types for override: env NYASH_PLUGIN_OVERRIDE_TYPES="ArrayBox,MapBox" (default: none)
|
||||
let allow: Vec<String> = if let Ok(list) = std::env::var("NYASH_PLUGIN_OVERRIDE_TYPES") {
|
||||
list.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect()
|
||||
let allow: Vec<String> = if let Ok(list) = std::env::var("NYASH_PLUGIN_OVERRIDE_TYPES")
|
||||
{
|
||||
list.split(',')
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
@ -128,8 +132,11 @@ impl UnifiedBoxRegistry {
|
||||
let v2 = get_global_registry();
|
||||
if let Some(provider) = v2.get_provider(name) {
|
||||
if let BoxProvider::Plugin(_lib) = provider {
|
||||
return v2.create_box(name, args)
|
||||
.map_err(|e| RuntimeError::InvalidOperation { message: format!("Plugin Box creation failed: {}", e) });
|
||||
return v2.create_box(name, args).map_err(|e| {
|
||||
RuntimeError::InvalidOperation {
|
||||
message: format!("Plugin Box creation failed: {}", e),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -185,15 +192,21 @@ impl UnifiedBoxRegistry {
|
||||
let cache = self.type_cache.read().unwrap();
|
||||
if let Some(&idx) = cache.get(name) {
|
||||
if let Some(factory) = self.factories.get(idx) {
|
||||
if factory.is_available() { return true; }
|
||||
if factory.is_available() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fallback: scan factories that can enumerate types
|
||||
for factory in &self.factories {
|
||||
if !factory.is_available() { continue; }
|
||||
if !factory.is_available() {
|
||||
continue;
|
||||
}
|
||||
let types = factory.box_types();
|
||||
if !types.is_empty() && types.contains(&name) { return true; }
|
||||
if !types.is_empty() && types.contains(&name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
@ -214,10 +227,10 @@ impl UnifiedBoxRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod builtin;
|
||||
pub mod plugin;
|
||||
/// Re-export submodules
|
||||
pub mod user_defined;
|
||||
pub mod plugin;
|
||||
pub mod builtin;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -31,7 +31,8 @@ impl BoxFactory for PluginBoxFactory {
|
||||
let registry = get_global_registry();
|
||||
|
||||
if let Some(_provider) = registry.get_provider(name) {
|
||||
registry.create_box(name, args)
|
||||
registry
|
||||
.create_box(name, args)
|
||||
.map_err(|e| RuntimeError::InvalidOperation {
|
||||
message: format!("Plugin Box creation failed: {}", e),
|
||||
})
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
|
||||
use super::BoxFactory;
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::interpreter::{RuntimeError, SharedState};
|
||||
use crate::instance_v2::InstanceBox;
|
||||
use crate::interpreter::{RuntimeError, SharedState};
|
||||
|
||||
/// Factory for user-defined Box types
|
||||
pub struct UserDefinedBoxFactory {
|
||||
@ -17,9 +17,7 @@ pub struct UserDefinedBoxFactory {
|
||||
|
||||
impl UserDefinedBoxFactory {
|
||||
pub fn new(shared_state: SharedState) -> Self {
|
||||
Self {
|
||||
shared_state,
|
||||
}
|
||||
Self { shared_state }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,12 +9,11 @@
|
||||
* static/dynamic hybrid dispatch for optimal performance.
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox};
|
||||
use crate::box_trait::{BoolBox, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::FloatBox;
|
||||
use crate::operator_traits::{
|
||||
NyashAdd, NyashSub, NyashMul, NyashDiv,
|
||||
DynamicAdd, DynamicSub, DynamicMul, DynamicDiv,
|
||||
OperatorError
|
||||
DynamicAdd, DynamicDiv, DynamicMul, DynamicSub, NyashAdd, NyashDiv, NyashMul, NyashSub,
|
||||
OperatorError,
|
||||
};
|
||||
|
||||
// ===== IntegerBox Operator Implementations =====
|
||||
@ -69,14 +68,19 @@ impl DynamicAdd for IntegerBox {
|
||||
|
||||
// IntegerBox + FloatBox -> FloatBox
|
||||
if let Some(other_float) = other.as_any().downcast_ref::<FloatBox>() {
|
||||
return Some(Box::new(FloatBox::new(self.value as f64 + other_float.value)));
|
||||
return Some(Box::new(FloatBox::new(
|
||||
self.value as f64 + other_float.value,
|
||||
)));
|
||||
}
|
||||
|
||||
// Fallback: Convert both to strings and concatenate
|
||||
// This preserves the existing AddBox behavior
|
||||
let left_str = self.to_string_box();
|
||||
let right_str = other.to_string_box();
|
||||
Some(Box::new(StringBox::new(format!("{}{}", left_str.value, right_str.value))))
|
||||
Some(Box::new(StringBox::new(format!(
|
||||
"{}{}",
|
||||
left_str.value, right_str.value
|
||||
))))
|
||||
}
|
||||
|
||||
fn can_add_with(&self, other_type: &str) -> bool {
|
||||
@ -93,7 +97,9 @@ impl DynamicSub for IntegerBox {
|
||||
|
||||
// IntegerBox - FloatBox -> FloatBox
|
||||
if let Some(other_float) = other.as_any().downcast_ref::<FloatBox>() {
|
||||
return Some(Box::new(FloatBox::new(self.value as f64 - other_float.value)));
|
||||
return Some(Box::new(FloatBox::new(
|
||||
self.value as f64 - other_float.value,
|
||||
)));
|
||||
}
|
||||
|
||||
None // Subtraction not supported for other types
|
||||
@ -113,12 +119,15 @@ impl DynamicMul for IntegerBox {
|
||||
|
||||
// IntegerBox * FloatBox -> FloatBox
|
||||
if let Some(other_float) = other.as_any().downcast_ref::<FloatBox>() {
|
||||
return Some(Box::new(FloatBox::new(self.value as f64 * other_float.value)));
|
||||
return Some(Box::new(FloatBox::new(
|
||||
self.value as f64 * other_float.value,
|
||||
)));
|
||||
}
|
||||
|
||||
// IntegerBox * StringBox -> Repeated string
|
||||
if let Some(other_str) = other.as_any().downcast_ref::<StringBox>() {
|
||||
if self.value >= 0 && self.value <= 10000 { // Safety limit
|
||||
if self.value >= 0 && self.value <= 10000 {
|
||||
// Safety limit
|
||||
let repeated = other_str.value.repeat(self.value as usize);
|
||||
return Some(Box::new(StringBox::new(repeated)));
|
||||
}
|
||||
@ -139,7 +148,9 @@ impl DynamicDiv for IntegerBox {
|
||||
if other_int.value == 0 {
|
||||
return None; // Division by zero
|
||||
}
|
||||
return Some(Box::new(FloatBox::new(self.value as f64 / other_int.value as f64)));
|
||||
return Some(Box::new(FloatBox::new(
|
||||
self.value as f64 / other_int.value as f64,
|
||||
)));
|
||||
}
|
||||
|
||||
// IntegerBox / FloatBox -> FloatBox
|
||||
@ -147,7 +158,9 @@ impl DynamicDiv for IntegerBox {
|
||||
if other_float.value == 0.0 {
|
||||
return None; // Division by zero
|
||||
}
|
||||
return Some(Box::new(FloatBox::new(self.value as f64 / other_float.value)));
|
||||
return Some(Box::new(FloatBox::new(
|
||||
self.value as f64 / other_float.value,
|
||||
)));
|
||||
}
|
||||
|
||||
None
|
||||
@ -217,7 +230,10 @@ impl DynamicAdd for FloatBox {
|
||||
// Fallback: Convert both to strings and concatenate
|
||||
let left_str = self.to_string_box();
|
||||
let right_str = other.to_string_box();
|
||||
Some(Box::new(StringBox::new(format!("{}{}", left_str.value, right_str.value))))
|
||||
Some(Box::new(StringBox::new(format!(
|
||||
"{}{}",
|
||||
left_str.value, right_str.value
|
||||
))))
|
||||
}
|
||||
|
||||
fn can_add_with(&self, other_type: &str) -> bool {
|
||||
@ -307,7 +323,8 @@ impl NyashMul<IntegerBox> for StringBox {
|
||||
type Output = StringBox;
|
||||
|
||||
fn mul(self, rhs: IntegerBox) -> Self::Output {
|
||||
if rhs.value >= 0 && rhs.value <= 10000 { // Safety limit
|
||||
if rhs.value >= 0 && rhs.value <= 10000 {
|
||||
// Safety limit
|
||||
StringBox::new(self.value.repeat(rhs.value as usize))
|
||||
} else {
|
||||
StringBox::new(String::new()) // Empty string for invalid repetition
|
||||
@ -320,12 +337,18 @@ impl DynamicAdd for StringBox {
|
||||
fn try_add(&self, other: &dyn NyashBox) -> Option<Box<dyn NyashBox>> {
|
||||
// StringBox + StringBox
|
||||
if let Some(other_str) = other.as_any().downcast_ref::<StringBox>() {
|
||||
return Some(Box::new(StringBox::new(format!("{}{}", self.value, other_str.value))));
|
||||
return Some(Box::new(StringBox::new(format!(
|
||||
"{}{}",
|
||||
self.value, other_str.value
|
||||
))));
|
||||
}
|
||||
|
||||
// StringBox + any other type -> Convert to string and concatenate
|
||||
let other_str = other.to_string_box();
|
||||
Some(Box::new(StringBox::new(format!("{}{}", self.value, other_str.value))))
|
||||
Some(Box::new(StringBox::new(format!(
|
||||
"{}{}",
|
||||
self.value, other_str.value
|
||||
))))
|
||||
}
|
||||
|
||||
fn can_add_with(&self, _other_type: &str) -> bool {
|
||||
@ -347,7 +370,8 @@ impl DynamicMul for StringBox {
|
||||
fn try_mul(&self, other: &dyn NyashBox) -> Option<Box<dyn NyashBox>> {
|
||||
// StringBox * IntegerBox -> Repeated string
|
||||
if let Some(other_int) = other.as_any().downcast_ref::<IntegerBox>() {
|
||||
if other_int.value >= 0 && other_int.value <= 10000 { // Safety limit
|
||||
if other_int.value >= 0 && other_int.value <= 10000 {
|
||||
// Safety limit
|
||||
let repeated = self.value.repeat(other_int.value as usize);
|
||||
return Some(Box::new(StringBox::new(repeated)));
|
||||
}
|
||||
@ -400,7 +424,10 @@ impl DynamicAdd for BoolBox {
|
||||
// Fallback to string concatenation
|
||||
let left_str = self.to_string_box();
|
||||
let right_str = other.to_string_box();
|
||||
Some(Box::new(StringBox::new(format!("{}{}", left_str.value, right_str.value))))
|
||||
Some(Box::new(StringBox::new(format!(
|
||||
"{}{}",
|
||||
left_str.value, right_str.value
|
||||
))))
|
||||
}
|
||||
|
||||
fn can_add_with(&self, other_type: &str) -> bool {
|
||||
@ -499,7 +526,10 @@ impl OperatorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(float_box) = right.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>() {
|
||||
if let Some(float_box) = right
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
{
|
||||
if let Some(result) = float_box.try_add(right) {
|
||||
return Ok(result);
|
||||
}
|
||||
@ -530,7 +560,10 @@ impl OperatorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(float_box) = left.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>() {
|
||||
if let Some(float_box) = left
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
{
|
||||
if let Some(result) = float_box.try_sub(right) {
|
||||
return Ok(result);
|
||||
}
|
||||
@ -561,7 +594,10 @@ impl OperatorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(float_box) = left.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>() {
|
||||
if let Some(float_box) = left
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
{
|
||||
if let Some(result) = float_box.try_mul(right) {
|
||||
return Ok(result);
|
||||
}
|
||||
@ -595,7 +631,10 @@ impl OperatorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(float_box) = left.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>() {
|
||||
if let Some(float_box) = left
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
{
|
||||
if let Some(result) = float_box.try_div(right) {
|
||||
return Ok(result);
|
||||
} else {
|
||||
|
||||
@ -6,12 +6,12 @@
|
||||
* implements the NyashBox trait.
|
||||
*/
|
||||
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
// 🔥 新しい型エイリアス - 将来的にBox<dyn NyashBox>を全て置き換える
|
||||
pub type SharedNyashBox = Arc<dyn NyashBox>;
|
||||
@ -26,14 +26,38 @@ pub fn next_box_id() -> u64 {
|
||||
/// 🔥 Phase 8.8: pack透明化システム - ビルトインBox判定リスト
|
||||
/// ユーザーは`pack`を一切意識せず、`from BuiltinBox()`で自動的に内部のpack機能が呼ばれる
|
||||
pub const BUILTIN_BOXES: &[&str] = &[
|
||||
"StringBox", "IntegerBox", "BoolBox", "NullBox",
|
||||
"ArrayBox", "MapBox", "FileBox", "ResultBox",
|
||||
"FutureBox", "ChannelBox", "MathBox", "FloatBox",
|
||||
"TimeBox", "DateTimeBox", "TimerBox", "RandomBox",
|
||||
"SoundBox", "DebugBox", "MethodBox", "ConsoleBox",
|
||||
"BufferBox", "RegexBox", "JSONBox", "StreamBox",
|
||||
"HTTPClientBox", "IntentBox", "P2PBox", "SocketBox",
|
||||
"HTTPServerBox", "HTTPRequestBox", "HTTPResponseBox", "JitConfigBox"
|
||||
"StringBox",
|
||||
"IntegerBox",
|
||||
"BoolBox",
|
||||
"NullBox",
|
||||
"ArrayBox",
|
||||
"MapBox",
|
||||
"FileBox",
|
||||
"ResultBox",
|
||||
"FutureBox",
|
||||
"ChannelBox",
|
||||
"MathBox",
|
||||
"FloatBox",
|
||||
"TimeBox",
|
||||
"DateTimeBox",
|
||||
"TimerBox",
|
||||
"RandomBox",
|
||||
"SoundBox",
|
||||
"DebugBox",
|
||||
"MethodBox",
|
||||
"ConsoleBox",
|
||||
"BufferBox",
|
||||
"RegexBox",
|
||||
"JSONBox",
|
||||
"StreamBox",
|
||||
"HTTPClientBox",
|
||||
"IntentBox",
|
||||
"P2PBox",
|
||||
"SocketBox",
|
||||
"HTTPServerBox",
|
||||
"HTTPRequestBox",
|
||||
"HTTPResponseBox",
|
||||
"JitConfigBox",
|
||||
];
|
||||
|
||||
/// 🔥 ビルトインBox判定関数 - pack透明化システムの核心
|
||||
@ -110,11 +134,17 @@ pub trait NyashBox: BoxCore + Debug {
|
||||
fn share_box(&self) -> Box<dyn NyashBox>;
|
||||
|
||||
/// Identity hint: boxes that wrap external/stateful handles should override to return true.
|
||||
fn is_identity(&self) -> bool { false }
|
||||
fn is_identity(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Helper: pick share or clone based on identity semantics.
|
||||
fn clone_or_share(&self) -> Box<dyn NyashBox> {
|
||||
if self.is_identity() { self.share_box() } else { self.clone_box() }
|
||||
if self.is_identity() {
|
||||
self.share_box()
|
||||
} else {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
/// Arc参照を返す新しいcloneメソッド(参照共有)
|
||||
@ -154,7 +184,8 @@ impl StringBox {
|
||||
/// Split string by delimiter and return ArrayBox
|
||||
pub fn split(&self, delimiter: &str) -> Box<dyn NyashBox> {
|
||||
let parts: Vec<String> = self.value.split(delimiter).map(|s| s.to_string()).collect();
|
||||
let array_elements: Vec<Box<dyn NyashBox>> = parts.into_iter()
|
||||
let array_elements: Vec<Box<dyn NyashBox>> = parts
|
||||
.into_iter()
|
||||
.map(|s| Box::new(StringBox::new(s)) as Box<dyn NyashBox>)
|
||||
.collect();
|
||||
Box::new(ArrayBox::new_with_elements(array_elements))
|
||||
@ -206,7 +237,10 @@ impl StringBox {
|
||||
/// Join array elements using this string as delimiter
|
||||
pub fn join(&self, array_box: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(array) = array_box.as_any().downcast_ref::<ArrayBox>() {
|
||||
let strings: Vec<String> = array.items.read().unwrap()
|
||||
let strings: Vec<String> = array
|
||||
.items
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|element| element.to_string_box().value)
|
||||
.collect();
|
||||
@ -318,7 +352,7 @@ impl IntegerBox {
|
||||
pub fn new(value: i64) -> Self {
|
||||
Self {
|
||||
value,
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -393,7 +427,7 @@ impl BoolBox {
|
||||
pub fn new(value: bool) -> Self {
|
||||
Self {
|
||||
value,
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -470,7 +504,7 @@ pub struct VoidBox {
|
||||
impl VoidBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -693,7 +727,9 @@ impl NyashBox for ErrorBox {
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_error) = other.as_any().downcast_ref::<ErrorBox>() {
|
||||
BoolBox::new(self.error_type == other_error.error_type && self.message == other_error.message)
|
||||
BoolBox::new(
|
||||
self.error_type == other_error.error_type && self.message == other_error.message,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
@ -719,12 +755,13 @@ impl Display for ErrorBox {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// FutureBox is now implemented in src/boxes/future/mod.rs using RwLock pattern
|
||||
// and re-exported from src/boxes/mod.rs as both NyashFutureBox and FutureBox
|
||||
|
||||
// Re-export operation boxes from the dedicated operations module
|
||||
pub use crate::box_arithmetic::{AddBox, SubtractBox, MultiplyBox, DivideBox, ModuloBox, CompareBox};
|
||||
pub use crate::box_arithmetic::{
|
||||
AddBox, CompareBox, DivideBox, ModuloBox, MultiplyBox, SubtractBox,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -1,25 +1,55 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AotCompilerBox { base: BoxBase }
|
||||
pub struct AotCompilerBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl AotCompilerBox { pub fn new() -> Self { Self { base: BoxBase::new() } } }
|
||||
impl AotCompilerBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for AotCompilerBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "AotCompilerBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "AotCompilerBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for AotCompilerBox {
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new("AotCompilerBox".to_string()) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<AotCompilerBox>()) }
|
||||
fn type_name(&self) -> &'static str { "AotCompilerBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new("AotCompilerBox".to_string())
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<AotCompilerBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"AotCompilerBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl AotCompilerBox {
|
||||
@ -28,26 +58,32 @@ impl AotCompilerBox {
|
||||
pub fn compile(&self, file: &str, out: &str) -> Box<dyn NyashBox> {
|
||||
let mut cmd = match std::env::current_exe() {
|
||||
Ok(p) => std::process::Command::new(p),
|
||||
Err(e) => return Box::new(StringBox::new(format!("ERR: current_exe(): {}", e)))
|
||||
Err(e) => return Box::new(StringBox::new(format!("ERR: current_exe(): {}", e))),
|
||||
};
|
||||
// Propagate relevant envs (AOT/JIT observe)
|
||||
let c = cmd.arg("--backend").arg("vm") // ensures runner path
|
||||
.arg("--compile-native")
|
||||
.arg("-o").arg(out)
|
||||
.arg(file)
|
||||
.envs(std::env::vars());
|
||||
let c = cmd
|
||||
.arg("--backend")
|
||||
.arg("vm") // ensures runner path
|
||||
.arg("--compile-native")
|
||||
.arg("-o")
|
||||
.arg(out)
|
||||
.arg(file)
|
||||
.envs(std::env::vars());
|
||||
match c.output() {
|
||||
Ok(o) => {
|
||||
let mut s = String::new();
|
||||
s.push_str(&String::from_utf8_lossy(&o.stdout));
|
||||
s.push_str(&String::from_utf8_lossy(&o.stderr));
|
||||
if !o.status.success() {
|
||||
s = format!("AOT FAILED (code={}):\n{}", o.status.code().unwrap_or(-1), s);
|
||||
s = format!(
|
||||
"AOT FAILED (code={}):\n{}",
|
||||
o.status.code().unwrap_or(-1),
|
||||
s
|
||||
);
|
||||
}
|
||||
Box::new(StringBox::new(s))
|
||||
}
|
||||
Err(e) => Box::new(StringBox::new(format!("ERR: spawn compile-native: {}", e)))
|
||||
Err(e) => Box::new(StringBox::new(format!("ERR: spawn compile-native: {}", e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -10,44 +10,104 @@ pub struct AotConfigBox {
|
||||
pub plugin_paths: Option<String>,
|
||||
}
|
||||
|
||||
impl AotConfigBox { pub fn new() -> Self { Self { base: BoxBase::new(), output_file: None, emit_obj_out: None, plugin_paths: None } } }
|
||||
impl AotConfigBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
output_file: None,
|
||||
emit_obj_out: None,
|
||||
plugin_paths: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for AotConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "AotConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "AotConfigBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for AotConfigBox {
|
||||
fn to_string_box(&self) -> StringBox { self.summary() }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<AotConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "AotConfigBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone(), output_file: self.output_file.clone(), emit_obj_out: self.emit_obj_out.clone(), plugin_paths: self.plugin_paths.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
self.summary()
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<AotConfigBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"AotConfigBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
output_file: self.output_file.clone(),
|
||||
emit_obj_out: self.emit_obj_out.clone(),
|
||||
plugin_paths: self.plugin_paths.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl AotConfigBox {
|
||||
pub fn set_output(&mut self, path: &str) -> Box<dyn NyashBox> { self.output_file = Some(path.to_string()); Box::new(VoidBox::new()) }
|
||||
pub fn set_obj_out(&mut self, path: &str) -> Box<dyn NyashBox> { self.emit_obj_out = Some(path.to_string()); Box::new(VoidBox::new()) }
|
||||
pub fn set_plugin_paths(&mut self, paths: &str) -> Box<dyn NyashBox> { self.plugin_paths = Some(paths.to_string()); Box::new(VoidBox::new()) }
|
||||
pub fn clear(&mut self) -> Box<dyn NyashBox> { self.output_file = None; self.emit_obj_out = None; self.plugin_paths = None; Box::new(VoidBox::new()) }
|
||||
pub fn set_output(&mut self, path: &str) -> Box<dyn NyashBox> {
|
||||
self.output_file = Some(path.to_string());
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn set_obj_out(&mut self, path: &str) -> Box<dyn NyashBox> {
|
||||
self.emit_obj_out = Some(path.to_string());
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn set_plugin_paths(&mut self, paths: &str) -> Box<dyn NyashBox> {
|
||||
self.plugin_paths = Some(paths.to_string());
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn clear(&mut self) -> Box<dyn NyashBox> {
|
||||
self.output_file = None;
|
||||
self.emit_obj_out = None;
|
||||
self.plugin_paths = None;
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
/// Apply staged config to environment for CLI/runner consumption
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
if let Some(p) = &self.output_file { std::env::set_var("NYASH_AOT_OUT", p); }
|
||||
if let Some(p) = &self.emit_obj_out { std::env::set_var("NYASH_AOT_OBJECT_OUT", p); }
|
||||
if let Some(p) = &self.plugin_paths { std::env::set_var("NYASH_PLUGIN_PATHS", p); }
|
||||
if let Some(p) = &self.output_file {
|
||||
std::env::set_var("NYASH_AOT_OUT", p);
|
||||
}
|
||||
if let Some(p) = &self.emit_obj_out {
|
||||
std::env::set_var("NYASH_AOT_OBJECT_OUT", p);
|
||||
}
|
||||
if let Some(p) = &self.plugin_paths {
|
||||
std::env::set_var("NYASH_PLUGIN_PATHS", p);
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn summary(&self) -> StringBox {
|
||||
let s = format!(
|
||||
"output={} obj_out={} plugin_paths={}",
|
||||
self.output_file.clone().unwrap_or_else(|| "<none>".to_string()),
|
||||
self.emit_obj_out.clone().unwrap_or_else(|| "<none>".to_string()),
|
||||
self.plugin_paths.clone().unwrap_or_else(|| "<none>".to_string()),
|
||||
self.output_file
|
||||
.clone()
|
||||
.unwrap_or_else(|| "<none>".to_string()),
|
||||
self.emit_obj_out
|
||||
.clone()
|
||||
.unwrap_or_else(|| "<none>".to_string()),
|
||||
self.plugin_paths
|
||||
.clone()
|
||||
.unwrap_or_else(|| "<none>".to_string()),
|
||||
);
|
||||
StringBox::new(s)
|
||||
}
|
||||
|
||||
@ -2,13 +2,13 @@
|
||||
// Nyashの箱システムによる配列・リスト操作を提供します。
|
||||
// RwLockパターンで内部可変性を実現(Phase 9.75-B Arc<Mutex>削除)
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::fmt::Display;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub struct ArrayBox {
|
||||
pub items: Arc<RwLock<Vec<Box<dyn NyashBox>>>>, // Arc追加
|
||||
pub items: Arc<RwLock<Vec<Box<dyn NyashBox>>>>, // Arc追加
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ impl ArrayBox {
|
||||
/// 新しいArrayBoxを作成
|
||||
pub fn new() -> Self {
|
||||
ArrayBox {
|
||||
items: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
|
||||
items: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -24,7 +24,7 @@ impl ArrayBox {
|
||||
/// 要素を持つArrayBoxを作成
|
||||
pub fn new_with_elements(elements: Vec<Box<dyn NyashBox>>) -> Self {
|
||||
ArrayBox {
|
||||
items: Arc::new(RwLock::new(elements)), // Arc::new追加
|
||||
items: Arc::new(RwLock::new(elements)), // Arc::new追加
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -61,7 +61,11 @@ impl ArrayBox {
|
||||
match items.get(idx) {
|
||||
Some(item) => {
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
|
||||
if item
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
.is_some()
|
||||
{
|
||||
return item.share_box();
|
||||
}
|
||||
item.clone_box()
|
||||
@ -161,32 +165,44 @@ impl ArrayBox {
|
||||
// Try to compare as numbers first
|
||||
if let (Some(a_int), Some(b_int)) = (
|
||||
a.as_any().downcast_ref::<IntegerBox>(),
|
||||
b.as_any().downcast_ref::<IntegerBox>()
|
||||
b.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return a_int.value.cmp(&b_int.value);
|
||||
}
|
||||
|
||||
// Try FloatBox comparison
|
||||
if let (Some(a_float), Some(b_float)) = (
|
||||
a.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
b.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
a.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
b.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
) {
|
||||
return a_float.value.partial_cmp(&b_float.value).unwrap_or(Ordering::Equal);
|
||||
return a_float
|
||||
.value
|
||||
.partial_cmp(&b_float.value)
|
||||
.unwrap_or(Ordering::Equal);
|
||||
}
|
||||
|
||||
// Mixed numeric types
|
||||
if let (Some(a_int), Some(b_float)) = (
|
||||
a.as_any().downcast_ref::<IntegerBox>(),
|
||||
b.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>()
|
||||
b.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
) {
|
||||
return (a_int.value as f64).partial_cmp(&b_float.value).unwrap_or(Ordering::Equal);
|
||||
return (a_int.value as f64)
|
||||
.partial_cmp(&b_float.value)
|
||||
.unwrap_or(Ordering::Equal);
|
||||
}
|
||||
|
||||
if let (Some(a_float), Some(b_int)) = (
|
||||
a.as_any().downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
b.as_any().downcast_ref::<IntegerBox>()
|
||||
a.as_any()
|
||||
.downcast_ref::<crate::boxes::math_box::FloatBox>(),
|
||||
b.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
return a_float.value.partial_cmp(&(b_int.value as f64)).unwrap_or(Ordering::Equal);
|
||||
return a_float
|
||||
.value
|
||||
.partial_cmp(&(b_int.value as f64))
|
||||
.unwrap_or(Ordering::Equal);
|
||||
}
|
||||
|
||||
// Fall back to string comparison
|
||||
@ -217,7 +233,9 @@ impl ArrayBox {
|
||||
start_int.value as usize
|
||||
}
|
||||
} else {
|
||||
return Box::new(StringBox::new("Error: slice() start index must be an integer"));
|
||||
return Box::new(StringBox::new(
|
||||
"Error: slice() start index must be an integer",
|
||||
));
|
||||
};
|
||||
|
||||
let end_idx = if let Some(end_int) = end.as_any().downcast_ref::<IntegerBox>() {
|
||||
@ -227,7 +245,9 @@ impl ArrayBox {
|
||||
(end_int.value as usize).min(items.len())
|
||||
}
|
||||
} else {
|
||||
return Box::new(StringBox::new("Error: slice() end index must be an integer"));
|
||||
return Box::new(StringBox::new(
|
||||
"Error: slice() end index must be an integer",
|
||||
));
|
||||
};
|
||||
|
||||
// Validate indices
|
||||
@ -240,7 +260,11 @@ impl ArrayBox {
|
||||
.iter()
|
||||
.map(|item| {
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
|
||||
if item
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
.is_some()
|
||||
{
|
||||
return item.share_box();
|
||||
}
|
||||
item.clone_box()
|
||||
@ -256,18 +280,23 @@ impl Clone for ArrayBox {
|
||||
fn clone(&self) -> Self {
|
||||
// ディープコピー(独立インスタンス)
|
||||
let items_guard = self.items.read().unwrap();
|
||||
let cloned_items: Vec<Box<dyn NyashBox>> = items_guard.iter()
|
||||
let cloned_items: Vec<Box<dyn NyashBox>> = items_guard
|
||||
.iter()
|
||||
.map(|item| {
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
if item.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
|
||||
if item
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
.is_some()
|
||||
{
|
||||
return item.share_box();
|
||||
}
|
||||
item.clone_box()
|
||||
}) // 要素もディープコピー(ハンドルは共有)
|
||||
}) // 要素もディープコピー(ハンドルは共有)
|
||||
.collect();
|
||||
|
||||
ArrayBox {
|
||||
items: Arc::new(RwLock::new(cloned_items)), // 新しいArc
|
||||
items: Arc::new(RwLock::new(cloned_items)), // 新しいArc
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -284,7 +313,8 @@ impl BoxCore for ArrayBox {
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let items = self.items.read().unwrap();
|
||||
let strings: Vec<String> = items.iter()
|
||||
let strings: Vec<String> = items
|
||||
.iter()
|
||||
.map(|item| item.to_string_box().value)
|
||||
.collect();
|
||||
write!(f, "[{}]", strings.join(", "))
|
||||
@ -306,7 +336,9 @@ impl Display for ArrayBox {
|
||||
}
|
||||
|
||||
impl NyashBox for ArrayBox {
|
||||
fn is_identity(&self) -> bool { true }
|
||||
fn is_identity(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
@ -314,26 +346,25 @@ impl NyashBox for ArrayBox {
|
||||
/// 🎯 状態共有の核心実装
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
let new_instance = ArrayBox {
|
||||
items: Arc::clone(&self.items), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
items: Arc::clone(&self.items), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
};
|
||||
Box::new(new_instance)
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
let items = self.items.read().unwrap();
|
||||
let strings: Vec<String> = items.iter()
|
||||
let strings: Vec<String> = items
|
||||
.iter()
|
||||
.map(|item| item.to_string_box().value)
|
||||
.collect();
|
||||
StringBox::new(format!("[{}]", strings.join(", ")))
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"ArrayBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_array) = other.as_any().downcast_ref::<ArrayBox>() {
|
||||
let self_items = self.items.read().unwrap();
|
||||
|
||||
@ -50,7 +50,7 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@ -58,8 +58,8 @@ use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_sys::{
|
||||
AudioContext, AudioBuffer, AudioBufferSourceNode, GainNode,
|
||||
AnalyserNode, AudioDestinationNode, PeriodicWave, OscillatorNode
|
||||
AnalyserNode, AudioBuffer, AudioBufferSourceNode, AudioContext, AudioDestinationNode, GainNode,
|
||||
OscillatorNode, PeriodicWave,
|
||||
};
|
||||
|
||||
/// 音声管理Box
|
||||
@ -109,8 +109,11 @@ impl AudioBox {
|
||||
gain.gain().set_value(self.volume as f32);
|
||||
|
||||
// ノード接続
|
||||
oscillator.connect_with_audio_node(&gain).unwrap_or_default();
|
||||
gain.connect_with_audio_node(&context.destination()).unwrap_or_default();
|
||||
oscillator
|
||||
.connect_with_audio_node(&gain)
|
||||
.unwrap_or_default();
|
||||
gain.connect_with_audio_node(&context.destination())
|
||||
.unwrap_or_default();
|
||||
|
||||
// 再生
|
||||
let start_time = context.current_time();
|
||||
@ -153,7 +156,8 @@ impl AudioBox {
|
||||
if let Ok(gain) = context.create_gain() {
|
||||
gain.gain().set_value(self.volume as f32);
|
||||
source.connect_with_audio_node(&gain).unwrap_or_default();
|
||||
gain.connect_with_audio_node(&context.destination()).unwrap_or_default();
|
||||
gain.connect_with_audio_node(&context.destination())
|
||||
.unwrap_or_default();
|
||||
|
||||
source.start().unwrap_or_default();
|
||||
return true;
|
||||
@ -201,7 +205,10 @@ impl AudioBox {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
/// Non-WASM環境用のダミー実装
|
||||
pub fn create_tone(&self, frequency: f64, duration: f64) -> bool {
|
||||
println!("AudioBox: Playing tone {}Hz for {}ms (simulated)", frequency, duration);
|
||||
println!(
|
||||
"AudioBox: Playing tone {}Hz for {}ms (simulated)",
|
||||
frequency, duration
|
||||
);
|
||||
true
|
||||
}
|
||||
|
||||
@ -220,13 +227,17 @@ impl AudioBox {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn get_frequency_data(&self) -> Vec<u8> {
|
||||
// シミュレーション用のダミーデータ
|
||||
(0..64).map(|i| ((i as f64 * 4.0).sin() * 128.0 + 128.0) as u8).collect()
|
||||
(0..64)
|
||||
.map(|i| ((i as f64 * 4.0).sin() * 128.0 + 128.0) as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn get_waveform_data(&self) -> Vec<u8> {
|
||||
// シミュレーション用のダミーデータ
|
||||
(0..128).map(|i| ((i as f64 * 0.1).sin() * 64.0 + 128.0) as u8).collect()
|
||||
(0..128)
|
||||
.map(|i| ((i as f64 * 0.1).sin() * 64.0 + 128.0) as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// オーディオコンテキストの状態を確認
|
||||
@ -266,7 +277,11 @@ impl BoxCore for AudioBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "AudioBox(volume={:.2}, playing={})", self.volume, self.is_playing)
|
||||
write!(
|
||||
f,
|
||||
"AudioBox(volume={:.2}, playing={})",
|
||||
self.volume, self.is_playing
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
@ -289,7 +304,10 @@ impl NyashBox for AudioBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("AudioBox(volume={:.2}, playing={})", self.volume, self.is_playing))
|
||||
StringBox::new(format!(
|
||||
"AudioBox(volume={:.2}, playing={})",
|
||||
self.volume, self.is_playing
|
||||
))
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
|
||||
@ -38,7 +38,7 @@
|
||||
* - `a or b` - OR演算子
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoxBase, BoxCore, NyashBox};
|
||||
use std::any::Any;
|
||||
use std::fmt::Display;
|
||||
|
||||
@ -83,7 +83,6 @@ impl NyashBox for BoolBox {
|
||||
"BoolBox"
|
||||
}
|
||||
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
@ -28,21 +28,21 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, RwLock}; // Arc追加
|
||||
use std::fmt::Display;
|
||||
use std::sync::{Arc, RwLock}; // Arc追加
|
||||
|
||||
pub struct BufferBox {
|
||||
data: Arc<RwLock<Vec<u8>>>, // Arc追加
|
||||
data: Arc<RwLock<Vec<u8>>>, // Arc追加
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl BufferBox {
|
||||
pub fn new() -> Self {
|
||||
BufferBox {
|
||||
data: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
|
||||
data: Arc::new(RwLock::new(Vec::new())), // Arc::new追加
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -59,7 +59,7 @@ impl BufferBox {
|
||||
|
||||
pub fn from_vec(data: Vec<u8>) -> Self {
|
||||
BufferBox {
|
||||
data: Arc::new(RwLock::new(data)), // Arc::new追加
|
||||
data: Arc::new(RwLock::new(data)), // Arc::new追加
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -67,7 +67,10 @@ impl BufferBox {
|
||||
/// データを書き込む
|
||||
pub fn write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
// ArrayBoxから変換 - use crate::boxes::array::ArrayBox directly
|
||||
if let Some(array_box) = data.as_any().downcast_ref::<crate::boxes::array::ArrayBox>() {
|
||||
if let Some(array_box) = data
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::array::ArrayBox>()
|
||||
{
|
||||
let mut buffer = self.data.write().unwrap();
|
||||
let items = array_box.items.read().unwrap();
|
||||
for item in items.iter() {
|
||||
@ -80,7 +83,10 @@ impl BufferBox {
|
||||
Box::new(IntegerBox::new(buffer.len() as i64))
|
||||
} else {
|
||||
let type_name = data.type_name();
|
||||
Box::new(StringBox::new(&format!("Error: write() requires ArrayBox of integers, got {}", type_name)))
|
||||
Box::new(StringBox::new(&format!(
|
||||
"Error: write() requires ArrayBox of integers, got {}",
|
||||
type_name
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,7 +145,7 @@ impl BufferBox {
|
||||
pub fn slice(&self, start: Box<dyn NyashBox>, end: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let (Some(start_int), Some(end_int)) = (
|
||||
start.as_any().downcast_ref::<IntegerBox>(),
|
||||
end.as_any().downcast_ref::<IntegerBox>()
|
||||
end.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
let data = self.data.read().unwrap();
|
||||
let start = (start_int.value as usize).min(data.len());
|
||||
@ -173,7 +179,7 @@ impl BufferBox {
|
||||
// Create a new BufferBox that shares the same Arc as this buffer
|
||||
let shared_buffer = BufferBox {
|
||||
data: Arc::clone(&self.data), // Share THIS buffer's data
|
||||
base: BoxBase::new(), // New ID but shared data
|
||||
base: BoxBase::new(), // New ID but shared data
|
||||
};
|
||||
Box::new(shared_buffer)
|
||||
}
|
||||
@ -192,7 +198,7 @@ impl Clone for BufferBox {
|
||||
// ディープコピー(独立インスタンス)
|
||||
let data_guard = self.data.read().unwrap();
|
||||
BufferBox {
|
||||
data: Arc::new(RwLock::new(data_guard.clone())), // 新しいArc
|
||||
data: Arc::new(RwLock::new(data_guard.clone())), // 新しいArc
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -235,8 +241,8 @@ impl NyashBox for BufferBox {
|
||||
/// 🎯 状态共享的核心实现
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
let new_instance = BufferBox {
|
||||
data: Arc::clone(&self.data), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
data: Arc::clone(&self.data), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
};
|
||||
Box::new(new_instance)
|
||||
}
|
||||
@ -246,12 +252,10 @@ impl NyashBox for BufferBox {
|
||||
StringBox::new(format!("BufferBox({} bytes)", data.len()))
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"BufferBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_buffer) = other.as_any().downcast_ref::<BufferBox>() {
|
||||
// RwLock内容を比較
|
||||
|
||||
@ -61,17 +61,14 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_sys::{
|
||||
HtmlCanvasElement, MouseEvent, TouchEvent, KeyboardEvent,
|
||||
EventTarget, Element
|
||||
};
|
||||
use web_sys::{Element, EventTarget, HtmlCanvasElement, KeyboardEvent, MouseEvent, TouchEvent};
|
||||
|
||||
/// Canvas入力イベント管理Box
|
||||
#[derive(Debug, Clone)]
|
||||
@ -140,7 +137,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(MouseEvent)>);
|
||||
|
||||
canvas.add_event_listener_with_callback("mousedown", closure.as_ref().unchecked_ref())
|
||||
canvas
|
||||
.add_event_listener_with_callback("mousedown", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget(); // メモリリークを防ぐため通常は適切な管理が必要
|
||||
}
|
||||
@ -154,7 +152,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(MouseEvent)>);
|
||||
|
||||
canvas.add_event_listener_with_callback("mouseup", closure.as_ref().unchecked_ref())
|
||||
canvas
|
||||
.add_event_listener_with_callback("mouseup", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget();
|
||||
}
|
||||
@ -168,7 +167,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(MouseEvent)>);
|
||||
|
||||
canvas.add_event_listener_with_callback("mousemove", closure.as_ref().unchecked_ref())
|
||||
canvas
|
||||
.add_event_listener_with_callback("mousemove", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget();
|
||||
}
|
||||
@ -182,7 +182,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(MouseEvent)>);
|
||||
|
||||
canvas.add_event_listener_with_callback("click", closure.as_ref().unchecked_ref())
|
||||
canvas
|
||||
.add_event_listener_with_callback("click", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget();
|
||||
}
|
||||
@ -196,7 +197,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(TouchEvent)>);
|
||||
|
||||
canvas.add_event_listener_with_callback("touchstart", closure.as_ref().unchecked_ref())
|
||||
canvas
|
||||
.add_event_listener_with_callback("touchstart", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget();
|
||||
}
|
||||
@ -210,7 +212,8 @@ impl CanvasEventBox {
|
||||
callback.call0(&JsValue::NULL).unwrap_or_default();
|
||||
}) as Box<dyn FnMut(KeyboardEvent)>);
|
||||
|
||||
window.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())
|
||||
window
|
||||
.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref())
|
||||
.unwrap_or_default();
|
||||
closure.forget();
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use crate::boxes::TimerBox;
|
||||
use std::any::Any;
|
||||
|
||||
@ -108,10 +108,14 @@ impl CanvasLoopBox {
|
||||
// アニメーションフレーム用のクロージャを作成
|
||||
let closure = Closure::wrap(Box::new(move |time: f64| {
|
||||
// ここでフレーム処理を実行
|
||||
callback.call1(&JsValue::NULL, &JsValue::from_f64(time)).unwrap_or_default();
|
||||
callback
|
||||
.call1(&JsValue::NULL, &JsValue::from_f64(time))
|
||||
.unwrap_or_default();
|
||||
}) as Box<dyn FnMut(f64)>);
|
||||
|
||||
let id = self.timer.request_animation_frame(closure.as_ref().unchecked_ref());
|
||||
let id = self
|
||||
.timer
|
||||
.request_animation_frame(closure.as_ref().unchecked_ref());
|
||||
self.animation_id = Some(id);
|
||||
|
||||
closure.forget(); // クロージャの所有権を手放す
|
||||
@ -170,10 +174,14 @@ impl CanvasLoopBox {
|
||||
self.last_frame_time = self.timer.now(); // 時間をリセット
|
||||
|
||||
let closure = Closure::wrap(Box::new(move |time: f64| {
|
||||
callback.call1(&JsValue::NULL, &JsValue::from_f64(time)).unwrap_or_default();
|
||||
callback
|
||||
.call1(&JsValue::NULL, &JsValue::from_f64(time))
|
||||
.unwrap_or_default();
|
||||
}) as Box<dyn FnMut(f64)>);
|
||||
|
||||
let id = self.timer.request_animation_frame(closure.as_ref().unchecked_ref());
|
||||
let id = self
|
||||
.timer
|
||||
.request_animation_frame(closure.as_ref().unchecked_ref());
|
||||
self.animation_id = Some(id);
|
||||
|
||||
closure.forget();
|
||||
@ -268,7 +276,11 @@ impl BoxCore for CanvasLoopBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "CanvasLoopBox(running={}, fps={:.1})", self.is_running, self.fps)
|
||||
write!(
|
||||
f,
|
||||
"CanvasLoopBox(running={}, fps={:.1})",
|
||||
self.is_running, self.fps
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
@ -291,7 +303,10 @@ impl NyashBox for CanvasLoopBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("CanvasLoopBox(running={}, fps={:.1})", self.is_running, self.fps))
|
||||
StringBox::new(format!(
|
||||
"CanvasLoopBox(running={}, fps={:.1})",
|
||||
self.is_running, self.fps
|
||||
))
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
|
||||
@ -45,7 +45,7 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::fmt::Display;
|
||||
|
||||
@ -59,7 +59,9 @@ pub struct ConsoleBox {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl ConsoleBox {
|
||||
pub fn new() -> Self {
|
||||
Self { base: BoxBase::new() }
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Log messages to browser console
|
||||
@ -128,7 +130,6 @@ impl NyashBox for ConsoleBox {
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Non-WASM版 - モックアップ実装
|
||||
@ -141,7 +142,9 @@ pub struct ConsoleBox {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
impl ConsoleBox {
|
||||
pub fn new() -> Self {
|
||||
Self { base: BoxBase::new() }
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Mock log method for non-WASM environments
|
||||
@ -207,10 +210,8 @@ impl NyashBox for ConsoleBox {
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Display implementations for both WASM and non-WASM versions
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl Display for ConsoleBox {
|
||||
|
||||
@ -99,13 +99,13 @@
|
||||
* - call stackは直近100件まで自動保持
|
||||
*/
|
||||
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use crate::instance_v2::InstanceBox;
|
||||
use crate::interpreter::RuntimeError;
|
||||
use chrono::Local;
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use chrono::Local;
|
||||
use crate::box_trait::{BoxCore, BoxBase, NyashBox, StringBox, BoolBox, VoidBox};
|
||||
use crate::interpreter::RuntimeError;
|
||||
use crate::instance_v2::InstanceBox;
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DebugBox {
|
||||
@ -156,7 +156,11 @@ impl DebugBox {
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
|
||||
pub fn track_box(&self, box_value: &dyn NyashBox, name: &str) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
pub fn track_box(
|
||||
&self,
|
||||
box_value: &dyn NyashBox,
|
||||
name: &str,
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let enabled = self.tracking_enabled.read().unwrap();
|
||||
if !*enabled {
|
||||
return Ok(Box::new(VoidBox::new()));
|
||||
@ -192,7 +196,10 @@ impl DebugBox {
|
||||
pub fn dump_all(&self) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let tracked = self.tracked_boxes.read().unwrap();
|
||||
let mut output = String::from("=== Box State Dump ===\n");
|
||||
output.push_str(&format!("Time: {}\n", Local::now().format("%Y-%m-%d %H:%M:%S")));
|
||||
output.push_str(&format!(
|
||||
"Time: {}\n",
|
||||
Local::now().format("%Y-%m-%d %H:%M:%S")
|
||||
));
|
||||
output.push_str(&format!("Total tracked boxes: {}\n\n", tracked.len()));
|
||||
|
||||
for (name, info) in tracked.iter() {
|
||||
@ -212,16 +219,19 @@ impl DebugBox {
|
||||
let content = dump_result.to_string_box().value;
|
||||
|
||||
// Write to file using std::fs
|
||||
std::fs::write(filename, content)
|
||||
.map_err(|e| RuntimeError::InvalidOperation {
|
||||
message: format!("Failed to write debug file: {}", e),
|
||||
})?;
|
||||
std::fs::write(filename, content).map_err(|e| RuntimeError::InvalidOperation {
|
||||
message: format!("Failed to write debug file: {}", e),
|
||||
})?;
|
||||
|
||||
println!("[DEBUG] Saved debug info to {}", filename);
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
|
||||
pub fn watch(&self, box_value: &dyn NyashBox, name: &str) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
pub fn watch(
|
||||
&self,
|
||||
box_value: &dyn NyashBox,
|
||||
name: &str,
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let value_str = box_value.to_string_box().value;
|
||||
let type_name = box_value.type_name();
|
||||
|
||||
@ -256,7 +266,11 @@ impl DebugBox {
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
|
||||
pub fn trace_call(&self, function_name: &str, args: Vec<String>) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
pub fn trace_call(
|
||||
&self,
|
||||
function_name: &str,
|
||||
args: Vec<String>,
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let mut stack = self.call_stack.write().unwrap();
|
||||
stack.push(CallInfo {
|
||||
function_name: function_name.to_string(),
|
||||
@ -277,7 +291,8 @@ impl DebugBox {
|
||||
let mut output = String::from("=== Call Stack ===\n");
|
||||
|
||||
for (i, call) in stack.iter().enumerate() {
|
||||
output.push_str(&format!("{}: [{}] {}({})\n",
|
||||
output.push_str(&format!(
|
||||
"{}: [{}] {}({})\n",
|
||||
i,
|
||||
call.timestamp,
|
||||
call.function_name,
|
||||
@ -306,13 +321,18 @@ impl DebugBox {
|
||||
|
||||
pub fn get_tracked_count(&self) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let tracked = self.tracked_boxes.read().unwrap();
|
||||
Ok(Box::new(crate::box_trait::IntegerBox::new(tracked.len() as i64)))
|
||||
Ok(Box::new(crate::box_trait::IntegerBox::new(
|
||||
tracked.len() as i64
|
||||
)))
|
||||
}
|
||||
|
||||
// --- Phase 1: JIT/Plugin shim tracing ---
|
||||
pub fn trace_plugin_calls(&self, on: bool) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
crate::jit::shim_trace::set_enabled(on);
|
||||
println!("[DEBUG] JIT shim trace: {}", if on {"ENABLED"} else {"DISABLED"});
|
||||
println!(
|
||||
"[DEBUG] JIT shim trace: {}",
|
||||
if on { "ENABLED" } else { "DISABLED" }
|
||||
);
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
|
||||
@ -398,6 +418,4 @@ impl NyashBox for DebugBox {
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -20,13 +20,19 @@ impl DebugConfigBox {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
jit_events: std::env::var("NYASH_JIT_EVENTS").ok().as_deref() == Some("1"),
|
||||
jit_events_compile: std::env::var("NYASH_JIT_EVENTS_COMPILE").ok().as_deref() == Some("1"),
|
||||
jit_events_runtime: std::env::var("NYASH_JIT_EVENTS_RUNTIME").ok().as_deref() == Some("1"),
|
||||
jit_events_compile: std::env::var("NYASH_JIT_EVENTS_COMPILE").ok().as_deref()
|
||||
== Some("1"),
|
||||
jit_events_runtime: std::env::var("NYASH_JIT_EVENTS_RUNTIME").ok().as_deref()
|
||||
== Some("1"),
|
||||
jit_stats: std::env::var("NYASH_JIT_STATS").ok().as_deref() == Some("1"),
|
||||
jit_stats_json: std::env::var("NYASH_JIT_STATS_JSON").ok().as_deref() == Some("1"),
|
||||
jit_dump: std::env::var("NYASH_JIT_DUMP").ok().as_deref() == Some("1"),
|
||||
jit_dot_path: std::env::var("NYASH_JIT_DOT").ok().filter(|s| !s.is_empty()),
|
||||
jit_events_path: std::env::var("NYASH_JIT_EVENTS_PATH").ok().filter(|s| !s.is_empty()),
|
||||
jit_dot_path: std::env::var("NYASH_JIT_DOT")
|
||||
.ok()
|
||||
.filter(|s| !s.is_empty()),
|
||||
jit_events_path: std::env::var("NYASH_JIT_EVENTS_PATH")
|
||||
.ok()
|
||||
.filter(|s| !s.is_empty()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +44,7 @@ impl DebugConfigBox {
|
||||
"jit_stats" => self.jit_stats = on,
|
||||
"jit_stats_json" => self.jit_stats_json = on,
|
||||
"jit_dump" => self.jit_dump = on,
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name)))
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name))),
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
@ -47,7 +53,7 @@ impl DebugConfigBox {
|
||||
match name {
|
||||
"jit_dot" | "jit_dot_path" => self.jit_dot_path = Some(value.to_string()),
|
||||
"jit_events_path" => self.jit_events_path = Some(value.to_string()),
|
||||
_ => return Box::new(StringBox::new(format!("Unknown path: {}", name)))
|
||||
_ => return Box::new(StringBox::new(format!("Unknown path: {}", name))),
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
@ -75,17 +81,29 @@ impl DebugConfigBox {
|
||||
}
|
||||
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
let setb = |k: &str, v: bool| { if v { std::env::set_var(k, "1"); } else { std::env::remove_var(k); } };
|
||||
let setb = |k: &str, v: bool| {
|
||||
if v {
|
||||
std::env::set_var(k, "1");
|
||||
} else {
|
||||
std::env::remove_var(k);
|
||||
}
|
||||
};
|
||||
setb("NYASH_JIT_EVENTS", self.jit_events);
|
||||
setb("NYASH_JIT_EVENTS_COMPILE", self.jit_events_compile);
|
||||
setb("NYASH_JIT_EVENTS_RUNTIME", self.jit_events_runtime);
|
||||
setb("NYASH_JIT_STATS", self.jit_stats);
|
||||
setb("NYASH_JIT_STATS_JSON", self.jit_stats_json);
|
||||
setb("NYASH_JIT_DUMP", self.jit_dump);
|
||||
if let Some(p) = &self.jit_dot_path { std::env::set_var("NYASH_JIT_DOT", p); }
|
||||
else { std::env::remove_var("NYASH_JIT_DOT"); }
|
||||
if let Some(p) = &self.jit_events_path { std::env::set_var("NYASH_JIT_EVENTS_PATH", p); }
|
||||
else { std::env::remove_var("NYASH_JIT_EVENTS_PATH"); }
|
||||
if let Some(p) = &self.jit_dot_path {
|
||||
std::env::set_var("NYASH_JIT_DOT", p);
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_DOT");
|
||||
}
|
||||
if let Some(p) = &self.jit_events_path {
|
||||
std::env::set_var("NYASH_JIT_EVENTS_PATH", p);
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_EVENTS_PATH");
|
||||
}
|
||||
// If any events are enabled and threshold is not set, default to 1 so lowering runs early
|
||||
if (self.jit_events || self.jit_events_compile || self.jit_events_runtime)
|
||||
&& std::env::var("NYASH_JIT_THRESHOLD").is_err()
|
||||
@ -108,17 +126,40 @@ impl DebugConfigBox {
|
||||
}
|
||||
|
||||
impl BoxCore for DebugConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "DebugConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "DebugConfigBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for DebugConfigBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<DebugConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "DebugConfigBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone(), ..self.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new("DebugConfigBox".to_string()) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<DebugConfigBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"DebugConfigBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
..self.clone()
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new("DebugConfigBox".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,11 +33,11 @@
|
||||
* - `run()`はブロッキング動作(アプリ終了まで制御を返さない)
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use crate::interpreter::RuntimeError;
|
||||
use eframe::{self, egui, epaint::Vec2};
|
||||
use std::any::Any;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use eframe::{self, egui, epaint::Vec2};
|
||||
|
||||
/// EguiBox - GUI アプリケーションを包むBox
|
||||
///
|
||||
@ -98,7 +98,7 @@ impl EguiBox {
|
||||
/// 更新関数を設定
|
||||
pub fn set_update_fn<F>(&mut self, f: F)
|
||||
where
|
||||
F: Fn(&mut Box<dyn Any + Send + Sync>, &egui::Context) + Send + Sync + 'static
|
||||
F: Fn(&mut Box<dyn Any + Send + Sync>, &egui::Context) + Send + Sync + 'static,
|
||||
{
|
||||
self.update_fn = Some(Arc::new(f));
|
||||
}
|
||||
@ -128,7 +128,11 @@ impl BoxCore for EguiBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "EguiBox('{}', {}x{})", self.title, self.size.x, self.size.y)
|
||||
write!(
|
||||
f,
|
||||
"EguiBox('{}', {}x{})",
|
||||
self.title, self.size.x, self.size.y
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
@ -148,9 +152,10 @@ impl std::fmt::Display for EguiBox {
|
||||
|
||||
impl NyashBox for EguiBox {
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(
|
||||
format!("EguiBox('{}', {}x{})", self.title, self.size.x, self.size.y)
|
||||
)
|
||||
StringBox::new(format!(
|
||||
"EguiBox('{}', {}x{})",
|
||||
self.title, self.size.x, self.size.y
|
||||
))
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
@ -162,7 +167,6 @@ impl NyashBox for EguiBox {
|
||||
self.clone_box()
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_egui) = other.as_any().downcast_ref::<EguiBox>() {
|
||||
BoolBox::new(self.title == other_egui.title && self.size == other_egui.size)
|
||||
@ -174,7 +178,6 @@ impl NyashBox for EguiBox {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"EguiBox"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// EguiBoxのメソッド実装(実際にはインタープリターから呼ばれない)
|
||||
@ -203,11 +206,7 @@ impl EguiBox {
|
||||
};
|
||||
|
||||
// 注意: これはブロッキング呼び出し
|
||||
let _ = eframe::run_native(
|
||||
&self.title,
|
||||
options,
|
||||
Box::new(|_cc| Ok(Box::new(app))),
|
||||
);
|
||||
let _ = eframe::run_native(&self.title, options, Box::new(|_cc| Ok(Box::new(app))));
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
// Nyashの箱システムによるファイル入出力を提供します。
|
||||
// 参考: 既存Boxの設計思想
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write, Result};
|
||||
use std::io::{Read, Result, Write};
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -38,8 +38,12 @@ impl FileBox {
|
||||
Err(_) => {
|
||||
// Fallback: create with empty file handle - only for dispatch
|
||||
use std::fs::OpenOptions;
|
||||
let file = OpenOptions::new().create(true).write(true).read(true)
|
||||
.open("/dev/null").unwrap_or_else(|_| File::open("/dev/null").unwrap());
|
||||
let file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.read(true)
|
||||
.open("/dev/null")
|
||||
.unwrap_or_else(|_| File::open("/dev/null").unwrap());
|
||||
FileBox {
|
||||
file: RwLock::new(file),
|
||||
path: String::new(),
|
||||
@ -50,7 +54,11 @@ impl FileBox {
|
||||
}
|
||||
|
||||
pub fn open(path: &str) -> Result<Self> {
|
||||
let file = OpenOptions::new().read(true).write(true).create(true).open(path)?;
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(path)?;
|
||||
Ok(FileBox {
|
||||
file: RwLock::new(file),
|
||||
path: path.to_string(),
|
||||
@ -137,7 +145,7 @@ impl NyashBox for FileBox {
|
||||
// Note: Cannot truly clone a File handle, so create a new one to the same path
|
||||
match FileBox::open(&self.path) {
|
||||
Ok(new_file) => Box::new(new_file),
|
||||
Err(_) => Box::new(crate::box_trait::VoidBox::new()) // Return void on error
|
||||
Err(_) => Box::new(crate::box_trait::VoidBox::new()), // Return void on error
|
||||
}
|
||||
}
|
||||
|
||||
@ -150,12 +158,10 @@ impl NyashBox for FileBox {
|
||||
StringBox::new(format!("FileBox({})", self.path))
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"FileBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_file) = other.as_any().downcast_ref::<FileBox>() {
|
||||
BoolBox::new(self.path == other_file.path)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::ast::ASTNode;
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Weak;
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ClosureEnv {
|
||||
@ -11,7 +11,12 @@ pub struct ClosureEnv {
|
||||
}
|
||||
|
||||
impl ClosureEnv {
|
||||
pub fn new() -> Self { Self { me_value: None, captures: HashMap::new() } }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
me_value: None,
|
||||
captures: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -24,32 +29,70 @@ pub struct FunctionBox {
|
||||
|
||||
impl FunctionBox {
|
||||
pub fn new(params: Vec<String>, body: Vec<ASTNode>) -> Self {
|
||||
Self { params, body, env: ClosureEnv::new(), base: BoxBase::new() }
|
||||
Self {
|
||||
params,
|
||||
body,
|
||||
env: ClosureEnv::new(),
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
pub fn with_env(params: Vec<String>, body: Vec<ASTNode>, env: ClosureEnv) -> Self {
|
||||
Self { params, body, env, base: BoxBase::new() }
|
||||
Self {
|
||||
params,
|
||||
body,
|
||||
env,
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for FunctionBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "FunctionBox(params={}, body={})", self.params.len(), self.body.len())
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"FunctionBox(params={}, body={})",
|
||||
self.params.len(),
|
||||
self.body.len()
|
||||
)
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
}
|
||||
|
||||
impl NyashBox for FunctionBox {
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(self.clone()) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new(format!("FunctionBox(params={}, captures={}, body={})", self.params.len(), self.env.captures.len(), self.body.len())) }
|
||||
fn type_name(&self) -> &'static str { "FunctionBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!(
|
||||
"FunctionBox(params={}, captures={}, body={})",
|
||||
self.params.len(),
|
||||
self.env.captures.len(),
|
||||
self.body.len()
|
||||
))
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"FunctionBox"
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(o) = other.as_any().downcast_ref::<FunctionBox>() {
|
||||
BoolBox::new(self.box_id() == o.box_id())
|
||||
} else { BoolBox::new(false) }
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,13 +100,20 @@ impl Clone for ClosureEnv {
|
||||
fn clone(&self) -> Self {
|
||||
let me_value = self.me_value.as_ref().map(|w| Weak::clone(w));
|
||||
let mut captures: HashMap<String, Box<dyn NyashBox>> = HashMap::new();
|
||||
for (k, v) in &self.captures { captures.insert(k.clone(), v.clone_box()); }
|
||||
for (k, v) in &self.captures {
|
||||
captures.insert(k.clone(), v.clone_box());
|
||||
}
|
||||
Self { me_value, captures }
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for FunctionBox {
|
||||
fn clone(&self) -> Self {
|
||||
Self { params: self.params.clone(), body: self.body.clone(), env: self.env.clone(), base: BoxBase::new() }
|
||||
Self {
|
||||
params: self.params.clone(),
|
||||
body: self.body.clone(),
|
||||
env: self.env.clone(),
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,9 +2,9 @@
|
||||
// Nyashの箱システムによる非同期処理の基盤を提供します。
|
||||
// 参考: 既存Boxの設計思想
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::sync::{Mutex, Condvar, Arc, Weak};
|
||||
use std::sync::{Arc, Condvar, Mutex, Weak};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NyashFutureBox {
|
||||
@ -33,7 +33,10 @@ pub struct FutureWeak {
|
||||
|
||||
impl Clone for NyashFutureBox {
|
||||
fn clone(&self) -> Self {
|
||||
Self { inner: self.inner.clone(), base: BoxBase::new() }
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,7 +44,10 @@ impl NyashFutureBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(Inner {
|
||||
state: Mutex::new(FutureState { result: None, ready: false }),
|
||||
state: Mutex::new(FutureState {
|
||||
result: None,
|
||||
ready: false,
|
||||
}),
|
||||
cv: Condvar::new(),
|
||||
}),
|
||||
base: BoxBase::new(),
|
||||
@ -71,7 +77,11 @@ impl NyashFutureBox {
|
||||
}
|
||||
|
||||
/// Create a non-owning weak handle to this Future's state
|
||||
pub fn downgrade(&self) -> FutureWeak { FutureWeak { inner: Arc::downgrade(&self.inner) } }
|
||||
pub fn downgrade(&self) -> FutureWeak {
|
||||
FutureWeak {
|
||||
inner: Arc::downgrade(&self.inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for NyashFutureBox {
|
||||
@ -98,12 +108,10 @@ impl NyashBox for NyashFutureBox {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"NyashFutureBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_future) = other.as_any().downcast_ref::<NyashFutureBox>() {
|
||||
BoolBox::new(self.base.id == other_future.base.id)
|
||||
@ -164,6 +172,8 @@ impl FutureBox {
|
||||
impl FutureWeak {
|
||||
/// Try to upgrade and check readiness
|
||||
pub(crate) fn is_ready(&self) -> Option<bool> {
|
||||
self.inner.upgrade().map(|arc| arc.state.lock().unwrap().ready)
|
||||
self.inner
|
||||
.upgrade()
|
||||
.map(|arc| arc.state.lock().unwrap().ready)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GcConfigBox { base: BoxBase, counting: bool, trace: bool, barrier_strict: bool }
|
||||
pub struct GcConfigBox {
|
||||
base: BoxBase,
|
||||
counting: bool,
|
||||
trace: bool,
|
||||
barrier_strict: bool,
|
||||
}
|
||||
|
||||
impl GcConfigBox {
|
||||
pub fn new() -> Self {
|
||||
@ -18,7 +23,7 @@ impl GcConfigBox {
|
||||
"counting" => self.counting = on,
|
||||
"trace" => self.trace = on,
|
||||
"barrier_strict" | "strict" => self.barrier_strict = on,
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name)))
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name))),
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
@ -32,31 +37,59 @@ impl GcConfigBox {
|
||||
Box::new(BoolBox::new(v))
|
||||
}
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
let setb = |k: &str, v: bool| { if v { std::env::set_var(k, "1"); } else { std::env::remove_var(k); } };
|
||||
let setb = |k: &str, v: bool| {
|
||||
if v {
|
||||
std::env::set_var(k, "1");
|
||||
} else {
|
||||
std::env::remove_var(k);
|
||||
}
|
||||
};
|
||||
setb("NYASH_GC_COUNTING", self.counting);
|
||||
setb("NYASH_GC_TRACE", self.trace);
|
||||
setb("NYASH_GC_BARRIER_STRICT", self.barrier_strict);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn summary(&self) -> Box<dyn NyashBox> {
|
||||
let s = format!("counting={} trace={} barrier_strict={}", self.counting, self.trace, self.barrier_strict);
|
||||
let s = format!(
|
||||
"counting={} trace={} barrier_strict={}",
|
||||
self.counting, self.trace, self.barrier_strict
|
||||
);
|
||||
Box::new(StringBox::new(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for GcConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "GcConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "GcConfigBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for GcConfigBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<GcConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "GcConfigBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(self.clone()) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new(self.summary().to_string_box().value) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<GcConfigBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"GcConfigBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(self.summary().to_string_box().value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
// NOTE: HTTPサポートは現在開発中です。
|
||||
// reqwestクレートの依存関係のため、一時的に無効化されています。
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -16,7 +16,7 @@ pub struct HttpClientBox {
|
||||
impl HttpClientBox {
|
||||
pub fn new() -> Self {
|
||||
HttpClientBox {
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,7 +41,12 @@ impl HttpClientBox {
|
||||
}
|
||||
|
||||
/// ヘッダー付きHTTPリクエスト(スタブ)
|
||||
pub fn request(&self, _method: Box<dyn NyashBox>, _url: Box<dyn NyashBox>, _options: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
pub fn request(
|
||||
&self,
|
||||
_method: Box<dyn NyashBox>,
|
||||
_url: Box<dyn NyashBox>,
|
||||
_options: Box<dyn NyashBox>,
|
||||
) -> Box<dyn NyashBox> {
|
||||
Box::new(StringBox::new("HTTP support is currently disabled"))
|
||||
}
|
||||
}
|
||||
@ -60,12 +65,10 @@ impl NyashBox for HttpClientBox {
|
||||
StringBox::new(format!("HttpClientBox(id: {})", self.base.id))
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"HttpClientBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_http) = other.as_any().downcast_ref::<HttpClientBox>() {
|
||||
BoolBox::new(self.base.id == other_http.base.id)
|
||||
|
||||
@ -48,7 +48,7 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::MapBox;
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
@ -180,11 +180,9 @@ impl HTTPRequestBox {
|
||||
/// Content-Length取得
|
||||
pub fn get_content_length(&self) -> Box<dyn NyashBox> {
|
||||
match self.headers.get("content-length") {
|
||||
Some(length_str) => {
|
||||
match length_str.parse::<i64>() {
|
||||
Ok(length) => Box::new(IntegerBox::new(length)),
|
||||
Err(_) => Box::new(IntegerBox::new(0)),
|
||||
}
|
||||
Some(length_str) => match length_str.parse::<i64>() {
|
||||
Ok(length) => Box::new(IntegerBox::new(length)),
|
||||
Err(_) => Box::new(IntegerBox::new(0)),
|
||||
},
|
||||
None => Box::new(IntegerBox::new(0)),
|
||||
}
|
||||
@ -202,8 +200,12 @@ impl NyashBox for HTTPRequestBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("HTTPRequest({} {} - {} headers)",
|
||||
self.method, self.path, self.headers.len()))
|
||||
StringBox::new(format!(
|
||||
"HTTPRequest({} {} - {} headers)",
|
||||
self.method,
|
||||
self.path,
|
||||
self.headers.len()
|
||||
))
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
@ -229,8 +231,13 @@ impl BoxCore for HTTPRequestBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "HTTPRequest({} {} - {} headers)",
|
||||
self.method, self.path, self.headers.len())
|
||||
write!(
|
||||
f,
|
||||
"HTTPRequest({} {} - {} headers)",
|
||||
self.method,
|
||||
self.path,
|
||||
self.headers.len()
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
@ -272,7 +279,11 @@ impl HTTPResponseBox {
|
||||
}
|
||||
|
||||
/// ステータスコード・メッセージ設定
|
||||
pub fn set_status(&self, code: Box<dyn NyashBox>, message: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
pub fn set_status(
|
||||
&self,
|
||||
code: Box<dyn NyashBox>,
|
||||
message: Box<dyn NyashBox>,
|
||||
) -> Box<dyn NyashBox> {
|
||||
// Note: This would need interior mutability for actual mutation
|
||||
// For now, this is a placeholder for the API structure
|
||||
let _code_val = code.to_string_box().value.parse::<i32>().unwrap_or(200);
|
||||
@ -283,7 +294,11 @@ impl HTTPResponseBox {
|
||||
}
|
||||
|
||||
/// ヘッダー設定
|
||||
pub fn set_header(&self, name: Box<dyn NyashBox>, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
pub fn set_header(
|
||||
&self,
|
||||
name: Box<dyn NyashBox>,
|
||||
value: Box<dyn NyashBox>,
|
||||
) -> Box<dyn NyashBox> {
|
||||
let _name_str = name.to_string_box().value;
|
||||
let _value_str = value.to_string_box().value;
|
||||
|
||||
@ -296,7 +311,7 @@ impl HTTPResponseBox {
|
||||
let content_type_str = content_type.to_string_box().value;
|
||||
self.set_header(
|
||||
Box::new(StringBox::new("Content-Type".to_string())),
|
||||
Box::new(StringBox::new(content_type_str))
|
||||
Box::new(StringBox::new(content_type_str)),
|
||||
)
|
||||
}
|
||||
|
||||
@ -321,8 +336,10 @@ impl HTTPResponseBox {
|
||||
let mut response = String::new();
|
||||
|
||||
// Status line
|
||||
response.push_str(&format!("{} {} {}\r\n",
|
||||
self.http_version, self.status_code, self.status_message));
|
||||
response.push_str(&format!(
|
||||
"{} {} {}\r\n",
|
||||
self.http_version, self.status_code, self.status_message
|
||||
));
|
||||
|
||||
// Headers
|
||||
for (name, value) in &self.headers {
|
||||
@ -348,7 +365,10 @@ impl HTTPResponseBox {
|
||||
let mut response = HTTPResponseBox::new();
|
||||
response.status_code = 200;
|
||||
response.status_message = "OK".to_string();
|
||||
response.headers.insert("Content-Type".to_string(), "text/html; charset=utf-8".to_string());
|
||||
response.headers.insert(
|
||||
"Content-Type".to_string(),
|
||||
"text/html; charset=utf-8".to_string(),
|
||||
);
|
||||
response.body = content.to_string_box().value;
|
||||
response
|
||||
}
|
||||
@ -358,7 +378,9 @@ impl HTTPResponseBox {
|
||||
let mut response = HTTPResponseBox::new();
|
||||
response.status_code = 200;
|
||||
response.status_message = "OK".to_string();
|
||||
response.headers.insert("Content-Type".to_string(), "application/json".to_string());
|
||||
response
|
||||
.headers
|
||||
.insert("Content-Type".to_string(), "application/json".to_string());
|
||||
response.body = content.to_string_box().value;
|
||||
response
|
||||
}
|
||||
@ -368,7 +390,10 @@ impl HTTPResponseBox {
|
||||
let mut response = HTTPResponseBox::new();
|
||||
response.status_code = 404;
|
||||
response.status_message = "Not Found".to_string();
|
||||
response.headers.insert("Content-Type".to_string(), "text/html; charset=utf-8".to_string());
|
||||
response.headers.insert(
|
||||
"Content-Type".to_string(),
|
||||
"text/html; charset=utf-8".to_string(),
|
||||
);
|
||||
response.body = "<html><body><h1>404 - Not Found</h1></body></html>".to_string();
|
||||
response
|
||||
}
|
||||
@ -385,8 +410,12 @@ impl NyashBox for HTTPResponseBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("HTTPResponse({} {} - {} bytes)",
|
||||
self.status_code, self.status_message, self.body.len()))
|
||||
StringBox::new(format!(
|
||||
"HTTPResponse({} {} - {} bytes)",
|
||||
self.status_code,
|
||||
self.status_message,
|
||||
self.body.len()
|
||||
))
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
@ -412,8 +441,13 @@ impl BoxCore for HTTPResponseBox {
|
||||
}
|
||||
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "HTTPResponse({} {} - {} bytes)",
|
||||
self.status_code, self.status_message, self.body.len())
|
||||
write!(
|
||||
f,
|
||||
"HTTPResponse({} {} - {} bytes)",
|
||||
self.status_code,
|
||||
self.status_message,
|
||||
self.body.len()
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
|
||||
@ -40,12 +40,12 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::boxes::SocketBox;
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::http_message_box::{HTTPRequestBox, HTTPResponseBox};
|
||||
use crate::boxes::SocketBox;
|
||||
use std::any::Any;
|
||||
use std::sync::RwLock;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use std::thread;
|
||||
|
||||
/// HTTP サーバーを提供するBox
|
||||
@ -68,12 +68,14 @@ impl Clone for HTTPServerBox {
|
||||
let socket_val = socket_guard.as_ref().map(|s| s.clone());
|
||||
|
||||
let routes_guard = self.routes.read().unwrap();
|
||||
let routes_val: HashMap<String, Box<dyn NyashBox>> = routes_guard.iter()
|
||||
let routes_val: HashMap<String, Box<dyn NyashBox>> = routes_guard
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone_box()))
|
||||
.collect();
|
||||
|
||||
let middleware_guard = self.middleware.read().unwrap();
|
||||
let middleware_val: Vec<Box<dyn NyashBox>> = middleware_guard.iter()
|
||||
let middleware_val: Vec<Box<dyn NyashBox>> = middleware_guard
|
||||
.iter()
|
||||
.map(|item| item.clone_box())
|
||||
.collect();
|
||||
|
||||
@ -82,7 +84,8 @@ impl Clone for HTTPServerBox {
|
||||
let timeout_val = *self.timeout_seconds.read().unwrap();
|
||||
|
||||
let connections_guard = self.active_connections.read().unwrap();
|
||||
let connections_val: Vec<Box<dyn NyashBox>> = connections_guard.iter()
|
||||
let connections_val: Vec<Box<dyn NyashBox>> = connections_guard
|
||||
.iter()
|
||||
.map(|item| item.clone_box())
|
||||
.collect();
|
||||
|
||||
@ -123,10 +126,10 @@ impl HTTPServerBox {
|
||||
Ok(mut socket_guard) => {
|
||||
*socket_guard = Some(socket);
|
||||
Box::new(BoolBox::new(true))
|
||||
},
|
||||
Err(_) => {
|
||||
Box::new(StringBox::new("Error: Failed to acquire socket lock".to_string()))
|
||||
}
|
||||
Err(_) => Box::new(StringBox::new(
|
||||
"Error: Failed to acquire socket lock".to_string(),
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
Box::new(BoolBox::new(false))
|
||||
@ -137,7 +140,11 @@ impl HTTPServerBox {
|
||||
pub fn listen(&self, _backlog: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let socket_guard = match self.socket.read() {
|
||||
Ok(guard) => guard,
|
||||
Err(_) => return Box::new(StringBox::new("Error: Failed to acquire socket lock".to_string())),
|
||||
Err(_) => {
|
||||
return Box::new(StringBox::new(
|
||||
"Error: Failed to acquire socket lock".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ref _socket) = *socket_guard {
|
||||
@ -158,12 +165,20 @@ impl HTTPServerBox {
|
||||
// Set running state
|
||||
match self.running.write() {
|
||||
Ok(mut running) => *running = true,
|
||||
Err(_) => return Box::new(StringBox::new("Error: Failed to set running state".to_string())),
|
||||
Err(_) => {
|
||||
return Box::new(StringBox::new(
|
||||
"Error: Failed to set running state".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let socket_guard = match self.socket.read() {
|
||||
Ok(guard) => guard,
|
||||
Err(_) => return Box::new(StringBox::new("Error: Failed to acquire socket lock".to_string())),
|
||||
Err(_) => {
|
||||
return Box::new(StringBox::new(
|
||||
"Error: Failed to acquire socket lock".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ref socket) = *socket_guard {
|
||||
@ -203,11 +218,12 @@ impl HTTPServerBox {
|
||||
// For RwLock pattern, we need to pass the data needed for the thread
|
||||
let routes_snapshot = match self.routes.read() {
|
||||
Ok(routes_guard) => {
|
||||
let routes_clone: HashMap<String, Box<dyn NyashBox>> = routes_guard.iter()
|
||||
let routes_clone: HashMap<String, Box<dyn NyashBox>> = routes_guard
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone_box()))
|
||||
.collect();
|
||||
routes_clone
|
||||
},
|
||||
}
|
||||
Err(_) => continue, // Skip this connection if we can't read routes
|
||||
};
|
||||
|
||||
@ -307,7 +323,7 @@ impl HTTPServerBox {
|
||||
/// クライアントリクエスト処理(内部メソッド)
|
||||
fn handle_client_request_with_routes(
|
||||
client_socket: SocketBox,
|
||||
routes: HashMap<String, Box<dyn NyashBox>>
|
||||
routes: HashMap<String, Box<dyn NyashBox>>,
|
||||
) {
|
||||
// Read HTTP request
|
||||
let raw_request = client_socket.read_http_request();
|
||||
@ -332,14 +348,14 @@ impl HTTPServerBox {
|
||||
let response = if let Some(_handler) = routes.get(&route_key) {
|
||||
// Found specific method route
|
||||
// TODO: Actual handler invocation would need method calling infrastructure
|
||||
HTTPResponseBox::create_json_response(
|
||||
Box::new(StringBox::new(r#"{"message": "Route found", "method": ""#.to_string() + &method + r#""}"#))
|
||||
)
|
||||
HTTPResponseBox::create_json_response(Box::new(StringBox::new(
|
||||
r#"{"message": "Route found", "method": ""#.to_string() + &method + r#""}"#,
|
||||
)))
|
||||
} else if let Some(_handler) = routes.get(&fallback_key) {
|
||||
// Found generic route
|
||||
HTTPResponseBox::create_json_response(
|
||||
Box::new(StringBox::new(r#"{"message": "Generic route found"}"#))
|
||||
)
|
||||
HTTPResponseBox::create_json_response(Box::new(StringBox::new(
|
||||
r#"{"message": "Generic route found"}"#,
|
||||
)))
|
||||
} else {
|
||||
// No route found - 404
|
||||
HTTPResponseBox::create_404_response()
|
||||
@ -411,8 +427,11 @@ impl BoxCore for HTTPServerBox {
|
||||
let routes_count = self.routes.read().unwrap().len();
|
||||
let connections_count = self.active_connections.read().unwrap().len();
|
||||
|
||||
write!(f, "HTTPServer(id: {}, running: {}, routes: {}, connections: {})",
|
||||
self.base.id, running, routes_count, connections_count)
|
||||
write!(
|
||||
f,
|
||||
"HTTPServer(id: {}, running: {}, routes: {}, connections: {})",
|
||||
self.base.id, running, routes_count, connections_count
|
||||
)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
|
||||
@ -32,10 +32,10 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::sync::RwLock;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::RwLock;
|
||||
|
||||
/// IntentBox - 構造化メッセージBox (RwLock pattern)
|
||||
#[derive(Debug)]
|
||||
@ -89,8 +89,8 @@ impl IntentBox {
|
||||
Ok(json_val) => {
|
||||
*self.payload.write().unwrap() = json_val;
|
||||
Box::new(BoolBox::new(true))
|
||||
},
|
||||
Err(_) => Box::new(BoolBox::new(false))
|
||||
}
|
||||
Err(_) => Box::new(BoolBox::new(false)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -151,4 +151,3 @@ impl std::fmt::Display for IntentBox {
|
||||
self.fmt_box(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, IntegerBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::jit::config::JitConfig;
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox, VoidBox};
|
||||
use crate::interpreter::RuntimeError;
|
||||
use crate::jit::config::JitConfig;
|
||||
use std::any::Any;
|
||||
use std::sync::RwLock;
|
||||
|
||||
@ -11,12 +11,19 @@ pub struct JitConfigBox {
|
||||
}
|
||||
|
||||
impl JitConfigBox {
|
||||
pub fn new() -> Self { Self { base: BoxBase::new(), config: RwLock::new(JitConfig::from_env()) } }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
config: RwLock::new(JitConfig::from_env()),
|
||||
}
|
||||
}
|
||||
/// Update internal config flags from runtime capability probe
|
||||
pub fn from_runtime_probe(&self) -> Box<dyn NyashBox> {
|
||||
let caps = crate::jit::config::probe_capabilities();
|
||||
let mut cfg = self.config.write().unwrap();
|
||||
if cfg.native_bool_abi && !caps.supports_b1_sig { cfg.native_bool_abi = false; }
|
||||
if cfg.native_bool_abi && !caps.supports_b1_sig {
|
||||
cfg.native_bool_abi = false;
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn set_flag(&self, name: &str, on: bool) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
@ -34,7 +41,11 @@ impl JitConfigBox {
|
||||
"bool_abi" | "native_bool_abi" => cfg.native_bool_abi = on,
|
||||
"ret_b1" | "ret_bool_b1" => cfg.ret_bool_b1 = on,
|
||||
"relax_numeric" | "hostcall_relax_numeric" => cfg.relax_numeric = on,
|
||||
_ => return Err(RuntimeError::InvalidOperation { message: format!("Unknown flag: {}", name) }),
|
||||
_ => {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: format!("Unknown flag: {}", name),
|
||||
})
|
||||
}
|
||||
}
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
@ -53,19 +64,28 @@ impl JitConfigBox {
|
||||
"bool_abi" | "native_bool_abi" => cfg.native_bool_abi,
|
||||
"ret_b1" | "ret_bool_b1" => cfg.ret_bool_b1,
|
||||
"relax_numeric" | "hostcall_relax_numeric" => cfg.relax_numeric,
|
||||
_ => return Err(RuntimeError::InvalidOperation { message: format!("Unknown flag: {}", name) }),
|
||||
_ => {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: format!("Unknown flag: {}", name),
|
||||
})
|
||||
}
|
||||
};
|
||||
Ok(Box::new(BoolBox::new(b)))
|
||||
}
|
||||
pub fn set_threshold(&self, v: i64) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let mut cfg = self.config.write().unwrap();
|
||||
if v <= 0 { cfg.threshold = None; }
|
||||
else { cfg.threshold = Some(v as u32); }
|
||||
if v <= 0 {
|
||||
cfg.threshold = None;
|
||||
} else {
|
||||
cfg.threshold = Some(v as u32);
|
||||
}
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
pub fn get_threshold(&self) -> Box<dyn NyashBox> {
|
||||
let cfg = self.config.read().unwrap();
|
||||
Box::new(IntegerBox::new(cfg.threshold.map(|v| v as i64).unwrap_or(0)))
|
||||
Box::new(IntegerBox::new(
|
||||
cfg.threshold.map(|v| v as i64).unwrap_or(0),
|
||||
))
|
||||
}
|
||||
pub fn to_json(&self) -> Box<dyn NyashBox> {
|
||||
let cfg = self.config.read().unwrap();
|
||||
@ -88,20 +108,49 @@ impl JitConfigBox {
|
||||
}
|
||||
pub fn from_json(&self, s: &str) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let mut cfg = self.config.write().unwrap();
|
||||
let v: serde_json::Value = serde_json::from_str(s).map_err(|e| RuntimeError::InvalidOperation { message: format!("Invalid JSON: {}", e) })?;
|
||||
if let Some(b) = v.get("exec").and_then(|x| x.as_bool()) { cfg.exec = b; }
|
||||
if let Some(b) = v.get("stats").and_then(|x| x.as_bool()) { cfg.stats = b; }
|
||||
if let Some(b) = v.get("stats_json").and_then(|x| x.as_bool()) { cfg.stats_json = b; }
|
||||
if let Some(b) = v.get("dump").and_then(|x| x.as_bool()) { cfg.dump = b; }
|
||||
if let Some(n) = v.get("threshold").and_then(|x| x.as_u64()) { cfg.threshold = Some(n as u32); }
|
||||
if let Some(b) = v.get("phi_min").and_then(|x| x.as_bool()) { cfg.phi_min = b; }
|
||||
if let Some(b) = v.get("hostcall").and_then(|x| x.as_bool()) { cfg.hostcall = b; }
|
||||
if let Some(b) = v.get("handle_debug").and_then(|x| x.as_bool()) { cfg.handle_debug = b; }
|
||||
if let Some(b) = v.get("native_f64").and_then(|x| x.as_bool()) { cfg.native_f64 = b; }
|
||||
if let Some(b) = v.get("native_bool").and_then(|x| x.as_bool()) { cfg.native_bool = b; }
|
||||
if let Some(b) = v.get("native_bool_abi").and_then(|x| x.as_bool()) { cfg.native_bool_abi = b; }
|
||||
if let Some(b) = v.get("ret_bool_b1").and_then(|x| x.as_bool()) { cfg.ret_bool_b1 = b; }
|
||||
if let Some(b) = v.get("relax_numeric").and_then(|x| x.as_bool()) { cfg.relax_numeric = b; }
|
||||
let v: serde_json::Value =
|
||||
serde_json::from_str(s).map_err(|e| RuntimeError::InvalidOperation {
|
||||
message: format!("Invalid JSON: {}", e),
|
||||
})?;
|
||||
if let Some(b) = v.get("exec").and_then(|x| x.as_bool()) {
|
||||
cfg.exec = b;
|
||||
}
|
||||
if let Some(b) = v.get("stats").and_then(|x| x.as_bool()) {
|
||||
cfg.stats = b;
|
||||
}
|
||||
if let Some(b) = v.get("stats_json").and_then(|x| x.as_bool()) {
|
||||
cfg.stats_json = b;
|
||||
}
|
||||
if let Some(b) = v.get("dump").and_then(|x| x.as_bool()) {
|
||||
cfg.dump = b;
|
||||
}
|
||||
if let Some(n) = v.get("threshold").and_then(|x| x.as_u64()) {
|
||||
cfg.threshold = Some(n as u32);
|
||||
}
|
||||
if let Some(b) = v.get("phi_min").and_then(|x| x.as_bool()) {
|
||||
cfg.phi_min = b;
|
||||
}
|
||||
if let Some(b) = v.get("hostcall").and_then(|x| x.as_bool()) {
|
||||
cfg.hostcall = b;
|
||||
}
|
||||
if let Some(b) = v.get("handle_debug").and_then(|x| x.as_bool()) {
|
||||
cfg.handle_debug = b;
|
||||
}
|
||||
if let Some(b) = v.get("native_f64").and_then(|x| x.as_bool()) {
|
||||
cfg.native_f64 = b;
|
||||
}
|
||||
if let Some(b) = v.get("native_bool").and_then(|x| x.as_bool()) {
|
||||
cfg.native_bool = b;
|
||||
}
|
||||
if let Some(b) = v.get("native_bool_abi").and_then(|x| x.as_bool()) {
|
||||
cfg.native_bool_abi = b;
|
||||
}
|
||||
if let Some(b) = v.get("ret_bool_b1").and_then(|x| x.as_bool()) {
|
||||
cfg.ret_bool_b1 = b;
|
||||
}
|
||||
if let Some(b) = v.get("relax_numeric").and_then(|x| x.as_bool()) {
|
||||
cfg.relax_numeric = b;
|
||||
}
|
||||
Ok(Box::new(VoidBox::new()))
|
||||
}
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
@ -124,20 +173,41 @@ impl JitConfigBox {
|
||||
}
|
||||
|
||||
impl BoxCore for JitConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitConfigBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitConfigBox {
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new(self.summary().to_string_box().value) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitConfigBox" }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(self.summary().to_string_box().value)
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitConfigBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitConfigBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
let cfg = self.config.read().unwrap().clone();
|
||||
Box::new(JitConfigBox { base: self.base.clone(), config: RwLock::new(cfg) })
|
||||
Box::new(JitConfigBox {
|
||||
base: self.base.clone(),
|
||||
config: RwLock::new(cfg),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
}
|
||||
|
||||
@ -1,25 +1,55 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JitEventsBox { base: BoxBase }
|
||||
pub struct JitEventsBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl JitEventsBox { pub fn new() -> Self { Self { base: BoxBase::new() } } }
|
||||
impl JitEventsBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for JitEventsBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitEventsBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitEventsBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitEventsBox {
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new("JitEventsBox") }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitEventsBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitEventsBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new("JitEventsBox")
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitEventsBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitEventsBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl JitEventsBox {
|
||||
@ -28,14 +58,17 @@ impl JitEventsBox {
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn enable(&self, on: bool) -> Box<dyn NyashBox> {
|
||||
if on { std::env::set_var("NYASH_JIT_EVENTS", "1"); }
|
||||
else { std::env::remove_var("NYASH_JIT_EVENTS"); }
|
||||
if on {
|
||||
std::env::set_var("NYASH_JIT_EVENTS", "1");
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_EVENTS");
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn log(&self, kind: &str, function: &str, note_json: &str) -> Box<dyn NyashBox> {
|
||||
let extra = serde_json::from_str::<serde_json::Value>(note_json).unwrap_or_else(|_| serde_json::json!({"note": note_json}));
|
||||
let extra = serde_json::from_str::<serde_json::Value>(note_json)
|
||||
.unwrap_or_else(|_| serde_json::json!({"note": note_json}));
|
||||
crate::jit::events::emit(kind, function, None, None, extra);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,17 +1,35 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JitHostcallRegistryBox { base: BoxBase }
|
||||
pub struct JitHostcallRegistryBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl JitHostcallRegistryBox { pub fn new() -> Self { Self { base: BoxBase::new() } } }
|
||||
impl JitHostcallRegistryBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for JitHostcallRegistryBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitHostcallRegistryBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitHostcallRegistryBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitHostcallRegistryBox {
|
||||
@ -20,18 +38,41 @@ impl NyashBox for JitHostcallRegistryBox {
|
||||
let payload = serde_json::json!({ "readonly": ro, "mutating": mu });
|
||||
StringBox::new(payload.to_string())
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitHostcallRegistryBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitHostcallRegistryBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitHostcallRegistryBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitHostcallRegistryBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl JitHostcallRegistryBox {
|
||||
pub fn add_readonly(&self, sym: &str) -> Box<dyn NyashBox> { crate::jit::hostcall_registry::add_readonly(sym); Box::new(VoidBox::new()) }
|
||||
pub fn add_mutating(&self, sym: &str) -> Box<dyn NyashBox> { crate::jit::hostcall_registry::add_mutating(sym); Box::new(VoidBox::new()) }
|
||||
pub fn set_from_csv(&self, ro_csv: &str, mu_csv: &str) -> Box<dyn NyashBox> { crate::jit::hostcall_registry::set_from_csv(ro_csv, mu_csv); Box::new(VoidBox::new()) }
|
||||
pub fn add_readonly(&self, sym: &str) -> Box<dyn NyashBox> {
|
||||
crate::jit::hostcall_registry::add_readonly(sym);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn add_mutating(&self, sym: &str) -> Box<dyn NyashBox> {
|
||||
crate::jit::hostcall_registry::add_mutating(sym);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn set_from_csv(&self, ro_csv: &str, mu_csv: &str) -> Box<dyn NyashBox> {
|
||||
crate::jit::hostcall_registry::set_from_csv(ro_csv, mu_csv);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn add_signature(&self, sym: &str, args_csv: &str, ret_str: &str) -> Box<dyn NyashBox> {
|
||||
let ok = crate::jit::hostcall_registry::set_signature_csv(sym, args_csv, ret_str);
|
||||
if ok { Box::new(VoidBox::new()) } else { Box::new(StringBox::new("Invalid signature")) }
|
||||
if ok {
|
||||
Box::new(VoidBox::new())
|
||||
} else {
|
||||
Box::new(StringBox::new("Invalid signature"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,61 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JitPolicyBox { base: BoxBase }
|
||||
pub struct JitPolicyBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl JitPolicyBox { pub fn new() -> Self { Self { base: BoxBase::new() } } }
|
||||
impl JitPolicyBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for JitPolicyBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitPolicyBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitPolicyBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitPolicyBox {
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
let p = crate::jit::policy::current();
|
||||
let s = format!("read_only={} whitelist={}", p.read_only, p.hostcall_whitelist.join(","));
|
||||
let s = format!(
|
||||
"read_only={} whitelist={}",
|
||||
p.read_only,
|
||||
p.hostcall_whitelist.join(",")
|
||||
);
|
||||
StringBox::new(s)
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitPolicyBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitPolicyBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitPolicyBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitPolicyBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
// Methods (exposed via VM dispatch):
|
||||
@ -32,19 +64,26 @@ impl JitPolicyBox {
|
||||
let mut cur = crate::jit::policy::current();
|
||||
match name {
|
||||
"read_only" | "readonly" => cur.read_only = on,
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name)))
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name))),
|
||||
}
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn get_flag(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let cur = crate::jit::policy::current();
|
||||
let v = match name { "read_only" | "readonly" => cur.read_only, _ => false };
|
||||
let v = match name {
|
||||
"read_only" | "readonly" => cur.read_only,
|
||||
_ => false,
|
||||
};
|
||||
Box::new(BoolBox::new(v))
|
||||
}
|
||||
pub fn set_whitelist_csv(&self, csv: &str) -> Box<dyn NyashBox> {
|
||||
let mut cur = crate::jit::policy::current();
|
||||
cur.hostcall_whitelist = csv.split(',').map(|t| t.trim().to_string()).filter(|s| !s.is_empty()).collect();
|
||||
cur.hostcall_whitelist = csv
|
||||
.split(',')
|
||||
.map(|t| t.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
@ -89,9 +128,15 @@ impl JitPolicyBox {
|
||||
crate::jit::r#extern::collections::SYM_ARRAY_SET_H,
|
||||
crate::jit::r#extern::collections::SYM_MAP_SET_H,
|
||||
];
|
||||
for id in ids { if !cur.hostcall_whitelist.iter().any(|s| s == id) { cur.hostcall_whitelist.push(id.to_string()); } }
|
||||
for id in ids {
|
||||
if !cur.hostcall_whitelist.iter().any(|s| s == id) {
|
||||
cur.hostcall_whitelist.push(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Box::new(StringBox::new(format!("Unknown preset: {}", name)));
|
||||
}
|
||||
_ => { return Box::new(StringBox::new(format!("Unknown preset: {}", name))); }
|
||||
}
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
|
||||
@ -1,15 +1,25 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JitStatsBox { base: BoxBase }
|
||||
pub struct JitStatsBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl JitStatsBox {
|
||||
pub fn new() -> Self { Self { base: BoxBase::new() } }
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
pub fn to_json(&self) -> Box<dyn NyashBox> {
|
||||
let cfg = crate::jit::config::current();
|
||||
let caps = crate::jit::config::probe_capabilities();
|
||||
let mode = if cfg.native_bool_abi && caps.supports_b1_sig { "b1_bool" } else { "i64_bool" };
|
||||
let mode = if cfg.native_bool_abi && caps.supports_b1_sig {
|
||||
"b1_bool"
|
||||
} else {
|
||||
"i64_bool"
|
||||
};
|
||||
let payload = serde_json::json!({
|
||||
"version": 1,
|
||||
"abi_mode": mode,
|
||||
@ -25,17 +35,37 @@ impl JitStatsBox {
|
||||
}
|
||||
|
||||
impl BoxCore for JitStatsBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitStatsBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitStatsBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitStatsBox {
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new(self.to_json().to_string_box().value) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitStatsBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitStatsBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(self.clone()) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(self.to_json().to_string_box().value)
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitStatsBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitStatsBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,25 +1,55 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox, VoidBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct JitStrictBox { base: BoxBase }
|
||||
pub struct JitStrictBox {
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl JitStrictBox { pub fn new() -> Self { Self { base: BoxBase::new() } } }
|
||||
impl JitStrictBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for JitStrictBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "JitStrictBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
fn box_id(&self) -> u64 {
|
||||
self.base.id
|
||||
}
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
||||
self.base.parent_type_id
|
||||
}
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "JitStrictBox")
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl NyashBox for JitStrictBox {
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new("JitStrictBox".to_string()) }
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<JitStrictBox>()) }
|
||||
fn type_name(&self) -> &'static str { "JitStrictBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new("JitStrictBox".to_string())
|
||||
}
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
BoolBox::new(other.as_any().is::<JitStrictBox>())
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JitStrictBox"
|
||||
}
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(Self {
|
||||
base: self.base.clone(),
|
||||
})
|
||||
}
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl JitStrictBox {
|
||||
@ -27,8 +57,12 @@ impl JitStrictBox {
|
||||
pub fn enable(&self, on: bool) -> Box<dyn NyashBox> {
|
||||
if on {
|
||||
std::env::set_var("NYASH_JIT_STRICT", "1");
|
||||
if std::env::var("NYASH_JIT_ONLY").ok().is_none() { std::env::set_var("NYASH_JIT_ONLY", "1"); }
|
||||
if std::env::var("NYASH_JIT_ARGS_HANDLE_ONLY").ok().is_none() { std::env::set_var("NYASH_JIT_ARGS_HANDLE_ONLY", "1"); }
|
||||
if std::env::var("NYASH_JIT_ONLY").ok().is_none() {
|
||||
std::env::set_var("NYASH_JIT_ONLY", "1");
|
||||
}
|
||||
if std::env::var("NYASH_JIT_ARGS_HANDLE_ONLY").ok().is_none() {
|
||||
std::env::set_var("NYASH_JIT_ARGS_HANDLE_ONLY", "1");
|
||||
}
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_STRICT");
|
||||
}
|
||||
@ -36,11 +70,19 @@ impl JitStrictBox {
|
||||
}
|
||||
|
||||
pub fn set_only(&self, on: bool) -> Box<dyn NyashBox> {
|
||||
if on { std::env::set_var("NYASH_JIT_ONLY", "1"); } else { std::env::remove_var("NYASH_JIT_ONLY"); }
|
||||
if on {
|
||||
std::env::set_var("NYASH_JIT_ONLY", "1");
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_ONLY");
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn set_handle_only(&self, on: bool) -> Box<dyn NyashBox> {
|
||||
if on { std::env::set_var("NYASH_JIT_ARGS_HANDLE_ONLY", "1"); } else { std::env::remove_var("NYASH_JIT_ARGS_HANDLE_ONLY"); }
|
||||
if on {
|
||||
std::env::set_var("NYASH_JIT_ARGS_HANDLE_ONLY", "1");
|
||||
} else {
|
||||
std::env::remove_var("NYASH_JIT_ARGS_HANDLE_ONLY");
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
@ -60,4 +102,3 @@ impl JitStrictBox {
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,12 +2,12 @@
|
||||
// Nyashの箱システムによるJSON解析・生成を提供します。
|
||||
// 参考: 既存Boxの設計思想
|
||||
|
||||
use crate::box_trait::{NyashBox, BoxCore, BoxBase, StringBox, BoolBox, IntegerBox};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use crate::boxes::map_box::MapBox;
|
||||
use serde_json::{Error, Value};
|
||||
use std::any::Any;
|
||||
use std::sync::RwLock;
|
||||
use serde_json::{Value, Error};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct JSONBox {
|
||||
@ -31,14 +31,14 @@ impl JSONBox {
|
||||
let value = serde_json::from_str(s)?;
|
||||
Ok(JSONBox {
|
||||
value: RwLock::new(value),
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new(value: Value) -> Self {
|
||||
JSONBox {
|
||||
value: RwLock::new(value),
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,10 +148,10 @@ impl BoxCore for JSONBox {
|
||||
Value::String(_) => "string",
|
||||
Value::Array(ref arr) => {
|
||||
return write!(f, "JSONBox[array:{}]", arr.len());
|
||||
},
|
||||
}
|
||||
Value::Object(ref obj) => {
|
||||
return write!(f, "JSONBox[object:{}]", obj.len());
|
||||
},
|
||||
}
|
||||
};
|
||||
write!(f, "JSONBox[{}]", json_type)
|
||||
}
|
||||
@ -186,12 +186,10 @@ impl NyashBox for JSONBox {
|
||||
StringBox::new(value.to_string())
|
||||
}
|
||||
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"JSONBox"
|
||||
}
|
||||
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_json) = other.as_any().downcast_ref::<JSONBox>() {
|
||||
let self_value = self.value.read().unwrap();
|
||||
@ -230,10 +228,7 @@ fn json_value_to_nyash_box(value: &Value) -> Box<dyn NyashBox> {
|
||||
Value::Object(obj) => {
|
||||
let map_box = MapBox::new();
|
||||
for (key, val) in obj {
|
||||
map_box.set(
|
||||
Box::new(StringBox::new(key)),
|
||||
json_value_to_nyash_box(val)
|
||||
);
|
||||
map_box.set(Box::new(StringBox::new(key)), json_value_to_nyash_box(val));
|
||||
}
|
||||
Box::new(map_box)
|
||||
}
|
||||
@ -242,7 +237,11 @@ fn json_value_to_nyash_box(value: &Value) -> Box<dyn NyashBox> {
|
||||
|
||||
/// NyashBox を JSON Value に変換
|
||||
fn nyash_box_to_json_value(value: Box<dyn NyashBox>) -> Value {
|
||||
if value.as_any().downcast_ref::<crate::boxes::null_box::NullBox>().is_some() {
|
||||
if value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::null_box::NullBox>()
|
||||
.is_some()
|
||||
{
|
||||
Value::Null
|
||||
} else if let Some(bool_box) = value.as_any().downcast_ref::<BoolBox>() {
|
||||
Value::Bool(bool_box.value)
|
||||
@ -259,7 +258,8 @@ fn nyash_box_to_json_value(value: Box<dyn NyashBox>) -> Value {
|
||||
Value::String(string_box.value.clone())
|
||||
} else if let Some(array_box) = value.as_any().downcast_ref::<ArrayBox>() {
|
||||
let items = array_box.items.read().unwrap();
|
||||
let arr: Vec<Value> = items.iter()
|
||||
let arr: Vec<Value> = items
|
||||
.iter()
|
||||
.map(|item| nyash_box_to_json_value(item.clone_box()))
|
||||
.collect();
|
||||
Value::Array(arr)
|
||||
|
||||
@ -103,23 +103,23 @@
|
||||
* - 存在しないキーの取得は "Key not found" メッセージ返却
|
||||
*/
|
||||
|
||||
use crate::box_trait::{BoxCore, BoxBase, NyashBox, StringBox, IntegerBox, BoolBox};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use crate::boxes::ArrayBox;
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock}; // Arc追加
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::sync::{Arc, RwLock}; // Arc追加
|
||||
|
||||
/// キーバリューストアを表すBox
|
||||
pub struct MapBox {
|
||||
data: Arc<RwLock<HashMap<String, Box<dyn NyashBox>>>>, // Arc追加
|
||||
data: Arc<RwLock<HashMap<String, Box<dyn NyashBox>>>>, // Arc追加
|
||||
base: BoxBase,
|
||||
}
|
||||
|
||||
impl MapBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: Arc::new(RwLock::new(HashMap::new())), // Arc::new追加
|
||||
data: Arc::new(RwLock::new(HashMap::new())), // Arc::new追加
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -137,7 +137,11 @@ impl MapBox {
|
||||
match self.data.read().unwrap().get(&key_str) {
|
||||
Some(value) => {
|
||||
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
|
||||
if value.as_any().downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>().is_some() {
|
||||
if value
|
||||
.as_any()
|
||||
.downcast_ref::<crate::runtime::plugin_loader_v2::PluginBoxV2>()
|
||||
.is_some()
|
||||
{
|
||||
return value.share_box();
|
||||
}
|
||||
value.clone_box()
|
||||
@ -149,7 +153,9 @@ impl MapBox {
|
||||
/// キーが存在するかチェック
|
||||
pub fn has(&self, key: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
let key_str = key.to_string_box().value;
|
||||
Box::new(BoolBox::new(self.data.read().unwrap().contains_key(&key_str)))
|
||||
Box::new(BoolBox::new(
|
||||
self.data.read().unwrap().contains_key(&key_str),
|
||||
))
|
||||
}
|
||||
|
||||
/// キーを削除
|
||||
@ -173,7 +179,10 @@ impl MapBox {
|
||||
|
||||
/// 全ての値を取得
|
||||
pub fn values(&self) -> Box<dyn NyashBox> {
|
||||
let values: Vec<Box<dyn NyashBox>> = self.data.read().unwrap()
|
||||
let values: Vec<Box<dyn NyashBox>> = self
|
||||
.data
|
||||
.read()
|
||||
.unwrap()
|
||||
.values()
|
||||
.map(|v| v.clone_box())
|
||||
.collect();
|
||||
@ -211,7 +220,8 @@ impl MapBox {
|
||||
let value_str = value.to_string_box().value;
|
||||
// 値が数値の場合はそのまま、文字列の場合は引用符で囲む
|
||||
let formatted_value = if value.as_any().downcast_ref::<IntegerBox>().is_some()
|
||||
|| value.as_any().downcast_ref::<BoolBox>().is_some() {
|
||||
|| value.as_any().downcast_ref::<BoolBox>().is_some()
|
||||
{
|
||||
value_str
|
||||
} else {
|
||||
format!("\"{}\"", value_str.replace("\"", "\\\""))
|
||||
@ -233,11 +243,12 @@ impl Clone for MapBox {
|
||||
fn clone(&self) -> Self {
|
||||
// ディープコピー(独立インスタンス)
|
||||
let data_guard = self.data.read().unwrap();
|
||||
let cloned_data: HashMap<String, Box<dyn NyashBox>> = data_guard.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone_box())) // 要素もディープコピー
|
||||
let cloned_data: HashMap<String, Box<dyn NyashBox>> = data_guard
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), v.clone_box())) // 要素もディープコピー
|
||||
.collect();
|
||||
MapBox {
|
||||
data: Arc::new(RwLock::new(cloned_data)), // 新しいArc
|
||||
data: Arc::new(RwLock::new(cloned_data)), // 新しいArc
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
@ -267,7 +278,9 @@ impl BoxCore for MapBox {
|
||||
}
|
||||
|
||||
impl NyashBox for MapBox {
|
||||
fn is_identity(&self) -> bool { true }
|
||||
fn is_identity(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn type_name(&self) -> &'static str {
|
||||
"MapBox"
|
||||
}
|
||||
@ -277,7 +290,6 @@ impl NyashBox for MapBox {
|
||||
StringBox::new(&format!("MapBox(size={})", size))
|
||||
}
|
||||
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
@ -285,8 +297,8 @@ impl NyashBox for MapBox {
|
||||
/// 🎯 状態共有の核心実装
|
||||
fn share_box(&self) -> Box<dyn NyashBox> {
|
||||
let new_instance = MapBox {
|
||||
data: Arc::clone(&self.data), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
data: Arc::clone(&self.data), // Arcクローンで状態共有
|
||||
base: BoxBase::new(), // 新しいID
|
||||
};
|
||||
Box::new(new_instance)
|
||||
}
|
||||
@ -299,7 +311,6 @@ impl NyashBox for MapBox {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for MapBox {
|
||||
|
||||
@ -56,9 +56,9 @@
|
||||
* - 整数演算は自動でFloatBoxに変換される場合あり
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, IntegerBox, BoolBox, BoxCore, BoxBase};
|
||||
use std::fmt::{Debug, Display};
|
||||
use crate::box_trait::{BoolBox, BoxBase, BoxCore, IntegerBox, NyashBox, StringBox};
|
||||
use std::any::Any;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
/// 数学演算を提供するBox
|
||||
#[derive(Debug, Clone)]
|
||||
@ -69,7 +69,7 @@ pub struct MathBox {
|
||||
impl MathBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,12 +88,12 @@ impl MathBox {
|
||||
pub fn max(&self, a: Box<dyn NyashBox>, b: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let (Some(a_int), Some(b_int)) = (
|
||||
a.as_any().downcast_ref::<IntegerBox>(),
|
||||
b.as_any().downcast_ref::<IntegerBox>()
|
||||
b.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
Box::new(IntegerBox::new(a_int.value.max(b_int.value)))
|
||||
} else if let (Some(a_float), Some(b_float)) = (
|
||||
a.as_any().downcast_ref::<FloatBox>(),
|
||||
b.as_any().downcast_ref::<FloatBox>()
|
||||
b.as_any().downcast_ref::<FloatBox>(),
|
||||
) {
|
||||
Box::new(FloatBox::new(a_float.value.max(b_float.value)))
|
||||
} else {
|
||||
@ -105,12 +105,12 @@ impl MathBox {
|
||||
pub fn min(&self, a: Box<dyn NyashBox>, b: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let (Some(a_int), Some(b_int)) = (
|
||||
a.as_any().downcast_ref::<IntegerBox>(),
|
||||
b.as_any().downcast_ref::<IntegerBox>()
|
||||
b.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
Box::new(IntegerBox::new(a_int.value.min(b_int.value)))
|
||||
} else if let (Some(a_float), Some(b_float)) = (
|
||||
a.as_any().downcast_ref::<FloatBox>(),
|
||||
b.as_any().downcast_ref::<FloatBox>()
|
||||
b.as_any().downcast_ref::<FloatBox>(),
|
||||
) {
|
||||
Box::new(FloatBox::new(a_float.value.min(b_float.value)))
|
||||
} else {
|
||||
@ -122,7 +122,7 @@ impl MathBox {
|
||||
pub fn pow(&self, base: Box<dyn NyashBox>, exp: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let (Some(base_int), Some(exp_int)) = (
|
||||
base.as_any().downcast_ref::<IntegerBox>(),
|
||||
exp.as_any().downcast_ref::<IntegerBox>()
|
||||
exp.as_any().downcast_ref::<IntegerBox>(),
|
||||
) {
|
||||
if exp_int.value >= 0 {
|
||||
let result = (base_int.value as f64).powi(exp_int.value as i32);
|
||||
@ -251,7 +251,7 @@ impl MathBox {
|
||||
/// 床関数(切り下げ)
|
||||
pub fn floor(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(int_box) = value.as_any().downcast_ref::<IntegerBox>() {
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
} else if let Some(float_box) = value.as_any().downcast_ref::<FloatBox>() {
|
||||
Box::new(IntegerBox::new(float_box.value.floor() as i64))
|
||||
} else {
|
||||
@ -262,7 +262,7 @@ impl MathBox {
|
||||
/// 天井関数(切り上げ)
|
||||
pub fn ceil(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(int_box) = value.as_any().downcast_ref::<IntegerBox>() {
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
} else if let Some(float_box) = value.as_any().downcast_ref::<FloatBox>() {
|
||||
Box::new(IntegerBox::new(float_box.value.ceil() as i64))
|
||||
} else {
|
||||
@ -273,7 +273,7 @@ impl MathBox {
|
||||
/// 四捨五入
|
||||
pub fn round(&self, value: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
||||
if let Some(int_box) = value.as_any().downcast_ref::<IntegerBox>() {
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
Box::new(IntegerBox::new(int_box.value)) // 整数はそのまま
|
||||
} else if let Some(float_box) = value.as_any().downcast_ref::<FloatBox>() {
|
||||
Box::new(IntegerBox::new(float_box.value.round() as i64))
|
||||
} else {
|
||||
@ -329,7 +329,6 @@ impl NyashBox for MathBox {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for MathBox {
|
||||
@ -349,7 +348,7 @@ impl FloatBox {
|
||||
pub fn new(value: f64) -> Self {
|
||||
Self {
|
||||
value,
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -403,7 +402,6 @@ impl NyashBox for FloatBox {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for FloatBox {
|
||||
@ -427,7 +425,7 @@ impl RangeBox {
|
||||
start,
|
||||
end,
|
||||
step,
|
||||
base: BoxBase::new()
|
||||
base: BoxBase::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -480,7 +478,10 @@ impl NyashBox for RangeBox {
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(&format!("Range({}, {}, {})", self.start, self.end, self.step))
|
||||
StringBox::new(&format!(
|
||||
"Range({}, {}, {})",
|
||||
self.start, self.end, self.step
|
||||
))
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
@ -495,15 +496,14 @@ impl NyashBox for RangeBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_range) = other.as_any().downcast_ref::<RangeBox>() {
|
||||
BoolBox::new(
|
||||
self.start == other_range.start &&
|
||||
self.end == other_range.end &&
|
||||
self.step == other_range.step
|
||||
self.start == other_range.start
|
||||
&& self.end == other_range.end
|
||||
&& self.step == other_range.step,
|
||||
)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for RangeBox {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user