Skip to main content

shape_jit/ffi/call_method/
matrix.rs

1//! Matrix method dispatch for JIT.
2
3use crate::jit_matrix::JitMatrix;
4use crate::ffi::jit_kinds::*;
5use crate::ffi::value_ffi::*;
6use shape_value::aligned_vec::AlignedVec;
7use shape_value::heap_value::MatrixData;
8use std::sync::Arc;
9
10/// Dispatch a method call on a Matrix receiver.
11pub fn call_matrix_method(receiver_bits: u64, method_name: &str, args: &[u64]) -> u64 {
12    if !is_heap_kind(receiver_bits, HK_MATRIX) {
13        return TAG_NULL;
14    }
15    let jm = unsafe { unified_unbox::<JitMatrix>(receiver_bits) };
16
17    match method_name {
18        "transpose" => matrix_transpose(jm),
19        "flatten" => matrix_flatten(jm),
20        "shape" => matrix_shape(jm),
21        "row" => {
22            let idx = if !args.is_empty() && is_number(args[0]) {
23                unbox_number(args[0]) as usize
24            } else {
25                return TAG_NULL;
26            };
27            matrix_row(jm, idx)
28        }
29        "col" => {
30            let idx = if !args.is_empty() && is_number(args[0]) {
31                unbox_number(args[0]) as usize
32            } else {
33                return TAG_NULL;
34            };
35            matrix_col(jm, idx)
36        }
37        "rows" => box_number(jm.rows as f64),
38        "cols" => box_number(jm.cols as f64),
39        _ => TAG_NULL,
40    }
41}
42
43fn matrix_transpose(jm: &JitMatrix) -> u64 {
44    let rows = jm.rows as usize;
45    let cols = jm.cols as usize;
46    let data = unsafe { std::slice::from_raw_parts(jm.data, jm.total_len as usize) };
47    let mut result = vec![0.0f64; rows * cols];
48    for i in 0..rows {
49        for j in 0..cols {
50            result[j * rows + i] = data[i * cols + j];
51        }
52    }
53    let aligned = AlignedVec::from_vec(result);
54    let mat_data = MatrixData::from_flat(aligned, cols as u32, rows as u32);
55    let arc = Arc::new(mat_data);
56    let new_jm = JitMatrix::from_arc(&arc);
57    std::mem::forget(arc); // JitMatrix::from_arc already cloned the Arc
58    unified_box(HK_MATRIX, new_jm)
59}
60
61// SURFACE (W10 jit-playbook §5 / ADR-006 §2.7.4): matrix_flatten /
62// matrix_shape / matrix_row / matrix_col all returned a freshly
63// allocated `JitArray` of `box_number` elements via the deleted
64// `JitArray::new()` / `push` / `heap_box` constructors. The kinded
65// rebuild allocates a `TypedArray<f64>` for the result and returns
66// it through the §2.7.6/Q8 carrier with `kind =
67// NativeKind::Ptr(HeapKind::TypedArray)` (element kind =
68// NativeKind::Float64). Every caller in `call_matrix_method` flows
69// through the surface below.
70
71fn matrix_flatten(_jm: &JitMatrix) -> u64 {
72    todo!(
73        "phase-2c §2.7.4 / W10 jit-playbook §5: JitArray rebuild — \
74         matrix_flatten. Result allocation needs `TypedArray<f64>` \
75         per ADR-006 §2.7.6/Q8."
76    )
77}
78
79fn matrix_shape(_jm: &JitMatrix) -> u64 {
80    todo!(
81        "phase-2c §2.7.4 / W10 jit-playbook §5: JitArray rebuild — \
82         matrix_shape. Result allocation needs `TypedArray<f64>` \
83         (or `TypedArray<i64>`) per ADR-006 §2.7.6/Q8."
84    )
85}
86
87fn matrix_row(_jm: &JitMatrix, _idx: usize) -> u64 {
88    todo!(
89        "phase-2c §2.7.4 / W10 jit-playbook §5: JitArray rebuild — \
90         matrix_row. Result allocation needs `TypedArray<f64>` \
91         per ADR-006 §2.7.6/Q8."
92    )
93}
94
95fn matrix_col(_jm: &JitMatrix, _idx: usize) -> u64 {
96    todo!(
97        "phase-2c §2.7.4 / W10 jit-playbook §5: JitArray rebuild — \
98         matrix_col. Result allocation needs `TypedArray<f64>` \
99         per ADR-006 §2.7.6/Q8."
100    )
101}