Skip to content

Commit

Permalink
Support dynamic resource parameters in Rib (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj authored Sep 24, 2024
1 parent 1623201 commit a516996
Show file tree
Hide file tree
Showing 28 changed files with 2,127 additions and 322 deletions.
53 changes: 52 additions & 1 deletion golem-api-grpc/proto/golem/rib/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,20 @@ message ResultExpr {
}

message CallExpr {
InvocationName name = 1;
optional InvocationName name = 1;
repeated Expr params = 2;
optional CallType call_type = 3;
}

message CallType {
oneof name {
golem.rib.DynamicParsedFunctionName parsed = 1;
string variant_constructor = 2;
string enum_constructor = 3;
}
}

/** Legacy call-type that holds fully formed function names and not dynamic functions. This is kept for backward compatibility */
message InvocationName {
oneof name {
golem.rib.ParsedFunctionName parsed = 1;
Expand Down Expand Up @@ -217,3 +227,44 @@ message TupleConstructorArmPattern {
message LiteralArmPattern {
Expr expr = 1;
}

message DynamicParsedFunctionName {
golem.rib.ParsedFunctionSite site = 1;
DynamicParsedFunctionReference function = 2;
}

message DynamicParsedFunctionReference {
oneof function_reference {
golem.rib.FunctionFunctionReference function = 1;
golem.rib.RawResourceConstructorFunctionReference raw_resource_constructor = 2;
golem.rib.RawResourceDropFunctionReference raw_resource_drop = 3;
golem.rib.RawResourceMethodFunctionReference raw_resource_method = 4;
golem.rib.RawResourceStaticMethodFunctionReference raw_resource_static_method = 5;
DynamicIndexedResourceConstructorFunctionReference indexed_resource_constructor = 6;
DynamicIndexedResourceMethodFunctionReference indexed_resource_method = 7;
DynamicIndexedResourceStaticMethodFunctionReference indexed_resource_static_method = 8;
DynamicIndexedResourceDropFunctionReference indexed_resource_drop = 9;
}
}

message DynamicIndexedResourceConstructorFunctionReference {
string resource = 1;
repeated golem.rib.Expr resource_params = 2;
}

message DynamicIndexedResourceMethodFunctionReference {
string resource = 1;
repeated golem.rib.Expr resource_params = 2;
string method = 3;
}

message DynamicIndexedResourceStaticMethodFunctionReference {
string resource = 1;
repeated golem.rib.Expr resource_params = 2;
string method = 3;
}

message DynamicIndexedResourceDropFunctionReference {
string resource = 1;
repeated golem.rib.Expr resource_params = 2;
}
2 changes: 1 addition & 1 deletion golem-api-grpc/proto/golem/rib/function_name.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ message IndexedResourceStaticMethodFunctionReference {
message IndexedResourceDropFunctionReference {
string resource = 1;
repeated string resource_params = 2;
}
}
68 changes: 67 additions & 1 deletion golem-api-grpc/proto/golem/rib/ir.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "wasm/ast/type.proto";

import "wasm/rpc/type_annotated_value.proto";

import "golem/rib/function_name.proto";

message RibIR {
oneof instruction {
wasm.rpc.TypeAnnotatedValue push_lit = 1;
Expand Down Expand Up @@ -39,6 +41,7 @@ message RibIR {
ConcatInstruction concat = 29;
EnumConstructionInstruction enum_construction = 30;
And and = 31;
CreateFunctionNameInstruction create_function_name = 32;
}
}

Expand Down Expand Up @@ -83,7 +86,6 @@ message JumpInstruction {
}

message CallInstruction {
string function_name = 1;
uint64 argument_count = 2;
wasm.ast.Type return_type = 3;
}
Expand All @@ -98,6 +100,12 @@ message EnumConstructionInstruction {
wasm.ast.Type return_type = 2;
}

message CreateFunctionNameInstruction {
golem.rib.ParsedFunctionSite site = 1;
FunctionReferenceType function_reference_details = 2;
}


message EqualTo {}
message GreaterThan {}
message LessThan {}
Expand All @@ -106,3 +114,61 @@ message LessThanOrEqualTo {}
message GetTag {}
message Negate {}
message And {}

message FunctionReferenceType {
oneof type {
Function function = 1;
RawResourceConstructor raw_resource_constructor = 2;
RawResourceDrop raw_resource_drop = 3;
RawResourceMethod raw_resource_method = 4;
RawResourceStaticMethod raw_resource_static_method = 5;
IndexedResourceConstructor indexed_resource_constructor = 6;
IndexedResourceMethod indexed_resource_method = 7;
IndexedResourceStaticMethod indexed_resource_static_method = 8;
IndexedResourceDrop indexed_resource_drop = 9;
}
}

message Function {
string name = 1;
}

message RawResourceConstructor {
string resource_name = 1;
}

message RawResourceDrop {
string resource_name = 1;
}

message RawResourceMethod {
string resource_name = 1;
string method_name = 2;
}

message RawResourceStaticMethod {
string resource_name = 1;
string method_name = 2;
}

message IndexedResourceConstructor {
string resource_name = 1;
uint32 arg_size = 2;
}

message IndexedResourceMethod {
string resource_name = 1;
uint32 arg_size = 2;
string method_name = 3;
}

message IndexedResourceStaticMethod {
string resource_name = 1;
uint32 arg_size = 2;
string method_name = 3;
}

message IndexedResourceDrop {
string resource_name = 1;
uint32 arg_size = 2;
}
78 changes: 48 additions & 30 deletions golem-rib/src/call_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::ParsedFunctionName;
use crate::{DynamicParsedFunctionName, ParsedFunctionName};
use bincode::{Decode, Encode};
use std::convert::TryFrom;
use std::fmt::Display;

#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
pub enum CallType {
Function(ParsedFunctionName),
Function(DynamicParsedFunctionName),
VariantConstructor(String),
EnumConstructor(String),
}
Expand All @@ -34,49 +34,67 @@ impl Display for CallType {
}
}

impl TryFrom<golem_api_grpc::proto::golem::rib::InvocationName> for CallType {
impl TryFrom<golem_api_grpc::proto::golem::rib::CallType> for CallType {
type Error = String;
fn try_from(
value: golem_api_grpc::proto::golem::rib::InvocationName,
) -> Result<Self, Self::Error> {
fn try_from(value: golem_api_grpc::proto::golem::rib::CallType) -> Result<Self, Self::Error> {
let invocation = value.name.ok_or("Missing name of invocation")?;
match invocation {
golem_api_grpc::proto::golem::rib::invocation_name::Name::Parsed(name) => {
Ok(CallType::Function(ParsedFunctionName::try_from(name)?))
}
golem_api_grpc::proto::golem::rib::invocation_name::Name::VariantConstructor(name) => {
golem_api_grpc::proto::golem::rib::call_type::Name::Parsed(name) => Ok(
CallType::Function(DynamicParsedFunctionName::try_from(name)?),
),
golem_api_grpc::proto::golem::rib::call_type::Name::VariantConstructor(name) => {
Ok(CallType::VariantConstructor(name))
}
golem_api_grpc::proto::golem::rib::invocation_name::Name::EnumConstructor(name) => {
golem_api_grpc::proto::golem::rib::call_type::Name::EnumConstructor(name) => {
Ok(CallType::EnumConstructor(name))
}
}
}
}

impl From<CallType> for golem_api_grpc::proto::golem::rib::InvocationName {
impl From<CallType> for golem_api_grpc::proto::golem::rib::CallType {
fn from(value: CallType) -> Self {
match value {
CallType::Function(parsed_name) => {
golem_api_grpc::proto::golem::rib::InvocationName {
name: Some(golem_api_grpc::proto::golem::rib::invocation_name::Name::Parsed(
parsed_name.into(),
)),
}
CallType::Function(parsed_name) => golem_api_grpc::proto::golem::rib::CallType {
name: Some(golem_api_grpc::proto::golem::rib::call_type::Name::Parsed(
parsed_name.into(),
)),
},
CallType::VariantConstructor(name) => golem_api_grpc::proto::golem::rib::CallType {
name: Some(
golem_api_grpc::proto::golem::rib::call_type::Name::VariantConstructor(name),
),
},
CallType::EnumConstructor(name) => golem_api_grpc::proto::golem::rib::CallType {
name: Some(
golem_api_grpc::proto::golem::rib::call_type::Name::EnumConstructor(name),
),
},
}
}
}

// InvocationName is a legacy structure to keep the backward compatibility.
// InvocationName is corresponding to the new CallType and the difference here is,
// InvocationName::Function will always hold a static function name and not a dynamic one
// with Expr representing resource construction parameters
impl TryFrom<golem_api_grpc::proto::golem::rib::InvocationName> for CallType {
type Error = String;
fn try_from(
value: golem_api_grpc::proto::golem::rib::InvocationName,
) -> Result<Self, Self::Error> {
let invocation = value.name.ok_or("Missing name of invocation")?;
match invocation {
golem_api_grpc::proto::golem::rib::invocation_name::Name::Parsed(name) => {
Ok(CallType::Function(DynamicParsedFunctionName::parse(
ParsedFunctionName::try_from(name)?.to_string(),
)?))
}
CallType::VariantConstructor(name) => {
golem_api_grpc::proto::golem::rib::InvocationName {
name: Some(golem_api_grpc::proto::golem::rib::invocation_name::Name::VariantConstructor(
name,
)),
}
golem_api_grpc::proto::golem::rib::invocation_name::Name::VariantConstructor(name) => {
Ok(CallType::VariantConstructor(name))
}
CallType::EnumConstructor(name) => {
golem_api_grpc::proto::golem::rib::InvocationName {
name: Some(golem_api_grpc::proto::golem::rib::invocation_name::Name::EnumConstructor(
name,
)),
}
golem_api_grpc::proto::golem::rib::invocation_name::Name::EnumConstructor(name) => {
Ok(CallType::EnumConstructor(name))
}
}
}
Expand Down
Loading

0 comments on commit a516996

Please sign in to comment.