1
//! `TRANSACTION-*` accessors — `SPLIT-COUNT`, `TAG-COUNT`,
2
//! `IS-MULTI-CURRENCY`, `POST-DATE`, `ENTER-DATE`. Each takes an
3
//! entity index and reads a single field from `TransactionData` at
4
//! the entity's wire-format offset.
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::CompileContext;
8
use crate::compiler::emit::FunctionEmitter;
9
use crate::compiler::expr::{LOCAL_TEMP_I32, eval_value};
10
use crate::error::Result;
11
use crate::runtime::SymbolTable;
12

            
13
use super::{arity_check, compile_idx_to_stack, emit_entity_data_offset};
14

            
15
664
pub(super) fn transaction_split_count(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
16
664
    arity_check("TRANSACTION-SPLIT-COUNT", args, 1)?;
17
664
    eval_value(symbols, &args[0])?;
18
664
    Ok(Expr::WasmRuntime(WasmType::I32))
19
664
}
20

            
21
249
pub(super) fn compile_transaction_split_count_to_stack(
22
249
    ctx: &mut CompileContext,
23
249
    emit: &mut FunctionEmitter,
24
249
    symbols: &mut SymbolTable,
25
249
    args: &[Expr],
26
249
) -> Result<WasmType> {
27
249
    arity_check("TRANSACTION-SPLIT-COUNT", args, 1)?;
28
249
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
29
249
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
30
249
    emit.i32_load(16); // split_count at TransactionData offset 16
31
249
    Ok(WasmType::I32)
32
249
}
33

            
34
23904
pub(super) fn transaction_tag_count(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
35
23904
    arity_check("TRANSACTION-TAG-COUNT", args, 1)?;
36
23904
    eval_value(symbols, &args[0])?;
37
23904
    Ok(Expr::WasmRuntime(WasmType::I32))
38
23904
}
39

            
40
4316
pub(super) fn compile_transaction_tag_count_to_stack(
41
4316
    ctx: &mut CompileContext,
42
4316
    emit: &mut FunctionEmitter,
43
4316
    symbols: &mut SymbolTable,
44
4316
    args: &[Expr],
45
4316
) -> Result<WasmType> {
46
4316
    arity_check("TRANSACTION-TAG-COUNT", args, 1)?;
47
4316
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
48
4316
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
49
4316
    emit.i32_load(20); // tag_count at TransactionData offset 20
50
4316
    Ok(WasmType::I32)
51
4316
}
52

            
53
416
pub(super) fn transaction_is_multi_currency(
54
416
    symbols: &mut SymbolTable,
55
416
    args: &[Expr],
56
416
) -> Result<Expr> {
57
416
    arity_check("TRANSACTION-IS-MULTI-CURRENCY", args, 1)?;
58
416
    eval_value(symbols, &args[0])?;
59
416
    Ok(Expr::WasmRuntime(WasmType::Bool))
60
416
}
61

            
62
415
pub(super) fn compile_transaction_is_multi_currency_to_stack(
63
415
    ctx: &mut CompileContext,
64
415
    emit: &mut FunctionEmitter,
65
415
    symbols: &mut SymbolTable,
66
415
    args: &[Expr],
67
415
) -> Result<WasmType> {
68
415
    arity_check("TRANSACTION-IS-MULTI-CURRENCY", args, 1)?;
69
415
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
70
415
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
71
415
    emit.i32_load8_u(24); // is_multi_currency at TransactionData offset 24
72
415
    Ok(WasmType::Bool)
73
415
}
74

            
75
22991
pub(super) fn transaction_post_date(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
76
22991
    arity_check("TRANSACTION-POST-DATE", args, 1)?;
77
22991
    eval_value(symbols, &args[0])?;
78
22991
    Ok(Expr::WasmRuntime(WasmType::Ratio))
79
22991
}
80

            
81
10458
pub(super) fn compile_transaction_post_date_to_stack(
82
10458
    ctx: &mut CompileContext,
83
10458
    emit: &mut FunctionEmitter,
84
10458
    symbols: &mut SymbolTable,
85
10458
    args: &[Expr],
86
10458
) -> Result<WasmType> {
87
10458
    arity_check("TRANSACTION-POST-DATE", args, 1)?;
88
10458
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
89
10458
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
90
10458
    emit.i64_load(0); // post_date at TransactionData offset 0
91
10458
    emit.call(ctx.ids.ratio_from_i64);
92
10458
    Ok(WasmType::Ratio)
93
10458
}
94

            
95
332
pub(super) fn transaction_enter_date(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
96
332
    arity_check("TRANSACTION-ENTER-DATE", args, 1)?;
97
332
    eval_value(symbols, &args[0])?;
98
332
    Ok(Expr::WasmRuntime(WasmType::Ratio))
99
332
}
100

            
101
166
pub(super) fn compile_transaction_enter_date_to_stack(
102
166
    ctx: &mut CompileContext,
103
166
    emit: &mut FunctionEmitter,
104
166
    symbols: &mut SymbolTable,
105
166
    args: &[Expr],
106
166
) -> Result<WasmType> {
107
166
    arity_check("TRANSACTION-ENTER-DATE", args, 1)?;
108
166
    compile_idx_to_stack(ctx, emit, symbols, &args[0])?;
109
166
    emit_entity_data_offset(ctx, emit, LOCAL_TEMP_I32)?;
110
166
    emit.i64_load(8); // enter_date at TransactionData offset 8
111
166
    emit.call(ctx.ids.ratio_from_i64);
112
166
    Ok(WasmType::Ratio)
113
166
}