-
Notifications
You must be signed in to change notification settings - Fork 38
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
capi: imported and exported tables #628
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,6 +168,42 @@ inline std::vector<fizzy::ImportedFunction> unwrap( | |
return imported_functions; | ||
} | ||
|
||
inline FizzyTable* wrap(fizzy::table_elements* table) noexcept | ||
{ | ||
return reinterpret_cast<FizzyTable*>(table); | ||
} | ||
|
||
inline fizzy::table_elements* unwrap(FizzyTable* table) noexcept | ||
{ | ||
return reinterpret_cast<fizzy::table_elements*>(table); | ||
} | ||
|
||
inline FizzyLimits wrap(const fizzy::Limits& limits) noexcept | ||
{ | ||
return {limits.min, (limits.max.has_value() ? *limits.max : 0), limits.max.has_value()}; | ||
} | ||
|
||
inline fizzy::Limits unwrap(const FizzyLimits& limits) noexcept | ||
{ | ||
return {limits.min, limits.has_max ? std::make_optional(limits.max) : std::nullopt}; | ||
} | ||
|
||
inline FizzyExternalTable wrap(const fizzy::ExternalTable& external_table) noexcept | ||
{ | ||
return {wrap(external_table.table), wrap(external_table.limits)}; | ||
} | ||
|
||
inline fizzy::ExternalTable unwrap(const FizzyExternalTable& external_table) noexcept | ||
{ | ||
return {unwrap(external_table.table), unwrap(external_table.limits)}; | ||
} | ||
|
||
inline std::vector<fizzy::ExternalTable> unwrap(const FizzyExternalTable* external_table) | ||
{ | ||
return external_table ? std::vector<fizzy::ExternalTable>{unwrap(*external_table)} : | ||
std::vector<fizzy::ExternalTable>{}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, |
||
} | ||
|
||
inline FizzyGlobalType wrap(const fizzy::GlobalType& global_type) noexcept | ||
{ | ||
return {wrap(global_type.value_type), global_type.is_mutable}; | ||
|
@@ -247,6 +283,17 @@ bool fizzy_find_exported_function( | |
return true; | ||
} | ||
|
||
bool fizzy_find_exported_table( | ||
FizzyInstance* instance, const char* name, FizzyExternalTable* out_table) | ||
{ | ||
const auto optional_external_table = fizzy::find_exported_table(*unwrap(instance), name); | ||
if (!optional_external_table) | ||
return false; | ||
|
||
*out_table = wrap(*optional_external_table); | ||
return true; | ||
} | ||
|
||
bool fizzy_find_exported_global( | ||
FizzyInstance* instance, const char* name, FizzyExternalGlobal* out_global) | ||
{ | ||
|
@@ -260,15 +307,17 @@ bool fizzy_find_exported_global( | |
|
||
FizzyInstance* fizzy_instantiate(const FizzyModule* module, | ||
const FizzyExternalFunction* imported_functions, size_t imported_functions_size, | ||
const FizzyExternalGlobal* imported_globals, size_t imported_globals_size) | ||
const FizzyExternalTable* imported_table, const FizzyExternalGlobal* imported_globals, | ||
size_t imported_globals_size) | ||
{ | ||
try | ||
{ | ||
auto functions = unwrap(imported_functions, imported_functions_size); | ||
auto table = unwrap(imported_table); | ||
auto globals = unwrap(imported_globals, imported_globals_size); | ||
|
||
auto instance = fizzy::instantiate(std::unique_ptr<const fizzy::Module>(unwrap(module)), | ||
std::move(functions), {}, {}, std::move(globals)); | ||
std::move(functions), std::move(table), {}, std::move(globals)); | ||
|
||
return wrap(instance.release()); | ||
} | ||
|
@@ -280,18 +329,20 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module, | |
|
||
FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module, | ||
const FizzyImportedFunction* c_imported_functions, size_t imported_functions_size, | ||
const FizzyExternalGlobal* imported_globals, size_t imported_globals_size) | ||
const FizzyExternalTable* imported_table, const FizzyExternalGlobal* imported_globals, | ||
size_t imported_globals_size) | ||
{ | ||
try | ||
{ | ||
auto imported_functions = unwrap(c_imported_functions, imported_functions_size); | ||
auto table = unwrap(imported_table); | ||
auto globals = unwrap(imported_globals, imported_globals_size); | ||
|
||
std::unique_ptr<const fizzy::Module> module{unwrap(c_module)}; | ||
auto resolved_imports = fizzy::resolve_imported_functions(*module, imported_functions); | ||
|
||
auto instance = fizzy::instantiate( | ||
std::move(module), std::move(resolved_imports), {}, {}, std::move(globals)); | ||
auto instance = fizzy::instantiate(std::move(module), std::move(resolved_imports), | ||
std::move(table), {}, std::move(globals)); | ||
|
||
return wrap(instance.release()); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to say why isn't this grouped with
find_exported_function
, but realised that is over the module. This still confuses me in our API, both C++ and in C.I think perhaps it would be clearer all these are instance related and later we could add some module inspection features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I put them separately, because this one operates on instance.
The change in #633 adds another
fizzy_find_exported_function
, that operates on instance and would be convenient, when you need to get an exported function and import it into another instance. With it there's going to be 4 similarfizzy_find_exported_*
as in C++ API.(However it has some complications with managing the lifetime of capture, so I'm not completely sure it's a good idea)
The function operating on module is renamed there to
fizzy_find_exported_function_index
, which better reflects what it does.