Skip to content

Commit 860f36a

Browse files
authored
refactor: decouple foreign functions from WasmBase. (#190)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 132c304 commit 860f36a

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

include/proxy-wasm/exports.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ ::proxy_wasm::ContextBase *contextOrEffectiveContext();
3030

3131
extern thread_local ContextBase *current_context_;
3232

33+
/**
34+
* WasmForeignFunction is used for registering host-specific host functions.
35+
* A foreign function can be registered via RegisterForeignFunction and available
36+
* to Wasm modules via proxy_call_foreign_function.
37+
* @param wasm is the WasmBase which the Wasm module is running on.
38+
* @param argument is the view to the argument to the function passed by the module.
39+
* @param alloc_result is used to allocate the result data of this foreign function.
40+
*/
41+
using WasmForeignFunction = std::function<WasmResult(
42+
WasmBase &wasm, std::string_view argument, std::function<void *(size_t size)> alloc_result)>;
43+
44+
/**
45+
* Used to get the foreign function registered via RegisterForeignFunction for a given name.
46+
* @param function_name is the name used to lookup the foreign function table.
47+
* @return a WasmForeignFunction if registered.
48+
*/
49+
WasmForeignFunction getForeignFunction(std::string_view function_name);
50+
51+
/**
52+
* RegisterForeignFunction is used to register a foreign function in the lookup table
53+
* used internally in getForeignFunction.
54+
*/
55+
struct RegisterForeignFunction {
56+
/**
57+
* @param function_name is the key for this foreign function.
58+
* @param f is the function instance.
59+
*/
60+
RegisterForeignFunction(std::string function_name, WasmForeignFunction f);
61+
};
62+
3363
namespace exports {
3464

3565
template <typename Pairs> size_t pairsSize(const Pairs &result) {

include/proxy-wasm/wasm.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ namespace proxy_wasm {
3434
#include "proxy_wasm_common.h"
3535

3636
class ContextBase;
37-
class WasmBase;
3837
class WasmHandleBase;
3938

40-
using WasmForeignFunction =
41-
std::function<WasmResult(WasmBase &, std::string_view, std::function<void *(size_t size)>)>;
4239
using WasmVmFactory = std::function<std::unique_ptr<WasmVm>()>;
4340
using CallOnThreadFunction = std::function<void(std::function<void()>)>;
4441

@@ -129,8 +126,6 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
129126
bool copyToPointerSize(std::string_view s, uint64_t ptr_ptr, uint64_t size_ptr);
130127
template <typename T> bool setDatatype(uint64_t ptr, const T &t);
131128

132-
WasmForeignFunction getForeignFunction(std::string_view function_name);
133-
134129
void fail(FailState fail_state, std::string_view message) {
135130
error(message);
136131
failed_ = fail_state;
@@ -439,8 +434,4 @@ template <typename T> inline bool WasmBase::setDatatype(uint64_t ptr, const T &t
439434
return wasm_vm_->setMemory(ptr, sizeof(T), &t);
440435
}
441436

442-
struct RegisterForeignFunction {
443-
RegisterForeignFunction(std::string name, WasmForeignFunction f);
444-
};
445-
446437
} // namespace proxy_wasm

src/exports.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ ContextBase *contextOrEffectiveContext() {
3636
// of current_context_.
3737
extern thread_local uint32_t effective_context_id_;
3838

39+
std::unordered_map<std::string, WasmForeignFunction> &foreignFunctions() {
40+
static auto ptr = new std::unordered_map<std::string, WasmForeignFunction>;
41+
return *ptr;
42+
}
43+
44+
WasmForeignFunction getForeignFunction(std::string_view function_name) {
45+
auto foreign_functions = foreignFunctions();
46+
auto it = foreign_functions.find(std::string(function_name));
47+
if (it != foreign_functions.end()) {
48+
return it->second;
49+
}
50+
return nullptr;
51+
}
52+
53+
RegisterForeignFunction::RegisterForeignFunction(std::string name, WasmForeignFunction f) {
54+
foreignFunctions()[name] = f;
55+
}
56+
3957
namespace exports {
4058

4159
namespace {
@@ -220,7 +238,7 @@ Word call_foreign_function(Word function_name, Word function_name_size, Word arg
220238
if (!args_opt) {
221239
return WasmResult::InvalidMemoryAccess;
222240
}
223-
auto f = context->wasm()->getForeignFunction(function.value());
241+
auto f = getForeignFunction(function.value());
224242
if (!f) {
225243
return WasmResult::NotFound;
226244
}

src/wasm.cc

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ thread_local std::unordered_map<std::string, std::weak_ptr<PluginHandleBase>> lo
4545
// Map from Wasm Key to the base Wasm instance, using a pointer to avoid the initialization fiasco.
4646
std::mutex base_wasms_mutex;
4747
std::unordered_map<std::string, std::weak_ptr<WasmHandleBase>> *base_wasms = nullptr;
48-
std::unordered_map<std::string, WasmForeignFunction> *foreign_functions = nullptr;
4948

5049
std::vector<uint8_t> Sha256(const std::vector<std::string_view> parts) {
5150
uint8_t sha256[SHA256_DIGEST_LENGTH];
@@ -85,13 +84,6 @@ class WasmBase::ShutdownHandle {
8584
std::shared_ptr<WasmBase> wasm_;
8685
};
8786

88-
RegisterForeignFunction::RegisterForeignFunction(std::string name, WasmForeignFunction f) {
89-
if (!foreign_functions) {
90-
foreign_functions = new std::remove_reference<decltype(*foreign_functions)>::type;
91-
}
92-
(*foreign_functions)[name] = f;
93-
}
94-
9587
void WasmBase::registerCallbacks() {
9688
#define _REGISTER(_fn) \
9789
wasm_vm_->registerCallback( \
@@ -454,14 +446,6 @@ void WasmBase::finishShutdown() {
454446
}
455447
}
456448

457-
WasmForeignFunction WasmBase::getForeignFunction(std::string_view function_name) {
458-
auto it = foreign_functions->find(std::string(function_name));
459-
if (it != foreign_functions->end()) {
460-
return it->second;
461-
}
462-
return nullptr;
463-
}
464-
465449
std::shared_ptr<WasmHandleBase> createWasm(std::string vm_key, std::string code,
466450
std::shared_ptr<PluginBase> plugin,
467451
WasmHandleFactory factory,

0 commit comments

Comments
 (0)