Skip to content

Commit cc0b178

Browse files
committed
Add Rust bindings for the runtime and the SpiderMonkey engines
1 parent 689dc97 commit cc0b178

87 files changed

Lines changed: 34630 additions & 23 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/.spin
2323

2424
# Rust compilation output
25-
/target
25+
/runtime/crates/target/
2626

2727
/tests/e2e/*/*.wasm
2828
/tests/e2e/*/*.log

CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ include("openssl")
3737
include("host_api")
3838
include("build-crates")
3939

40-
add_library(extension_api INTERFACE include/extension-api.h runtime/encode.h runtime/decode.h)
40+
add_library(extension_api INTERFACE include/extension-api.h runtime/cpp/encode.h runtime/cpp/decode.h)
4141
target_link_libraries(extension_api INTERFACE rust-url spidermonkey)
42-
target_include_directories(extension_api INTERFACE include deps/include runtime)
42+
target_include_directories(extension_api INTERFACE include deps/include runtime/cpp)
4343

4444
include("builtins")
4545

@@ -49,14 +49,14 @@ if (ENABLE_WPT)
4949
endif()
5050

5151
add_executable(starling.wasm
52-
runtime/js.cpp
53-
runtime/allocator.cpp
54-
runtime/encode.cpp
55-
runtime/decode.cpp
56-
runtime/engine.cpp
57-
runtime/event_loop.cpp
58-
runtime/builtin.cpp
59-
runtime/script_loader.cpp
52+
runtime/cpp/js.cpp
53+
runtime/cpp/allocator.cpp
54+
runtime/cpp/encode.cpp
55+
runtime/cpp/decode.cpp
56+
runtime/cpp/engine.cpp
57+
runtime/cpp/event_loop.cpp
58+
runtime/cpp/builtin.cpp
59+
runtime/cpp/script_loader.cpp
6060
)
6161

6262
option(USE_WASM_OPT "use wasm-opt to optimize the StarlingMonkey binary" ON)

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2020 Fastly, Inc.
189+
Copyright StarlingMonkey project contributors
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

builtins/install_builtins.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
#include "extension-api.h"
22

