Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of std::function in C++ API #615

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ inline fizzy::ExecutionResult unwrap(const FizzyExecutionResult& result) noexcep
return unwrap(result.value);
}

inline auto unwrap(FizzyExternalFn func, void* context) noexcept
fizzy::ExecutionResult unwrapped_external_function(
void* context, fizzy::Instance& instance, const fizzy::Value* args, int depth) noexcept
{
return [func, context](fizzy::Instance& instance, const fizzy::Value* args,
int depth) noexcept -> fizzy::ExecutionResult {
const auto result = func(context, wrap(&instance), wrap(args), depth);
return unwrap(result);
};
const auto* c_external_function = static_cast<const FizzyExternalFunction*>(context);
return unwrap(c_external_function->function(
c_external_function->context, wrap(&instance), wrap(args), depth));
}


inline fizzy::ExternalFunction unwrap(const FizzyExternalFunction& external_func)
{
return fizzy::ExternalFunction{
unwrap(external_func.function, external_func.context), unwrap(external_func.type)};
return fizzy::ExternalFunction{unwrapped_external_function,
const_cast<FizzyExternalFunction*>(&external_func), unwrap(external_func.type)};
}

inline std::vector<fizzy::ExternalFunction> unwrap(
Expand Down Expand Up @@ -152,8 +152,8 @@ inline fizzy::ImportedFunction unwrap(const FizzyImportedFunction& c_imported_fu
std::nullopt :
std::make_optional(unwrap(c_type.output));

imported_func.function = unwrap(
c_imported_func.external_function.function, c_imported_func.external_function.context);
imported_func.function = unwrapped_external_function;
imported_func.context = const_cast<FizzyExternalFunction*>(&c_imported_func.external_function);

return imported_func;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, uint32_t
}

inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance,
OperandStack& stack, int depth)
OperandStack& stack, int depth) noexcept
{
const auto num_args = func_type.inputs.size();
assert(stack.size() >= num_args);
Expand All @@ -501,7 +501,7 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan

} // namespace

ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth)
ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth) noexcept
{
assert(depth >= 0);
if (depth > CallStackLimit)
Expand All @@ -511,7 +511,8 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,

assert(instance.module->imported_function_types.size() == instance.imported_functions.size());
if (func_idx < instance.imported_functions.size())
return instance.imported_functions[func_idx].function(instance, args, depth);
return instance.imported_functions[func_idx].function(
instance.imported_functions[func_idx].context, instance, args, depth);

const auto& code = instance.module->get_code(func_idx);
auto* const memory = instance.memory.get();
Expand Down
5 changes: 3 additions & 2 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ constexpr ExecutionResult Void{true};
constexpr ExecutionResult Trap{false};

// Execute a function on an instance.
ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0);
ExecutionResult execute(
Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0) noexcept;

inline ExecutionResult execute(
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args)
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args) noexcept
{
assert(args.size() == instance.module->get_function_type(func_idx).inputs.size());
return execute(instance, func_idx, args.begin());
Expand Down
15 changes: 10 additions & 5 deletions lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ std::vector<ExternalFunction> resolve_imported_functions(
}

external_functions.emplace_back(
ExternalFunction{std::move(it->function), module_func_type});
ExternalFunction{it->function, it->context, module_func_type});
}

return external_functions;
Expand All @@ -420,18 +420,23 @@ std::optional<FuncIdx> find_exported_function(const Module& module, std::string_
return find_export(module, ExternalKind::Function, name);
}

std::optional<ExternalFunction> find_exported_function(Instance& instance, std::string_view name)
std::optional<ExportedFunction> find_exported_function(Instance& instance, std::string_view name)
{
const auto opt_index = find_export(*instance.module, ExternalKind::Function, name);
if (!opt_index.has_value())
return std::nullopt;

const auto idx = *opt_index;
auto func = [idx, &instance](fizzy::Instance&, const Value* args, int depth) {
return execute(instance, idx, args, depth);
auto func = [](void* context, fizzy::Instance&, const Value* args, int depth) noexcept {
auto* instance_and_idx = static_cast<std::pair<Instance*, FuncIdx>*>(context);
return execute(*instance_and_idx->first, instance_and_idx->second, args, depth);
};

return ExternalFunction{std::move(func), instance.module->get_function_type(idx)};
ExportedFunction exported_func;
exported_func.context.reset(new std::pair<Instance*, FuncIdx>(&instance, idx));
exported_func.external_function = {
std::move(func), exported_func.context.get(), instance.module->get_function_type(idx)};
return exported_func;
}

std::optional<ExternalGlobal> find_exported_global(Instance& instance, std::string_view name)
Expand Down
17 changes: 14 additions & 3 deletions lib/fizzy/instantiate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ namespace fizzy
struct ExecutionResult;
struct Instance;

using ExternalFunctionPtr = ExecutionResult (*)(
void* context, Instance&, const Value*, int depth) noexcept;

struct ExternalFunction
{
std::function<ExecutionResult(Instance&, const Value*, int depth)> function;
ExternalFunctionPtr function;
void* context;
FuncType type;
};

Expand Down Expand Up @@ -114,7 +118,8 @@ struct ImportedFunction
std::string name;
std::vector<ValType> inputs;
std::optional<ValType> output;
std::function<ExecutionResult(Instance&, const Value*, int depth)> function;
ExternalFunctionPtr function;
void* context;
};

