ferricel-core 0.1.3

Core compiler and runtime library for ferricel (CEL → Wasm)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use cel::common::ast::Expr;
use ferricel_types::functions::RuntimeFunction;
use walrus::{InstrSeqBuilder, ValType};

use crate::compiler::{
    context::{CompilerContext, CompilerEnv},
    helpers::{emit_string_const, get_memory_id},
};

/// Resolves a type name using container-based hierarchical resolution.
///
/// This implements the CEL container resolution algorithm:
/// For a name like "MyType" with container "A.B.C", tries in order:
/// 1. A.B.C.MyType (most specific)
/// 2. A.B.MyType (parent level)
/// 3. A.MyType (grandparent level)
/// 4. MyType (root level)
///
/// Special case: Leading dot (.MyType) bypasses container and only tries root level.
///
/// Returns the resolved fully-qualified type name, or None if not found.
pub fn resolve_type_name(
    name: &str,
    container: &Option<String>,
    schema: &Option<crate::schema::ProtoSchema>,
) -> Option<String> {
    // Early return if no schema - can't resolve anything
    let schema = schema.as_ref()?;

    // Special case: Leading dot means root scope only
    // ".MyType" -> try only "MyType" (bypass container)
    if let Some(stripped) = name.strip_prefix('.') {
        return if schema.has_message_type(stripped) {
            Some(stripped.to_string())
        } else {
            None
        };
    }

    // Try exact name first (optimization for fully-qualified names)
    if schema.has_message_type(name) {
        return Some(name.to_string());
    }

    // Try container-based hierarchical resolution
    if let Some(container_str) = container {
        let parts: Vec<&str> = container_str.split('.').collect();

        // Try from most specific to least: A.B.C.name -> A.B.name -> A.name -> name
        for i in (1..=parts.len()).rev() {
            let prefix = parts[0..i].join(".");
            let qualified = format!("{}.{}", prefix, name);

            if schema.has_message_type(&qualified) {
                return Some(qualified);
            }
        }
    }

    // Final attempt: try name at root level again
    // (This handles the case where container is set but name is at root)
    if schema.has_message_type(name) {
        return Some(name.to_string());
    }

    None
}

/// Build the ordered list of candidate variable names for a given base name and container.
///
/// CEL name resolution order (most specific to least):
///   container.A.B.C.name, ..., A.name, name
///
/// If the name has a leading dot, only the root-scope name is returned (container bypassed).
pub fn variable_candidates(name: &str, container: &Option<String>) -> Vec<String> {
    // Leading dot means root scope only — bypass container
    if let Some(stripped) = name.strip_prefix('.') {
        return vec![stripped.to_string()];
    }

    let mut candidates = Vec::new();

    if let Some(container_str) = container {
        let parts: Vec<&str> = container_str.split('.').collect();
        // Most specific first: A.B.C.name, A.B.name, A.name
        for i in (1..=parts.len()).rev() {
            let prefix = parts[0..i].join(".");
            candidates.push(format!("{}.{}", prefix, name));
        }
    }

    // Always try the bare name last (root scope)
    candidates.push(name.to_string());

    candidates
}

/// Emit Wasm code that tries each candidate variable name in order, returning the
/// first non-null result. Leaves a `*mut CelValue` (i32) on the stack.
///
/// If none of the candidates resolves, the result is a **null pointer**. Use this
/// only when the caller needs the null as a "not found" sentinel (e.g. `compile_select`
/// which falls back to field access). For `compile_ident`, use
/// `emit_variable_lookup_chain` which converts null to a runtime error.
fn emit_variable_lookup_chain_raw(
    candidates: &[String],
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    module: &mut walrus::Module,
) -> Result<walrus::LocalId, anyhow::Error> {
    let memory_id = get_memory_id(module)?;
    let result_local = module.locals.add(ValType::I32);

    // Helper: emit `cel_get_variable(name)` and store into result_local.
    let emit_get = |name: &str,
                    body: &mut InstrSeqBuilder,
                    module: &mut walrus::Module|
     -> Result<(), anyhow::Error> {
        let name_bytes = name.as_bytes();
        let name_len = name_bytes.len() as i32;
        let ptr_local = module.locals.add(ValType::I32);

        body.i32_const(name_len)
            .call(env.get(RuntimeFunction::Malloc))
            .local_set(ptr_local);

        for (offset, &byte) in name_bytes.iter().enumerate() {
            body.local_get(ptr_local);
            body.i32_const(byte as i32);
            body.store(
                memory_id,
                walrus::ir::StoreKind::I32_8 { atomic: false },
                walrus::ir::MemArg {
                    align: 1,
                    offset: offset as u64,
                },
            );
        }

        body.local_get(ptr_local)
            .i32_const(name_len)
            .call(env.get(RuntimeFunction::GetVariable))
            .local_set(result_local);

        Ok(())
    };

    if candidates.is_empty() {
        body.i32_const(0).local_set(result_local);
        body.local_get(result_local);
        return Ok(result_local);
    }

    if candidates.len() == 1 {
        emit_get(&candidates[0], body, module)?;
        body.local_get(result_local);
        return Ok(result_local);
    }

    emit_get(&candidates[0], body, module)?;
    build_fallback_chain(&candidates[1..], result_local, body, env, module, memory_id)?;

    body.local_get(result_local);
    Ok(result_local)
}

