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

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

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

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

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

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

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

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

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

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