Skip to content

Commit 105ee03

Browse files
authored
chore: Remove fastly.h references from js-compute-builtins.cpp (#606)
1 parent cd189d4 commit 105ee03

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

runtime/js-compute-runtime/host_interface/host_api.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,21 @@ Result<HostBytes> Random::get_bytes(size_t num_bytes) {
10121012
return res;
10131013
}
10141014

1015+
Result<uint32_t> Random::get_u32() {
1016+
Result<uint32_t> res;
1017+
1018+
uint32_t storage;
1019+
auto err = fastly::random_get(reinterpret_cast<uint32_t>(static_cast<void *>(&storage)),
1020+
sizeof(storage));
1021+
if (err != 0) {
1022+
res.emplace_err(err);
1023+
} else {
1024+
res.emplace(storage);
1025+
}
1026+
1027+
return res;
1028+
}
1029+
10151030
bool CacheState::is_found() const {
10161031
return this->state & FASTLY_COMPUTE_AT_EDGE_FASTLY_CACHE_LOOKUP_STATE_FOUND;
10171032
}

runtime/js-compute-runtime/host_interface/host_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ class SecretStore final {
523523
class Random final {
524524
public:
525525
static Result<HostBytes> get_bytes(size_t num_bytes);
526+
527+
static Result<uint32_t> get_u32();
526528
};
527529

528530
struct CacheLookupOptions final {

runtime/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "js/shadow/Object.h"
3333
#include "zlib.h"
3434

35-
#include "host_interface/fastly.h"
3635
#include "host_interface/host_api.h"
3736

3837
#include "builtin.h"
@@ -960,9 +959,9 @@ static bool init(JSContext *cx, HandleObject global) {
960959
} // namespace GlobalProperties
961960

962961
bool math_random(JSContext *cx, unsigned argc, Value *vp) {
963-
uint32_t storage;
964-
fastly::random_get(reinterpret_cast<int32_t>(&storage), sizeof(storage));
965-
double newvalue = static_cast<double>(storage) / std::pow(2.0, 32.0);
962+
auto res = host_api::Random::get_u32();
963+
MOZ_ASSERT(!res.is_err());
964+
double newvalue = static_cast<double>(res.unwrap()) / std::pow(2.0, 32.0);
966965

967966
CallArgs args = CallArgsFromVp(argc, vp);
968967
args.rval().setDouble(newvalue);

0 commit comments

Comments
 (0)