/// Emit `cel_unbound_variable_error(name_ptr, name_len)` and store result into `result_local`.
fn emit_unbound_error(
    name: &str,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    module: &mut walrus::Module,
    memory_id: walrus::MemoryId,
) -> Result<walrus::LocalId, anyhow::Error> {
    let result_local = module.locals.add(ValType::I32);
    let name_bytes = name.as_bytes();
    let name_len = name_bytes.len() as i32;
    let ptr_local = module.locals.add(ValType::I32);

    body.i32_const(name_len)
        .call(env.get(RuntimeFunction::Malloc))
        .local_set(ptr_local);

    for (offset, &byte) in name_bytes.iter().enumerate() {
        body.local_get(ptr_local);
        body.i32_const(byte as i32);
        body.store(
            memory_id,
            walrus::ir::StoreKind::I32_8 { atomic: false },
            walrus::ir::MemArg {
                align: 1,
                offset: offset as u64,
            },
        );
    }

    body.local_get(ptr_local)
        .i32_const(name_len)
        .call(env.get(RuntimeFunction::UnboundVariableError))
        .local_set(result_local);

    Ok(result_local)
}

/// After the lookup chain, if `result_local` is still null emit `cel_unbound_variable_error`
/// and store its result back into `result_local`.
fn emit_null_to_unbound_error(
    name: &str,
    result_local: walrus::LocalId,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    module: &mut walrus::Module,
    memory_id: walrus::MemoryId,
) -> Result<(), anyhow::Error> {
    body.local_get(result_local)
        .unop(walrus::ir::UnaryOp::I32Eqz);

    let then_seq = body.dangling_instr_seq(None);
    let then_id = then_seq.id();
    let else_seq = body.dangling_instr_seq(None);
    let else_id = else_seq.id();

    body.instr(walrus::ir::IfElse {
        consequent: then_id,
        alternative: else_id,
    });

    // Then branch (null): emit error and store into result_local
    {
        let mut then_body = body.instr_seq(then_id);
        let name_bytes = name.as_bytes();
        let name_len = name_bytes.len() as i32;
        let ptr_local = module.locals.add(ValType::I32);

        then_body
            .i32_const(name_len)
            .call(env.get(RuntimeFunction::Malloc))
            .local_set(ptr_local);

        for (offset, &byte) in name_bytes.iter().enumerate() {
            then_body.local_get(ptr_local);
            then_body.i32_const(byte as i32);
            then_body.store(
                memory_id,
                walrus::ir::StoreKind::I32_8 { atomic: false },
                walrus::ir::MemArg {
                    align: 1,
                    offset: offset as u64,
                },
            );
        }

        then_body
            .local_get(ptr_local)
            .i32_const(name_len)
            .call(env.get(RuntimeFunction::UnboundVariableError))
            .local_set(result_local);
    }

    // Else branch: result already valid, nothing to do

    Ok(())
}

/// Emit Wasm code that tries each candidate variable name in order, returning the
/// first non-null result. If no candidate resolves, emits a call to
/// `cel_unbound_variable_error` with the bare (last) candidate name so the result
/// is always a valid (non-null) `*mut CelValue`. Leaves an i32 on the stack.
///
/// Callers that need a null sentinel for "not found" (e.g. `compile_select`) must
/// use `emit_variable_lookup_chain_raw` instead.
fn emit_variable_lookup_chain(
    candidates: &[String],
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    module: &mut walrus::Module,
) -> Result<walrus::LocalId, anyhow::Error> {
    let memory_id = get_memory_id(module)?;

    if candidates.is_empty() {
        // No candidates at all — emit an error directly with an empty name
        let result_local = emit_unbound_error("", body, env, module, memory_id)?;
        body.local_get(result_local);
        return Ok(result_local);
    }

    // Run the raw lookup chain (may leave null in result_local on miss)
    let result_local = emit_variable_lookup_chain_raw(candidates, body, env, module)?;
    // Pop the value left on the stack by the raw chain — we'll put it back after the check
    body.local_set(result_local);

    // If null, replace with a proper error value naming the variable
    let bare_name = candidates.last().unwrap().as_str();
    emit_null_to_unbound_error(bare_name, result_local, body, env, module, memory_id)?;

    body.local_get(result_local);
    Ok(result_local)
}

