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

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

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

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

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

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

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

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

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

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