Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
stringify.c File Reference

VM opcode snippet for converting a Fun Value to a JSON string. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 push_value (vm, make_string(js ? js :""))
 json_object_put (j)
 free_value (vpretty)
 free_value (any)

Variables

case OP_JSON_STRINGIFY
Value any = pop_value(vm)
int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0
json_object * j = fun_to_json(&any)
int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN
const char * js = json_object_to_json_string_ext(j, flags)
 break

Detailed Description

VM opcode snippet for converting a Fun Value to a JSON string.

Implements the OP_JSON_STRINGIFY instruction. Expects a boolean/integer flag indicating pretty-printing and a Value to serialize. Uses json-c to build a json_object from the Value and then renders it to a string, which is pushed back to the stack.

Build gating: compiled only when FUN_WITH_JSON is enabled. Otherwise the opcode consumes its two arguments and pushes the string "null".

Stack effect (with FUN_WITH_JSON):

  • Pops: pretty (bool/int), value (any)
  • Pushes: string (JSON representation)

Errors and edge cases:

  • If conversion to json_object fails, an empty string is pushed.
  • Pretty printing selects JSON_C_TO_STRING_PRETTY; otherwise plain output.

Definition in file stringify.c.

Function Documentation

◆ free_value() [1/2]

free_value ( any )

◆ free_value() [2/2]

free_value ( vpretty )

◆ json_object_put()

json_object_put ( j )

◆ push_value()

push_value ( vm ,
make_string(js ? js :"")  )

Variable Documentation

◆ any

Value any = pop_value(vm)

Definition at line 35 of file stringify.c.

◆ break

break

Definition at line 52 of file stringify.c.

◆ flags

int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN

Definition at line 38 of file stringify.c.

◆ j

json_object* j = fun_to_json(&any)

Definition at line 37 of file stringify.c.

◆ js

const char* js = json_object_to_json_string_ext(j, flags)

Definition at line 39 of file stringify.c.

◆ OP_JSON_STRINGIFY

case OP_JSON_STRINGIFY

Definition at line 32 of file stringify.c.

◆ pretty

int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0

Definition at line 36 of file stringify.c.