/// Recursively builds the "if null, try next" chain for a slice of candidate names.
fn build_fallback_chain(
    remaining: &[String],
    result_local: walrus::LocalId,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    module: &mut walrus::Module,
    memory_id: walrus::MemoryId,
) -> Result<(), anyhow::Error> {
    if remaining.is_empty() {
        return Ok(());
    }

    // if result_local == 0 (null): try next candidate
    body.local_get(result_local)
        .unop(walrus::ir::UnaryOp::I32Eqz);

    let then_seq = body.dangling_instr_seq(None);
    let then_id = then_seq.id();
    let else_seq = body.dangling_instr_seq(None);
    let else_id = else_seq.id();

    body.instr(walrus::ir::IfElse {
        consequent: then_id,
        alternative: else_id,
    });

    // Then branch: try next candidate
    {
        let mut then_body = body.instr_seq(then_id);
        let name = &remaining[0];
        let name_bytes = name.as_bytes();
        let name_len = name_bytes.len() as i32;
        let ptr_local = module.locals.add(ValType::I32);

        then_body
            .i32_const(name_len)
            .call(env.get(RuntimeFunction::Malloc))
            .local_set(ptr_local);

        for (offset, &byte) in name_bytes.iter().enumerate() {
            then_body.local_get(ptr_local);
            then_body.i32_const(byte as i32);
            then_body.store(
                memory_id,
                walrus::ir::StoreKind::I32_8 { atomic: false },
                walrus::ir::MemArg {
                    align: 1,
                    offset: offset as u64,
                },
            );
        }

        then_body
            .local_get(ptr_local)
            .i32_const(name_len)
            .call(env.get(RuntimeFunction::GetVariable))
            .local_set(result_local);

        build_fallback_chain(
            &remaining[1..],
            result_local,
            &mut then_body,
            env,
            module,
            memory_id,
        )?;
    }

    // Else branch: result is already set (non-null), nothing to do
    // (empty else branch is valid in Wasm)

    Ok(())
}

/// Attempt to collect a `Select` (or `Ident`) chain into a dotted qualified name.
///
/// Returns `Some("a.b.c")` when the entire expression is of the form
/// `Ident("a") -> select "b" -> select "c"` with no `has()` test nodes.
/// Returns `None` as soon as any node is not a simple ident or a non-test select,
/// which means the expression represents a real runtime field access.
pub fn try_collect_qualified_ident(expr: &Expr) -> Option<String> {
    match expr {
        Expr::Ident(name) => Some(name.clone()),
        Expr::Select(s) if !s.test => {
            let base = try_collect_qualified_ident(&s.operand.expr)?;
            Some(format!("{}.{}", base, s.field))
        }
        _ => None,
    }
}

/// Return the root identifier name of a Select chain, or the ident name itself.
/// e.g. `x.y.z` → `Some("x")`, `x` → `Some("x")`, `f(x).y` → `None`
fn root_ident(expr: &Expr) -> Option<&str> {
    match expr {
        Expr::Ident(name) => Some(name.as_str()),
        Expr::Select(s) if !s.test => root_ident(&s.operand.expr),
        _ => None,
    }
}

/// Compile an `Expr::Ident` node.
///
/// Resolution order (per CEL spec):
/// 1. Local variables from comprehension scope (fast path via local_get)
/// 2. Type denotations (bool, int, uint, double, string, bytes, list, map, etc.)
/// 3. Runtime variable lookup with container-aware resolution:
///    - With container "A.B": tries A.B.name, A.name, name (first non-null wins)
///    - Without container: tries name directly
pub fn compile_ident(
    name: &str,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    ctx: &CompilerContext,
    module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
    // First check if this is a local variable (from comprehension scope)
    if let Some(&local_id) = ctx.local_vars.get(name) {
        body.local_get(local_id);
        return Ok(());
    }

    // Type denotations - these are constant Type values
    // Note: "dyn" is NOT a type denotation - it's only valid as a function call
    match name {
        "bool" | "int" | "uint" | "double" | "string" | "bytes" | "list" | "map" | "null_type"
        | "type" | "timestamp" | "duration" | "optional_type" => {
            let memory_id = get_memory_id(module)?;
            emit_string_const(name, body, env, memory_id, module);
            body.call(env.get(RuntimeFunction::CreateType));
        }
        _ => {
            // Build the ordered list of candidates using container resolution
            let candidates = variable_candidates(name, &ctx.container);
            emit_variable_lookup_chain(&candidates, body, env, module)?;
        }
    }

    Ok(())
}

