Browse Source

Fix DUK_USE_GET_RANDOM_DOUBLE() arg handling

pull/2457/head
Sami Vaarala 3 years ago
parent
commit
0c1e7659ab
  1. 2
      src-input/duk_api_random.c
  2. 2
      src-input/duk_bi_array.c
  3. 2
      src-input/duk_bi_math.c
  4. 8
      src-input/duk_util.h
  5. 12
      src-input/duk_util_misc.c

2
src-input/duk_api_random.c

@ -5,5 +5,5 @@
#include "duk_internal.h"
DUK_EXTERNAL duk_double_t duk_random(duk_hthread *thr) {
return (duk_double_t) DUK_UTIL_GET_RANDOM_DOUBLE(thr);
return (duk_double_t) duk_util_get_random_double(thr);
}

2
src-input/duk_bi_array.c

@ -836,7 +836,7 @@ DUK_LOCAL void duk__array_qsort(duk_hthread *thr, duk_int_t lo, duk_int_t hi) {
DUK_ASSERT(hi - lo + 1 >= 2);
/* randomized pivot selection */
p = lo + (duk_int_t) (DUK_UTIL_GET_RANDOM_DOUBLE(thr) * (duk_double_t) (hi - lo + 1));
p = lo + (duk_int_t) (duk_util_get_random_double(thr) * (duk_double_t) (hi - lo + 1));
DUK_ASSERT(p >= lo && p <= hi);
DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld", (long) lo, (long) hi, (long) p));

2
src-input/duk_bi_math.c

@ -323,7 +323,7 @@ DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_hthread *thr) {
}
DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_hthread *thr) {
duk_push_number(thr, (duk_double_t) DUK_UTIL_GET_RANDOM_DOUBLE(thr));
duk_push_number(thr, (duk_double_t) duk_util_get_random_double(thr));
return 1;
}

8
src-input/duk_util.h

@ -5,12 +5,6 @@
#if !defined(DUK_UTIL_H_INCLUDED)
#define DUK_UTIL_H_INCLUDED
#if defined(DUK_USE_GET_RANDOM_DOUBLE)
#define DUK_UTIL_GET_RANDOM_DOUBLE(thr) DUK_USE_GET_RANDOM_DOUBLE((thr)->heap_udata)
#else
#define DUK_UTIL_GET_RANDOM_DOUBLE(thr) duk_util_tinyrandom_get_double(thr)
#endif
/*
* Some useful constants
*/
@ -620,6 +614,8 @@ DUK_INTERNAL_DECL void duk_raw_writeinc_cesu8(duk_uint8_t **p, duk_ucodepoint_t
DUK_INTERNAL_DECL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len);
#endif
DUK_INTERNAL_DECL duk_double_t duk_util_get_random_double(duk_hthread *thr);
/* memcpy(), memmove() etc wrappers. The plain variants like duk_memcpy()
* assume C99+ and 'src' and 'dst' pointers must be non-NULL even when the
* operation size is zero. The unsafe variants like duk_memcpy_safe() deal

12
src-input/duk_util_misc.c

@ -151,3 +151,15 @@ DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) {
}
}
#endif
/*
* Random
*/
DUK_INTERNAL duk_double_t duk_util_get_random_double(duk_hthread *thr) {
#if defined(DUK_USE_GET_RANDOM_DOUBLE)
return DUK_USE_GET_RANDOM_DOUBLE(thr->heap->heap_udata);
#else
return duk_util_tinyrandom_get_double(thr);
#endif
}

Loading…
Cancel
Save