Skip to content

Commit 3ffb9be

Browse files
author
Guy Bedford
authored
component: reactor-based initialization (#407)
1 parent 6bad106 commit 3ffb9be

File tree

14 files changed

+102
-81
lines changed

14 files changed

+102
-81
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ jobs:
295295
- name: Locate component resources
296296
run: |
297297
cp c-dependencies/js-compute-runtime/c-at-e.wit .
298-
cp c-dependencies/js-compute-runtime/wasi_snapshot_preview1.wasm .
298+
cp c-dependencies/js-compute-runtime/wasi_snapshot_preview1.reactor.wasm .
299299
- run: yarn
300300
shell: bash
301301
- run: npm test

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ node_modules/
1616
/c-dependencies/js-compute-runtime/obj
1717
tests/wpt-harness/wpt-test-runner.js
1818
/c-at-e.wit
19-
/wasi_snapshot_preview1.wasm
19+
/wasi_snapshot_preview1.reactor.wasm
2020
wpt-runtime.wasm
2121
docs-app/bin/main.wasm
2222
docs-app/pkg/*.tar.gz

c-dependencies/js-compute-runtime/Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ LD_FLAGS += -L$(BUILD)/openssl/libx32 -lcrypto
109109
.PHONY: all
110110
all: $(FSM_SRC)/js-compute-runtime.wasm
111111
all: $(FSM_SRC)/js-compute-runtime-component.wasm
112-
all: wasi_snapshot_preview1.wasm
112+
all: wasi_snapshot_preview1.reactor.wasm
113113

114114
# Remove just the build artifacts for the current runtime build.
115115
.PHONY: clean
@@ -141,6 +141,9 @@ $(OBJ_DIR):
141141
$(OBJ_DIR)/c-at-e-world:
142142
$(call cmd,mkdir,$@)
143143

144+
$(OBJ_DIR)/impl:
145+
$(call cmd,mkdir,$@)
146+
144147
$(OBJ_DIR)/builtins:
145148
$(call cmd,mkdir,$@)
146149

@@ -162,8 +165,8 @@ $(BUILD)/openssl-$(OPENSSL_VERSION).tar.gz: URL=https://www.openssl.org/source/o
162165
$(BUILD)/openssl-$(OPENSSL_VERSION).tar.gz: | $(BUILD)
163166
$(call cmd,wget,$@)
164167

165-
wasi_snapshot_preview1.wasm: URL=https://github.com/bytecodealliance/preview2-prototyping/releases/download/latest/wasi_snapshot_preview1.wasm
166-
wasi_snapshot_preview1.wasm:
168+
wasi_snapshot_preview1.reactor.wasm: URL=https://github.com/bytecodealliance/preview2-prototyping/releases/download/latest/wasi_snapshot_preview1.reactor.wasm
169+
wasi_snapshot_preview1.reactor.wasm:
167170
$(call cmd,wget,$@)
168171

169172
# OpenSSL build ################################################################
@@ -238,7 +241,7 @@ regenerate-world:
238241
@echo ""
239242
@echo "No wit-bindgen found in PATH, consider running"
240243
@echo ""
241-
@echo " cargo install --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen-cli"
244+
@echo " cargo install --git https://github.com/bytecodealliance/wit-bindgen wit-bindgen-cli --no-default-features --features c"
242245
@echo ""
243246
@exit 1
244247
else
@@ -271,11 +274,13 @@ $(eval $(call compile_cxx,$(FSM_SRC)/c-at-e-world/c_at_e_world_adapter.cpp))
271274
# includes a script called wasm-opt that immediately exits successfully. See
272275
# that script for more information about why we do this.
273276
$(OBJ_DIR)/js-compute-runtime.wasm: $(FSM_OBJ) $(SM_OBJ) $(RUST_URL_LIB)
277+
$(OBJ_DIR)/js-compute-runtime.wasm: $(OBJ_DIR)/impl/main.o
274278
$(OBJ_DIR)/js-compute-runtime.wasm: $(OBJ_DIR)/c-at-e-world/c_at_e_world_adapter.o
275279
$(call cmd_format,WASI_LD,$@) PATH="$(FSM_SRC)/scripts:$$PATH" \
276280
$(WASI_CXX) $(LD_FLAGS) $(OPENSSL_LIBS) -o $@ $^
277281
$(call cmd_format,WASM_STRIP,$@) $(call WASM_STRIP,$@)
278282

283+
$(eval $(call compile_cxx,$(FSM_SRC)/impl/main.cpp))
279284

280285
# Compute runtime component build ##############################################
281286

@@ -288,11 +293,13 @@ $(eval $(call compile_c,$(FSM_SRC)/c-at-e-world/c_at_e_world.c))
288293
# includes a script called wasm-opt that immediately exits successfully. See
289294
# that script for more information about why we do this.
290295
$(OBJ_DIR)/js-compute-runtime-component.wasm: $(FSM_OBJ) $(SM_OBJ) $(RUST_URL_LIB)
296+
$(OBJ_DIR)/js-compute-runtime-component.wasm: $(OBJ_DIR)/impl/main_component.o
291297
$(OBJ_DIR)/js-compute-runtime-component.wasm: $(OBJ_DIR)/c-at-e-world/c_at_e_world.o
292298
$(call cmd_format,WASI_LD,$@) PATH="$(FSM_SRC)/scripts:$$PATH" \
293299
$(WASI_CXX) $(LD_FLAGS) $(OPENSSL_LIBS) -o $@ $^
294300
$(call cmd_format,WASM_STRIP,$@) $(call WASM_STRIP,$@)
295301

302+
$(eval $(call compile_cxx,$(FSM_SRC)/impl/main_component.cpp))
296303

297304
# Shared builtins build ########################################################
298305

c-dependencies/js-compute-runtime/builtins/fetch-event.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,14 @@ JSObject *FetchEvent::prepare_downstream_request(JSContext *cx) {
7777
return Request::create(cx, requestInstance, INVALID_HANDLE, INVALID_HANDLE, true);
7878
}
7979

80-
bool FetchEvent::init_downstream_request(JSContext *cx, JS::HandleObject request) {
80+
bool FetchEvent::init_downstream_request(JSContext *cx, JS::HandleObject request,
81+
fastly_request_t *req) {
8182
MOZ_ASSERT(Request::request_handle(request) == INVALID_HANDLE);
8283

83-
fastly_request_t req;
8484
fastly_error_t err;
85-
if (!fastly_http_req_body_downstream_get(&req, &err)) {
86-
HANDLE_ERROR(cx, err);
87-
return false;
88-
}
8985

90-
fastly_request_handle_t request_handle = req.f0;
91-
fastly_body_handle_t body_handle = req.f1;
86+
fastly_request_handle_t request_handle = req->f0;
87+
fastly_body_handle_t body_handle = req->f1;
9288

9389
JS::SetReservedSlot(request, static_cast<uint32_t>(Request::Slots::Request),
9490
JS::Int32Value(request_handle));
@@ -405,10 +401,10 @@ JSObject *FetchEvent::create(JSContext *cx) {
405401

406402
JS::HandleObject FetchEvent::instance() { return INSTANCE; }
407403

408-
bool FetchEvent::init_request(JSContext *cx, JS::HandleObject self) {
404+
bool FetchEvent::init_request(JSContext *cx, JS::HandleObject self, fastly_request_t *req) {
409405
JS::RootedObject request(
410406
cx, &JS::GetReservedSlot(self, static_cast<uint32_t>(Slots::Request)).toObject());
411-
return init_downstream_request(cx, request);
407+
return init_downstream_request(cx, request, req);
412408
}
413409

414410
bool FetchEvent::is_active(JSObject *self) {

c-dependencies/js-compute-runtime/builtins/fetch-event.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BUILTINS_FETCH_EVENT_H
33

44
#include "builtin.h"
5+
#include "host_interface/c-at-e.h"
56

67
namespace builtins {
78

@@ -48,10 +49,11 @@ class FetchEvent final : public BuiltinNoConstructor<FetchEvent> {
4849
/**
4950
* Fully initialize the Request object based on the incoming request.
5051
*/
51-
static bool init_downstream_request(JSContext *cx, JS::HandleObject request);
52+
static bool init_downstream_request(JSContext *cx, JS::HandleObject request,
53+
fastly_request_t *req);
5254

5355
static bool respondWithError(JSContext *cx, JS::HandleObject self);
54-
static bool init_request(JSContext *cx, JS::HandleObject self);
56+
static bool init_request(JSContext *cx, JS::HandleObject self, fastly_request_t *req);
5557
static bool is_active(JSObject *self);
5658
static bool is_dispatching(JSObject *self);
5759
static void start_dispatching(JSObject *self);

c-dependencies/js-compute-runtime/c-at-e-world/c_at_e_world.c

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen` 0.3.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.4.0. DO NOT EDIT!
22
#include "c_at_e_world.h"
33

44

@@ -49,14 +49,6 @@ typedef struct {
4949
} val;
5050
} fastly_result_log_endpoint_handle_error_t;
5151

52-
typedef struct {
53-
bool is_err;
54-
union {
55-
fastly_request_t ok;
56-
fastly_error_t err;
57-
} val;
58-
} fastly_result_request_error_t;
59-
6052
typedef struct {
6153
bool is_err;
6254
union {
@@ -225,6 +217,12 @@ typedef struct {
225217
} val;
226218
} fastly_result_purge_result_error_t;
227219

220+
typedef struct {
221+
bool is_err;
222+
union {
223+
} val;
224+
} js_compute_runtime_result_void_void_t;
225+
228226
__attribute__((import_module("fastly"), import_name("abi-init")))
229227
void __wasm_import_fastly_abi_init(int64_t, int32_t);
230228

@@ -252,9 +250,6 @@ void __wasm_import_fastly_log_endpoint_get(int32_t, int32_t, int32_t);
252250
__attribute__((import_module("fastly"), import_name("log-write")))
253251
void __wasm_import_fastly_log_write(int32_t, int32_t, int32_t, int32_t);
254252

255-
__attribute__((import_module("fastly"), import_name("http-req-body-downstream-get")))
256-
void __wasm_import_fastly_http_req_body_downstream_get(int32_t);
257-
258253
__attribute__((import_module("fastly"), import_name("http-req-cache-override-set")))
259254
void __wasm_import_fastly_http_req_cache_override_set(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t);
260255

@@ -700,36 +695,6 @@ bool fastly_log_write(fastly_log_endpoint_handle_t h, c_at_e_world_string_t *msg
700695
}
701696
}
702697

703-
bool fastly_http_req_body_downstream_get(fastly_request_t *ret, fastly_error_t *err) {
704-
__attribute__((aligned(4)))
705-
uint8_t ret_area[12];
706-
int32_t ptr = (int32_t) &ret_area;
707-
__wasm_import_fastly_http_req_body_downstream_get(ptr);
708-
fastly_result_request_error_t result;
709-
switch ((int32_t) (*((uint8_t*) (ptr + 0)))) {
710-
case 0: {
711-
result.is_err = false;
712-
result.val.ok = (fastly_request_t) {
713-
(uint32_t) (*((int32_t*) (ptr + 4))),
714-
(uint32_t) (*((int32_t*) (ptr + 8))),
715-
};
716-
break;
717-
}
718-
case 1: {
719-
result.is_err = true;
720-
result.val.err = (int32_t) (*((uint8_t*) (ptr + 4)));
721-
break;
722-
}
723-
}
724-
if (!result.is_err) {
725-
*ret = result.val.ok;
726-
return 1;
727-
} else {
728-
*err = result.val.err;
729-
return 0;
730-
}
731-
}
732-
733698
bool fastly_http_req_cache_override_set(fastly_request_handle_t h, fastly_http_cache_override_tag_t tag, uint32_t *maybe_ttl, uint32_t *maybe_stale_while_revalidate, c_at_e_world_string_t *maybe_sk, fastly_error_t *err) {
734699
__attribute__((aligned(1)))
735700
uint8_t ret_area[2];
@@ -2725,6 +2690,23 @@ bool fastly_purge_surrogate_key(c_at_e_world_string_t *surrogate_key, bool soft_
27252690
}
27262691
}
27272692

2693+
__attribute__((export_name("js-compute-runtime#serve")))
2694+
int32_t __wasm_export_js_compute_runtime_serve(int32_t arg, int32_t arg0) {
2695+
js_compute_runtime_request_t arg1 = (js_compute_runtime_request_t) {
2696+
(uint32_t) (arg),
2697+
(uint32_t) (arg0),
2698+
};
2699+
js_compute_runtime_result_void_void_t ret;
2700+
ret.is_err = !js_compute_runtime_serve(&arg1);
2701+
int32_t result;
2702+
if ((ret).is_err) {
2703+
result = 1;
2704+
} else {
2705+
result = 0;
2706+
}
2707+
return result;
2708+
}
2709+
27282710
extern void __component_type_object_force_link_c_at_e_world(void);
27292711
void __component_type_object_force_link_c_at_e_world_public_use_in_this_compilation_unit(void) {
27302712
__component_type_object_force_link_c_at_e_world();

c-dependencies/js-compute-runtime/c-at-e-world/c_at_e_world.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen` 0.3.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.4.0. DO NOT EDIT!
22
#ifndef __BINDINGS_C_AT_E_WORLD_H
33
#define __BINDINGS_C_AT_E_WORLD_H
44
#ifdef __cplusplus
@@ -237,6 +237,15 @@ typedef struct {
237237
size_t len;
238238
} fastly_list_async_handle_t;
239239

240+
typedef uint32_t js_compute_runtime_request_handle_t;
241+
242+
typedef uint32_t js_compute_runtime_body_handle_t;
243+
244+
typedef struct {
245+
js_compute_runtime_request_handle_t f0;
246+
js_compute_runtime_body_handle_t f1;
247+
} js_compute_runtime_request_t;
248+
240249
// Imported Functions from `fastly`
241250
bool fastly_abi_init(uint64_t abi_version, fastly_error_t *err);
242251
bool fastly_uap_parse(c_at_e_world_string_t *user_agent, fastly_user_agent_t *ret,
@@ -253,7 +262,6 @@ bool fastly_log_endpoint_get(c_at_e_world_string_t *name, fastly_log_endpoint_ha
253262
fastly_error_t *err);
254263
bool fastly_log_write(fastly_log_endpoint_handle_t h, c_at_e_world_string_t *msg,
255264
fastly_error_t *err);
256-
bool fastly_http_req_body_downstream_get(fastly_request_t *ret, fastly_error_t *err);
257265
bool fastly_http_req_cache_override_set(fastly_request_handle_t h,
258266
fastly_http_cache_override_tag_t tag, uint32_t *maybe_ttl,
259267
uint32_t *maybe_stale_while_revalidate,
@@ -382,6 +390,9 @@ bool fastly_async_io_is_ready(fastly_async_handle_t handle, bool *ret, fastly_er
382390
bool fastly_purge_surrogate_key(c_at_e_world_string_t *surrogate_key, bool soft_purge,
383391
fastly_purge_result_t *ret, fastly_error_t *err);
384392

393+
// Exported Functions from `js-compute-runtime`
394+
bool js_compute_runtime_serve(js_compute_runtime_request_t *req);
395+
385396
#ifdef __cplusplus
386397
}
387398
#endif

c-dependencies/js-compute-runtime/c-at-e.wit

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ default world c-at-e-world {
108108
type request = tuple<request-handle, body-handle>
109109
type response = tuple<response-handle, body-handle>
110110

111-
http-req-body-downstream-get: func() -> result<request, error>
112-
113111
flags http-cache-override-tag {
114112
/// Do not cache the response to this request, regardless of the origin response's headers.
115113
pass,
@@ -486,4 +484,16 @@ default world c-at-e-world {
486484

487485
purge-surrogate-key: func(surrogate-key: string, soft-purge: bool) -> result<purge-result, error>
488486
}
487+
488+
export js-compute-runtime: interface {
489+
type request-handle = u32
490+
type body-handle = u32
491+
type request = tuple<request-handle, body-handle>
492+
493+
/// Serve the given request
494+
///
495+
/// response handle not currently returned, because in the case of a streamed response
496+
/// send downstream must be fully streamed due to the run to completion semantics.
497+
serve: func(req: request) -> result
498+
}
489499
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "host_interface/c-at-e.h"
2+
3+
int main(int argc, const char *argv[]) {
4+
fastly_request_t req;
5+
if (c_at_e_req_body_downstream_get(&req.f0, &req.f1) != 0) {
6+
abort();
7+
return 1;
8+
}
9+
10+
js_compute_runtime_serve(static_cast<js_compute_runtime_request_t *>(static_cast<void *>(&req)));
11+
12+
// Note: we deliberately skip shutdown, because it takes quite a while,
13+
// and serves no purpose for us.
14+
// TODO: investigate also skipping the destructors deliberately run in
15+
// wizer.h. GLOBAL = nullptr; CONTEXT = nullptr; JS_DestroyContext(cx);
16+
// JS_ShutDown();
17+
return 0;
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "host_interface/c-at-e.h"
2+
3+
int main() { return 0; }

0 commit comments

Comments
 (0)