3-
#define NS_DEF(ns) \
4-
namespace ns { \
5-
extern bool install(api::Engine *engine); \
3+
#define NS_DEF(ns) \
4+
namespace ns { \
5+
extern bool install(api::Engine *engine); \
66
}
7+
#define RS_DEF(install_fn) \
8+
extern "C" bool install_fn(api::Engine *engine);
79
#include "builtins.incl"
10+
#undef RS_DEF
811
#undef NS_DEF
912

13+
1014
bool install_builtins(api::Engine *engine) {
11-
#define NS_DEF(ns) \
12-
if (!ns::install(engine)) \
15+
#define NS_DEF(ns) \
16+
if (!ns::install(engine)) \
17+
return false;
18+
#define RS_DEF(install_fn) \
19+
if (!install_fn(engine)) \
1320
return false;
1421
#include "builtins.incl"
22+
#undef RS_DEF
1523
#undef NS_DEF
1624

1725
return true;

cmake/add_builtin.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,37 @@ function(add_builtin)
9595
file(APPEND $CACHE{INSTALL_BUILTINS} "NS_DEF(${NS})\n")
9696
return(PROPAGATE LIB_NAME)
9797
endfunction()
98+
99+
100+
function(add_rust_builtin)
101+
# TODO: restore additional config args
102+
list(GET ARGN 0 LIB_NAME)
103+
set(DEFAULT_ENABLE ON)
104+
set(LIB_TARGET_NAME ${LIB_NAME})
105+
string(REPLACE "-" "_" LIB_NAME ${LIB_NAME})
106+
string(PREPEND LIB_NAME "builtin_")
107+
string(TOUPPER ${LIB_NAME} LIB_NAME_UPPER)
108+
set(OPT_NAME ENABLE_${LIB_NAME_UPPER})
109+
set(DESCRIPTION "${LIB_TARGET_NAME} (option: ${OPT_NAME}, default: ${DEFAULT_ENABLE})")
110+
111+
# In script-mode, just show the available builtins.
112+
if(CMAKE_SCRIPT_MODE_FILE)
113+
message(STATUS " ${DESCRIPTION}")
114+
return()
115+
endif()
116+
117+
option(${OPT_NAME} "Enable ${LIB_NAME}" ${DEFAULT_ENABLE})
118+
if (${${OPT_NAME}})
119+
else()
120+
message(STATUS "Skipping builtin ${DESCRIPTION}")
121+
return()
122+
endif()
123+
124+
message(STATUS "Adding builtin ${DESCRIPTION}")
125+
126+
target_link_libraries(builtins PRIVATE ${LIB_TARGET_NAME} rust-glue)
127+
add_dependencies(${LIB_TARGET_NAME} rust-bindings)
128+
129+
file(APPEND $CACHE{INSTALL_BUILTINS} "RS_DEF(${LIB_NAME}_install)\n")
130+
return(PROPAGATE LIB_NAME)
131+
endfunction()

cmake/build-crates.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/Cargo.toml NO_LINKER_OVERRIDE)
2+
3+
corrosion_import_crate(
4+
MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/Cargo.toml
5+
NO_LINKER_OVERRIDE
6+
CRATE_TYPES "staticlib"
7+
IMPORTED_CRATES crates_list
8+
)
9+
10+
#list(REMOVE_ITEM crates_list "generate-bindings")
11+
foreach (crate IN LISTS crates_list)
12+
if (crate STREQUAL "generate-bindings")
13+
continue()
14+
endif ()
15+
add_dependencies("cargo-prebuild_${crate}" cargo-build_generate-bindings)
16+
endforeach ()
17+
18+
message(STATUS "Imported crates: ${crates_list}")
19+
20+
add_library(rust-glue STATIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/jsapi-rs/cpp/jsglue.cpp)
21+
target_include_directories(rust-glue PRIVATE ${SM_INCLUDE_DIR})
22+
add_dependencies(cargo-prebuild_generate-bindings rust-glue)
23+
24+
corrosion_set_env_vars(generate-bindings
25+
LIBCLANG_PATH=/opt/homebrew/opt/llvm/lib
26+
SYSROOT=${WASI_SDK_PREFIX}/share/wasi-sysroot
27+
CXXFLAGS="${CMAKE_CXX_FLAGS}"
28+
BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
29+
SM_HEADERS=${SM_INCLUDE_DIR}
30+
RUST_LOG=bindgen
31+
)
32+
233
set_property(TARGET rust-url PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/)

cmake/builtins.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,5 @@ add_builtin(
9595
fmt
9696
INCLUDE_DIRS
9797
runtime)
98+
99+
add_rust_builtin(test-builtin)

crates/rust-url/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
22
name = "rust-url"
33
version = "0.1.0"
4-
edition = "2018"
4+
license = "Apache-2.0 WITH LLVM-exception"
5+
edition = "2021"
56

67
[lib]
78
crate-type = ["staticlib"]

include/extension-api.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Engine {
3737
public:
3838
Engine();
3939
JSContext *cx();
40-
HandleObject global();
40+
JS::HandleObject global();
4141

4242
/// Initialize the engine with the given filename
4343
bool initialize(const char * filename);
@@ -54,7 +54,7 @@ class Engine {
5454
*
5555
* Once loaded, the instance is cached and reused as a singleton.
5656
*/
57-
bool define_builtin_module(const char *id, HandleValue builtin);
57+
bool define_builtin_module(const char *id, JS::HandleValue builtin);
5858

5959
/**
6060
* Treat the top-level script as a module or classic JS script.
@@ -105,7 +105,7 @@ class Engine {
105105
* Get the JS value associated with the top-level script execution -
106106
* the last expression for a script, or the module namespace for a module.
107107
*/
108-
HandleValue script_value();
108+
JS::HandleValue script_value();
109109

110110
bool has_pending_async_tasks();
111111
void queue_async_task(AsyncTask *task);
@@ -118,11 +118,11 @@ class Engine {
118118
bool dump_value(JS::Value val, FILE *fp = stdout);
119119
bool print_stack(FILE *fp);
120120
void dump_pending_exception(const char *description = "");
121-
void dump_promise_rejection(HandleValue reason, HandleObject promise, FILE *fp);
121+
void dump_promise_rejection(HandleValue reason, JS::HandleObject promise, FILE *fp);
122122
};
123123

124124

125-
typedef bool (*TaskCompletionCallback)(JSContext* cx, HandleObject receiver);
125+
typedef bool (*TaskCompletionCallback)(JSContext* cx, JS::HandleObject receiver);
126126

127127
class AsyncTask {
128128
protected:

include/host_api.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@
1919
#include "jsapi.h"
2020
#pragma clang diagnostic pop
2121

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

0 commit comments

Comments
 (0)