Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code structure #194

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ if (DEFINED ENV{HOST_API})
if (EXISTS ${HOST_API})
cmake_path(ABSOLUTE_PATH HOST_API)
else ()
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/host-apis/$ENV{HOST_API})
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/src/host-apis/$ENV{HOST_API})
endif()
if (NOT EXISTS ${HOST_API})
message(FATAL_ERROR "Host API `$ENV{HOST_API}` not found. The HOST_API environment \
variable must be the name of a host API implementation provided in the `host-apis` \
variable must be the name of a host API implementation provided in the `src/host-apis` \
folder of StarlingMonkey, or be an absolute path pointing to custom host API implementation.")
endif()
else()
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/host-apis/wasi-0.2.0)
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/src/host-apis/wasi-0.2.0)
endif()
message(STATUS "Using host API: ${HOST_API}")

Expand All @@ -37,9 +37,9 @@ include("openssl")
include("${HOST_API}/host_api.cmake")
include("build-crates")

add_library(extension_api INTERFACE include/extension-api.h runtime/encode.h runtime/decode.h)
add_library(extension_api INTERFACE src/include/extension-api.h src/runtime/encode.h src/runtime/decode.h)
target_link_libraries(extension_api INTERFACE rust-url spidermonkey)
target_include_directories(extension_api INTERFACE include deps/include runtime)
target_include_directories(extension_api INTERFACE src/include deps/include src/runtime)

include("builtins")

Expand All @@ -49,14 +49,14 @@ if (ENABLE_WPT)
endif()

add_executable(starling-raw.wasm
runtime/js.cpp
runtime/allocator.cpp
runtime/encode.cpp
runtime/decode.cpp
runtime/engine.cpp
runtime/event_loop.cpp
runtime/builtin.cpp
runtime/script_loader.cpp
src/runtime/js.cpp
src/runtime/allocator.cpp
src/runtime/encode.cpp
src/runtime/decode.cpp
src/runtime/engine.cpp
src/runtime/event_loop.cpp
src/runtime/builtin.cpp
src/runtime/script_loader.cpp
)

option(USE_WASM_OPT "use wasm-opt to optimize the StarlingMonkey binary" ON)
Expand Down Expand Up @@ -112,10 +112,13 @@ endif()

set(RUNTIME_FILE "starling-raw.wasm")
set(ADAPTER_FILE "preview1-adapter.wasm")
configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)

configure_file("scripts/componentize.sh.in" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)

if(EXISTS ${ADAPTER})
configure_file(${ADAPTER} "${CMAKE_CURRENT_BINARY_DIR}/${ADAPTER_FILE}" COPYONLY)
endif()

configure_file(spin.toml spin.toml COPYONLY)

function(componentize OUTPUT)
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug

3. Build the runtime

