Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --cxxopt="-std=c++17"
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Language: Cpp
ColumnLimit: 100
AccessModifierOffset: -2
DerivePointerAlignment: false
PointerAlignment: Right
SortIncludes: false
...

---
Language: Proto
ColumnLimit: 100
AllowShortFunctionsOnASingleLine: false
ReflowComments: false
SpacesInContainerLiterals: false
...
90 changes: 58 additions & 32 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,76 @@ package(default_visibility = ["//visibility:public"])
cc_library(
name = "include",
hdrs = [
"include/proxy-wasm/context.h",
"include/proxy-wasm/compat.h",
"include/proxy-wasm/exports.h",
"include/proxy-wasm/null.h",
"include/proxy-wasm/null_plugin.h",
"include/proxy-wasm/null_vm.h",
"include/proxy-wasm/null_vm_plugin.h",
"include/proxy-wasm/v8.h",
"include/proxy-wasm/wasm.h",
"include/proxy-wasm/context.h",
"include/proxy-wasm/wasm_vm.h",
"include/proxy-wasm/wasm_api_impl.h",
"include/proxy-wasm/word.h",
],
copts = ["-std=c++14"],
deps = [
"@proxy_wasm_cpp_sdk//:common_lib",
],
)

# TODO: remove when dependent projects have been upgraded.
cc_library(
name = "lib",
srcs = [
"src/exports.cc",
"src/foreign.cc",
"src/context.cc",
"src/wasm.cc",
"src/base64.cc",
"src/base64.h",
"src/v8/v8.cc",
"src/null/null.cc",
"src/null/null_plugin.cc",
"src/null/null_vm.cc",
name = "include14",
hdrs = [
"include/proxy-wasm/compat.h",
"include/proxy-wasm/context.h",
"include/proxy-wasm/wasm_vm.h",
"include/proxy-wasm/word.h",
],
copts = ["-std=c++14"],
deps = [
":include",
"@proxy_wasm_cpp_sdk//:api_lib",
"@proxy_wasm_cpp_sdk//:common_lib",
"@envoy//external:abseil_flat_hash_map",
"@envoy//external:abseil_strings",
"@envoy//external:abseil_optional",
"@envoy//external:zlib",
"@boringssl//:ssl",
"@envoy//external:wee8",
"@envoy//external:protobuf",
],
)

cc_test(
name = "wasm_vm_test",
srcs = ["wasm_vm_test.cc"],
copts = ["-std=c++17"],
deps = [
":include",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "context_test",
srcs = ["context_test.cc"],
deps = [
":include",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

# TODO: remove when dependent projects have been upgraded.
cc_test(
name = "wasm_vm_14_test",
srcs = ["wasm_vm_test.cc"],
copts = ["-std=c++14"],
deps = [
":include14",
"@com_google_absl//absl/base",
"@com_google_absl//absl/strings:strings",
"@com_google_absl//absl/types:optional",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "context_14_test",
srcs = ["context_test.cc"],
copts = ["-std=c++14"],
deps = [
":include14",
"@com_google_absl//absl/base",
"@com_google_absl//absl/strings:strings",
"@com_google_absl//absl/types:optional",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
24 changes: 24 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
workspace(name = "proxy_wasm_cpp_host")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "proxy_wasm_cpp_sdk",
sha256 = "14f66f67e8f37ec81d28d7f5307be4407d206ac5f0daaf6d22fa5536797bcac1",
strip_prefix = "proxy-wasm-cpp-sdk-31f1fc5b7e09f231fa532d2d296e479a113c3e10",
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/archive/31f1fc5b7e09f231fa532d2d296e479a113c3e10.tar.gz"],
)

http_archive(
name = "com_google_absl",
sha256 = "19391fb4882601a65cb648d638c11aa301ce5f525ef02da1a9eafd22f72d7c59",
strip_prefix = "abseil-cpp-37dd2562ec830d547a1524bb306be313ac3f2556",
# 2020-01-29
urls = ["https://github.com/abseil/abseil-cpp/archive/37dd2562ec830d547a1524bb306be313ac3f2556.tar.gz"],
)

http_archive(
name = "com_google_googletest",
sha256 = "9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb",
strip_prefix = "googletest-release-1.10.0",
urls = ["https://github.com/google/googletest/archive/release-1.10.0.tar.gz"],
)
20 changes: 20 additions & 0 deletions context_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "include/proxy-wasm/context.h"

#include "gtest/gtest.h"

namespace proxy_wasm {
namespace {

class Context : public ContextBase {
public:
Context(std::function<void(string_view s)> error_function) : error_function_(error_function) {}
void error(string_view message) { error_function_(message); }

private:
std::function<void(string_view s)> error_function_;
};

TEST(Context, IncludeParses) {}

} // namespace
} // namespace proxy_wasm
21 changes: 13 additions & 8 deletions include/proxy-wasm/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@

#pragma once

// #ifdef PROXY_WASM_USE_ABSL
#ifndef __cpp_lib_optional
#include "absl/types/optional.h"
#include "absl/strings/string_view.h"
#else
// Provide compatibiliby for projects which have not yet moved to C++17.
// TODO: remove this when all dependent projects have upgraded.

#if __cplusplus >= 201703L
#include <optional>
#include <string_view>
#else
#include "absl/types/optional.h"
#include "absl/strings/string_view.h"
#endif

namespace proxy_wasm {
// #ifdef PROXY_WASM_USE_ABSL
#ifndef __cpp_lib_optional
using string_view = absl::string_view;
template <typename T> using optional = absl::optional<T>;
// Only available in C++17
// inline constexpr absl::nullopt_t nullopt = absl::nullopt;
#define PROXY_WASM_NULLOPT absl::nullopt
#else
using string_view = std::string_view;
template <typename T> using optional = std::optional<T>;
// Only available in C++17
// inline constexpr std::nullopt_t nullopt = std::nullopt;
#define PROXY_WASM_NULLOPT std::nullopt
#endif

#ifndef __cpp_lib_string_view
using string_view = absl::string_view;
#else
using string_view = std::string_view;
#endif
} // namespace proxy_wasm
13 changes: 12 additions & 1 deletion include/proxy-wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <time.h>
#include <atomic>
#include <chrono>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
Expand All @@ -39,7 +41,16 @@ using CallOnThreadFunction = std::function<void(std::function<void()>)>;
struct BufferInterface {
virtual ~BufferInterface() {}
virtual size_t size() const = 0;
// Returns true on success.
/**
* Copy bytes from the buffer into the Wasm VM corresponding to 'wasm'.
* @param start is the first buffer byte to copy.
* @param length is the length of sequence of buffer bytes to copy.
* @param ptr_ptr is the location in the VM address space to place the address of the newly
* allocated memory block which contains the copied bytes.
* @param size_ptr is the location in the VM address space to place the size of the newly
* allocated memory block which contains the copied bytes (e.g. length).
* @return true on success.
*/
virtual bool copyTo(WasmBase *wasm, size_t start, size_t length, uint64_t ptr_ptr,
uint64_t size_ptr) const = 0;
};
Expand Down
1 change: 1 addition & 0 deletions include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "include/proxy-wasm/compat.h"

#include <functional>
#include <memory>

#include "include/proxy-wasm/word.h"
Expand Down
1 change: 1 addition & 0 deletions include/proxy-wasm/word.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace proxy_wasm {
struct Word {
Word(uint64_t w) : u64_(w) {} // Implicit conversion into Word.
uint32_t u32() const { return static_cast<uint32_t>(u64_); }
operator uint64_t() const { return u64_; }
uint64_t u64_;
};

Expand Down
31 changes: 31 additions & 0 deletions wasm_vm_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "include/proxy-wasm/wasm_vm.h"

#include "gtest/gtest.h"

namespace proxy_wasm {
namespace {

TEST(WasmVm, Compat) {
string_view foo = "foo";
std::string bar = "bar";

EXPECT_NE(foo, bar);
EXPECT_EQ(foo, "foo");

optional<int> o = PROXY_WASM_NULLOPT;
EXPECT_FALSE(o);

o = 1;
EXPECT_TRUE(o);
}

TEST(WasmVm, Word) {
Word w(1);
EXPECT_EQ(w.u32(), 1);
EXPECT_EQ(sizeof(w.u32()), sizeof(uint32_t));
EXPECT_EQ(w, 1);
EXPECT_EQ(sizeof(w), sizeof(uint64_t));
}

} // namespace
} // namespace proxy_wasm