Skip to content

Commit

Permalink
Support invoking wasi:http/incoming-handler (#1249)
Browse files Browse the repository at this point in the history
* Support invoking wasi:http/incoming-handler

* unsafety comment

* Update golem-common/src/virtual_exports/http_incoming_handler.rs

Co-authored-by: Daniel Vigovszky <daniel.vigovszky@gmail.com>

* comments

* test using wasm echo server

* test restore of state when implementing wasi:http/incoming-handler

* remove debug log

---------

Co-authored-by: Daniel Vigovszky <daniel.vigovszky@gmail.com>
  • Loading branch information
mschuwalow and vigoo authored Jan 21, 2025
1 parent 3b373c7 commit d22a30c
Show file tree
Hide file tree
Showing 139 changed files with 34,092 additions and 147 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ exclude = [
"test-components/update-test-v3-11",
"test-components/update-test-v4",
"test-components/variant-service",
"test-components/wasi-http-incoming-request-handler",
"test-components/wasi-http-incoming-request-handler-echo",
"test-components/wasi-http-incoming-request-handler-state",
"test-components/write-stderr",
"test-components/write-stdout",
]
Expand Down
1 change: 1 addition & 0 deletions golem-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ prost-types = { workspace = true, optional = true }
rand = { workspace = true }
range-set-blaze = "0.1.16"
regex = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions golem-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub mod tracing;

pub mod uri;

pub mod virtual_exports;

#[cfg(test)]
test_r::enable!();

Expand Down
14 changes: 13 additions & 1 deletion golem-common/src/model/component_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bincode::{Decode, Encode};
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};

use crate::SafeDisplay;
use crate::{virtual_exports, SafeDisplay};
use golem_wasm_ast::analysis::AnalysedFunctionParameter;
use golem_wasm_ast::core::Mem;
use golem_wasm_ast::metadata::Producers as WasmAstProducers;
Expand Down Expand Up @@ -182,6 +182,7 @@ impl RawComponentMetadata {
.map_err(ComponentProcessingError::Analysis)?;

add_resource_drops(&mut exports);
add_virtual_exports(&mut exports);

let exports = exports.into_iter().collect::<Vec<_>>();

Expand Down Expand Up @@ -327,6 +328,17 @@ fn drop_from_constructor(constructor: &AnalysedFunction) -> AnalysedFunction {
}
}

fn add_virtual_exports(exports: &mut Vec<AnalysedExport>) {
// Some interfaces like the golem/http:incoming-handler do not exist on the component,
// but are dynamically created by the worker executor based on other existing interfaces.

if virtual_exports::http_incoming_handler::implements_required_interfaces(exports) {
exports.extend(vec![
virtual_exports::http_incoming_handler::ANALYZED_EXPORT.clone(),
]);
};
}

#[cfg(feature = "protobuf")]
mod protobuf {
use crate::model::component_metadata::{
Expand Down
21 changes: 2 additions & 19 deletions golem-common/src/model/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,7 @@ use golem_wasm_ast::analysis::{AnalysedExport, AnalysedFunction, AnalysedInstanc

use rib::{ParsedFunctionName, ParsedFunctionReference, ParsedFunctionSite};

pub trait AnalysedExportExtensions {
fn function_names(&self) -> Vec<String>;
}

impl AnalysedExportExtensions for AnalysedExport {
fn function_names(&self) -> Vec<String> {
match self {
AnalysedExport::Instance(instance) => instance
.functions
.iter()
.map(|function| format!("{}.{{{}}}", instance.name, function.name))
.collect(),
AnalysedExport::Function(function) => vec![function.name.clone()],
}
}
}

pub fn instances(exports: &Vec<AnalysedExport>) -> Vec<AnalysedInstance> {
fn instances(exports: &Vec<AnalysedExport>) -> Vec<AnalysedInstance> {
let mut instances = vec![];
for export in exports {
if let AnalysedExport::Instance(instance) = export {
Expand All @@ -43,7 +26,7 @@ pub fn instances(exports: &Vec<AnalysedExport>) -> Vec<AnalysedInstance> {
instances
}

pub fn functions(exports: &Vec<AnalysedExport>) -> Vec<AnalysedFunction> {
fn functions(exports: &Vec<AnalysedExport>) -> Vec<AnalysedFunction> {
let mut functions = vec![];
for export in exports {
if let AnalysedExport::Function(function) = export {
Expand Down
Loading

0 comments on commit d22a30c

Please sign in to comment.