Browse Source

README.md: Adjust Windows execution for examples with JIT compiler

We're about to run code samples from the README.md file with "cargo
test", including as part of the CI. Examples that use the JIT compiler
will not work on Windows, given that the compiler is not implemented for
that platform. Let's fall back to the interpreter for Windows for these
examples.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Quentin Monnet 2 years ago
parent
commit
0d47458f92
1 changed files with 23 additions and 11 deletions
  1. 23 11
      README.md

+ 23 - 11
README.md

@@ -305,12 +305,17 @@ fn main() {
     // directly reads from packet data)
     let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
 
-    // This time we JIT-compile the program.
-    vm.jit_compile().unwrap();
+    #[cfg(windows)] {
+        assert_eq!(vm.execute_program(mem).unwrap(), 0x11);
+    }
+    #[cfg(not(windows))] {
+        // This time we JIT-compile the program.
+        vm.jit_compile().unwrap();
 
-    // Then we execute it. For this kind of VM, a reference to the packet data
-    // must be passed to the function that executes the program.
-    unsafe { assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11); }
+        // Then we execute it. For this kind of VM, a reference to the packet
+        // data must be passed to the function that executes the program.
+        unsafe { assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11); }
+    }
 }
 ```
 ### Using a metadata buffer
@@ -346,12 +351,19 @@ fn main() {
     // This eBPF VM is for program that use a metadata buffer.
     let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
 
-    // Here again we JIT-compile the program.
-    vm.jit_compile().unwrap();
-
-    // Here we must provide both a reference to the packet data, and to the
-    // metadata buffer we use.
-    unsafe { assert_eq!(vm.execute_program_jit(mem, mbuff).unwrap(), 0x2211); }
+    #[cfg(windows)] {
+        assert_eq!(vm.execute_program(mem, mbuff).unwrap(), 0x2211);
+    }
+    #[cfg(not(windows))] {
+        // Here again we JIT-compile the program.
+        vm.jit_compile().unwrap();
+
+        // Here we must provide both a reference to the packet data, and to the
+        // metadata buffer we use.
+        unsafe {
+            assert_eq!(vm.execute_program_jit(mem, mbuff).unwrap(), 0x2211);
+        }
+    }
 }
 ```