hara-native 0.1.13

HAL-free native host runtime and package launcher for Hara
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
use std::collections::HashSet;

use super::{reps, NativeModule, Rep};
use crate::core::Value;
use crate::kernel::{Form, FunctionSchema, SchemaField, SchemaType};
use crate::vm::FunctionId;

const INVOCATION_ARGUMENTS_ERROR: &str =
    "hta/invocation-malformed: expected an HTA sequence of arguments";
const INVOCATION_ABI_ERROR: &str =
    "hta/invocation-abi: whole-Wasm function must declare handle-backed arguments and result";
const INVOCATION_SCHEMA_ERROR: &str = "hta/invocation-schema";

impl NativeModule {
    /// Calls a whole-Wasm function through the portable HTA0 value boundary.
    ///
    /// `request` is one HTA0-encoded Hara list, tuple, or vector containing
    /// the function arguments. The result is returned as one HTA0 frame.
    /// Internally, decoded values use the process-local scoped arena so calls
    /// between compiled Hara functions do not repeatedly encode or decode.
    ///
    /// The portable adapter accepts functions whose declared arguments and
    /// result use the dynamic handle representation, including the structured
    /// schemas retained by the compiled program. Scalar kernels retain the
    /// existing `call_i64` fast path.
    pub fn call_hta(&mut self, function: FunctionId, request: &[u8]) -> Result<Vec<u8>, String> {
        let schema = ensure_hta_value_abi(self, function)?;
        let arguments = decode_arguments(request)?;
        validate_hta_arguments(&self.artifact().program, &schema, &arguments)?;
        let result = self.call_value(function, &arguments)?;
        validate_hta_value(
            &self.artifact().program,
            &schema.output,
            &result,
            "result",
            &mut HashSet::new(),
        )?;
        crate::hta::encode(&result)
    }
}

fn decode_arguments(request: &[u8]) -> Result<Vec<Value>, String> {
    match crate::hta::decode_canonical(request)? {
        Value::List(values) => Ok(values.iter().cloned().collect()),
        Value::Tuple(values) => Ok(values.iter().cloned().collect()),
        Value::Vector(values) => Ok(values.iter().cloned().collect()),
        _ => Err(INVOCATION_ARGUMENTS_ERROR.into()),
    }
}

fn ensure_hta_value_abi(
    module: &NativeModule,
    function: FunctionId,
) -> Result<FunctionSchema, String> {
    let program = &module.artifact().program;
    let prototype = program
        .functions
        .get(usize::from(function))
        .ok_or_else(|| format!("unknown whole-Wasm function {function}"))?;

    let parameters_are_handles = (0..usize::from(prototype.arity)).all(|parameter| {
        reps::declared_parameter_rep(program, function, prototype, parameter)
            == Some(Rep::TruthyHandle)
    });
    let result_is_handle = reps::declared_result_rep(program, function) == Some(Rep::TruthyHandle);

    if parameters_are_handles && result_is_handle {
        match program.function_schema(function) {
            Some(SchemaType::Function(arities)) => arities
                .iter()
                .find(|arity| {
                    arity.fixed.len() == usize::from(prototype.arity)
                        && arity.rest.is_some() == prototype.variadic
                })
                .cloned()
                .ok_or_else(|| format!("{INVOCATION_ABI_ERROR}: {function}")),
            _ => Err(format!("{INVOCATION_ABI_ERROR}: {function}")),
        }
    } else {
        Err(format!("{INVOCATION_ABI_ERROR}: {function}"))
    }
}

fn validate_hta_arguments(
    program: &crate::vm::Program,
    schema: &FunctionSchema,
    arguments: &[Value],
) -> Result<(), String> {
    let fixed = schema.fixed.len();
    if arguments.len() < fixed || (schema.rest.is_none() && arguments.len() != fixed) {
        let expected = if schema.rest.is_some() {
            format!("at least {fixed}")
        } else {
            fixed.to_string()
        };
        return Err(format!(
            "{INVOCATION_SCHEMA_ERROR}: arguments expected {expected} values, got {}",
            arguments.len()
        ));
    }

    for (index, (schema, value)) in schema.fixed.iter().zip(arguments).enumerate() {
        validate_hta_value(
            program,
            schema,
            value,
            &format!("argument {index}"),
            &mut HashSet::new(),
        )?;
    }
    if let Some(rest) = schema.rest.as_deref() {
        for (index, value) in arguments.iter().enumerate().skip(fixed) {
            validate_hta_value(
                program,
                rest,
                value,
                &format!("argument {index}"),
                &mut HashSet::new(),
            )?;
        }
    }
    Ok(())
}

fn validate_hta_value(
    program: &crate::vm::Program,
    schema: &SchemaType,
    value: &Value,
    path: &str,
    references: &mut HashSet<String>,
) -> Result<(), String> {
    match schema {
        SchemaType::Primitive(name) => validate_primitive(name, value, path),
        SchemaType::Reference(name) => {
            if !references.insert(name.clone()) {
                return Ok(());
            }
            let result = match program.schema_types.get(name) {
                Some(schema) => validate_hta_value(program, schema, value, path, references),
                None => Err(format!(
                    "{INVOCATION_SCHEMA_ERROR}: {path} references unavailable schema {name}"
                )),
            };
            references.remove(name);
            result
        }
        SchemaType::Union(types) => {
            if types.iter().any(|schema| {
                validate_hta_value(program, schema, value, path, &mut references.clone()).is_ok()
            }) {
                Ok(())
            } else {
                Err(schema_mismatch(path, "a declared union", value))
            }
        }
        SchemaType::Vector(item) => {
            let Some(values) = sequence_values(value) else {
                return Err(schema_mismatch(path, "a vector", value));
            };
            for (index, item_value) in values.iter().enumerate() {
                validate_hta_value(
                    program,
                    item,
                    item_value,
                    &format!("{path}[{index}]"),
                    references,
                )?;
            }
            Ok(())
        }
        SchemaType::Set(item) => {
            let values = match value {
                Value::Set(values) => values.iter().collect::<Vec<_>>(),
                Value::OrderedSet(values) => values.iter().collect::<Vec<_>>(),
                Value::SortedSet(values) => values.iter().collect::<Vec<_>>(),
                _ => return Err(schema_mismatch(path, "a set", value)),
            };
            for (index, item_value) in values.iter().enumerate() {
                validate_hta_value(
                    program,
                    item,
                    item_value,
                    &format!("{path}[{index}]"),
                    references,
                )?;
            }
            Ok(())
        }
        SchemaType::Tuple(types) => {
            let Value::Tuple(values) = value else {
                return Err(schema_mismatch(path, "a tuple", value));
            };
            if values.len() != types.len() {
                return Err(format!(
                    "{INVOCATION_SCHEMA_ERROR}: {path} expected tuple arity {}, got {}",
                    types.len(),
                    values.len()
                ));
            }
            for (index, (schema, item_value)) in types.iter().zip(values.iter()).enumerate() {
                validate_hta_value(
                    program,
                    schema,
                    item_value,
                    &format!("{path}[{index}]"),
                    references,
                )?;
            }
            Ok(())
        }
        SchemaType::Map(fields) => validate_map(program, fields, value, path, references),
        SchemaType::Struct {
            name,
            mutable: true,
            ..
        } => Err(format!(
            "{INVOCATION_SCHEMA_ERROR}: {path} mutable struct {name} is not transportable over HTA0"
        )),
        SchemaType::Struct {
            name,
            fields,
            mutable: false,
        } => validate_struct(program, name, fields, value, path, references),
        SchemaType::WithProperties { schema, .. } => {
            validate_hta_value(program, schema, value, path, references)
        }
        // These forms do not yet carry an executable HTA value predicate. They
        // remain dynamic so older declarations do not acquire a false negative
        // merely because they use an extension or retained source form.
        SchemaType::Function(_)
        | SchemaType::Enum(_)
        | SchemaType::Extension { .. }
        | SchemaType::Unknown(_) => Ok(()),
    }
}

fn validate_primitive(name: &str, value: &Value, path: &str) -> Result<(), String> {
    let valid = match name {
        "any" | "value" => true,
        "nil" => matches!(value, Value::Nil),
        "bool" | "boolean" => matches!(value, Value::Bool(_)),
        "int" | "long" | "i64" => crate::numeric::is_long_value(value),
        "bigint" => crate::numeric::is_big_integer_value(value),
        "integer" => crate::numeric::integer_kind(value).is_some(),
        "float" | "f64" => matches!(value, Value::Float(_)),
        "number" => matches!(
            value,
            Value::Number(_) | Value::BigInteger(_) | Value::Float(_)
        ),
        "str" | "string" => matches!(value, Value::String(_)),
        "keyword" => matches!(value, Value::Keyword(_)),
        "symbol" => matches!(value, Value::Symbol(_)),
        "bytes" => matches!(value, Value::Bytes(_) | Value::ByteBuffer(_)),
        "char" | "character" => matches!(value, Value::Character(_)),
        _ => true,
    };
    if valid {
        Ok(())
    } else {
        Err(schema_mismatch(path, &format!(":{name}"), value))
    }
}