/// Compile an `Expr::Select` node.
///
/// Resolution order (per CEL spec — longest prefix wins):
/// 1. If the entire chain is a known proto type or K8s type literal → type denotation
/// 2. If the root ident is a comprehension local → skip variable lookup, do field access
/// 3. Try to resolve the full dotted name as a variable (with container expansion):
///    - e.g. `x.y` with container `A.B` tries: `A.B.x.y`, `A.x.y`, `x.y`
///    - If found, use it
/// 4. Fall back to field access: resolve the operand (recursively applying same rules),
///    then call cel_get_field
pub fn compile_select(
    select_expr: &cel::common::ast::SelectExpr,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    ctx: &CompilerContext,
    module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
    // Well-known Kubernetes CEL extension type literals.
    const K8S_TYPE_LITERALS: &[&str] = &["net.IP", "net.CIDR"];

    // Check whether the entire Select chain forms a qualified type name.
    let qualified_name = if !select_expr.test {
        try_collect_qualified_ident(&Expr::Select(select_expr.clone())).filter(|name| {
            K8S_TYPE_LITERALS.contains(&name.as_str())
                || ctx
                    .schema
                    .as_ref()
                    .map(|s| s.has_message_type(name))
                    .unwrap_or(false)
        })
    } else {
        None
    };

    if let Some(type_name) = qualified_name {
        // Type denotation: cel_create_type(ptr, len)
        let memory_id = get_memory_id(module)?;
        emit_string_const(&type_name, body, env, memory_id, module);
        body.call(env.get(RuntimeFunction::CreateType));
        return Ok(());
    }

    // If the root of this Select chain is a comprehension local, skip variable
    // lookup entirely — it must be a field access on the local value.
    let root_is_local = root_ident(&Expr::Select(select_expr.clone()))
        .map(|r| ctx.local_vars.contains_key(r))
        .unwrap_or(false);

    if !select_expr.test && !root_is_local {
        // Try to resolve the full dotted name as a variable first (longest prefix rule).
        if let Some(full_name) = try_collect_qualified_ident(&Expr::Select(select_expr.clone())) {
            let candidates = variable_candidates(&full_name, &ctx.container);

            // Emit: result = try_variable_chain(candidates)
            // If non-null, use it. If null, fall through to field access.
            let memory_id = get_memory_id(module)?;
            let result_local = module.locals.add(ValType::I32);

            // Try all candidates (raw — returns null if not found, for field-access fallback)
            emit_variable_lookup_chain_raw(&candidates, body, env, module)?;
            body.local_set(result_local);

            // if result != null: use it; else: do field access
            body.local_get(result_local)
                .unop(walrus::ir::UnaryOp::I32Eqz);

            let then_seq = body.dangling_instr_seq(Some(ValType::I32));
            let then_id = then_seq.id();
            let else_seq = body.dangling_instr_seq(Some(ValType::I32));
            let else_id = else_seq.id();

            body.instr(walrus::ir::IfElse {
                consequent: then_id,
                alternative: else_id,
            });

            // Then branch (null — not found as variable): do field access
            {
                let mut then_body = body.instr_seq(then_id);
                compile_field_access(select_expr, &mut then_body, env, ctx, module, memory_id)?;
            }

            // Else branch (non-null — found as variable): return result
            body.instr_seq(else_id).local_get(result_local);

            return Ok(());
        }
    }

    // Default: plain field access (test selects, local-rooted chains, non-collectable chains)
    let memory_id = get_memory_id(module)?;
    compile_field_access(select_expr, body, env, ctx, module, memory_id)?;

    Ok(())
}

/// Emit field-access Wasm for a SelectExpr: compile the operand, then call GetField/HasField.
fn compile_field_access(
    select_expr: &cel::common::ast::SelectExpr,
    body: &mut InstrSeqBuilder,
    env: &CompilerEnv,
    ctx: &CompilerContext,
    module: &mut walrus::Module,
    memory_id: walrus::MemoryId,
) -> Result<(), anyhow::Error> {
    super::expr::compile_expr(&select_expr.operand.expr, body, env, ctx, module)?;

    let field_name = &select_expr.field;
    let field_bytes = field_name.as_bytes();
    let field_len = field_bytes.len() as i32;

    let field_ptr_local = module.locals.add(ValType::I32);

    body.i32_const(field_len)
        .call(env.get(RuntimeFunction::Malloc))
        .local_tee(field_ptr_local);

    for (offset, &byte) in field_bytes.iter().enumerate() {
        body.local_get(field_ptr_local);
        body.i32_const(byte as i32);
        body.store(
            memory_id,
            walrus::ir::StoreKind::I32_8 { atomic: false },
            walrus::ir::MemArg {
                align: 1,
                offset: offset as u64,
            },
        );
    }

    body.i32_const(field_len);

    if select_expr.test {
        body.call(env.get(RuntimeFunction::HasField));
    } else {
        body.call(env.get(RuntimeFunction::GetField));
    }

    Ok(())
}