Skip to content

Commit

Permalink
Add Rust bindings for the runtime and the SpiderMonkey engines
Browse files Browse the repository at this point in the history
  • Loading branch information
tschneidereit committed Aug 1, 2024
1 parent 689dc97 commit cc0b178
Show file tree
Hide file tree
Showing 87 changed files with 34,630 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/.spin

# Rust compilation output
/target
/runtime/crates/target/

/tests/e2e/*/*.wasm
/tests/e2e/*/*.log
Expand Down
20 changes: 10 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ include("openssl")
include("host_api")
include("build-crates")

add_library(extension_api INTERFACE include/extension-api.h runtime/encode.h runtime/decode.h)
add_library(extension_api INTERFACE include/extension-api.h runtime/cpp/encode.h runtime/cpp/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 include deps/include runtime/cpp)

include("builtins")

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

add_executable(starling.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
runtime/cpp/js.cpp
runtime/cpp/allocator.cpp
runtime/cpp/encode.cpp
runtime/cpp/decode.cpp
runtime/cpp/engine.cpp
runtime/cpp/event_loop.cpp
runtime/cpp/builtin.cpp
runtime/cpp/script_loader.cpp
)

option(USE_WASM_OPT "use wasm-opt to optimize the StarlingMonkey binary" ON)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020 Fastly, Inc.
Copyright StarlingMonkey project contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
18 changes: 13 additions & 5 deletions builtins/install_builtins.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
#include "extension-api.h"

#define NS_DEF(ns) \
namespace ns { \
extern bool install(api::Engine *engine); \
#define NS_DEF(ns) \
namespace ns { \
extern bool install(api::Engine *engine); \
}
#define RS_DEF(install_fn) \
extern "C" bool install_fn(api::Engine *engine);
#include "builtins.incl"
#undef RS_DEF
#undef NS_DEF


bool install_builtins(api::Engine *engine) {
#define NS_DEF(ns) \
if (!ns::install(engine)) \
#define NS_DEF(ns) \
if (!ns::install(engine)) \
return false;
#define RS_DEF(install_fn) \
if (!install_fn(engine)) \
return false;
#include "builtins.incl"
#undef RS_DEF
#undef NS_DEF

return true;
Expand Down
34 changes: 34 additions & 0 deletions cmake/add_builtin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,37 @@ function(add_builtin)
file(APPEND $CACHE{INSTALL_BUILTINS} "NS_DEF(${NS})\n")
return(PROPAGATE LIB_NAME)
endfunction()


function(add_rust_builtin)
# TODO: restore additional config args
list(GET ARGN 0 LIB_NAME)
set(DEFAULT_ENABLE ON)
set(LIB_TARGET_NAME ${LIB_NAME})
string(REPLACE "-" "_" LIB_NAME ${LIB_NAME})
string(PREPEND LIB_NAME "builtin_")
string(TOUPPER ${LIB_NAME} LIB_NAME_UPPER)
set(OPT_NAME ENABLE_${LIB_NAME_UPPER})
set(DESCRIPTION "${LIB_TARGET_NAME} (option: ${OPT_NAME}, default: ${DEFAULT_ENABLE})")

# In script-mode, just show the available builtins.
if(CMAKE_SCRIPT_MODE_FILE)
message(STATUS " ${DESCRIPTION}")
return()
endif()

option(${OPT_NAME} "Enable ${LIB_NAME}" ${DEFAULT_ENABLE})
if (${${OPT_NAME}})
else()
message(STATUS "Skipping builtin ${DESCRIPTION}")
return()
endif()

message(STATUS "Adding builtin ${DESCRIPTION}")

target_link_libraries(builtins PRIVATE ${LIB_TARGET_NAME} rust-glue)
add_dependencies(${LIB_TARGET_NAME} rust-bindings)

file(APPEND $CACHE{INSTALL_BUILTINS} "RS_DEF(${LIB_NAME}_install)\n")
return(PROPAGATE LIB_NAME)
endfunction()
31 changes: 31 additions & 0 deletions cmake/build-crates.cmake
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/Cargo.toml NO_LINKER_OVERRIDE)

corrosion_import_crate(
MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/Cargo.toml
NO_LINKER_OVERRIDE
CRATE_TYPES "staticlib"
IMPORTED_CRATES crates_list
)

#list(REMOVE_ITEM crates_list "generate-bindings")
foreach (crate IN LISTS crates_list)
if (crate STREQUAL "generate-bindings")
continue()
endif ()
add_dependencies("cargo-prebuild_${crate}" cargo-build_generate-bindings)
endforeach ()

message(STATUS "Imported crates: ${crates_list}")

add_library(rust-glue STATIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/jsapi-rs/cpp/jsglue.cpp)
target_include_directories(rust-glue PRIVATE ${SM_INCLUDE_DIR})
add_dependencies(cargo-prebuild_generate-bindings rust-glue)

corrosion_set_env_vars(generate-bindings
LIBCLANG_PATH=/opt/homebrew/opt/llvm/lib
SYSROOT=${WASI_SDK_PREFIX}/share/wasi-sysroot
CXXFLAGS="${CMAKE_CXX_FLAGS}"
BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
SM_HEADERS=${SM_INCLUDE_DIR}
RUST_LOG=bindgen
)

set_property(TARGET rust-url PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/)
2 changes: 2 additions & 0 deletions cmake/builtins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ add_builtin(
fmt
INCLUDE_DIRS
runtime)

add_rust_builtin(test-builtin)
3 changes: 2 additions & 1 deletion crates/rust-url/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "rust-url"
version = "0.1.0"
edition = "2018"
license = "Apache-2.0 WITH LLVM-exception"
edition = "2021"

[lib]
crate-type = ["staticlib"]
Expand Down
10 changes: 5 additions & 5 deletions include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Engine {
public:
Engine();
JSContext *cx();
HandleObject global();
JS::HandleObject global();

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

/**
* Treat the top-level script as a module or classic JS script.
Expand Down Expand Up @@ -105,7 +105,7 @@ class Engine {
* Get the JS value associated with the top-level script execution -
* the last expression for a script, or the module namespace for a module.
*/
HandleValue script_value();
JS::HandleValue script_value();

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


typedef bool (*TaskCompletionCallback)(JSContext* cx, HandleObject receiver);
typedef bool (*TaskCompletionCallback)(JSContext* cx, JS::HandleObject receiver);

class AsyncTask {
protected:
Expand Down
22 changes: 22 additions & 0 deletions include/host_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
#include "jsapi.h"
#pragma clang diagnostic pop

/**
* <div rustbindgen="true" replaces="std::optional">
*/
template<typename T> class simple_optional {
T* ptr;
};

/**
* <div rustbindgen="true" replaces="std::unique_ptr">
*/
template<typename T> class simple_unique_ptr {
T* ptr;
};

/**
* <div rustbindgen="true" replaces="std::vector">
*/
template<typename T> class simple_vector {
T* ptr;
};

using api::PollableHandle;
using std::optional;
using std::string_view;
using std::tuple;
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.
Loading

0 comments on commit cc0b178

Please sign in to comment.