Skip to content

Commit 6a57b58

Browse files
authored
Enable onForeignFunction and proxy-specific extensions to the ABI. (proxy-wasm#24)
* Enable onForeignFunction and proxy-specific extensions to the ABI. Signed-off-by: John Plevyak <[email protected]>
1 parent 37ed0ed commit 6a57b58

9 files changed

+24
-12
lines changed

BUILD

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ package(default_visibility = ["//visibility:public"])
44

55
cc_library(
66
name = "api_lib",
7-
hdrs = ["proxy_wasm_api.h"],
7+
hdrs = [
8+
"proxy_wasm_api.h",
9+
"proxy_wasm_externs.h",
10+
],
811
)
912

1013
cc_library(

proxy_wasm_api.h

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Intrinsic high-level support functions available to WASM modules.
2020
*/
2121
// NOLINT(namespace-envoy)
22+
#pragma once
23+
2224
#include <functional>
2325
#include <string>
2426
#include <tuple>
@@ -283,6 +285,8 @@ class ContextBase {
283285
// Called to indicate that no more calls will come and this context is being
284286
// deleted.
285287
virtual void onDelete() {} // Called when the stream or VM is being deleted.
288+
// Called when a foreign function event arrives.
289+
virtual void onForeignFunction(uint32_t /* foreign_function_id */, uint32_t /* data_size */) {}
286290

287291
using HttpCallCallback =
288292
std::function<void(uint32_t, size_t, uint32_t)>; // headers, body_size, trailers
@@ -477,6 +481,8 @@ class Context : public ContextBase {
477481
// Returns nullptr if the Context no longer exists (i.e. the stream has been
478482
// destroyed).
479483
Context *getContext(uint32_t context_id);
484+
RootContext *getRootContext(uint32_t context_id);
485+
ContextBase *getContextBase(uint32_t context_id);
480486

481487
using RootFactory = std::function<std::unique_ptr<RootContext>(uint32_t id, StringView root_id)>;
482488
using ContextFactory = std::function<std::unique_ptr<Context>(uint32_t id, RootContext *root)>;

proxy_wasm_common.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* Common enumerations available to WASM modules and shared with sandbox.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
2423

2524
#include <string>
@@ -95,7 +94,8 @@ enum class WasmBufferType : int32_t {
9594
GrpcReceiveBuffer = 5, // Immutable
9695
VmConfiguration = 6, // Immutable
9796
PluginConfiguration = 7, // Immutable
98-
MAX = 7,
97+
CallData = 8, // Immutable
98+
MAX = 8,
9999
};
100100
enum class WasmBufferFlags : int32_t {
101101
// These must be powers of 2.

proxy_wasm_enums.h

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* Intrinsic enumerations available to WASM modules.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
2423

2524
enum class LogLevel : int32_t { trace, debug, info, warn, error, critical, Max = critical };

proxy_wasm_externs.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* Proxy-WASM ABI.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
2423

2524
#include <stddef.h>
@@ -39,7 +38,7 @@ extern "C" WasmResult proxy_get_status(uint32_t *status_code_ptr, const char **m
3938
// Logging
4039
extern "C" WasmResult proxy_log(LogLevel level, const char *logMessage, size_t messageSize);
4140

42-
// Timer (must be called from a root context, e.g. onStart, onTick).
41+
// Timer (will be set for the root context, e.g. onStart, onTick).
4342
extern "C" WasmResult proxy_set_tick_period_milliseconds(uint32_t millisecond);
4443

4544
// Time
@@ -159,6 +158,8 @@ extern "C" uint32_t proxy_validate_configuration(uint32_t root_context_id,
159158
uint32_t configuration_size);
160159
extern "C" uint32_t proxy_on_configure(uint32_t root_context_id, uint32_t configuration_size);
161160
extern "C" void proxy_on_tick(uint32_t root_context_id);
161+
extern "C" void proxy_on_foreign_function(uint32_t root_context_id, uint32_t function_id,
162+
uint32_t data_size);
162163
extern "C" void proxy_on_queue_ready(uint32_t root_context_id, uint32_t token);
163164

164165
// Stream calls.

proxy_wasm_intrinsics.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static RootContext *ensureRootContext(uint32_t context_id) {
9090
return root_context;
9191
}
9292

93-
static ContextBase *getContextBase(uint32_t context_id) {
93+
ContextBase *getContextBase(uint32_t context_id) {
9494
auto it = context_map.find(context_id);
9595
if (it == context_map.end()) {
9696
return nullptr;
@@ -106,7 +106,7 @@ Context *getContext(uint32_t context_id) {
106106
return it->second->asContext();
107107
}
108108

109-
static RootContext *getRootContext(uint32_t context_id) {
109+
RootContext *getRootContext(uint32_t context_id) {
110110
auto it = context_map.find(context_id);
111111
if (it == context_map.end() || !it->second->asRoot()) {
112112
return nullptr;
@@ -266,3 +266,8 @@ extern "C" PROXY_WASM_KEEPALIVE void proxy_on_grpc_close(uint32_t context_id, ui
266266
extern "C" PROXY_WASM_KEEPALIVE void proxy_on_queue_ready(uint32_t context_id, uint32_t token) {
267267
getRootContext(context_id)->onQueueReady(token);
268268
}
269+
270+
extern "C" PROXY_WASM_KEEPALIVE void
271+
proxy_on_foreign_function(uint32_t context_id, uint32_t foreign_function_id, uint32_t data_size) {
272+
getContextBase(context_id)->onForeignFunction(foreign_function_id, data_size);
273+
}

proxy_wasm_intrinsics.h

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* API Available to WASM modules.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
2423

2524
#ifndef PROXY_WASM_KEEPALIVE
@@ -38,7 +37,6 @@ template <typename T> using Optional = std::optional<T>;
3837

3938
#include "proxy_wasm_enums.h"
4039
#include "proxy_wasm_common.h"
41-
#include "proxy_wasm_enums.h"
4240
#include "proxy_wasm_externs.h"
4341
#ifdef PROXY_WASM_PROTOBUF_FULL
4442
#define PROXY_WASM_PROTOBUF 1

proxy_wasm_intrinsics_full.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* API Available to WASM modules.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
23+
2424
#define PROXY_WASM_PROTOBUF_FULL 1
2525
#include "proxy_wasm_intrinsics.h"

proxy_wasm_intrinsics_lite.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* API Available to WASM modules.
2020
*/
2121
// NOLINT(namespace-envoy)
22-
2322
#pragma once
23+
2424
#define PROXY_WASM_PROTOBUF_LITE 1
2525
#include "proxy_wasm_intrinsics.h"

0 commit comments

Comments
 (0)