-
Notifications
You must be signed in to change notification settings - Fork 34
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: find_exported_function #633
Conversation
4fcf1d4
to
218da78
Compare
I think this would also require a benchmark to see if the overhead changed. |
5203da1
to
d045bf1
Compare
Well the old option to access exported function via index stays, and |
Codecov Report
@@ Coverage Diff @@
## master #633 +/- ##
=======================================
Coverage 98.37% 98.38%
=======================================
Files 69 69
Lines 9682 9712 +30
=======================================
+ Hits 9525 9555 +30
Misses 157 157 |
4ab7c9d
to
9e0a584
Compare
f09f1f4
to
0036adc
Compare
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.
Seems ok, but there is always a question if this can be done without allocating new object on heap.
c609c6a
to
6849511
Compare
6849511
to
2f4b021
Compare
include/fizzy/fizzy.h
Outdated
/// @param instance Pointer to instance. | ||
/// @param name The function name. NULL-terminated string. Cannot be NULL. | ||
/// @param out_function Pointer to output struct to store the found function. Cannot be NULL. | ||
/// @param out_context_ptr Pointer to opaque output pointer to a context associated with this |
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.
Should also mention that in the case of failure (returns false
), what will be the value of this (whether undefined or set to null) and whether that needs to be freed, or not.
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.
Added a sentence.
I think only if it were allocated on the heap already on C++ side. Or if we made some other, yet more complicated change in C++ to get rid of |
2f4b021
to
571ed0b
Compare
be82911
to
97ebaca
Compare
include/fizzy/fizzy.h
Outdated
/// | ||
/// @note This function must be called only with external functions returned from | ||
/// fizzy_find_exported_function. | ||
void fizzy_free_exported_function(const FizzyExternalFunction* external_function); |
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 wonder if we should actually make this work on a copy (fizzy_free_exported_function(FizzyExternalFunction)
), given the only thing we're freeing is a pointer in the struct.
From an API PoV I think the current one makes more sense, but it may not be fully intuitive for the user?
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.
Not sure, it's a function operating on a struct, so we pass it by const pointer to avoid unnecessaty copying, seems usual for C ?
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 mean the fact we don't obtain it as a pointer, but rather allocate it on the stack in the caller.
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.
Yeah I see, I'm not sure what's better.
Another option is to call it with _context
anyway: fizzy_free_exported_function_context(FizzyExternalFunction func)
- this might be less weird than passing an instance copy to a function that's supposed to "free function".
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.
According to comment #633 (comment) @chfast seems to prefer FizzyExternalFunction*
argument
include/fizzy/fizzy.h
Outdated
|
||
/// Free resources associated with exported function. | ||
/// | ||
/// If passed pointer is NULL, has no effect. |
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.
This is a bit confusing since we never return FizzyExternalFunction
as a pointer.
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.
Well we pass it by non-const pointer in fizzy_find_exported_function
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 understand that. This comment is connected to the one above.
lib/fizzy/capi.cpp
Outdated
@@ -120,6 +120,20 @@ inline auto unwrap(FizzyExternalFn func, void* context) noexcept | |||
}; | |||
} | |||
|
|||
inline FizzyExternalFunction wrap(fizzy::ExternalFunction external_func) | |||
{ | |||
FizzyExternalFn c_function = [](void* context, FizzyInstance* instance, const FizzyValue* args, |
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.
FizzyExternalFn c_function = [](void* context, FizzyInstance* instance, const FizzyValue* args, | |
static constexpr FizzyExternalFn c_function = [](void* context, FizzyInstance* instance, const FizzyValue* args, |
return wrap((func->function)(*unwrap(instance), unwrap(args), depth)); | ||
}; | ||
|
||
auto context = std::make_unique<fizzy::ExternalFunction>(std::move(external_func)); |
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.
This is not great, but it can stay until we remove usage of std::function
.
include/fizzy/fizzy.h
Outdated
/// | ||
/// @note This function must be called only with external functions returned from | ||
/// fizzy_find_exported_function. | ||
void fizzy_free_exported_function(const FizzyExternalFunction* external_function); |
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.
void fizzy_free_exported_function(const FizzyExternalFunction* external_function); | |
void fizzy_free_exported_function(FizzyExternalFunction* external_function); |
Although technically possible, this should be passed without const
because you are logically modifying the object.
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.
My motivation was to show exactly that the object itself is not modifed, to make it look a bit less weird when we call it with an instance on stack.
FizzyExternalFunction function;
...
fizzy_free_exported_function(&function);
But I don't insist, removed const
now.
97ebaca
to
3966d37
Compare
What is the status here? |
It's ready from my side. You suggested passing argument by value to |
I think neither of the options are good, but it is still better than not having it. Probably with the Rust implementation we'll be informed if we should revisit this. |
ASSERT_NE(function.function, nullptr); | ||
EXPECT_THAT(function.function(function.context, instance, nullptr, 0), CResult(42)); | ||
|
||
fizzy_free_exported_function(&function); |
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.
Since you state fizzy_free_exported_function(nullptr)
works, can you add it somewhere?
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.
It was a mistake, it's not allowed, will change the comment.
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.
Changed.
include/fizzy/fizzy.h
Outdated
@@ -275,6 +275,28 @@ uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance); | |||
/// @note Function returns memory size regardless of whether memory is exported or not. | |||
size_t fizzy_get_instance_memory_size(FizzyInstance* instance); | |||
|
|||
/// Find exported function by name. | |||
/// | |||
/// @param instance Pointer to instance. |
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.
/// @param instance Pointer to instance. | |
/// @param instance Pointer to instance. Cannot be NULL? |
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.
Added here and in other functions.
3966d37
to
216909e
Compare
I'm not completely sure it's a good idea to include this, because complications with context can be confusing, but the motivation is
capi.imported_function_from_anothe_module
test change.fizzy::find_exported_*
functions.