-
Notifications
You must be signed in to change notification settings - Fork 1
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
Host API implementation and runtime updates #81
Open
mariopil
wants to merge
11
commits into
master
Choose a base branch
from
#79_host_api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f9f3d43
Added custom ExternalInterface class and host API class.
mariopil ce90844
Changed binaryen version to 1.38.28.
mariopil a779a03
Moved back to binaryen version 100. Added all host API methods needed…
mariopil 1116adf
Added Version struct, updated executor, other updates and fixes.
mariopil 9729177
Logging fixes and small updates
mariopil 2565ef5
Added unit tests, more small udpates.
mariopil f837912
Added memory allocator test.
mariopil 1ed77ee
Merge branch 'master' into #79_host_api
mariopil 6c6824d
Code refactor.
mariopil c4c722b
Fixes
mariopil cff7137
Review fixes.
mariopil 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
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 |
---|---|---|
|
@@ -51,4 +51,3 @@ index 8c1d72085..799e14c58 100644 | |
#ifndef NDEBUG | ||
-- | ||
2.32.1 (Apple Git-133) | ||
|
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "host/api.h" | ||
|
||
#include <wasm-interpreter.h> | ||
#include "runtime/ptr.h" | ||
|
||
namespace plc::core::host { | ||
|
||
Api::Api() { | ||
m_wasm_log_level_map[soralog::Level::ERROR] = 0; | ||
m_wasm_log_level_map[soralog::Level::WARN] = 1; | ||
m_wasm_log_level_map[soralog::Level::INFO] = 2; | ||
m_wasm_log_level_map[soralog::Level::VERBOSE] = 3; | ||
m_wasm_log_level_map[soralog::Level::DEBUG] = 4; | ||
m_wasm_log_level_map[soralog::Level::TRACE] = 5; | ||
} | ||
|
||
wasm::Literals Api::ext_logging_max_level_version_1(const wasm::LiteralList& arguments) { | ||
ensureArgumentsSize(arguments.size(), 0); | ||
return wasm::Literals({wasm::Literal(m_wasm_log_level_map[m_log->level()])}); | ||
} | ||
|
||
wasm::Literals Api::ext_logging_log_version_1(const wasm::LiteralList& arguments) { | ||
ensureArgumentsSize(arguments.size(), 3); | ||
ensureArgumentsType(arguments, {wasm::Type::i32, wasm::Type::i64, wasm::Type::i64}); | ||
|
||
auto level = arguments[0].geti32(); | ||
plc::core::runtime::Ptr target(arguments[1].geti64()); | ||
plc::core::runtime::Ptr message(arguments[2].geti64()); | ||
|
||
auto it = std::find_if(m_wasm_log_level_map.begin(), m_wasm_log_level_map.end(), [level](auto kv){ | ||
return level == kv.second; | ||
}); | ||
|
||
auto soralog_level = soralog::Level::OFF; | ||
if (it != m_wasm_log_level_map.end()) { | ||
soralog_level = it->first; | ||
} | ||
auto targetStr = m_memory->loadString(target); | ||
auto messageStr = m_memory->loadString(message); | ||
|
||
m_log->log(soralog_level, "{}: {}", targetStr, messageStr); | ||
|
||
return wasm::Literals(); | ||
} | ||
|
||
wasm::Literals Api::ext_allocator_malloc_version_1(const wasm::LiteralList& arguments) { | ||
ensureArgumentsSize(arguments.size(), 1); | ||
ensureArgumentsType(arguments, {wasm::Type::i32}); | ||
|
||
auto size = arguments[0].geti32(); | ||
|
||
return wasm::Literals({wasm::Literal(m_memory->allocate(size))}); | ||
} | ||
|
||
wasm::Literals Api::ext_allocator_free_version_1(const wasm::LiteralList& arguments) { | ||
ensureArgumentsSize(arguments.size(), 1); | ||
ensureArgumentsType(arguments, {wasm::Type::i32}); | ||
|
||
auto ptr = arguments[0].geti32(); | ||
|
||
auto result = m_memory->deallocate(ptr); | ||
if (!result) { | ||
m_log->warn("Ptr {} does not point to any memory chunk in wasm memory. Nothing deallocated"); | ||
} | ||
return wasm::Literals(); | ||
} | ||
|
||
} //namespace plc::core::host |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
|
||
#include <boost/assert.hpp> | ||
#include <libp2p/log/logger.hpp> | ||
#include <literal.h> | ||
|
||
#include "runtime/memory.h" | ||
|
||
namespace wasm { | ||
typedef std::vector<Literal> LiteralList; | ||
} | ||
|
||
namespace plc::core::host { | ||
|
||
class Api final { | ||
public: | ||
Api(); | ||
|
||
void init(std::shared_ptr<plc::core::runtime::Memory> memory) { | ||
m_memory = memory; | ||
} | ||
|
||
wasm::Literals ext_logging_max_level_version_1(const wasm::LiteralList& arguments); | ||
wasm::Literals ext_logging_log_version_1(const wasm::LiteralList& arguments); | ||
wasm::Literals ext_allocator_malloc_version_1(const wasm::LiteralList& arguments); | ||
wasm::Literals ext_allocator_free_version_1(const wasm::LiteralList& arguments); | ||
|
||
private: | ||
void ensureArgumentsSize(int argsSize, int size) { | ||
BOOST_ASSERT(argsSize == size); | ||
} | ||
void ensureArgumentsType(const wasm::LiteralList& arguments, const std::vector<wasm::Type> &types) { | ||
for (auto i = 0; i < arguments.size(); ++i) { | ||
BOOST_ASSERT(arguments[i].type == types[i]); | ||
} | ||
} | ||
std::shared_ptr<plc::core::runtime::Memory> m_memory; | ||
std::map<soralog::Level, int> m_wasm_log_level_map; | ||
|
||
libp2p::log::Logger m_log = libp2p::log::createLogger("host::Api", "host"); | ||
}; | ||
|
||
} //namespace plc::core::host |
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#include "api.h" | ||
|
||
namespace plc::core::runtime { | ||
Result<Ptr> Api::coreVersion() { | ||
return m_executor->call("Core_version"); | ||
Result<Version> Api::coreVersion() { | ||
return m_executor->call<Version>("Core_version"); | ||
} | ||
} //namespace plc::core::runtime |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "runtime/executor.h" | ||
|
||
OUTCOME_CPP_DEFINE_CATEGORY(plc::core::runtime, Executor::Error, e) { | ||
using E = plc::core::runtime::Executor::Error; | ||
switch (e) { | ||
case E::MissingReturnValue: | ||
return "Missing return value from runtime API call!"; | ||
} | ||
return "Unknown error"; | ||
} |
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
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.
m_wasm_log_level_map looks like something constant (the map is initialized with const values in constructor in api.cpp file) - it's only searched and retrieved values.
So it could be at least const (or even maybe constexpr).
Well, it could be thought about using unordered_map as it would speed up the looking log level in ext_logging_max_level_version_1 method and possibility of element collision is none. Ordering seems also to not matter.