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

VM opcode snippet for writing a Fun Value as JSON to a file. More...

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

Go to the source code of this file.

Functions

 free_value (vpretty)
 free_value (vpath)
 if (!path)
 json_object_put (j)
 free (path)
 free_value (any)
 push_value (vm, make_int(rc==0 ? 1 :0))

Variables

case OP_JSON_TO_FILE
Value any = pop_value(vm)
Value vpath = pop_value(vm)
char * path = value_to_string_alloc(&vpath)
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
int rc = json_object_to_file_ext(path, j, flags)
 break

Detailed Description

VM opcode snippet for writing a Fun Value as JSON to a file.

Implements the OP_JSON_TO_FILE instruction. Expects a pretty-print flag, a Value to serialize, and a path. Serializes the Value via json-c and writes it to the specified file path.

Build gating: compiled only when FUN_WITH_JSON is enabled. Otherwise the opcode consumes three arguments and pushes 0 (failure).

Stack effect (with FUN_WITH_JSON):

  • Pops: pretty (bool/int), value (any), path (any; converted to string)
  • Pushes: int (1 on success, 0 on failure)

Errors and edge cases:

  • If the path cannot be converted to a C string or file writing fails, the opcode pushes 0.
  • Pretty printing selects JSON_C_TO_STRING_PRETTY; otherwise plain output.

Definition in file to_file.c.

Function Documentation

◆ free()

free ( path )

◆ free_value() [1/3]

free_value ( any )

◆ free_value() [2/3]

free_value ( vpath )

◆ free_value() [3/3]

free_value ( vpretty )

◆ if()

if ( ! path)

Definition at line 41 of file to_file.c.

◆ json_object_put()

json_object_put ( j )

◆ push_value()

push_value ( vm ,
make_int(rc==0 ? 1 :0)  )

Variable Documentation

◆ any

Value any = pop_value(vm)

Definition at line 35 of file to_file.c.

◆ break

break

Definition at line 62 of file to_file.c.

◆ flags

int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN

Definition at line 47 of file to_file.c.

◆ j

json_object* j = fun_to_json(&any)

Definition at line 46 of file to_file.c.

◆ OP_JSON_TO_FILE

case OP_JSON_TO_FILE

Definition at line 32 of file to_file.c.

◆ path

char* path = value_to_string_alloc(&vpath)

Definition at line 37 of file to_file.c.

◆ pretty

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

Definition at line 38 of file to_file.c.

◆ rc

int rc = json_object_to_file_ext(path, j, flags)

Definition at line 48 of file to_file.c.

◆ vpath

Value vpath = pop_value(vm)

Definition at line 36 of file to_file.c.