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

API for resolving function imports #318

Merged
merged 4 commits into from
May 20, 2020
Merged

API for resolving function imports #318

merged 4 commits into from
May 20, 2020

Conversation

gumb0
Copy link
Collaborator

@gumb0 gumb0 commented May 18, 2020

No description provided.

@gumb0 gumb0 force-pushed the linking-api branch 2 times, most recently from 1f02f40 to fc6787d Compare May 18, 2020 17:21
@gumb0 gumb0 changed the title instantiate with imported functions referred by import name API for resolving function imports May 18, 2020
std::move(it->function), module.typesec[import.desc.function_type_index]});
}

if (external_functions.size() != imported_functions.size())
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this.

@axic
Copy link
Member

axic commented May 18, 2020

Can you add some unit tests?

@chfast
Copy link
Collaborator

chfast commented May 19, 2020

Maybe to so much functional, but I was imaging the instantiation workflow to look something like:

auto module = parse(...);
auto builder = InstanceBuilder{module};
builder.add_imported_function("host", "name", func_ptr);
builder.add_imported_function("host", "xxx", func_ptr_xxx);
auto instance = builder.instantiate();

@axic
Copy link
Member

axic commented May 19, 2020

I think that is an option too, but shouldn't we design that as part of the API discussion topic?

Here my goal would be something quick we can add to test the overhead of host calls.

@gumb0
Copy link
Collaborator Author

gumb0 commented May 19, 2020

Maybe to so much functional, but I was imaging the instantiation workflow to look something like:

auto module = parse(...);
auto builder = InstanceBuilder{module};
builder.add_imported_function("host", "name", func_ptr);
builder.add_imported_function("host", "xxx", func_ptr_xxx);
auto instance = builder.instantiate();

Looks good for C++ API, but maybe this should be a part of higher-level C++ bindings... I'd leave it simpler in this PR.

@gumb0 gumb0 force-pushed the linking-api branch 4 times, most recently from 9a23f3c to 297bcb3 Compare May 19, 2020 15:30
@@ -100,6 +100,20 @@ execution_result execute(
// TODO: remove this helper
execution_result execute(const Module& module, FuncIdx func_idx, std::vector<uint64_t> args);

// Function that should be used by instantiate as imports, identified by module and function name.
struct ImportedFunction
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not very happy with the name, maybe NamedFunctionImport or NamedFunction?

@gumb0 gumb0 marked this pull request as ready for review May 19, 2020 15:31
@gumb0 gumb0 requested review from chfast and axic May 19, 2020 15:34

if (it == imported_functions.end())
{
throw instantiate_error(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Decided it's not worth making separate resolve_error

@axic
Copy link
Member

axic commented May 19, 2020

Changelog entry perhaps?

{
std::string module;
std::string name;
std::function<execution_result(Instance&, std::vector<uint64_t>, int depth)> 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 think ImportedFunction should have two more fields:

  • std::vector<ValType> inputs;
  • std::optional<ValType> result; (where empty means void)

And check that against the type section. Abort on mismatch.

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 can be then

struct NamedExternalFunction
{
    std::string module;
    std::string name;
    ExternalFunction 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.

Yeah I guess it could, but that's quite a few different structs to address when declaring these. (From a user perspective.)

Copy link
Member

Choose a reason for hiding this comment

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

Alternative is to use a string (similar to what wasm3 and emscripten is doing): parameters followed by colon followed by return types.

e.g. ii:v would mean it takes two 32-bit inputs and returns void.

The same in wasm3 is v(ii) and iiv in emscripten.

{"mod1", "foo1", {}, ValType::i32, function_returning_value(0)},
{"mod1", "foo2", {ValType::i32}, ValType::i32, function_returning_value(1)},
{"mod2", "foo1", {ValType::i32}, ValType::i32, function_returning_value(2)},
{"mod2", "foo2", {ValType::i64}, std::nullopt, function_returning_void},
Copy link
Member

Choose a reason for hiding this comment

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

Can you add one test case somewhere which takes 2 inputs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made this void function take 2 parameters

@axic
Copy link
Member

axic commented May 20, 2020

Looks good to me, but as discussed in chat lets do the changelog in a single PR at the release time instead, so please remove the commit :)

@gumb0
Copy link
Collaborator Author

gumb0 commented May 20, 2020

Looks good to me, but as discussed in chat lets do the changelog in a single PR at the release time instead, so please remove the commit :)

Looks good to me, but as discussed in chat lets do the changelog in a single PR at the release time instead, so please remove the commit :)

@chfast though wanted to have it in each PR, as I understood.

@gumb0 gumb0 merged commit 94320a4 into master May 20, 2020
@gumb0 gumb0 deleted the linking-api branch May 20, 2020 16:26
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