|
| 1 | +#include <assert.h> |
| 2 | +#include <stdio.h> |
| 3 | + |
| 4 | +#include <rune.h> |
| 5 | + |
| 6 | +/** |
| 7 | + * A custom C function that interacts with Rune. This is registered below with |
| 8 | + * rune_module_function. |
| 9 | + */ |
| 10 | +void custom_function(rune_stack *stack, uintptr_t count, rune_vm_error *e) { |
| 11 | + rune_value value = rune_value_unit(); |
| 12 | + |
| 13 | + if (count != 1) { |
| 14 | + vm_error_bad_argument_count(e, count, 1); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // Note: Error will be automatically propagated since it's used as an output |
| 19 | + // argument. |
| 20 | + if (!rune_stack_pop_value(stack, &value, e)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + int64_t integer = 0; |
| 25 | + |
| 26 | + if (!rune_value_as_integer(&value, &integer)) { |
| 27 | + vm_error_bad_argument_at(e, 0, &value, RUNE_INTEGER_TYPE); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + rune_stack_push_unit(stack); |
| 32 | + rune_stack_push_integer(stack, integer * 10); |
| 33 | + rune_stack_push_tuple(stack, 2, e); |
| 34 | +} |
| 35 | + |
| 36 | +int main() { |
| 37 | + rune_context context = rune_context_new(); |
| 38 | + rune_module module = rune_module_new(); |
| 39 | + rune_runtime_context runtime = rune_runtime_context_new(); |
| 40 | + rune_sources sources = rune_sources_new(); |
| 41 | + rune_standard_stream out = rune_standard_stream_stderr(RUNE_COLOR_CHOICE_ALWAYS); |
| 42 | + rune_unit unit = rune_unit_new(); |
| 43 | + rune_vm vm = rune_vm_new(); |
| 44 | + rune_vm_error error = rune_vm_error_new(); |
| 45 | + rune_context_error context_error = rune_context_error_new(); |
| 46 | + |
| 47 | + if (!rune_module_function(&module, "test", custom_function, &context_error)) { |
| 48 | + rune_context_error_emit(&context_error, &out); |
| 49 | + goto EXIT; |
| 50 | + } |
| 51 | + |
| 52 | + if (!rune_context_install(&context, &module, &context_error)) { |
| 53 | + rune_context_error_emit(&context_error, &out); |
| 54 | + goto EXIT; |
| 55 | + } |
| 56 | + |
| 57 | + rune_module_free(&module); |
| 58 | + |
| 59 | + rune_source source = rune_source_new("<in>", "pub fn main(n) { test(n) }"); |
| 60 | + assert(rune_sources_insert(&sources, &source)); |
| 61 | + rune_source_free(&source); |
| 62 | + |
| 63 | + rune_diagnostics diag = rune_diagnostics_new(); |
| 64 | + |
| 65 | + rune_build build = rune_build_prepare(&sources); |
| 66 | + rune_build_with_diagnostics(&build, &diag); |
| 67 | + rune_build_with_context(&build, &context); |
| 68 | + |
| 69 | + bool ok = rune_build_build(&build, &unit); |
| 70 | + |
| 71 | + if (!rune_diagnostics_is_empty(&diag)) { |
| 72 | + assert(rune_diagnostics_emit(&diag, &out, &sources)); |
| 73 | + } |
| 74 | + |
| 75 | + rune_diagnostics_free(&diag); |
| 76 | + |
| 77 | + if (!ok) { |
| 78 | + goto EXIT; |
| 79 | + } |
| 80 | + |
| 81 | + assert(rune_context_runtime(&context, &runtime)); |
| 82 | + assert(rune_vm_setup(&vm, &runtime, &unit)); |
| 83 | + |
| 84 | + rune_hash entry = rune_hash_name("main"); |
| 85 | + |
| 86 | + if (!rune_vm_set_entrypoint(&vm, entry, 1, &error)) { |
| 87 | + assert(rune_vm_error_emit(&error, &out, &sources)); |
| 88 | + goto EXIT; |
| 89 | + } |
| 90 | + |
| 91 | + rune_stack_push_integer(rune_vm_stack_mut(&vm), 42); |
| 92 | + rune_value ret = rune_value_unit(); |
| 93 | + |
| 94 | + if (!rune_vm_complete(&vm, &ret, &error)) { |
| 95 | + assert(rune_vm_error_emit(&error, &out, &sources)); |
| 96 | + } |
| 97 | + |
| 98 | + int64_t output = 0; |
| 99 | + |
| 100 | + if (rune_value_as_integer(&ret, &output)) { |
| 101 | + printf("output = %lld\n", output); |
| 102 | + } else { |
| 103 | + rune_hash type_hash = rune_hash_empty(); |
| 104 | + |
| 105 | + if (rune_value_type_hash(&ret, &type_hash, &error)) { |
| 106 | + printf("output = %lld\n", type_hash); |
| 107 | + } else { |
| 108 | + printf("output = ?\n"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + rune_value_free(&ret); |
| 113 | + |
| 114 | +EXIT: |
| 115 | + rune_context_free(&context); |
| 116 | + rune_module_free(&module); |
| 117 | + rune_runtime_context_free(&runtime); |
| 118 | + rune_sources_free(&sources); |
| 119 | + rune_standard_stream_free(&out); |
| 120 | + rune_unit_free(&unit); |
| 121 | + rune_vm_error_free(&error); |
| 122 | + rune_vm_free(&vm); |
| 123 | + return 0; |
| 124 | +} |
0 commit comments