-
Notifications
You must be signed in to change notification settings - Fork 39
Introduce ExecutionContext instead of depth param #766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b3c94e
Add ExecutionContext type
chfast 4bb2817
Change host function to receive ExecutionContext&
chfast e2af5e9
Require ExecutionContext in execute()
chfast 60ee89e
test: Drop depth param in execute(Module, ...)
chfast 6e8a0a5
capi: Allocate new execution context if needed
chfast 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -85,6 +85,16 @@ inline fizzy::Value* unwrap(FizzyValue* value) noexcept | |
return reinterpret_cast<fizzy::Value*>(value); | ||
} | ||
|
||
inline FizzyExecutionContext* wrap(fizzy::ExecutionContext& ctx) noexcept | ||
{ | ||
return reinterpret_cast<FizzyExecutionContext*>(&ctx); | ||
} | ||
|
||
inline fizzy::ExecutionContext& unwrap(FizzyExecutionContext* ctx) noexcept | ||
{ | ||
return *reinterpret_cast<fizzy::ExecutionContext*>(ctx); | ||
} | ||
|
||
inline FizzyInstance* wrap(fizzy::Instance* instance) noexcept | ||
{ | ||
return reinterpret_cast<FizzyInstance*>(instance); | ||
|
@@ -113,29 +123,36 @@ inline fizzy::ExecutionResult unwrap(const FizzyExecutionResult& result) noexcep | |
inline fizzy::ExecuteFunction unwrap(FizzyExternalFn c_function, void* c_host_context) | ||
{ | ||
static constexpr fizzy::HostFunctionPtr function = | ||
[](std::any& host_context, fizzy::Instance& instance, const fizzy::Value* args, | ||
int depth) noexcept { | ||
[](std::any& host_ctx, fizzy::Instance& instance, const fizzy::Value* args, | ||
fizzy::ExecutionContext& ctx) noexcept { | ||
const auto [c_func, c_host_ctx] = | ||
*std::any_cast<std::pair<FizzyExternalFn, void*>>(&host_context); | ||
return unwrap(c_func(c_host_ctx, wrap(&instance), wrap(args), depth)); | ||
*std::any_cast<std::pair<FizzyExternalFn, void*>>(&host_ctx); | ||
return unwrap(c_func(c_host_ctx, wrap(&instance), wrap(args), wrap(ctx))); | ||
}; | ||
|
||
return {function, std::make_any<std::pair<FizzyExternalFn, void*>>(c_function, c_host_context)}; | ||
} | ||
|
||
inline FizzyExternalFunction wrap(fizzy::ExternalFunction external_func) | ||
{ | ||
static constexpr FizzyExternalFn c_function = [](void* context, FizzyInstance* instance, | ||
const FizzyValue* args, | ||
int depth) -> FizzyExecutionResult { | ||
auto* func = static_cast<fizzy::ExternalFunction*>(context); | ||
return wrap((func->function)(*unwrap(instance), unwrap(args), depth)); | ||
static constexpr FizzyExternalFn c_function = | ||
[](void* host_ctx, FizzyInstance* instance, const FizzyValue* args, | ||
FizzyExecutionContext* c_ctx) -> FizzyExecutionResult { | ||
// If execution context not provided, allocate new one. | ||
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. is not provided` or could just say "Allocate new execution context if none provided." |
||
// It must be allocated on heap otherwise the stack will explode in recursive calls. | ||
std::unique_ptr<fizzy::ExecutionContext> new_ctx; | ||
if (c_ctx == nullptr) | ||
new_ctx = std::make_unique<fizzy::ExecutionContext>(); | ||
auto& ctx = new_ctx ? *new_ctx : unwrap(c_ctx); | ||
|
||
auto* func = static_cast<fizzy::ExternalFunction*>(host_ctx); | ||
return wrap((func->function)(*unwrap(instance), unwrap(args), ctx)); | ||
}; | ||
|
||
auto context = std::make_unique<fizzy::ExternalFunction>(std::move(external_func)); | ||
const auto c_type = wrap(context->input_types, context->output_types); | ||
void* c_context = context.release(); | ||
return {c_type, c_function, c_context}; | ||
auto host_ctx = std::make_unique<fizzy::ExternalFunction>(std::move(external_func)); | ||
const auto c_type = wrap(host_ctx->input_types, host_ctx->output_types); | ||
void* c_host_ctx = host_ctx.release(); | ||
return {c_type, c_function, c_host_ctx}; | ||
} | ||
|
||
inline fizzy::ExternalFunction unwrap(const FizzyExternalFunction& external_func) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.