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

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

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

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

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

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

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

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

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

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