Skip to content

Commit 911cace

Browse files
committed
Update Rust bindings integration
This is a large commit with a grab-bag of changes needed (or nice to have) to get this PR to work on top of #255. The bindings are still pretty rough, but the result passes the test suite, so at least nothing existing is broken by it. I'd like to get this landed soon-ish and iterate on things from here. Signed-off-by: Till Schneidereit <till@tillschneidereit.net>
1 parent 267ce42 commit 911cace

File tree

27 files changed

+20206
-12900
lines changed

27 files changed

+20206
-12900
lines changed

builtins/web/fetch/fetch-api.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ using blob::Blob;
1818
using fetch::Headers;
1919
using host_api::HostString;
2020

21-
static api::Engine *ENGINE;
22-
2321
enum class FetchScheme {
2422
About,
2523
Blob,

builtins/web/fetch/request-response.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ bool RequestOrResponse::append_body(JSContext *cx, JS::HandleObject self, JS::Ha
537537
api::TaskCompletionCallback callback, HandleObject callback_receiver) {
538538
MOZ_ASSERT(!body_used(source));
539539
MOZ_ASSERT(self != source);
540-
auto engine = api::Engine::from_context(cx);
540+
auto& engine = api::Engine::from_context(cx);
541541
host_api::HttpIncomingBody *source_body = incoming_body_handle(source);
542542
host_api::HttpOutgoingBody *dest_body = outgoing_body_handle(self);
543543
auto res = dest_body->append(&engine, source_body, callback, callback_receiver);
@@ -1160,7 +1160,7 @@ bool reader_for_outgoing_body_then_handler(JSContext *cx, JS::HandleObject body_
11601160
bytes = host_api::HostBytes(unique_ptr<uint8_t[]>(ptr), length);
11611161
}
11621162

1163-
auto res = body->write_all(ENGINE, std::move(bytes),
1163+
auto res = body->write_all(&api::Engine::from_context(cx), std::move(bytes),
11641164
write_all_finish_callback, then_handler);
11651165
if (auto *err = res.to_err()) {
11661166
HANDLE_ERROR(cx, *err);
@@ -1198,7 +1198,7 @@ bool RequestOrResponse::maybe_stream_body(JSContext *cx, JS::HandleObject body_o
11981198
if (is_incoming(body_owner)) {
11991199
auto *source_body = incoming_body_handle(body_owner);
12001200
auto *dest_body = destination->body().unwrap();
1201-
auto engine = api::Engine::from_context(cx);
1201+
auto& engine = api::Engine::from_context(cx);
12021202
auto res = dest_body->append(&engine, source_body, finish_outgoing_body_streaming, nullptr);
12031203
if (auto *err = res.to_err()) {
12041204
HANDLE_ERROR(cx, *err);

builtins/web/streams/buf-reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class StreamTask final : public api::AsyncTask {
2020

2121
public:
2222
explicit StreamTask(const HandleObject reader) : reader_(reader) {
23-
handle_ = IMMEDIATE_TASK_HANDLE;
23+
handle_ = api::IMMEDIATE_TASK_HANDLE;
2424
}
2525

2626
[[nodiscard]] bool run(api::Engine *engine) override {
@@ -78,7 +78,7 @@ class StreamTask final : public api::AsyncTask {
7878
}
7979

8080
[[nodiscard]] bool cancel(api::Engine *engine) override {
81-
handle_ = INVALID_POLLABLE_HANDLE;
81+
handle_ = api::INVALID_POLLABLE_HANDLE;
8282
return true;
8383
}
8484

cmake/build-crates.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_dependencies("cargo-prebuild_rust_staticlib" cargo-build_generate_bindings)
4747
add_library(rust-glue STATIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/jsapi-rs/cpp/jsglue.cpp)
4848
target_include_directories(rust-glue PRIVATE ${SM_INCLUDE_DIR})
4949
add_dependencies(rust_staticlib rust-glue)
50+
target_link_libraries(rust-glue PRIVATE spidermonkey)
5051

5152
# Add a Rust library to the staticlib bundle.
5253
function(add_rust_lib name path)
@@ -61,7 +62,7 @@ endfunction()
6162
add_library(rust-hooks-wrappers STATIC "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-hooks/src/wrappers.cpp")
6263
target_link_libraries(rust-hooks-wrappers PRIVATE spidermonkey)
6364
add_library(rust-crates STATIC ${CMAKE_CURRENT_BINARY_DIR}/null.cpp)
64-
target_link_libraries(rust-crates PRIVATE rust_staticlib rust-hooks-wrappers)
65+
target_link_libraries(rust-crates PRIVATE rust_staticlib rust-glue rust-hooks-wrappers)
6566

6667
# Add crates as needed here:
6768
add_rust_lib(rust-url "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url")

host-apis/wasi-0.2.0/handles.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class IncomingBodyHandle final : public WASIHandle<host_api::HttpIncomingBody> {
217217

218218
public:
219219
explicit IncomingBodyHandle(HandleOps<host_api::HttpIncomingBody>::owned handle)
220-
: WASIHandle(handle), pollable_handle_(INVALID_POLLABLE_HANDLE) {
220+
: WASIHandle(handle), pollable_handle_(api::INVALID_POLLABLE_HANDLE) {
221221
HandleOps<InputStream>::owned stream{};
222222
if (!wasi_http_types_method_incoming_body_stream(borrow(), &stream)) {
223223
MOZ_ASSERT_UNREACHABLE("Getting a body's stream should never fail");
@@ -238,7 +238,7 @@ class OutgoingBodyHandle final : public WASIHandle<host_api::HttpOutgoingBody> {
238238

239239
public:
240240
explicit OutgoingBodyHandle(HandleOps<host_api::HttpOutgoingBody>::owned handle)
241-
: WASIHandle(handle), pollable_handle_(INVALID_POLLABLE_HANDLE) {
241+
: WASIHandle(handle), pollable_handle_(api::INVALID_POLLABLE_HANDLE) {
242242
HandleOps<OutputStream>::owned stream{};
243243
if (!wasi_http_types_method_outgoing_body_write(borrow(), &stream)) {
244244
MOZ_ASSERT_UNREACHABLE("Getting a body's stream should never fail");

host-apis/wasi-0.2.3/sockets.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TCPSocketHandle final : public WASIHandle<host_api::TCPSocket> {
1717

1818
public:
1919
explicit TCPSocketHandle(HandleOps<host_api::TCPSocket>::owned handle)
20-
: WASIHandle(handle), pollable_handle_(INVALID_POLLABLE_HANDLE) {
20+
: WASIHandle(handle), pollable_handle_(api::INVALID_POLLABLE_HANDLE) {
2121
network_ = wasi_sockets_instance_network_instance_network();
2222
}
2323

@@ -29,7 +29,7 @@ class TCPSocketHandle final : public WASIHandle<host_api::TCPSocket> {
2929
return wasi_sockets_tcp_borrow_network_t(network_.__handle);
3030
}
3131
PollableHandle pollable_handle() {
32-
if (pollable_handle_ == INVALID_POLLABLE_HANDLE) {
32+
if (pollable_handle_ == api::INVALID_POLLABLE_HANDLE) {
3333
pollable_handle_ = wasi_sockets_tcp_method_tcp_socket_subscribe(borrow()).__handle;
3434
}
3535
return pollable_handle_;
@@ -95,7 +95,7 @@ void TCPSocket::close() {
9595
WASI_SOCKETS_TCP_SHUTDOWN_TYPE_BOTH, &err);
9696
wasi_io_streams_output_stream_drop_own(state->output_);
9797
wasi_io_streams_input_stream_drop_own(state->input_);
98-
if (state->pollable_handle_ != INVALID_POLLABLE_HANDLE) {
98+
if (state->pollable_handle_ != api::INVALID_POLLABLE_HANDLE) {
9999
wasi_io_poll_pollable_drop_own(own_pollable_t{state->pollable_handle_});
100100
}
101101
wasi_sockets_tcp_tcp_socket_drop_own(state->take());

include/config-parser.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ConfigParser {
1313

1414
public:
1515
ConfigParser() : config_(std::make_unique<api::EngineConfig>()) {
16-
config_->content_script_path = mozilla::Some(DEFAULT_SCRIPT_PATH);
16+
config_->content_script_path = DEFAULT_SCRIPT_PATH;
1717
}
1818

1919
/**
@@ -73,13 +73,13 @@ class ConfigParser {
7373
for (size_t i = 1; i < args.size(); i++) {
7474
if (args[i] == "-e" || args[i] == "--eval") {
7575
if (i + 1 < args.size()) {
76-
config_->content_script = mozilla::Some(args[i + 1]);
77-
config_->content_script_path.reset();
76+
config_->content_script = args[i + 1];
77+
config_->content_script_path = nullptr;
7878
i++;
7979
}
8080
} else if (args[i] == "-i" || args[i] == "--initializer-script-path") {
8181
if (i + 1 < args.size()) {
82-
config_->initializer_script_path = mozilla::Some(args[i + 1]);
82+
config_->initializer_script_path = args[i + 1];
8383
i++;
8484
}
8585
} else if (args[i] == "-v" || args[i] == "--verbose") {
@@ -88,13 +88,13 @@ class ConfigParser {
8888
config_->debugging = true;
8989
} else if (args[i] == "--strip-path-prefix") {
9090
if (i + 1 < args.size()) {
91-
config_->path_prefix = mozilla::Some(args[i + 1]);
91+
config_->path_prefix = args[i + 1];
9292
i++;
9393
}
9494
} else if (args[i] == "--legacy-script") {
9595
config_->module_mode = false;
9696
if (i + 1 < args.size()) {
97-
config_->content_script_path = mozilla::Some(args[i + 1]);
97+
config_->content_script_path = args[i + 1];
9898
i++;
9999
}
100100
} else if (args[i] == "--wpt-mode") {
@@ -103,7 +103,7 @@ class ConfigParser {
103103
std::cerr << "Unknown option: " << args[i] << std::endl;
104104
exit(1);
105105
} else {
106-
config_->content_script_path = mozilla::Some(args[i]);
106+
config_->content_script_path = args[i];
107107
}
108108
}
109109

include/extension-api.h

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ constexpr PollableHandle IMMEDIATE_TASK_HANDLE = -2;
3535
class AsyncTask;
3636

3737
struct EngineConfig {
38-
mozilla::Maybe<std::string> content_script_path = mozilla::Nothing();
39-
mozilla::Maybe<std::string> content_script = mozilla::Nothing();
40-
mozilla::Maybe<std::string> path_prefix = mozilla::Nothing();
38+
std::string content_script_path;
39+
std::string content_script;
40+
std::string path_prefix;
4141
bool module_mode = true;
4242

4343
/**
@@ -47,7 +47,7 @@ struct EngineConfig {
4747
* available to content. It can be used to set up the environment for the content
4848
* script, e.g. by registering builtin modules or adding global properties.
4949
*/
50-
mozilla::Maybe<std::string> initializer_script_path = mozilla::Nothing();
50+
std::string initializer_script_path;
5151

5252
/**
5353
* Whether to evaluate the top-level script in pre-initialization mode or not.
@@ -95,6 +95,10 @@ class Engine {
9595
// TODO: change to static Engine& from_context(JSContext *cx);
9696
static Engine *get(JSContext *cx);
9797

98+
static Engine& from_context(JSContext *cx) {
99+
return *get(cx);
100+
}
101+
98102
/**
99103
* Returns the `JSContext` associated with the `Engine` instance.
100104
*
@@ -237,4 +241,36 @@ class AsyncTask {
237241

238242
} // namespace api
239243

244+
namespace rust_bindgen_details {
245+
246+
/**
247+
* <div rustbindgen="true" replaces="std::optional">
248+
*/
249+
template<typename T> class simple_optional {
250+
T* ptr;
251+
};
252+
253+
/**
254+
* <div rustbindgen="true" replaces="std::unique_ptr">
255+
*/
256+
template<typename T> class simple_unique_ptr {
257+
T* ptr;
258+
};
259+
260+
/**
261+
* <div rustbindgen="true" replaces="std::vector">
262+
*/
263+
template<typename T> class simple_vector {
264+
T* ptr;
265+
};
266+
267+
/**
268+
* <div rustbindgen="true" replaces="mozilla::Maybe">
269+
*/
270+
template<typename T> class simple_maybe {
271+
T* ptr;
272+
};
273+
274+
}
275+
240276
#endif // EXTENSION_API_H

include/host_api.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,6 @@
2020
#include "jsapi.h"
2121
#pragma clang diagnostic pop
2222

23-
/**
24-
* <div rustbindgen="true" replaces="std::optional">
25-
*/
26-
template<typename T> class simple_optional {
27-
T* ptr;
28-
};
29-
30-
/**
31-
* <div rustbindgen="true" replaces="std::unique_ptr">
32-
*/
33-
template<typename T> class simple_unique_ptr {
34-
T* ptr;
35-
};
36-
37-
/**
38-
* <div rustbindgen="true" replaces="std::vector">
39-
*/
40-
template<typename T> class simple_vector {
41-
T* ptr;
42-
};
43-
4423
using api::PollableHandle;
4524
using std::optional;
4625
using std::string_view;

0 commit comments

Comments
 (0)