Skip to content

Commit 52947a6

Browse files
elliotttJakeChampion
authored andcommitted
Remove the include of fastly_world.h from the host_api
Instead of re-exporting the api defined by fastly_world.h through host_api.h, introduce type synonyms for handle types and enforce through static asserts that the representation is the same. Consumers of host_api.h that require access to fastly_world.h now access it directly, reducing the number of files that are affected directly by changes to fastly.wit, and making it a little more difficult to use the generated api directly.
1 parent ca1c44c commit 52947a6

File tree

12 files changed

+150
-78
lines changed

12 files changed

+150
-78
lines changed

runtime/js-compute-runtime/builtin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef JS_COMPUTE_RUNTIME_BUILTIN_H
22
#define JS_COMPUTE_RUNTIME_BUILTIN_H
33

4+
#include <optional>
5+
#include <span>
46
#include <tuple>
57

68
// TODO: remove these once the warnings are fixed
@@ -16,8 +18,6 @@
1618
#include "jsapi.h"
1719
#include "jsfriendapi.h"
1820
#include "rust-url/rust-url.h"
19-
#include <span>
20-
2121
#pragma clang diagnostic pop
2222

2323
std::optional<std::span<uint8_t>> value_to_buffer(JSContext *cx, JS::HandleValue val,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "builtins/backend.h"
2121
#include "builtins/request-response.h"
22+
#include "host_interface/fastly.h"
2223
#include "host_interface/host_call.h"
2324
#include "js-compute-builtins.h"
2425
#include "js/Conversions.h"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "js/Conversions.h"
99

1010
#include "cache-override.h"
11+
#include "host_interface/fastly.h"
1112
#include "host_interface/host_call.h"
1213
#include "js-compute-builtins.h"
1314

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "builtin.h"
33
#include "builtins/native-stream-source.h"
44
#include "builtins/shared/url.h"
5+
#include "host_interface/fastly.h"
56
#include "host_interface/host_api.h"
67
#include "host_interface/host_call.h"
78
#include "js-compute-builtins.h"
@@ -64,14 +65,13 @@ bool SimpleCacheEntry::constructor(JSContext *cx, unsigned argc, JS::Value *vp)
6465
return false;
6566
}
6667