Building the runtime is done in two phases: first, cmake is used to build a raw version as a WebAssembly core module. Then, that module is turned into a [WebAssembly Component](https://component-model.bytecodealliance.org/) using the `componentize.sh` script.
Building the runtime is done in two phases:

1. `cmake` is used to build a raw version as a WebAssembly core module (`starling-raw.wasm`).
2. `starling-raw.wasm` is turned into a [WebAssembly Component](https://component-model.bytecodealliance.org/) using [`scripts/componentize.sh.in`](./scripts/componentize.sh.in).

> [!NOTE]
> `scripts/componentize.sh.in` is used as a template and written to the build directory by `cmake` during the build

The following command will build the `starling-raw.wasm` runtime module in the `cmake-build-release` directory:

```bash
# Use cmake-build-debug for the debug build
# Change the value for `--parallel` to match the number of CPU cores in your system
Expand Down Expand Up @@ -91,7 +98,7 @@ cd cmake-build-release

This way, the JS file will be loaded during componentization, and the top-level code will be executed, and can e.g. register a handler for the `fetch` event to serve HTTP requests.

4. Testing the build
4. Testing the build

After completing the build (a debug build in this case), the integration test runner can be built:

Expand Down Expand Up @@ -209,7 +216,7 @@ just mode=release build

This command will set the build mode to release, and the build directory will automatically change to `cmake-build-release`.

If you want to override the default build directory, you can use the `builddir` parameter.
If you want to override the default build directory, you can use the `builddir` parameter.

``` shell
just builddir=mybuilddir mode=release build
Expand Down Expand Up @@ -276,9 +283,9 @@ If your builtin requires multiple `.cpp` files, you can pass all of them to `add

### Providing a custom host API implementation

The [host-apis](host-apis) directory can contain implementations of the host API for different
versions of WASI—or in theory any other host interface. Those can be selected by setting the
`HOST_API` environment variable to the
The [host-apis](host-apis) directory can contain implementations of the host API for different
versions of WASI—or in theory any other host interface. Those can be selected by setting the
`HOST_API` environment variable to the
name of one of the directories. Currently, only an implementation in terms of [wasi-0.2.0]
(host-apis/wasi-0.2.0) is provided, and used by default.

Expand Down
3 changes: 2 additions & 1 deletion cmake/add_builtin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function(add_builtin)
list(GET ARGN 0 SRC)
cmake_path(GET SRC STEM NAME)
cmake_path(GET SRC PARENT_PATH DIR)
string(REPLACE "/" "::" NS ${DIR})
string(REPLACE "src/" "" NS ${DIR})
string(REPLACE "/" "::" NS ${NS})
set(NS ${NS}::${NAME})
set(DEFAULT_ENABLE ON)
else()
Expand Down
81 changes: 41 additions & 40 deletions cmake/builtins.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(CMAKE_SCRIPT_MODE_FILE)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
else()
add_library(builtins STATIC builtins/install_builtins.cpp)
add_library(builtins STATIC src/builtins/install_builtins.cpp)
target_include_directories(builtins PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(builtins PRIVATE extension_api)
endif()
Expand All @@ -13,86 +13,87 @@ file(WRITE ${INSTALL_BUILTINS} "// This file is generated by CMake\n")


# These builtins are always enabled.
add_builtin(builtins/web/global_self.cpp)
add_builtin(builtins/web/queue-microtask.cpp)
add_builtin(builtins/web/structured-clone.cpp)
add_builtin(builtins/web/base64.cpp)
add_builtin(builtins/web/blob.cpp)
add_builtin(src/builtins/web/global_self.cpp)
add_builtin(src/builtins/web/queue-microtask.cpp)
add_builtin(src/builtins/web/structured-clone.cpp)
add_builtin(src/builtins/web/base64.cpp)
add_builtin(src/builtins/web/blob.cpp)

add_builtin(
builtins::web::dom_exception
SRC
builtins/web/dom-exception.cpp
src/builtins/web/dom-exception.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(
builtins::web::url
SRC
builtins/web/url.cpp
src/builtins/web/url.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(builtins/web/console.cpp)
add_builtin(src/builtins/web/console.cpp)

add_builtin(builtins/web/performance.cpp)
add_builtin(src/builtins/web/performance.cpp)

add_builtin(
builtins::web::timers
SRC
builtins/web/timers.cpp
src/builtins/web/timers.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(builtins/web/worker-location.cpp)
add_builtin(src/builtins/web/worker-location.cpp)

add_builtin(
builtins::web::text-codec
SRC
builtins/web/text-codec/text-codec.cpp
builtins/web/text-codec/text-decoder.cpp
builtins/web/text-codec/text-encoder.cpp
src/builtins/web/text-codec/text-codec.cpp
src/builtins/web/text-codec/text-decoder.cpp
src/builtins/web/text-codec/text-encoder.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(
builtins::web::streams
SRC
builtins/web/streams/compression-stream.cpp
builtins/web/streams/decompression-stream.cpp
builtins/web/streams/native-stream-sink.cpp
builtins/web/streams/native-stream-source.cpp
builtins/web/streams/streams.cpp
builtins/web/streams/transform-stream.cpp
builtins/web/streams/transform-stream-default-controller.cpp
src/builtins/web/streams/compression-stream.cpp
src/builtins/web/streams/decompression-stream.cpp
src/builtins/web/streams/native-stream-sink.cpp
src/builtins/web/streams/native-stream-source.cpp
src/builtins/web/streams/streams.cpp
src/builtins/web/streams/transform-stream.cpp
src/builtins/web/streams/transform-stream-default-controller.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(
builtins::web::fetch
SRC
builtins/web/fetch/fetch-api.cpp
builtins/web/fetch/headers.cpp
builtins/web/fetch/request-response.cpp)
src/builtins/web/fetch/fetch-api.cpp
src/builtins/web/fetch/headers.cpp
src/builtins/web/fetch/request-response.cpp)

add_builtin(
builtins::web::fetch::fetch_event
SRC
builtins/web/fetch/fetch_event.cpp
src/builtins/web/fetch/fetch_event.cpp
DEPENDENCIES
host_api)
host_api)

add_builtin(
builtins::web::crypto
SRC
builtins/web/crypto/crypto.cpp
builtins/web/crypto/crypto-algorithm.cpp
builtins/web/crypto/crypto-key.cpp
builtins/web/crypto/crypto-key-ec-components.cpp
builtins/web/crypto/crypto-key-rsa-components.cpp
builtins/web/crypto/json-web-key.cpp
builtins/web/crypto/subtle-crypto.cpp
src/builtins/web/crypto/crypto.cpp
src/builtins/web/crypto/crypto-algorithm.cpp
src/builtins/web/crypto/crypto-key.cpp
src/builtins/web/crypto/crypto-key-ec-components.cpp
src/builtins/web/crypto/crypto-key-rsa-components.cpp
src/builtins/web/crypto/json-web-key.cpp
src/builtins/web/crypto/subtle-crypto.cpp
DEPENDENCIES
OpenSSL::Crypto
fmt
INCLUDE_DIRS
runtime)
src/runtime)
5 changes: 4 additions & 1 deletion componentize.sh → scripts/componentize.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash

#set -euo pipefail
#
# NOTE: this file is used as a template by cmake, and should not be invoked manually.
# See README.md for more details
#

wizer="${WIZER:-@WIZER_DIR@/wizer}"
wasm_tools="${WASM_TOOLS:-@WASM_TOOLS_BIN@}"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ add_library(host_api STATIC
${HOST_API}/host_call.cpp
${HOST_API}/bindings/bindings.c
${HOST_API}/bindings/bindings_component_type.o
${CMAKE_CURRENT_SOURCE_DIR}/include/host_api.h
${CMAKE_CURRENT_SOURCE_DIR}/src/include/host_api.h
)

target_link_libraries(host_api PRIVATE spidermonkey)
target_include_directories(host_api PRIVATE include)
target_include_directories(host_api PRIVATE src/include)
target_include_directories(host_api PRIVATE ${HOST_API})
target_include_directories(host_api PUBLIC ${HOST_API}/include)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/host_api.h → src/include/host_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <variant>
#include <vector>

#include "../crates/rust-url/rust-url.h"
#include "../../crates/rust-url/rust-url.h"
#include "extension-api.h"
#include "js/TypeDecls.h"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/wpt-harness/wpt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ endif()

add_builtin(wpt_support
SRC "${CMAKE_CURRENT_LIST_DIR}/wpt_builtins.cpp"
INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/builtins/web/")
INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src/builtins/web/")

add_custom_command(
OUTPUT wpt-runtime.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env PATH=${WASM_TOOLS_DIR}:${WIZER_DIR}:$ENV{PATH} env "COMPONENTIZE_FLAGS=${COMPONENTIZE_FLAGS}" WPT_ROOT=${WPT_ROOT} ${CMAKE_CURRENT_SOURCE_DIR}/tests/wpt-harness/build-wpt-runtime.sh
DEPENDS starling-raw.wasm componentize.sh tests/wpt-harness/build-wpt-runtime.sh tests/wpt-harness/pre-harness.js tests/wpt-harness/post-harness.js
DEPENDS starling-raw.wasm scripts/componentize.sh.in tests/wpt-harness/build-wpt-runtime.sh tests/wpt-harness/pre-harness.js tests/wpt-harness/post-harness.js
VERBATIM
)

Expand Down
Loading