// Create vector of ExternalFunctions ready to be passed to instantiate.
Expand All @@ -126,8 +131,14 @@ std::vector<ExternalFunction> resolve_imported_functions(
// Find exported function index by name.
std::optional<FuncIdx> find_exported_function(const Module& module, std::string_view name);

struct ExportedFunction
{
std::unique_ptr<std::pair<Instance*, FuncIdx>> context;
ExternalFunction external_function;
};

// Find exported function by name.
std::optional<ExternalFunction> find_exported_function(Instance& instance, std::string_view name);
std::optional<ExportedFunction> find_exported_function(Instance& instance, std::string_view name);

// Find exported global by name.
std::optional<ExternalGlobal> find_exported_global(Instance& instance, std::string_view name);
Expand Down
55 changes: 43 additions & 12 deletions test/spectests/spectests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct test_results

struct imports
{
std::vector<fizzy::ExternalFunction> functions;
std::vector<fizzy::ExportedFunction> functions;
std::vector<fizzy::ExternalTable> tables;
std::vector<fizzy::ExternalMemory> memories;
std::vector<fizzy::ExternalGlobal> globals;
Expand Down Expand Up @@ -128,8 +128,17 @@ class test_runner
continue;
}


std::vector<fizzy::ExternalFunction> external_functions(
imports.functions.size());
std::transform(imports.functions.begin(), imports.functions.end(),
external_functions.begin(),
[](const auto& imported_func) { return imported_func.external_function; });

m_instances_external_functions[name] = std::move(imports.functions);

m_instances[name] =
fizzy::instantiate(std::move(module), std::move(imports.functions),
fizzy::instantiate(std::move(module), std::move(external_functions),
std::move(imports.tables), std::move(imports.memories),
std::move(imports.globals), TestMemoryPagesLimit);

Expand All @@ -139,20 +148,23 @@ class test_runner
{
fail(std::string{"Parsing failed with error: "} + ex.what());
m_instances.erase(name);
m_instances_external_functions.erase(name);
m_last_module_name.clear();
continue;
}
catch (const fizzy::validation_error& ex)
{
fail(std::string{"Validation failed with error: "} + ex.what());
m_instances.erase(name);
m_instances_external_functions.erase(name);
m_last_module_name.clear();
continue;
}
catch (const fizzy::instantiate_error& ex)
{
fail(std::string{"Instantiation failed with error: "} + ex.what());
m_instances.erase(name);
m_instances_external_functions.erase(name);
m_last_module_name.clear();
continue;
}
Expand Down Expand Up @@ -338,7 +350,13 @@ class test_runner
continue;
}

fizzy::instantiate(std::move(module), std::move(imports.functions),
std::vector<fizzy::ExternalFunction> external_functions(
imports.functions.size());
std::transform(imports.functions.begin(), imports.functions.end(),
external_functions.begin(),
[](const auto& imported_func) { return imported_func.external_function; });

fizzy::instantiate(std::move(module), std::move(external_functions),
std::move(imports.tables), std::move(imports.memories),
std::move(imports.globals));
}
Expand Down Expand Up @@ -564,32 +582,41 @@ class test_runner
{
const auto it_registered = m_registered_names.find(import.module);
if (it_registered == m_registered_names.end())
return {{}, "Module \"" + import.module + "\" not registered."};
{
imports empty_imports;
return {
std::move(empty_imports), "Module \"" + import.module + "\" not registered."};
}

const auto module_name = it_registered->second;
const auto it_instance = m_instances.find(module_name);
if (it_instance == m_instances.end())
return {{}, "Module not instantiated."};
{
imports empty_imports;
return {std::move(empty_imports), "Module not instantiated."};
}

auto& instance = it_instance->second;

if (import.kind == fizzy::ExternalKind::Function)
{
const auto func = fizzy::find_exported_function(*instance, import.name);
auto func = fizzy::find_exported_function(*instance, import.name);
if (!func.has_value())
{
return {{},
imports empty_imports;
return {std::move(empty_imports),
"Function \"" + import.name + "\" not found in \"" + import.module + "\"."};
}

result.functions.emplace_back(*func);
result.functions.emplace_back(std::move(*func));
}
else if (import.kind == fizzy::ExternalKind::Table)
{
const auto table = fizzy::find_exported_table(*instance, import.name);
if (!table.has_value())
{
return {{},
imports empty_imports;
return {std::move(empty_imports),
"Table \"" + import.name + "\" not found in \"" + import.module + "\"."};
}

Expand All @@ -600,7 +627,8 @@ class test_runner
const auto memory = fizzy::find_exported_memory(*instance, import.name);
if (!memory.has_value())
{
return {{},
imports empty_imports;
return {std::move(empty_imports),
"Memory \"" + import.name + "\" not found in \"" + import.module + "\"."};
}

Expand All @@ -611,15 +639,16 @@ class test_runner
const auto global = fizzy::find_exported_global(*instance, import.name);
if (!global.has_value())
{
return {{},
imports empty_imports;
return {std::move(empty_imports),
"Global \"" + import.name + "\" not found in \"" + import.module + "\"."};
}

result.globals.emplace_back(*global);
}
}

return {result, ""};
return {std::move(result), ""};
}

void pass()
Expand Down Expand Up @@ -661,6 +690,8 @@ class test_runner

test_settings m_settings;
std::unordered_map<std::string, std::unique_ptr<fizzy::Instance>> m_instances;
std::unordered_map<std::string, std::vector<fizzy::ExportedFunction>>
m_instances_external_functions;
std::unordered_map<std::string, std::string> m_registered_names;
std::string m_last_module_name;
test_results m_results;
Expand Down
Loading