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

capi: find_exported_function #633

Merged
merged 3 commits into from
Nov 18, 2020
Merged

capi: find_exported_function #633

merged 3 commits into from
Nov 18, 2020

Conversation

gumb0
Copy link
Collaborator

@gumb0 gumb0 commented Nov 3, 2020

I'm not completely sure it's a good idea to include this, because complications with context can be confusing, but the motivation is

  1. to make it simpler to import exported function into another module - see capi.imported_function_from_anothe_module test change.
  2. symmetry with C++ API fizzy::find_exported_* functions.

@axic
Copy link
Member

axic commented Nov 3, 2020

I think this would also require a benchmark to see if the overhead changed.

@gumb0 gumb0 force-pushed the capi-find-exports branch 4 times, most recently from 5203da1 to d045bf1 Compare November 3, 2020 19:14
@gumb0
Copy link
Collaborator Author

gumb0 commented Nov 3, 2020

I think this would also require a benchmark to see if the overhead changed.

Well the old option to access exported function via index stays, and FizzyCEngine still uses it as before. So what kind of benchmark do you have in mind?

@codecov
Copy link

codecov bot commented Nov 3, 2020

Codecov Report

Merging #633 (216909e) into master (c044c54) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #633   +/-   ##
=======================================
  Coverage   98.37%   98.38%           
=======================================
  Files          69       69           
  Lines        9682     9712   +30     
=======================================
+ Hits         9525     9555   +30     
  Misses        157      157           

@gumb0 gumb0 force-pushed the capi-find-exports branch 5 times, most recently from 4ab7c9d to 9e0a584 Compare November 4, 2020 15:13
@gumb0 gumb0 marked this pull request as ready for review November 4, 2020 15:19
@gumb0 gumb0 force-pushed the capi-find-exports branch 3 times, most recently from f09f1f4 to 0036adc Compare November 4, 2020 17:28
@gumb0 gumb0 requested review from chfast and axic November 4, 2020 17:33
Copy link
Collaborator

@chfast chfast left a 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.

@axic axic force-pushed the capi-find-exports branch 2 times, most recently from c609c6a to 6849511 Compare November 6, 2020 21:58
include/fizzy/fizzy.h Outdated Show resolved Hide resolved
include/fizzy/fizzy.h Outdated Show resolved Hide resolved
/// @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
Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a sentence.

@gumb0
Copy link
Collaborator Author

gumb0 commented Nov 9, 2020

Seems ok, but there is always a question if this can be done without allocating new object on heap.

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 std::function.

@gumb0 gumb0 marked this pull request as draft November 9, 2020 11:53
@gumb0 gumb0 marked this pull request as ready for review November 9, 2020 14:11
@gumb0 gumb0 requested review from axic and chfast November 9, 2020 14:25
///
/// @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);
Copy link
Member

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?

Copy link
Collaborator Author

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 ?

Copy link
Member

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.

Copy link
Collaborator Author

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".

Copy link
Collaborator Author

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


/// Free resources associated with exported function.
///
/// If passed pointer is NULL, has no effect.
Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Member

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.

@@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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));
Copy link
Collaborator

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.

///
/// @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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Collaborator Author

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.

@axic
Copy link
Member

axic commented Nov 17, 2020

What is the status here?

@gumb0
Copy link
Collaborator Author

gumb0 commented Nov 17, 2020

What is the status here?

It's ready from my side.

You suggested passing argument by value to fizzy_free_exported_function, but I prefer for it to stay a pointer.

@axic
Copy link
Member

axic commented Nov 17, 2020

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);
Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// @param instance Pointer to instance.
/// @param instance Pointer to instance. Cannot be NULL?

Copy link
Collaborator Author

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.

@gumb0 gumb0 merged commit 0ae57d0 into master Nov 18, 2020
@gumb0 gumb0 deleted the capi-find-exports branch November 18, 2020 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants