diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 000000000..f64d0a7fd --- /dev/null +++ b/.bazelrc @@ -0,0 +1 @@ +build --cxxopt="-std=c++17" diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..4e3be99a1 --- /dev/null +++ b/.clang-format @@ -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 +... diff --git a/BUILD b/BUILD index 1fde73c05..4ad2ede29 100644 --- a/BUILD +++ b/BUILD @@ -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", ], ) diff --git a/WORKSPACE b/WORKSPACE index e5d0df342..6389d4031 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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"], +) diff --git a/context_test.cc b/context_test.cc new file mode 100644 index 000000000..14cd13771 --- /dev/null +++ b/context_test.cc @@ -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 error_function) : error_function_(error_function) {} + void error(string_view message) { error_function_(message); } + +private: + std::function error_function_; +}; + +TEST(Context, IncludeParses) {} + +} // namespace +} // namespace proxy_wasm diff --git a/include/proxy-wasm/compat.h b/include/proxy-wasm/compat.h index 253f7dc09..f92732c71 100644 --- a/include/proxy-wasm/compat.h +++ b/include/proxy-wasm/compat.h @@ -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 #include +#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 using optional = absl::optional; // 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 using optional = std::optional; // 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 diff --git a/include/proxy-wasm/context.h b/include/proxy-wasm/context.h index 04a32ccc6..a975d95b1 100644 --- a/include/proxy-wasm/context.h +++ b/include/proxy-wasm/context.h @@ -19,6 +19,8 @@ #include #include +#include +#include #include #include #include @@ -39,7 +41,16 @@ using CallOnThreadFunction = std::function)>; 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; }; diff --git a/include/proxy-wasm/wasm_vm.h b/include/proxy-wasm/wasm_vm.h index b3a2106ab..da6f4daa2 100644 --- a/include/proxy-wasm/wasm_vm.h +++ b/include/proxy-wasm/wasm_vm.h @@ -17,6 +17,7 @@ #include "include/proxy-wasm/compat.h" +#include #include #include "include/proxy-wasm/word.h" diff --git a/include/proxy-wasm/word.h b/include/proxy-wasm/word.h index 22263d894..b28b199a7 100644 --- a/include/proxy-wasm/word.h +++ b/include/proxy-wasm/word.h @@ -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(u64_); } + operator uint64_t() const { return u64_; } uint64_t u64_; }; diff --git a/wasm_vm_test.cc b/wasm_vm_test.cc new file mode 100644 index 000000000..317a96b07 --- /dev/null +++ b/wasm_vm_test.cc @@ -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 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