### Start WPT server Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Starts the Web Platform Tests server for interactive testing. ```bash just wpt-server ``` -------------------------------- ### Run Web Platform Tests Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/testing.html Setup and execution commands for the WPT suite, including host file configuration. ```bash cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug cmake --build cmake-build-debug --parallel $(nproc) --target wpt-runtime cat deps/wpt-hosts | sudo tee -a /etc/hosts # Required to resolve test server hostnames cd cmake-build-debug ctest -R wpt --verbose # Note: some of the tests run fairly slowly in debug builds, so be patient ``` -------------------------------- ### Run Web Platform Tests Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Setup and execution commands for the WPT suite, including host configuration and filtering. ```bash cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug cmake --build cmake-build-debug --parallel $(nproc) --target wpt-runtime cat deps/wpt-hosts | sudo tee -a /etc/hosts # Required to resolve test server hostnames cd cmake-build-debug ctest -R wpt --verbose # Note: some of the tests run fairly slowly in debug builds, so be patient ``` ```bash WPT_FILTER=fetch ctest -R wpt -v ``` ```bash WPT_FLAGS="--update-expectations" ctest -R wpt -v ``` -------------------------------- ### Example JavaScript for HTTP Server Component Source: https://bytecodealliance.github.io/StarlingMonkey/index.html An example `index.js` file that registers a `fetch` event handler, suitable for creating an HTTP server component. This code will respond to incoming fetch requests. ```javascript addEventListener('fetch', event => { event.respondWith(new Response('Hello, world!')); }); ``` -------------------------------- ### Run Specialized HTTP Server Component with Wasmtime Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Run the componentized JavaScript code using Wasmtime's `serve` command. This command starts an HTTP server that listens for requests handled by the specialized component. ```bash wasmtime serve -S cli --dir . index.wasm ``` -------------------------------- ### Example JavaScript Class Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html This JavaScript class serves as an example for implementing a native class in StarlingMonkey, demonstrating constructor, getters, methods, and static members. ```javascript class MyClass { constructor(a, b) { this._a = a; this._b = b; } get prop() { return 42; } method() { return this.a + this.b; } static get static_prop() { return "static"; } static static_method(a, b) { return a + b; } } ``` -------------------------------- ### Define a C++ builtin for StarlingMonkey Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Implement a C++ builtin by including 'extension-api.h' and defining an install function within a namespace matching the CMakeLists.txt definition. ```cpp // The extension API is automatically on the include path for builtins. #include "extension-api.h" // The namespace name must match the name passed to `add_builtin` in the CMakeLists.txt namespace my_project::my_builtin { bool install(api::Engine* engine) { printf("installing my-builtin\n"); return true; } } // namespace my_builtin ``` -------------------------------- ### Clone the repository Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/building.html Initial step to download the project source code. ```bash git clone https://github.com/bytecodealliance/StarlingMonkey cd StarlingMonkey ``` -------------------------------- ### Run Web Platform Tests Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Commands for setting up, running all, or running specific Web Platform Tests. ```bash just wpt-setup # prepare WPT hosts just wpt-test # run all tests just wpt-test console/console-log-symbol.any.js # run specific test ``` -------------------------------- ### List available recipes Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Displays a complete list of available just recipes. ```bash just --list ``` -------------------------------- ### Configure build environments Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/building.html Set up the build directory for either release or debug modes using CMake. ```bash cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release ``` ```bash cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug ``` -------------------------------- ### Project workflow with Just Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Common tasks managed by the Just task runner, including building, serving, and testing. ```bash just build ``` ```bash just serve .js ``` ```bash just test ``` ```bash just wpt-setup # prepare WPT hosts just wpt-test # run all tests just wpt-test console/console-log-symbol.any.js # run specific test ``` ```bash just --list ``` ```bash just reconfigure=true wpt-build ``` ```bash just mode=release build ``` ```bash just builddir=mybuilddir mode=release build ``` -------------------------------- ### Build the runtime Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/building.html Compile the project using CMake. Adjust the parallel flag based on available CPU cores. ```bash # Use cmake-build-debug for the debug build # Change the value for `--parallel` to match the number of CPU cores in your system cmake --build cmake-build-release --parallel 8 --target starling ``` -------------------------------- ### Build the project Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Configures a default cmake-build-debug directory and builds the project. ```bash just build ``` -------------------------------- ### Build and run integration tests Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Commands for building the integration test server and executing tests via ctest or Wasmtime. ```bash cmake --build cmake-build-debug --target integration-test-server ``` ```bash ctest --test-dir cmake-build-debug -j$(nproc) --output-on-failure ``` ```bash wasmtime serve -S common cmake-build-debug/test-server.wasm ``` -------------------------------- ### Run Integration Test Server Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/testing.html Directly execute the integration test server using wasmtime. ```bash wasmtime serve -S common cmake-build-debug/test-server.wasm ``` -------------------------------- ### Run integration tests Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Builds and executes the project integration tests. ```bash just test ``` -------------------------------- ### Build and Run Integration Tests Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/testing.html Commands to build the integration test server and execute tests using ctest. ```bash cmake --build cmake-build-debug --target integration-test-server ``` ```bash ctest --test-dir cmake-build-debug -j$(nproc) --output-on-failure ``` -------------------------------- ### Build StarlingMonkey Runtime Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Compile the runtime targets using CMake. ```bash # Use cmake-build-debug for the debug build cmake --build cmake-build-release -t starling --parallel $(nproc) ``` ```bash # Use cmake-build-debug for the debug build cmake --build cmake-build-release -t starling-raw.wasm --parallel $(nproc) ``` -------------------------------- ### Specify build mode Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Switches the build mode to release, changing the build directory to cmake-build-release. ```bash just mode=release build ``` -------------------------------- ### Serve a JS script Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Loads a JS script during componentization and serves its output using Wasmtime. ```bash just serve .js ``` -------------------------------- ### Test the runtime Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/building.html Execute JavaScript code or files using the Wasmtime runtime. ```bash wasmtime -S http starling.wasm -e "console.log('hello world')" # or, to load a file: wasmtime -S http --dir . starling.wasm index.js ``` -------------------------------- ### Override build directory and mode Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Configures a custom build directory and sets the build mode simultaneously. ```bash just builddir=mybuilddir mode=release build ``` -------------------------------- ### Create components from JS files Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/building.html Use the componentize script to bundle a JavaScript file into a WebAssembly component. ```bash cd cmake-build-release ./componentize.sh index.js wasmtime -S http --dir . index.wasm ``` -------------------------------- ### Native Class Implementation API Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Guidelines and structures for defining native JavaScript classes, including instance/static methods, properties, and constructor logic. ```APIDOC ## Native Class Definition ### Description Native classes are implemented by defining `JSFunctionSpec` and `JSPropertySpec` arrays for instance and static members. The class state is managed using reserved slots. ### Implementation Details - **Constructor**: Use `CTOR_HEADER(name, required_argc)` to initialize arguments and enforce `new` keyword usage. - **Instance Methods**: Use `METHOD_HEADER(required_argc)` to validate the receiver and argument count. - **State Management**: Use `SetReservedSlot` and `GetReservedSlot` to store and retrieve internal object state. - **Registration**: Classes are registered via an `install` function, which is automatically invoked during engine initialization if added to `builtins.incl` via CMake. ### Helper Macros - `CTOR_HEADER(name, required_argc)`: Initializes constructor arguments and checks minimum argument count. - `METHOD_HEADER(required_argc)`: Initializes method arguments and verifies the `this` context. - `is_instance(JSObject *obj)`: Verifies if an object is an instance of the builtin class. ``` -------------------------------- ### Build the project with CMake Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Compiles the project using CMake with a specified number of parallel jobs. ```bash cmake --build cmake-build-release --parallel 8 --target starling ``` -------------------------------- ### Host API Configuration Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Configuring the host API implementation for WASI interfaces. ```APIDOC ## Host API Configuration ### Description StarlingMonkey supports different WASI host API implementations. The implementation is selected via the `HOST_API` environment variable. ### Configuration - **Environment Variable**: Set `HOST_API` to the name of a directory within `host-apis` (e.g., `wasi-0.2.10`). - **Custom Implementation**: Set `HOST_API` to an absolute path of a directory containing a custom implementation. ``` -------------------------------- ### Fetch test via curl Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Retrieves a specific test file from the running WPT server. ```bash curl http://127.0.0.1:7676/console/console-log-symbol.any.js ``` -------------------------------- ### Implement a Native Class in C++ Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Defines the structure, methods, properties, and implementation logic for a native class, including constructor and static method handling. ```cpp #include "my_class.h" namespace builtins { namespace my_class { const JSFunctionSpec MyClass::methods[] = { JS_FN("method", MyClass::method, 0, JSPROP_ENUMERATE), JS_FS_END, }; const JSPropertySpec MyClass::properties[] = { JS_PSG("prop", MyClass::prop_get, JSPROP_ENUMERATE), JS_STRING_SYM_PS(toStringTag, "MyClass", JSPROP_READONLY), JS_PS_END, }; const JSFunctionSpec MyClass::static_methods[] = { JS_FN("static_method", MyClass::static_method, 2, JSPROP_ENUMERATE), JS_FS_END, }; const JSPropertySpec MyClass::static_properties[] = { JS_PSG("static_prop", MyClass::static_prop_get, JSPROP_ENUMERATE), JS_PS_END, }; // Constructor implementation bool MyClass::constructor(JSContext *cx, unsigned argc, JS::Value *vp) { CTOR_HEADER("MyClass", 2); RootedObject self(cx, JS_NewObjectForConstructor(cx, &class_, args)); if (!self) { return false; } SetReservedSlot(self, Slots::SlotA, args.get(0)); SetReservedSlot(self, Slots::SlotB, args.get(1)); args.rval().setObject(*self); return true; } // Instance method implementation bool MyClass::method(JSContext *cx, unsigned argc, JS::Value *vp) { METHOD_HEADER(0); double a, b; if (!JS::ToNumber(cx, JS::GetReservedSlot(self, Slots::SlotA), &a) || !JS::ToNumber(cx, JS::GetReservedSlot(self, Slots::SlotB), &b)) { return false; } args.rval().setNumber(a + b); return true; } // Instance property getter implementation bool MyClass::prop_get(JSContext *cx, unsigned argc, JS::Value *vp) { METHOD_HEADER(0); args.rval().setInt32(42); return true; } // Static method implementation bool MyClass::static_method(JSContext *cx, unsigned argc, JS::Value *vp) { CallArgs args = CallArgsFromVp(argc, vp); if (!args.requireAtLeast(cx, "static_method", 2)) { return false; } double a, b; if (!JS::ToNumber(cx, args.get(0), &a) || !JS::ToNumber(cx, args.get(1), &b)) { return false; } args.rval().setNumber(a + b); return true; } // Static property getter implementation bool MyClass::static_prop_get(JSContext *cx, unsigned argc, JS::Value *vp) { CallArgs args = CallArgsFromVp(argc, vp); args.rval().setString(JS_NewStringCopyZ(cx, "static")); return true; } // Class initialization bool MyClass::init_class(JSContext *cx, JS::HandleObject global) { return init_class_impl(cx, global); } bool install(api::Engine *engine) { return MyClass::init_class(engine->cx(), engine->global()); } } // namespace my_class } // namespace builtins ``` -------------------------------- ### Configure Release Build with CMake Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Configure the build for a release version of StarlingMonkey using CMake. This command sets up the build directory and specifies the build type. ```bash cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release ``` -------------------------------- ### Build Raw StarlingMonkey Core Module Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Build the raw WebAssembly core module (`starling-raw.wasm`). This module is a prerequisite for creating specialized WebAssembly Components. ```bash # Use cmake-build-debug for the debug build cmake --build cmake-build-release -t starling-raw.wasm --parallel $(nproc) ``` -------------------------------- ### Add builtin to CMakeLists.txt Source: https://bytecodealliance.github.io/StarlingMonkey/print.html Use the add_builtin command in CMakeLists.txt to include your C++ builtin implementation, specifying the namespace and source file. ```cmake add_builtin(my_project::my_builtin SRC my-builtin.cpp) ``` -------------------------------- ### Configure StarlingMonkey to Use Local SpiderMonkey Build Source: https://bytecodealliance.github.io/StarlingMonkey/developer/spidermonkey.html Set the SPIDERMONKEY_BINARIES environment variable to point to the local release directory of your modified SpiderMonkey build. Then, configure and build StarlingMonkey using CMake. ```bash export SPIDERMONKEY_BINARIES=/path/to/spidermonkey-wasi-embedding/release cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release cmake --build cmake-build-release --parallel 8 ``` -------------------------------- ### Build Starling.wasm Runtime Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Build the componentized StarlingMonkey runtime module (`starling.wasm`). This module can be used directly with a WebAssembly Component-aware runtime like wasmtime for runtime evaluation of JavaScript code. ```bash # Use cmake-build-debug for the debug build cmake --build cmake-build-release -t starling --parallel $(nproc) ``` -------------------------------- ### Filter Web Platform Tests Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/testing.html Filter WPT execution using the WPT_FILTER environment variable. ```bash WPT_FILTER=fetch ctest -R wpt -v ``` -------------------------------- ### Execute JavaScript with Wasmtime Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Use the built `starling.wasm` runtime with Wasmtime to execute JavaScript code. This can be done by passing code directly or by loading a JavaScript file. ```bash wasmtime -S http cmake-build-release/starling.wasm -e "console.log('hello world')" # or, to load a file: wasmtime -S http --dir . starling.wasm index.js ``` -------------------------------- ### Configure Debug Build with CMake Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Configure the build for a debug version of StarlingMonkey using CMake. This command sets up the build directory and specifies the build type. ```bash cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug ``` -------------------------------- ### Verify Class Instance with is_instance Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Uses the is_instance helper to validate that a JSObject is an instance of the expected native class before accessing internal slots. ```cpp JSString *MyObject::my_method(JSObject *self) { MOZ_ASSERT(is_instance(self)); return JS::GetReservedSlot(self, Slots::MySlot).toString(); } ``` -------------------------------- ### Clone Repositories for SpiderMonkey Modification Source: https://bytecodealliance.github.io/StarlingMonkey/developer/spidermonkey.html Clone the spidermonkey-wasi-embedding and gecko-dev repositories. Ensure gecko-dev is a subdirectory of spidermonkey-wasi-embedding. ```bash git clone https://github.com/bytecodealliance/spidermonkey-wasi-embedding cd spidermonkey-wasi-embedding/ git clone https://github.com/bytecodealliance/gecko-dev ``` -------------------------------- ### Declare Native Class Header in StarlingMonkey Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Define the header file for a native JavaScript class using StarlingMonkey's `BuiltinImpl` template. This includes declarations for instance and static methods, properties, and the constructor. ```cpp #ifndef BUILTINS_MY_CLASS_H #define BUILTINS_MY_CLASS_H #include "builtin.h" namespace builtins { namespace my_class { class MyClass : public BuiltinImpl { // Instance methods static bool method(JSContext *cx, unsigned argc, JS::Value *vp); static bool prop_get(JSContext *cx, unsigned argc, JS::Value *vp); // Static methods static bool static_method(JSContext *cx, unsigned argc, JS::Value *vp); static bool static_prop_get(JSContext *cx, unsigned argc, JS::Value *vp); public: enum Slots : uint8_t { SlotA, SlotB, Count }; static constexpr const char *class_name = "MyClass"; static constexpr unsigned ctor_length = 2; static const JSFunctionSpec static_methods[]; static const JSPropertySpec static_properties[]; static const JSFunctionSpec methods[]; static const JSPropertySpec properties[]; static bool init_class(JSContext *cx, HandleObject global); static bool constructor(JSContext *cx, unsigned argc, Value *vp); }; bool install(api::Engine *engine); } // namespace my_class } // namespace builtins #endif // BUILTINS_MY_CLASS_H ``` -------------------------------- ### Implement a Custom Built-in Function Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Define a C++ function for a built-in that takes an `api::Engine*` and returns a boolean. Ensure the namespace matches the one specified in `CMakeLists.txt`. The extension API is available via `extension-api.h`. ```cpp #include "extension-api.h" // The namespace name must match the name passed to `add_builtin` in the CMakeLists.txt namespace my_project::my_builtin { bool install(api::Engine* engine) { printf("installing my-builtin\n"); return true; } } // namespace my_builtin ``` -------------------------------- ### Configure WPT Runner Flags Source: https://bytecodealliance.github.io/StarlingMonkey/getting-started/testing.html Pass custom flags to the WPT test runner using the WPT_FLAGS environment variable. ```bash WPT_FLAGS="--update-expectations" ctest -R wpt -v ``` -------------------------------- ### Force CMake reconfiguration Source: https://bytecodealliance.github.io/StarlingMonkey/developer/just.html Forces CMake to reconfigure the build directory by adding the reconfigure=true parameter. ```bash just reconfigure=true wpt-build ``` -------------------------------- ### Componentize JavaScript Code Source: https://bytecodealliance.github.io/StarlingMonkey/index.html Use the `componentize.sh` script to turn the `starling-raw.wasm` module into a WebAssembly Component specialized for your JavaScript code. This is typically used for creating HTTP server components. ```bash cd cmake-build-release ./componentize.sh index.js -o index.wasm ``` -------------------------------- ### Checkout SpiderMonkey Commit Source: https://bytecodealliance.github.io/StarlingMonkey/print.html After cloning, checkout the specific SpiderMonkey commit used by StarlingMonkey. This ensures compatibility. You can then edit the SpiderMonkey source code. ```bash git checkout `cat ../gecko-revision` # now edit the source ``` -------------------------------- ### Add Built-in Function to CMakeLists.txt Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Use `add_builtin` in your project's `CMakeLists.txt` to include a custom C++ built-in function. The namespace must match the one defined in the C++ source. ```cmake add_builtin(my_project::my_builtin SRC my-builtin.cpp) ``` -------------------------------- ### Rebuild SpiderMonkey Artifacts Source: https://bytecodealliance.github.io/StarlingMonkey/developer/spidermonkey.html After making necessary edits to the SpiderMonkey source code, rebuild the artifacts from the spidermonkey-wasi-embedding root directory. This generates a 'release/' directory containing the modified binaries. ```bash cd ../ # back to spidermonkey-wasi-embedding ./rebuild.sh release ``` -------------------------------- ### Checkout Specific SpiderMonkey Commit Source: https://bytecodealliance.github.io/StarlingMonkey/developer/spidermonkey.html Switch to the specific SpiderMonkey commit used by StarlingMonkey. This ensures compatibility and allows for targeted modifications. ```bash git checkout `cat ../gecko-revision` ``` -------------------------------- ### Define JSClass with Reserved Slots Source: https://bytecodealliance.github.io/StarlingMonkey/developer/builtins-cpp.html Uses a static constexpr JSClass definition to register the class with reserved slots for internal state. ```cpp static constexpr JSClass class_{ Impl::class_name, JSCLASS_HAS_RESERVED_SLOTS(static_cast(Impl::Slots::Count)) | class_flags, &class_ops, }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.