67-
JSObject *SimpleCacheEntry::create(JSContext *cx,
68-
fastly_compute_at_edge_fastly_body_handle_t body_handle) {
68+
JSObject *SimpleCacheEntry::create(JSContext *cx, HttpBody body_handle) {
6969
JS::RootedObject SimpleCacheEntry(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
7070
if (!SimpleCacheEntry)
7171
return nullptr;
7272

7373
JS::SetReservedSlot(SimpleCacheEntry, static_cast<uint32_t>(Slots::Body),
74-
JS::Int32Value(body_handle));
74+
JS::Int32Value(body_handle.handle));
7575
JS::SetReservedSlot(SimpleCacheEntry, static_cast<uint32_t>(Slots::BodyStream), JS::NullValue());
7676
JS::SetReservedSlot(SimpleCacheEntry, static_cast<uint32_t>(Slots::HasBody),
7777
JS::BooleanValue(true));
@@ -467,9 +467,9 @@ bool SimpleCache::getOrSetThenHandler(JSContext *cx, JS::HandleObject owner, JS:
467467
}
468468
}
469469

470-
fastly_compute_at_edge_fastly_body_handle_t bodyHandle = INVALID_HANDLE;
470+
HttpBody bodyHandle;
471471
fastly_compute_at_edge_fastly_cache_get_body_options_t opts;
472-
if (!fastly_compute_at_edge_fastly_cache_get_body(ret.f1, &opts, &bodyHandle, &err)) {
472+
if (!fastly_compute_at_edge_fastly_cache_get_body(ret.f1, &opts, &bodyHandle.handle, &err)) {
473473
HANDLE_ERROR(cx, err);
474474
if (!fastly_compute_at_edge_fastly_transaction_cancel(handle, &err)) {
475475
HANDLE_ERROR(cx, err);
@@ -552,9 +552,9 @@ bool SimpleCache::getOrSet(JSContext *cx, unsigned argc, JS::Value *vp) {
552552
}
553553
args.rval().setObject(*promise);
554554
if (state & FASTLY_COMPUTE_AT_EDGE_FASTLY_CACHE_LOOKUP_STATE_USABLE) {
555-
fastly_compute_at_edge_fastly_body_handle_t body = INVALID_HANDLE;
555+
HttpBody body;
556556
fastly_compute_at_edge_fastly_cache_get_body_options_t opts;
557-
if (!fastly_compute_at_edge_fastly_cache_get_body(handle, &opts, &body, &err)) {
557+
if (!fastly_compute_at_edge_fastly_cache_get_body(handle, &opts, &body.handle, &err)) {
558558
if (!fastly_compute_at_edge_fastly_transaction_cancel(handle, &err)) {
559559
HANDLE_ERROR(cx, err);
560560
return ReturnPromiseRejectedWithPendingError(cx, args);
@@ -838,14 +838,14 @@ bool SimpleCache::get(JSContext *cx, unsigned argc, JS::Value *vp) {
838838
return false;
839839
}
840840

841-
fastly_compute_at_edge_fastly_body_handle_t body = INVALID_HANDLE;
841+
HttpBody body;
842842
fastly_compute_at_edge_fastly_cache_get_body_options_t opts;
843-
if (!fastly_compute_at_edge_fastly_cache_get_body(handle, &opts, &body, &err)) {
843+
if (!fastly_compute_at_edge_fastly_cache_get_body(handle, &opts, &body.handle, &err)) {
844844
HANDLE_ERROR(cx, err);
845845
return false;
846846
}
847847

848-
if (body == INVALID_HANDLE) {
848+
if (!body.valid()) {
849849
args.rval().setNull();
850850
} else {
851851
JS::RootedObject entry(cx, SimpleCacheEntry::create(cx, body));

runtime/js-compute-runtime/builtins/cache-simple.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SimpleCacheEntry final : public BuiltinImpl<SimpleCacheEntry> {
2626

2727
static bool init_class(JSContext *cx, JS::HandleObject global);
2828
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
29-
static JSObject *create(JSContext *cx, fastly_compute_at_edge_fastly_body_handle_t body_handle);
29+
static JSObject *create(JSContext *cx, HttpBody body_handle);
3030
};
3131

3232
class SimpleCache : public BuiltinImpl<SimpleCache> {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "config-store.h"
2+
#include "host_interface/fastly.h"
23
#include "host_interface/host_api.h"
34

45
namespace builtins {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "dictionary.h"
2+
#include "host_interface/fastly.h"
23
#include "host_interface/host_api.h"
34

45
namespace builtins {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "builtins/kv-store.h"
1919
#include "builtins/native-stream-source.h"
2020
#include "builtins/shared/url.h"
21+
#include "host_interface/fastly.h"
2122
#include "host_interface/host_api.h"
2223
#include "js-compute-builtins.h"
2324

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "secret-store.h"
2+
#include "host_interface/fastly.h"
23
#include "host_interface/host_api.h"
34

45
namespace builtins {

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

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <algorithm>
2+
#include <type_traits>
23

34
#include "core/allocator.h"
45
#include "fastly-world/fastly_world.h"
@@ -13,8 +14,33 @@ fastly_world_string_t string_view_to_world_string(std::string_view str) {
1314
};
1415
}
1516

17+
HostString make_host_string(fastly_world_string_t str) {
18+
return HostString{JS::UniqueChars{str.ptr}, str.len};
19+
}
20+
21+
HostBytes make_host_bytes(fastly_world_list_u8_t str) {
22+
return HostBytes{std::unique_ptr<uint8_t[]>{str.ptr}, str.len};
23+
}
24+
25+
Response make_response(fastly_compute_at_edge_fastly_response_t &resp) {
26+
return Response{HttpResp{resp.f0}, HttpBody{resp.f1}};
27+
}
28+
1629
} // namespace
1730

31+
// Ensure that the handle types stay in sync with fastly-world.h
32+
static_assert(std::is_same_v<AsyncHandle::Handle, fastly_compute_at_edge_fastly_async_handle_t>);
33+
static_assert(std::is_same_v<HttpBody::Handle, fastly_compute_at_edge_fastly_body_handle_t>);
34+
static_assert(
35+
std::is_same_v<HttpPendingReq::Handle, fastly_compute_at_edge_fastly_pending_request_handle_t>);
36+
static_assert(std::is_same_v<HttpReq::Handle, fastly_compute_at_edge_fastly_request_handle_t>);
37+
static_assert(std::is_same_v<HttpResp::Handle, fastly_compute_at_edge_fastly_response_handle_t>);
38+
static_assert(
39+
std::is_same_v<LogEndpoint::Handle, fastly_compute_at_edge_fastly_log_endpoint_handle_t>);
40+
static_assert(std::is_same_v<Dict::Handle, fastly_compute_at_edge_fastly_dictionary_handle_t>);
41+
static_assert(
42+
std::is_same_v<ObjectStore::Handle, fastly_compute_at_edge_fastly_object_store_handle_t>);
43+
1844
Result<bool> AsyncHandle::is_ready() const {
1945
Result<bool> res;
2046

@@ -155,7 +181,7 @@ Result<std::vector<HostString>> generic_get_header_names(auto handle) {
155181
std::vector<HostString> names;
156182

157183
for (int i = 0; i < ret.len; i++) {
158-
names.emplace_back(HostString{ret.ptr[i]});
184+
names.emplace_back(make_host_string(ret.ptr[i]));
159185
}
160186

161187
// Free the vector of string pointers, but leave the individual strings alone.
@@ -183,7 +209,7 @@ Result<std::optional<std::vector<HostString>>> generic_get_header_values(auto ha
183209
std::vector<HostString> names;
184210

185211
for (int i = 0; i < ret.val.len; i++) {
186-
names.emplace_back(HostString{ret.val.ptr[i]});
212+
names.emplace_back(make_host_string(ret.val.ptr[i]));
187213
}
188214

189215
// Free the vector of string pointers, but leave the individual strings alone.
@@ -235,7 +261,7 @@ Result<std::optional<Response>> HttpPendingReq::poll() {
235261
if (!fastly_compute_at_edge_fastly_http_req_pending_req_poll(this->handle, &ret, &err)) {
236262
res.emplace_err(err);
237263
} else if (ret.is_some) {
238-
res.emplace(ret.val);
264+
res.emplace(make_response(ret.val));
239265
} else {
240266
res.emplace(std::nullopt);
241267
}
@@ -251,7 +277,7 @@ Result<Response> HttpPendingReq::wait() {
251277
if (!fastly_compute_at_edge_fastly_http_req_pending_req_wait(this->handle, &ret, &err)) {
252278
res.emplace_err(err);
253279
} else {
254-
res.emplace(ret);
280+
res.emplace(make_response(ret));
255281
}
256282

257283
return res;
@@ -377,7 +403,7 @@ Result<Response> HttpReq::send(HttpBody body, std::string_view backend) {
377403
&err)) {
378404
res.emplace_err(err);
379405
} else {
380-
res.emplace(ret);
406+
res.emplace(make_response(ret));
381407
}
382408

383409
return res;
@@ -435,7 +461,7 @@ Result<HostString> HttpReq::get_method() const {
435461
if (!fastly_compute_at_edge_fastly_http_req_method_get(this->handle, &ret, &err)) {
436462
res.emplace_err(err);
437463
} else {
438-
res.emplace(ret);
464+
res.emplace(make_host_string(ret));
439465
}
440466

441467
return res;
@@ -463,7 +489,7 @@ Result<HostString> HttpReq::get_uri() const {
463489
if (!fastly_compute_at_edge_fastly_http_req_uri_get(this->handle, &uri, &err)) {
464490
res.emplace_err(err);
465491
} else {
466-
res.emplace(uri);
492+
res.emplace(make_host_string(uri));
467493
}
468494

469495
return res;
@@ -509,7 +535,7 @@ Result<HostBytes> HttpReq::downstream_client_ip_addr() {
509535
if (!fastly_compute_at_edge_fastly_http_req_downstream_client_ip_addr(&octets, &err)) {
510536
res.emplace_err(err);
511537
} else {
512-
res.emplace(octets);
538+
res.emplace(make_host_bytes(octets));
513539
}
514540

515541
return res;
@@ -524,7 +550,7 @@ Result<HostString> HttpReq::http_req_downstream_tls_cipher_openssl_name() {
524550
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_cipher_openssl_name(&ret, &err)) {
525551
res.emplace_err(err);
526552
} else {
527-
res.emplace(ret);
553+
res.emplace(make_host_string(ret));
528554
}
529555

530556
return res;
@@ -539,7 +565,7 @@ Result<HostString> HttpReq::http_req_downstream_tls_protocol() {
539565
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_protocol(&ret, &err)) {
540566
res.emplace_err(err);
541567
} else {
542-
res.emplace(ret);
568+
res.emplace(make_host_string(ret));
543569
}
544570

545571
return res;
@@ -554,7 +580,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_client_hello() {
554580
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_client_hello(&ret, &err)) {
555581
res.emplace_err(err);
556582
} else {
557-
res.emplace(ret);
583+
res.emplace(make_host_bytes(ret));
558584
}
559585

560586
return res;
@@ -569,7 +595,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_raw_client_certificate() {
569595
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_raw_client_certificate(&ret, &err)) {
570596
res.emplace_err(err);
571597
} else {
572-
res.emplace(ret);
598+
res.emplace(make_host_bytes(ret));
573599
}
574600

575601
return res;
@@ -584,7 +610,7 @@ Result<HostBytes> HttpReq::http_req_downstream_tls_ja3_md5() {
584610
if (!fastly_compute_at_edge_fastly_http_req_downstream_tls_ja3_md5(&ret, &err)) {
585611
res.emplace_err(err);
586612
} else {
587-
res.emplace(ret);
613+
res.emplace(make_host_bytes(ret));
588614
}
589615

590616
return res;
@@ -736,7 +762,7 @@ Result<HostString> GeoIp::lookup(std::span<uint8_t> bytes) {
736762
if (!fastly_compute_at_edge_fastly_geo_lookup(&octets_list, &ret, &err)) {
737763
res.emplace_err(err);
738764
} else {
739-
res.emplace(ret);
765+
res.emplace(make_host_string(ret));
740766
}
741767

742768
return res;
@@ -795,7 +821,7 @@ Result<std::optional<HostString>> Dict::get(std::string_view name) {
795821
if (!fastly_compute_at_edge_fastly_dictionary_get(this->handle, &name_str, &ret, &err)) {
796822
res.emplace_err(err);
797823
} else if (ret.is_some) {
798-
res.emplace(ret.val);
824+
res.emplace(make_host_string(ret.val));
799825
} else {
800826
res.emplace(std::nullopt);
801827
}
@@ -852,6 +878,10 @@ Result<Void> ObjectStore::insert(std::string_view name, HttpBody body) {
852878

853879
namespace host_api {
854880

881+
static_assert(std::is_same_v<Secret::Handle, fastly_compute_at_edge_fastly_secret_handle_t>);
882+
static_assert(
883+
std::is_same_v<SecretStore::Handle, fastly_compute_at_edge_fastly_secret_store_handle_t>);
884+
855885
Result<std::optional<HostString>> Secret::plaintext() const {
856886
Result<std::optional<HostString>> res;
857887

@@ -860,7 +890,7 @@ Result<std::optional<HostString>> Secret::plaintext() const {
860890
if (!fastly_compute_at_edge_fastly_secret_store_plaintext(this->handle, &ret, &err)) {
861891
res.emplace_err(err);
862892
} else if (ret.is_some) {
863-
res.emplace(ret.val);
893+
res.emplace(make_host_string(ret.val));
864894
} else {
865895
res.emplace(std::nullopt);
866896
}

0 commit comments

Comments
 (0)