Skip to content

Commit 65eeae9

Browse files
authored
chore: Finish migrating to the host_api (#539)
1 parent 426b346 commit 65eeae9

File tree

14 files changed

+609
-257
lines changed

14 files changed

+609
-257
lines changed

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

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -712,109 +712,93 @@ bool Backend::isCipherSuiteSupportedByFastly(std::string_view cipherSpec) {
712712
JS::Result<mozilla::Ok> Backend::register_dynamic_backend(JSContext *cx, JS::HandleObject backend) {
713713
MOZ_ASSERT(is_instance(backend));
714714

715-
fastly_world_string_t name_str;
716715
JS::RootedString name(cx, JS::GetReservedSlot(backend, Backend::Slots::Name).toString());
717-
JS::UniqueChars nameChars = encode(cx, name, &name_str.len);
718-
name_str.ptr = nameChars.get();
716+
size_t name_len;
717+
JS::UniqueChars nameChars = encode(cx, name, &name_len);
718+
std::string_view name_str{nameChars.get(), name_len};
719719

720-
fastly_world_string_t target_str;
721720
JS::RootedString target(cx, JS::GetReservedSlot(backend, Backend::Slots::Target).toString());
722-
JS::UniqueChars targetChars = encode(cx, target, &target_str.len);
723-
target_str.ptr = targetChars.get();
721+
size_t target_len;
722+
JS::UniqueChars targetChars = encode(cx, target, &target_len);
723+
std::string_view target_str{targetChars.get(), target_len};
724724

725-
fastly_dynamic_backend_config_t backend_config;
726-
std::memset(&backend_config, 0, sizeof(backend_config));
725+
BackendConfig backend_config;
727726

728-
std::string hostOverride;
729727
auto hostOverrideSlot = JS::GetReservedSlot(backend, Backend::Slots::HostOverride);
730-
if ((backend_config.host_override.is_some = !hostOverrideSlot.isNullOrUndefined())) {
728+
if (!hostOverrideSlot.isNullOrUndefined()) {
731729
JS::RootedString hostOverrideString(cx, hostOverrideSlot.toString());
732730
size_t hostOverride_len;
733731
JS::UniqueChars hostOverrideChars = encode(cx, hostOverrideString, &hostOverride_len);
734-
hostOverride = std::string(hostOverrideChars.get(), hostOverride_len);
735-
backend_config.host_override.val.ptr = const_cast<char *>(hostOverride.c_str());
736-
backend_config.host_override.val.len = hostOverride.length();
732+
backend_config.host_override.emplace(std::move(hostOverrideChars), hostOverride_len);
737733
}
738734

739735
auto connectTimeoutSlot = JS::GetReservedSlot(backend, Backend::Slots::ConnectTimeout);
740-
if ((backend_config.connect_timeout.is_some = !connectTimeoutSlot.isNullOrUndefined())) {
741-
backend_config.connect_timeout.val = connectTimeoutSlot.toInt32();
736+
if (!connectTimeoutSlot.isNullOrUndefined()) {
737+
backend_config.connect_timeout = connectTimeoutSlot.toInt32();
742738
}
743739

744740
auto firstByteTimeoutSlot = JS::GetReservedSlot(backend, Backend::Slots::FirstByteTimeout);
745-
if ((backend_config.first_byte_timeout.is_some = !firstByteTimeoutSlot.isNullOrUndefined())) {
746-
backend_config.first_byte_timeout.val = firstByteTimeoutSlot.toInt32();
741+
if (!firstByteTimeoutSlot.isNullOrUndefined()) {
742+
backend_config.first_byte_timeout = firstByteTimeoutSlot.toInt32();
747743
}
748744

749745
auto betweenBytesTimeoutSlot = JS::GetReservedSlot(backend, Backend::Slots::BetweenBytesTimeout);
750-
if ((backend_config.between_bytes_timeout.is_some =
751-
!betweenBytesTimeoutSlot.isNullOrUndefined())) {
752-
backend_config.between_bytes_timeout.val = betweenBytesTimeoutSlot.toInt32();
746+
if (!betweenBytesTimeoutSlot.isNullOrUndefined()) {
747+
backend_config.between_bytes_timeout = betweenBytesTimeoutSlot.toInt32();
753748
}
754749

755750
auto useSslSlot = JS::GetReservedSlot(backend, Backend::Slots::UseSsl);
756-
if ((backend_config.use_ssl.is_some = !useSslSlot.isNullOrUndefined())) {
757-
backend_config.use_ssl.val = useSslSlot.toBoolean();
751+
if (!useSslSlot.isNullOrUndefined()) {
752+
backend_config.use_ssl = useSslSlot.toBoolean();
758753
}
759754

760755
auto tlsMinVersion = JS::GetReservedSlot(backend, Backend::Slots::TlsMinVersion);
761-
if ((backend_config.ssl_min_version.is_some = !tlsMinVersion.isNullOrUndefined())) {
762-
backend_config.ssl_min_version.val = (int8_t)tlsMinVersion.toInt32();
756+
if (!tlsMinVersion.isNullOrUndefined()) {
757+
backend_config.ssl_min_version = static_cast<uint8_t>(tlsMinVersion.toInt32());
763758
}
764759

765760
auto tlsMaxVersion = JS::GetReservedSlot(backend, Backend::Slots::TlsMaxVersion);
766-
if ((backend_config.ssl_max_version.is_some = !tlsMaxVersion.isNullOrUndefined())) {
767-
backend_config.ssl_max_version.val = (int8_t)tlsMaxVersion.toInt32();
761+
if (!tlsMaxVersion.isNullOrUndefined()) {
762+
backend_config.ssl_max_version = static_cast<int8_t>(tlsMaxVersion.toInt32());
768763
}
769764

770-
std::string certificateHostname;
771765
auto certificateHostnameSlot = JS::GetReservedSlot(backend, Backend::Slots::CertificateHostname);
772-
if ((backend_config.cert_hostname.is_some = !certificateHostnameSlot.isNullOrUndefined())) {
766+
if (!certificateHostnameSlot.isNullOrUndefined()) {
773767
JS::RootedString certificateHostnameString(cx, certificateHostnameSlot.toString());
774768
size_t certificateHostname_len;
775769
JS::UniqueChars certificateHostnameChars =
776770
encode(cx, certificateHostnameString, &certificateHostname_len);
777-
certificateHostname = std::string(certificateHostnameChars.get(), certificateHostname_len);
778-
backend_config.cert_hostname.val.ptr = const_cast<char *>(certificateHostname.c_str());
779-
backend_config.cert_hostname.val.len = certificateHostname.length();
771+
backend_config.cert_hostname.emplace(std::move(certificateHostnameChars),
772+
certificateHostname_len);
780773
}
781774

782-
std::string caCertificate;
783775
auto caCertificateSlot = JS::GetReservedSlot(backend, Backend::Slots::CaCertificate);
784-
if ((backend_config.ca_cert.is_some = !caCertificateSlot.isNullOrUndefined())) {
776+
if (!caCertificateSlot.isNullOrUndefined()) {
785777
JS::RootedString caCertificateString(cx, caCertificateSlot.toString());
786778
size_t caCertificate_len;
787779
JS::UniqueChars caCertificateChars = encode(cx, caCertificateString, &caCertificate_len);
788-
caCertificate = std::string(caCertificateChars.get(), caCertificate_len);
789-
backend_config.ca_cert.val.ptr = const_cast<char *>(caCertificate.c_str());
790-
backend_config.ca_cert.val.len = caCertificate.length();
780+
backend_config.ca_cert.emplace(std::move(caCertificateChars), caCertificate_len);
791781
}
792782

793-
std::string ciphers;
794783
auto ciphersSlot = JS::GetReservedSlot(backend, Backend::Slots::Ciphers);
795-
if ((backend_config.ciphers.is_some = !ciphersSlot.isNullOrUndefined())) {
784+
if (!ciphersSlot.isNullOrUndefined()) {
796785
JS::RootedString ciphersString(cx, ciphersSlot.toString());
797786
size_t ciphers_len;
798787
JS::UniqueChars ciphersChars = encode(cx, ciphersString, &ciphers_len);
799-
ciphers = std::string(ciphersChars.get(), ciphers_len);
800-
backend_config.ciphers.val.ptr = const_cast<char *>(ciphers.c_str());
801-
backend_config.ciphers.val.len = ciphers.length();
788+
backend_config.ciphers.emplace(std::move(ciphersChars), ciphers_len);
802789
}
803790

804-
std::string sniHostname;
805791
auto sniHostnameSlot = JS::GetReservedSlot(backend, Backend::Slots::SniHostname);
806-
if ((backend_config.sni_hostname.is_some = !sniHostnameSlot.isNullOrUndefined())) {
792+
if (!sniHostnameSlot.isNullOrUndefined()) {
807793
JS::RootedString sniHostnameString(cx, sniHostnameSlot.toString());
808794
size_t sniHostname_len;
809795
JS::UniqueChars sniHostnameChars = encode(cx, sniHostnameString, &sniHostname_len);
810-
sniHostname = std::string(sniHostnameChars.get(), sniHostname_len);
811-
backend_config.sni_hostname.val.ptr = const_cast<char *>(sniHostname.c_str());
812-
backend_config.sni_hostname.val.len = sniHostname.length();
796+
backend_config.sni_hostname.emplace(std::move(sniHostnameChars), sniHostname_len);
813797
}
814798

815-
fastly_error_t err;
816-
if (!fastly_http_req_register_dynamic_backend(&name_str, &target_str, &backend_config, &err)) {
817-
HANDLE_ERROR(cx, err);
799+
auto res = HttpReq::register_dynamic_backend(name_str, target_str, backend_config);
800+
if (auto *err = res.to_err()) {
801+
HANDLE_ERROR(cx, *err);
818802
return JS::Result<mozilla::Ok>(JS::Error());
819803
}
820804
return mozilla::Ok();

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "builtins/client-info.h"
22
#include "core/geo_ip.h"
3-
#include "host_interface/host_call.h"
3+
#include "host_interface/host_api.h"
44

55
#include "js/JSON.h"
66
#include <arpa/inet.h>
@@ -20,45 +20,43 @@ JSString *geo_info(JSObject *obj) {
2020
}
2121

2222
static JSString *retrieve_address(JSContext *cx, JS::HandleObject self) {
23-
JS::RootedString address(cx);
24-
25-
fastly_world_list_u8_t octets;
26-
fastly_error_t err;
27-
if (!fastly_http_req_downstream_client_ip_addr(&octets, &err)) {
28-
HANDLE_ERROR(cx, err);
23+
auto res = HttpReq::downstream_client_ip_addr();
24+
if (auto *err = res.to_err()) {
25+
HANDLE_ERROR(cx, *err);
2926
return nullptr;
3027
}
3128

29+
auto octets = std::move(res.unwrap());
30+
char address_chars[INET6_ADDRSTRLEN];
31+
int addr_family = 0;
32+
socklen_t size = 0;
33+
3234
switch (octets.len) {
3335
case 0: {
3436
// No address to be had, leave `address` as a nullptr.
35-
JS_free(cx, octets.ptr);
3637
break;
3738
}
3839
case 4: {
39-
char address_chars[INET_ADDRSTRLEN];
40-
// TODO: do we need to do error handling here, or can we depend on the
41-
// host giving us a valid address?
42-
inet_ntop(AF_INET, octets.ptr, address_chars, INET_ADDRSTRLEN);
43-
address = JS_NewStringCopyZ(cx, address_chars);
44-
JS_free(cx, octets.ptr);
45-
if (!address)
46-
return nullptr;
47-
40+
addr_family = AF_INET;
41+
size = INET_ADDRSTRLEN;
4842
break;
4943
}
5044
case 16: {
51-
char address_chars[INET6_ADDRSTRLEN];
45+
addr_family = AF_INET6;
46+
size = INET6_ADDRSTRLEN;
47+
break;
48+
}
49+
}
50+
51+
JS::RootedString address(cx);
52+
if (octets.len > 0) {
5253
// TODO: do we need to do error handling here, or can we depend on the
5354
// host giving us a valid address?
54-
inet_ntop(AF_INET6, octets.ptr, address_chars, INET6_ADDRSTRLEN);
55+
inet_ntop(addr_family, octets.begin(), address_chars, size);
5556
address = JS_NewStringCopyZ(cx, address_chars);
56-
JS_free(cx, octets.ptr);
57-
if (!address)
57+
if (!address) {
5858
return nullptr;
59-
60-
break;
61-
}
59+
}
6260
}
6361

6462
JS::SetReservedSlot(self, static_cast<uint32_t>(ClientInfo::Slots::Address),

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
#include "config-store.h"
2-
#include "host_interface/host_call.h"
2+
#include "host_interface/host_api.h"
33

44
namespace builtins {
55

6-
fastly_dictionary_handle_t ConfigStore::config_store_handle(JSObject *obj) {
6+
Dict ConfigStore::config_store_handle(JSObject *obj) {
77
JS::Value val = JS::GetReservedSlot(obj, ConfigStore::Slots::Handle);
8-
return static_cast<fastly_dictionary_handle_t>(val.toInt32());
8+
return Dict{static_cast<fastly_dictionary_handle_t>(val.toInt32())};
99
}
1010

1111
bool ConfigStore::get(JSContext *cx, unsigned argc, JS::Value *vp) {
1212
METHOD_HEADER(1)
1313

14-
fastly_world_string_t key_str;
15-
JS::UniqueChars key = encode(cx, args[0], &key_str.len);
14+
size_t key_len;
15+
JS::UniqueChars key = encode(cx, args[0], &key_len);
1616
// If the converted string has a length of 0 then we throw an Error
1717
// because Dictionary keys have to be at-least 1 character.
18-
if (!key || key_str.len == 0) {
18+
if (!key || key_len == 0) {
1919
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_KEY_EMPTY);
2020
return false;
2121
}
22-
key_str.ptr = key.get();
2322

2423
// key has to be less than 256
25-
if (key_str.len > 255) {
24+
if (key_len > 255) {
2625
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_KEY_TOO_LONG);
2726
return false;
2827
}
2928

30-
fastly_world_option_string_t ret;
31-
fastly_error_t err;
29+
std::string_view key_str{key.get(), key_len};
3230
// Ensure that we throw an exception for all unexpected host errors.
33-
if (!fastly_dictionary_get(ConfigStore::config_store_handle(self), &key_str, &ret, &err)) {
34-
HANDLE_ERROR(cx, err);
31+
auto get_res = ConfigStore::config_store_handle(self).get(key_str);
32+
if (auto *err = get_res.to_err()) {
33+
HANDLE_ERROR(cx, *err);
3534
return false;
3635
}
3736

3837
// None indicates the key wasn't found, so we return null.
39-
if (!ret.is_some) {
38+
auto ret = std::move(get_res.unwrap());
39+
if (!ret.has_value()) {
4040
args.rval().setNull();
4141
return true;
4242
}
4343

44-
JS::RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(ret.val.ptr, ret.val.len)));
45-
JS_free(cx, ret.val.ptr);
46-
if (!text)
44+
JS::RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(ret->begin(), ret->size())));
45+
if (!text) {
4746
return false;
47+
}
4848

4949
args.rval().setString(text);
5050
return true;
@@ -66,26 +66,24 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
6666
REQUEST_HANDLER_ONLY("The ConfigStore builtin");
6767
CTOR_HEADER("ConfigStore", 1);
6868

69-
fastly_world_string_t name_str;
70-
JS::UniqueChars name_chars = encode(cx, args[0], &name_str.len);
71-
name_str.ptr = name_chars.get();
69+
size_t name_len;
70+
JS::UniqueChars name_chars = encode(cx, args[0], &name_len);
71+
std::string_view name{name_chars.get(), name_len};
7272

7373
// If the converted string has a length of 0 then we throw an Error
7474
// because Dictionary names have to be at-least 1 character.
75-
if (name_str.len == 0) {
75+
if (name.empty()) {
7676
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_NAME_EMPTY);
7777
return false;
7878
}
7979

8080
// If the converted string has a length of more than 255 then we throw an Error
8181
// because Dictionary names have to be less than 255 characters.
82-
if (name_str.len > 255) {
82+
if (name.size() > 255) {
8383
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_NAME_TOO_LONG);
8484
return false;
8585
}
8686

87-
std::string_view name(name_str.ptr, name_str.len);
88-
8987
// Name must start with ascii alphabetical and contain only ascii alphanumeric, underscore, and
9088
// whitespace
9189
if (!std::isalpha(name.front())) {
@@ -105,20 +103,20 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
105103
}
106104

107105
JS::RootedObject config_store(cx, JS_NewObjectForConstructor(cx, &class_, args));
108-
fastly_dictionary_handle_t dict_handle = INVALID_HANDLE;
109-
fastly_error_t err;
110-
if (!fastly_dictionary_open(&name_str, &dict_handle, &err)) {
111-
if (err == FASTLY_ERROR_BAD_HANDLE) {
106+
auto open_res = Dict::open(name);
107+
if (auto *err = open_res.to_err()) {
108+
if (*err == FASTLY_ERROR_BAD_HANDLE) {
112109
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_DOES_NOT_EXIST,
113110
name.data());
114111
return false;
115112
} else {
116-
HANDLE_ERROR(cx, err);
113+
HANDLE_ERROR(cx, *err);
117114
return false;
118115
}
119116
}
120117

121-
JS::SetReservedSlot(config_store, ConfigStore::Slots::Handle, JS::Int32Value(dict_handle));
118+
JS::SetReservedSlot(config_store, ConfigStore::Slots::Handle,
119+
JS::Int32Value(open_res.unwrap().handle));
122120
if (!config_store)
123121
return false;
124122
args.rval().setObject(*config_store);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define JS_COMPUTE_RUNTIME_CONFIG_STORE_H
33

44
#include "builtin.h"
5+
#include "host_interface/host_api.h"
56
#include "js-compute-builtins.h"
67

78
namespace builtins {
@@ -20,7 +21,7 @@ class ConfigStore : public BuiltinImpl<ConfigStore> {
2021

2122
static bool get(JSContext *cx, unsigned argc, JS::Value *vp);
2223

23-
static fastly_dictionary_handle_t config_store_handle(JSObject *obj);
24+
static Dict config_store_handle(JSObject *obj);
2425
static bool constructor(JSContext *cx, unsigned argc, JS::Value *vp);
2526

2627
static bool init_class(JSContext *cx, JS::HandleObject global);

0 commit comments

Comments
 (0)