-
Notifications
You must be signed in to change notification settings - Fork 74
Enable onForeignFunction and proxy-specific extensions to the ABI. #23
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
Changes from all commits
938c41d
55cb53d
5220cf0
8cc8602
303a5c0
2d1ea29
bd68788
83b3009
ddeac98
f8fab0f
2523cfb
e41ca7c
38cda11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,46 @@ struct PluginBase { | |
std::string log_prefix_; | ||
}; | ||
|
||
struct BufferBase : public BufferInterface { | ||
BufferBase() = default; | ||
~BufferBase() override = default; | ||
|
||
// BufferInterface | ||
size_t size() const override { | ||
if (owned_data_) { | ||
return owned_data_size_; | ||
} | ||
return data_.size(); | ||
} | ||
WasmResult copyTo(WasmBase *wasm, size_t start, size_t length, uint64_t ptr_ptr, | ||
uint64_t size_ptr) const override; | ||
WasmResult copyFrom(size_t /* start */, size_t /* length */, string_view /* data */) override { | ||
// Setting a string buffer not supported (no use case). | ||
return WasmResult::BadArgument; | ||
} | ||
|
||
virtual void clear() { | ||
data_ = ""; | ||
owned_data_ = nullptr; | ||
} | ||
BufferBase *set(string_view data) { | ||
clear(); | ||
data_ = data; | ||
return this; | ||
} | ||
BufferBase *set(std::unique_ptr<char[]> owned_data, uint32_t owned_data_size) { | ||
clear(); | ||
owned_data_ = std::move(owned_data); | ||
owned_data_size_ = owned_data_size; | ||
return this; | ||
} | ||
|
||
protected: | ||
string_view data_; | ||
std::unique_ptr<char[]> owned_data_; | ||
uint32_t owned_data_size_; | ||
}; | ||
|
||
/** | ||
* ContextBase is the interface between the VM host and the VM. It has several uses: | ||
* | ||
|
@@ -94,7 +134,7 @@ class ContextBase : public RootInterface, | |
ContextBase(); // Testing. | ||
ContextBase(WasmBase *wasm); // Vm Context. | ||
ContextBase(WasmBase *wasm, std::shared_ptr<PluginBase> plugin); // Root Context. | ||
ContextBase(WasmBase *wasm, uint32_t root_context_id, | ||
ContextBase(WasmBase *wasm, uint32_t parent_context_id, | ||
std::shared_ptr<PluginBase> plugin); // Stream context. | ||
virtual ~ContextBase(); | ||
|
||
|
@@ -103,8 +143,17 @@ class ContextBase : public RootInterface, | |
// The VM Context used for calling "malloc" has an id_ == 0. | ||
bool isVmContext() const { return id_ == 0; } | ||
// Root Contexts have the VM Context as a parent. | ||
bool isRootContext() const { return root_context_id_ == 0; } | ||
ContextBase *root_context() const { return root_context_; } | ||
bool isRootContext() const { return parent_context_id_ == 0; } | ||
ContextBase *parent_context() const { return parent_context_; } | ||
ContextBase *root_context() const { | ||
const ContextBase *previous = this; | ||
ContextBase *parent = parent_context_; | ||
while (parent != previous) { | ||
previous = parent; | ||
parent = parent->parent_context_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that will result in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parent_context_ is never nullptr. The list is terminated by parent_context_ == this. This is enforced by the constructors for Context. |
||
} | ||
return parent; | ||
} | ||
string_view root_id() const { return isRootContext() ? root_id_ : plugin_->root_id_; } | ||
string_view log_prefix() const { | ||
return isRootContext() ? root_log_prefix_ : plugin_->log_prefix(); | ||
|
@@ -121,10 +170,11 @@ class ContextBase : public RootInterface, | |
*/ | ||
|
||
// Context | ||
void onCreate(uint32_t parent_context_id) override; | ||
void onCreate() override; | ||
bool onDone() override; | ||
void onLog() override; | ||
void onDelete() override; | ||
void onForeignFunction(uint32_t foreign_function_id, uint32_t data_size) override; | ||
|
||
// Root | ||
bool onStart(std::shared_ptr<PluginBase> plugin) override; | ||
|
@@ -173,10 +223,6 @@ class ContextBase : public RootInterface, | |
WasmResult log(uint32_t /* level */, string_view /* message */) override { | ||
return unimplemented(); | ||
} | ||
WasmResult setTimerPeriod(std::chrono::milliseconds /* period */, | ||
uint32_t * /* timer_token_ptr */) override { | ||
return unimplemented(); | ||
} | ||
uint64_t getCurrentTimeNanoseconds() override { | ||
struct timespec tpe; | ||
clock_gettime(CLOCK_REALTIME, &tpe); | ||
|
@@ -189,6 +235,7 @@ class ContextBase : public RootInterface, | |
unimplemented(); | ||
return std::make_pair(1, "unimplmemented"); | ||
} | ||
WasmResult setTimerPeriod(std::chrono::milliseconds period, uint32_t *timer_token_ptr) override; | ||
|
||
// Buffer | ||
BufferInterface *getBuffer(WasmBufferType /* type */) override { | ||
|
@@ -316,15 +363,24 @@ class ContextBase : public RootInterface, | |
|
||
WasmBase *wasm_{nullptr}; | ||
uint32_t id_{0}; | ||
uint32_t root_context_id_{0}; // 0 for roots and the general context. | ||
ContextBase *root_context_{nullptr}; // set in all contexts. | ||
std::string root_id_; // set only in root context. | ||
std::string root_log_prefix_; // set only in root context. | ||
uint32_t parent_context_id_{0}; // 0 for roots and the general context. | ||
ContextBase *parent_context_{nullptr}; // set in all contexts. | ||
std::string root_id_; // set only in root context. | ||
std::string root_log_prefix_; // set only in root context. | ||
std::shared_ptr<PluginBase> plugin_; | ||
bool in_vm_context_created_ = false; | ||
bool destroyed_ = false; | ||
}; | ||
|
||
class DeferAfterCallActions { | ||
public: | ||
DeferAfterCallActions(ContextBase *context) : wasm_(context->wasm()) {} | ||
~DeferAfterCallActions(); | ||
|
||
private: | ||
WasmBase *const wasm_; | ||
}; | ||
|
||
uint32_t resolveQueueForTest(string_view vm_id, string_view queue_name); | ||
|
||
} // namespace proxy_wasm |
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.
Now that this is independent from Envoy, we should probably stop using
string_view
to represent bytes, and usespan
instead.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'll open an issue for that. I presume that absl::Span is a suitable substitute. #28