fn validate_map(
    program: &crate::vm::Program,
    fields: &[SchemaField],
    value: &Value,
    path: &str,
    references: &mut HashSet<String>,
) -> Result<(), String> {
    let Some(entries) = crate::core::map_entries(value) else {
        return Err(schema_mismatch(path, "a map", value));
    };
    for field in fields {
        let Some(name) = form_field_name(&field.name) else {
            return Err(format!(
                "{INVOCATION_SCHEMA_ERROR}: {path} contains an invalid field name {}",
                field.name
            ));
        };
        let found = entries.iter().find_map(|(key, value)| {
            value_field_name(key)
                .filter(|key_name| *key_name == name)
                .map(|_| value)
        });
        match found {
            Some(field_value) => {
                if is_optional(field.properties.as_ref()) && matches!(field_value, Value::Nil) {
                    continue;
                }
                validate_hta_value(
                    program,
                    &field.value_type,
                    field_value,
                    &format!("{path}.{}", name),
                    references,
                )?;
            }
            None if is_optional(field.properties.as_ref()) => {}
            None => {
                return Err(format!(
                    "{INVOCATION_SCHEMA_ERROR}: {path} is missing required field {name}"
                ))
            }
        }
    }
    Ok(())
}

fn validate_struct(
    program: &crate::vm::Program,
    name: &str,
    fields: &[SchemaField],
    value: &Value,
    path: &str,
    references: &mut HashSet<String>,
) -> Result<(), String> {
    let Value::Struct(value) = value else {
        return Err(schema_mismatch(path, &format!("struct {name}"), value));
    };
    if value.ty.name != name {
        return Err(format!(
            "{INVOCATION_SCHEMA_ERROR}: {path} expected struct {name}, got struct {}",
            value.ty.name
        ));
    }
    let expected_fields = fields
        .iter()
        .filter_map(|field| form_field_name(&field.name))
        .map(str::to_owned)
        .collect::<Vec<_>>();
    if expected_fields.len() != fields.len() || value.ty.fields != expected_fields {
        return Err(format!(
            "{INVOCATION_SCHEMA_ERROR}: {path} has a field layout different from struct {name}"
        ));
    }
    for field in fields {
        let field_name = form_field_name(&field.name).expect("validated struct field name");
        let Some(field_value) = value.get(field_name) else {
            if is_optional(field.properties.as_ref()) {
                continue;
            }
            return Err(format!(
                "{INVOCATION_SCHEMA_ERROR}: {path} is missing required field {field_name}"
            ));
        };
        if is_optional(field.properties.as_ref()) && matches!(field_value, Value::Nil) {
            continue;
        }
        validate_hta_value(
            program,
            &field.value_type,
            field_value,
            &format!("{path}.{}", field_name),
            references,
        )?;
    }
    Ok(())
}

fn sequence_values(value: &Value) -> Option<Vec<&Value>> {
    match value {
        Value::Tuple(values) => Some(values.iter().collect()),
        Value::Vector(values) => Some(values.iter().collect()),
        _ => None,
    }
}

fn form_field_name(form: &Form) -> Option<&str> {
    match form {
        Form::Symbol(name) | Form::Keyword(name) | Form::String(name) => Some(name.as_str()),
        _ => None,
    }
}

fn value_field_name(value: &Value) -> Option<&str> {
    match value {
        Value::String(name) => Some(name.as_str()),
        Value::Keyword(name) => Some(name.as_str()),
        Value::Symbol(name) => Some(name.as_str()),
        _ => None,
    }
}

fn is_optional(properties: Option<&Form>) -> bool {
    let Some(Form::Map(entries)) = properties else {
        return false;
    };
    entries.iter().any(|(key, value)| {
        matches!(key, Form::Keyword(name) if name == "optional")
            && matches!(value, Form::Bool(true))
    })
}

fn schema_mismatch(path: &str, expected: &str, value: &Value) -> String {
    format!(
        "{INVOCATION_SCHEMA_ERROR}: {path} expected {expected}, got {}",
        value_kind(value)
    )
}

fn value_kind(value: &Value) -> &'static str {
    match value {
        Value::Nil => "nil",
        Value::Bool(_) => "boolean",
        Value::Number(_) => "long",
        Value::BigInteger(_) if crate::numeric::is_long_value(value) => "long",
        Value::BigInteger(_) => "bigint",
        Value::Float(_) => "float",
        Value::String(_) => "string",
        Value::Keyword(_) => "keyword",
        Value::Symbol(_) => "symbol",
        Value::Bytes(_) | Value::ByteBuffer(_) => "bytes",
        Value::Vector(_) => "vector",
        Value::List(_) => "list",
        Value::Tuple(_) => "tuple",
        Value::MapEntry(_) => "map-entry",
        Value::Set(_) | Value::OrderedSet(_) | Value::SortedSet(_) => "set",
        Value::Map(_)
        | Value::OrderedMap(_)
        | Value::SortedMap(_)
        | Value::Trie(_)
        | Value::PriorityMap(_) => "map",
        Value::Struct(_) => "struct",
        Value::Mutable(_) => "mutable",
        _ => "value",
    }
}