Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
stringify.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
30
31/* JSON_STRINGIFY */
33#ifdef FUN_WITH_JSON
34 Value vpretty = pop_value(vm);
35 Value any = pop_value(vm);
36 int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0;
37 json_object *j = fun_to_json(&any);
38 int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN;
39 const char *js = json_object_to_json_string_ext(j, flags);
40 push_value(vm, make_string(js ? js : ""));
41 json_object_put(j);
42 free_value(vpretty);
44#else
45 /* Fallback: consume two args, push "null" */
46 Value vpretty = pop_value(vm);
47 free_value(vpretty);
48 Value any = pop_value(vm);
50 push_value(vm, make_string("null"));
51#endif
52 break;
53}
@ OP_JSON_STRINGIFY
Definition bytecode.h:168
Value any
Definition stringify.c:48
push_value(vm, make_string("null"))
free_value(vpretty)
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_BOOL
Definition value.h:52
@ VAL_INT
Definition value.h:51