Skip to content

Commit 663c4c8

Browse files
committed
WIP
1 parent cd611c4 commit 663c4c8

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

tools/wit-codegen/code_generator.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ void CodeGenerator::generateWAMRBindings(const std::vector<InterfaceInfo> &inter
680680
{
681681
std::string funcName = sanitize_identifier(func.name);
682682

683+
// If this is a resource method, prefix with resource name to match header generation
684+
if (!func.resource_name.empty())
685+
{
686+
funcName = sanitize_identifier(func.resource_name) + "_" + funcName;
687+
}
688+
683689
// For standalone functions, reference directly in host namespace
684690
// For interface functions, reference within their interface namespace
685691
if (iface->is_standalone_function)
@@ -764,6 +770,13 @@ void CodeGenerator::generateWAMRBindings(const std::vector<InterfaceInfo> &inter
764770
for (const auto &func : iface_info->functions)
765771
{
766772
std::string funcName = sanitize_identifier(func.name);
773+
774+
// If this is a resource method, prefix with resource name to match header generation
775+
if (!func.resource_name.empty())
776+
{
777+
funcName = sanitize_identifier(func.resource_name) + "_" + funcName;
778+
}
779+
767780
out << " host_function(\"" << func.name << "\", host::" << sanitize_identifier(local_namespace) << "::" << funcName << "),\n";
768781
}
769782

@@ -999,6 +1012,16 @@ void CodeGenerator::generateGuestFunctionWrappers(std::ofstream &out, const std:
9991012
std::replace(type_alias_name.begin(), type_alias_name.end(), '-', '_');
10001013
std::replace(type_alias_name.begin(), type_alias_name.end(), '.', '_');
10011014

1015+
// If this is a resource method, prefix with resource name to match type alias generation
1016+
if (!func.resource_name.empty())
1017+
{
1018+
std::string resource_name = func.resource_name;
1019+
std::replace(resource_name.begin(), resource_name.end(), '-', '_');
1020+
std::replace(resource_name.begin(), resource_name.end(), '.', '_');
1021+
type_alias_name = resource_name + "_" + type_alias_name;
1022+
function_name = resource_name + "_" + function_name;
1023+
}
1024+
10021025
// Build the export name (e.g., "example:sample/booleans#and")
10031026
std::string export_name = packageName + "/" + iface->name + "#" + func.name;
10041027

@@ -1584,6 +1607,19 @@ void CodeGenerator::generateGuestFunctionTypeAlias(std::ofstream &out, const Fun
15841607
}
15851608
}
15861609

1610+
// Check in resources
1611+
if (!found)
1612+
{
1613+
for (const auto &resource : iface->resources)
1614+
{
1615+
if (resource.name == potential_type)
1616+
{
1617+
found = true;
1618+
break;
1619+
}
1620+
}
1621+
}
1622+
15871623
// If not found in any defined types, it's unknown
15881624
if (!found)
15891625
{
@@ -1644,6 +1680,15 @@ void CodeGenerator::generateGuestFunctionTypeAlias(std::ofstream &out, const Fun
16441680
std::replace(type_alias_name.begin(), type_alias_name.end(), '-', '_');
16451681
std::replace(type_alias_name.begin(), type_alias_name.end(), '.', '_');
16461682

1683+
// If this is a resource method, prefix with resource name to match other declarations
1684+
if (!func.resource_name.empty())
1685+
{
1686+
std::string resource_name = func.resource_name;
1687+
std::replace(resource_name.begin(), resource_name.end(), '-', '_');
1688+
std::replace(resource_name.begin(), resource_name.end(), '.', '_');
1689+
type_alias_name = resource_name + "_" + type_alias_name;
1690+
}
1691+
16471692
out << "// Guest function signature for use with guest_function<" << type_alias_name << "_t>()\n";
16481693
out << "using " << type_alias_name << "_t = " << return_type << "(";
16491694

0 commit comments

Comments
 (0)