Skip to content

Commit

Permalink
Fix a possible library build problem
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin D. Howard <gavin@gavinhoward.com>
  • Loading branch information
gavinhoward committed Mar 3, 2023
1 parent 18c2901 commit 284e329
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/vm.h
Expand Up @@ -560,9 +560,13 @@ typedef struct BcVm
/// The vector for creating strings to pass to the client.
BcVec out;

#if BC_ENABLE_EXTRA_MATH

/// The PRNG.
BcRNG rng;

#endif // BC_ENABLE_EXTRA_MATH

/// The current error.
BclError err;

Expand Down
10 changes: 10 additions & 0 deletions src/library.c
Expand Up @@ -217,10 +217,14 @@ bcl_init(void)
bc_vec_init(&vm->ctxts, sizeof(BclContext), BC_DTOR_NONE);
bc_vec_init(&vm->out, sizeof(uchar), BC_DTOR_NONE);

#if BC_ENABLE_EXTRA_MATH

// We need to seed this in case /dev/random and /dev/urandom don't work.
srand((unsigned int) time(NULL));
bc_rand_init(&vm->rng);

#endif // BC_ENABLE_EXTRA_MATH

err:

BC_FUNC_FOOTER(vm, e);
Expand Down Expand Up @@ -285,7 +289,9 @@ bcl_free(void)
vm->refs -= 1;
if (vm->refs) return;

#if BC_ENABLE_EXTRA_MATH
bc_rand_free(&vm->rng);
#endif // BC_ENABLE_EXTRA_MATH
bc_vec_free(&vm->out);

for (i = 0; i < vm->ctxts.len; ++i)
Expand Down Expand Up @@ -1411,6 +1417,8 @@ bcl_string_keep(BclNumber n)
return bcl_string_helper(n, false);
}

#if BC_ENABLE_EXTRA_MATH

static BclNumber
bcl_irand_helper(BclNumber a, bool destruct)
{
Expand Down Expand Up @@ -1755,4 +1763,6 @@ bcl_rand_bounded(BclRandInt bound)
return (BclRandInt) bc_rand_bounded(&vm->rng, (BcRand) bound);
}

#endif // BC_ENABLE_EXTRA_MATH

#endif // BC_ENABLE_LIBRARY
2 changes: 2 additions & 0 deletions src/vm.c
Expand Up @@ -643,12 +643,14 @@ bc_vm_shutdown(void)
if (vm->catalog != BC_VM_INVALID_CATALOG) catclose(vm->catalog);
#endif // BC_ENABLE_NLS

#if !BC_ENABLE_LIBRARY
#if BC_ENABLE_HISTORY
// This must always run to ensure that the terminal is back to normal, i.e.,
// has raw mode disabled. But we should only do it if we did not have a bad
// terminal because history was not initialized if it is a bad terminal.
if (BC_TTY && !vm->history.badTerm) bc_history_free(&vm->history);
#endif // BC_ENABLE_HISTORY
#endif // !BC_ENABLE_LIBRARY

#if BC_DEBUG
#if !BC_ENABLE_LIBRARY
Expand Down

0 comments on commit 284e329

Please sign in to comment.