diff --git a/.gitignore b/.gitignore index c72de14..4ad458a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ build/ ### Mac OS ### .DS_Store + +### Rust ### +target/ diff --git a/Makefile b/Makefile index 11be792..2e73f8c 100755 --- a/Makefile +++ b/Makefile @@ -3,9 +3,24 @@ # # $ make build-go-examples # +# To build the Rust examples, install rustup and cargo install the wasm target with run `rustup target add wasm32-wasip1` +# and then run: +# +# $ make build-rust-examples +# + +.PHONY: all +all: build-go-examples build-rust-examples .PHONY: build-go-examples build-go-examples: @find ./src/test/go-examples -mindepth 1 -type f -name "main.go" \ | xargs -I {} bash -c 'dirname {}' \ | xargs -I {} bash -c 'cd {} && env GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o main.wasm ./main.go' + + +.PHONY: build-rust-examples +build-rust-examples: + @find ./src/test/rust-examples -mindepth 1 -type f -name "Cargo.toml" \ + | xargs -I {} bash -c 'dirname {}' \ + | xargs -I {} bash -c 'cd {} && cargo build --target wasm32-wasip1 --release; cp ./target/wasm32-wasip1/release/*.wasm ./main.wasm' diff --git a/src/main/java/io/roastedroot/proxywasm/ABI.java b/src/main/java/io/roastedroot/proxywasm/ABI.java index cd8a762..5fff8a1 100644 --- a/src/main/java/io/roastedroot/proxywasm/ABI.java +++ b/src/main/java/io/roastedroot/proxywasm/ABI.java @@ -1107,10 +1107,24 @@ int proxyContinueStream(int arg) { return result.getValue(); } - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/v0.2.1#proxy_close_stream - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/v0.2.1#proxy_get_status + /** + * implements: https://github.com/proxy-wasm/spec/tree/main/abi-versions/v0.2.1#proxy_close_stream + */ + @WasmExport + int proxyCloseStream(int proxyStreamType) { + // TODO: implement + return WasmResult.UNIMPLEMENTED.getValue(); + } + + /** + * implements: https://github.com/proxy-wasm/spec/tree/main/abi-versions/v0.2.1#proxy_get_status + */ + @WasmExport + int proxyGetStatus( + int returnStatusCode, int returnStatusMessageData, int returnStatusMessageSize) { + // TODO: implement + return WasmResult.UNIMPLEMENTED.getValue(); + } // ////////////////////////////////////////////////////////////////////// // TCP streams @@ -1421,16 +1435,109 @@ void proxyOnHttpCallResponse(int arg0, int arg1, int arg2, int arg3, int arg4) { // gRPC calls // ////////////////////////////////////////////////////////////////////// - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_call - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_stream - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_send - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_cancel - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_close + /** + * implements https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_call + */ + @WasmExport + int proxyGrpcCall( + int upstreamNameData, + int upstreamNameSize, + int serviceNameData, + int serviceNameSize, + int methodNameData, + int methodNameSize, + int serialized_initial_metadataData, + int serialized_initial_metadataSize, + int messageData, + int messageSize, + int timeout, + int returnCalloutID) { + + try { + var upstreamName = string(readMemory(upstreamNameData, upstreamNameSize)); + var serviceName = string(readMemory(serviceNameData, serviceNameSize)); + var methodName = string(readMemory(methodNameData, methodNameSize)); + var initialMetadata = + decodeMap(serialized_initial_metadataData, serialized_initial_metadataSize); + var message = readMemory(messageData, messageSize); + + int callId = + handler.grpcCall( + upstreamName, + serviceName, + methodName, + initialMetadata, + message, + timeout); + putUint32(returnCalloutID, callId); + return callId; + } catch (WasmException e) { + return e.result().getValue(); + } + } + + /** + * implements https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_stream + */ + @WasmExport + int proxyGrpcStream( + int upstreamNameData, + int upstreamNameSize, + int serviceNameData, + int serviceNameSize, + int methodNameData, + int methodNameSize, + int serialized_initial_metadataData, + int serialized_initial_metadataSize, + int returnStreamId) { + + try { + var upstreamName = string(readMemory(upstreamNameData, upstreamNameSize)); + var serviceName = string(readMemory(serviceNameData, serviceNameSize)); + var methodName = string(readMemory(methodNameData, methodNameSize)); + var initialMetadata = + decodeMap(serialized_initial_metadataData, serialized_initial_metadataSize); + + int streamId = + handler.grpcStream(upstreamName, serviceName, methodName, initialMetadata); + putUint32(returnStreamId, streamId); + return streamId; + } catch (WasmException e) { + return e.result().getValue(); + } + } + + /** + * https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_send + */ + @WasmExport + int proxyGrpcSend(int streamId, int messageData, int messageSize, int endStream) { + try { + byte[] message = readMemory(messageData, messageSize); + WasmResult result = handler.grpcSend(streamId, message, endStream); + return result.getValue(); + } catch (WasmException e) { + return e.result().getValue(); + } + } + + /** + * https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_cancel + */ + @WasmExport + int proxyGrpcCancel(int callOrstreamId) { + WasmResult result = handler.grpcCancel(callOrstreamId); + return result.getValue(); + } + + /** + * https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_grpc_close + */ + @WasmExport + int proxyGrpcClose(int callOrstreamId) { + WasmResult result = handler.grpcClose(callOrstreamId); + return result.getValue(); + } /** * implements https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_on_grpc_receive_initial_metadata @@ -1720,8 +1827,26 @@ int proxyGetProperty(int keyPtr, int keySize, int returnValueData, int returnVal } } - // TODO: implement - // https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_set_property + /** + * https://github.com/proxy-wasm/spec/tree/main/abi-versions/vNEXT#proxy_set_property + */ + @WasmExport + int proxySetProperty(int pathDataPtr, int pathSize, int valueDataPtr, int valueSize) { + try { + // Get key from memory + String path = string(readMemory(pathDataPtr, pathSize)); + + // Get value from memory + String value = string(readMemory(valueDataPtr, valueSize)); + + // Set property value using handler + WasmResult result = handler.setProperty(path, value); + return result.getValue(); + + } catch (WasmException e) { + return e.result().getValue(); + } + } // ////////////////////////////////////////////////////////////////////// // Foreign function interface (FFI) diff --git a/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java b/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java index 23b13aa..255eef2 100644 --- a/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java +++ b/src/main/java/io/roastedroot/proxywasm/ChainedHandler.java @@ -318,4 +318,49 @@ public byte[] dequeueSharedQueue(int queueId) throws WasmException { public WasmResult enqueueSharedQueue(int queueId, byte[] value) { return next().enqueueSharedQueue(queueId, value); } + + @Override + public LogLevel getLogLevel() throws WasmException { + return next().getLogLevel(); + } + + @Override + public WasmResult setProperty(String path, String value) { + return next().setProperty(path, value); + } + + @Override + public int grpcCall( + String upstreamName, + String serviceName, + String methodName, + ProxyMap initialMetadata, + byte[] message, + int timeout) + throws WasmException { + return next().grpcCall( + upstreamName, serviceName, methodName, initialMetadata, message, timeout); + } + + @Override + public int grpcStream( + String upstreamName, String serviceName, String methodName, ProxyMap initialMetadata) + throws WasmException { + return next().grpcStream(upstreamName, serviceName, methodName, initialMetadata); + } + + @Override + public WasmResult grpcSend(int streamId, byte[] message, int endStream) { + return next().grpcSend(streamId, message, endStream); + } + + @Override + public WasmResult grpcCancel(int callOrstreamId) { + return next().grpcCancel(callOrstreamId); + } + + @Override + public WasmResult grpcClose(int callOrstreamId) { + return next().grpcClose(callOrstreamId); + } } diff --git a/src/main/java/io/roastedroot/proxywasm/Handler.java b/src/main/java/io/roastedroot/proxywasm/Handler.java index e4160d7..73594db 100644 --- a/src/main/java/io/roastedroot/proxywasm/Handler.java +++ b/src/main/java/io/roastedroot/proxywasm/Handler.java @@ -49,6 +49,10 @@ default String getProperty(String key) throws WasmException { return null; } + default WasmResult setProperty(String path, String value) { + return WasmResult.UNIMPLEMENTED; + } + /** * Get the HTTP request body. * @@ -420,6 +424,35 @@ default long getMetric(int metricId) throws WasmException { throw new WasmException(WasmResult.UNIMPLEMENTED); } + default int grpcCall( + String upstreamName, + String serviceName, + String methodName, + ProxyMap initialMetadata, + byte[] message, + int timeout) + throws WasmException { + throw new WasmException(WasmResult.UNIMPLEMENTED); + } + + default int grpcStream( + String upstreamName, String serviceName, String methodName, ProxyMap initialMetadata) + throws WasmException { + throw new WasmException(WasmResult.UNIMPLEMENTED); + } + + default WasmResult grpcSend(int streamId, byte[] message, int endStream) { + return WasmResult.UNIMPLEMENTED; + } + + default WasmResult grpcCancel(int callOrstreamId) { + return WasmResult.UNIMPLEMENTED; + } + + default WasmResult grpcClose(int callOrstreamId) { + return WasmResult.UNIMPLEMENTED; + } + class SharedData { public byte[] data; public int cas; diff --git a/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java b/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java new file mode 100644 index 0000000..a2103cf --- /dev/null +++ b/src/test/java/io/roastedroot/proxywasm/examples/RustHelloWorldTest.java @@ -0,0 +1,28 @@ +package io.roastedroot.proxywasm.examples; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.dylibso.chicory.wasm.Parser; +import io.roastedroot.proxywasm.ProxyWasm; +import io.roastedroot.proxywasm.StartException; +import java.nio.file.Path; +import org.junit.jupiter.api.Test; + +/** + * Test loading https://github.com/proxy-wasm/proxy-wasm-rust-sdk/tree/c8b2335df66a569a6306c58e346dd0cf9dbc0f3a/examples/hello_world + */ +public class RustHelloWorldTest { + + @Test + public void pauseUntilEOS() throws StartException { + var handler = new MockHandler(); + var module = Parser.parse(Path.of("./src/test/rust-examples/hello_world/main.wasm")); + try (var host = ProxyWasm.builder().withPluginHandler(handler).build(module)) { + + handler.assertLogsEqual("Hello, World!"); + host.tick(); + + assertTrue(handler.loggedMessages().get(1).contains("your lucky number is")); + } + } +} diff --git a/src/test/rust-examples/hello_world/Cargo.lock b/src/test/rust-examples/hello_world/Cargo.lock new file mode 100644 index 0000000..9280868 --- /dev/null +++ b/src/test/rust-examples/hello_world/Cargo.lock @@ -0,0 +1,418 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + +[[package]] +name = "cc" +version = "1.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-link", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.171" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" + +[[package]] +name = "log" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc" + +[[package]] +name = "proc-macro2" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proxy-wasm" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14a5a4df5a1ab77235e36a0a0f638687ee1586d21ee9774037693001e94d4e11" +dependencies = [ + "hashbrown", + "log", +] + +[[package]] +name = "proxy-wasm-example-hello-world" +version = "0.0.1" +dependencies = [ + "cfg-if", + "chrono", + "getrandom", + "log", + "proxy-wasm", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-link" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dccfd733ce2b1753b03b6d3c65edf020262ea35e20ccdf3e288043e6dd620e3" + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/src/test/rust-examples/hello_world/Cargo.toml b/src/test/rust-examples/hello_world/Cargo.toml new file mode 100644 index 0000000..5d43f75 --- /dev/null +++ b/src/test/rust-examples/hello_world/Cargo.toml @@ -0,0 +1,27 @@ +[package] +publish = false +name = "proxy-wasm-example-hello-world" +version = "0.0.1" +authors = ["Piotr Sikora "] +description = "Proxy-Wasm plugin example: Hello World" +license = "Apache-2.0" +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +cfg-if = "1.0" +chrono = { version = "0.4", default-features = false, features = ["clock", "std"] } +log = "0.4" +proxy-wasm = "0.2.2" + +[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies] +getrandom = "0.3" + +[profile.release] +lto = true +opt-level = 3 +codegen-units = 1 +panic = "abort" +strip = "debuginfo" diff --git a/src/test/rust-examples/hello_world/README.md b/src/test/rust-examples/hello_world/README.md new file mode 100644 index 0000000..4dcc8fa --- /dev/null +++ b/src/test/rust-examples/hello_world/README.md @@ -0,0 +1,5 @@ +## Attribution + +This example originally came from: +https://github.com/proxy-wasm/proxy-wasm-rust-sdk/tree/c8b2335df66a569a6306c58e346dd0cf9dbc0f3a/examples/hello_world +``` diff --git a/src/test/rust-examples/hello_world/main.wasm b/src/test/rust-examples/hello_world/main.wasm new file mode 100755 index 0000000..c2fec81 Binary files /dev/null and b/src/test/rust-examples/hello_world/main.wasm differ diff --git a/src/test/rust-examples/hello_world/src/lib.rs b/src/test/rust-examples/hello_world/src/lib.rs new file mode 100644 index 0000000..7a00581 --- /dev/null +++ b/src/test/rust-examples/hello_world/src/lib.rs @@ -0,0 +1,52 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use cfg_if::cfg_if; +use chrono::{DateTime, Utc}; +use log::info; +use proxy_wasm::traits::*; +use proxy_wasm::types::*; +use std::time::Duration; + +proxy_wasm::main! {{ + proxy_wasm::set_log_level(LogLevel::Trace); + proxy_wasm::set_root_context(|_| -> Box { Box::new(HelloWorld) }); +}} + +struct HelloWorld; + +impl Context for HelloWorld {} + +impl RootContext for HelloWorld { + fn on_vm_start(&mut self, _: usize) -> bool { + info!("Hello, World!"); + self.set_tick_period(Duration::from_secs(5)); + true + } + + fn on_tick(&mut self) { + cfg_if! { + if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + let now: DateTime = self.get_current_time().into(); + info!("It's {}, there is no lucky number.", now); + + } else { + let now: DateTime = Utc::now(); + let mut buf = [0u8; 1]; + getrandom::fill(&mut buf).unwrap(); + info!("It's {}, your lucky number is {}.", now, buf[0]); + } + } + } +}