mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
feat(bindings)!: add RVM/Program support across FFI and language bindings (#565)
- FFI: add RVM/Program APIs, execution state accessors, HostAwait handling, and buffer/result helpers in rvm.rs, common.rs, engine.rs. - Compiler: emit HostAwait for __builtin_host_await in function_calls.rs. - RVM tests: add HostAwait regression cases and extend harness for suspend/resume responses in host_await.yaml and mod.rs. - C/C++: add RVM tests/examples and wrapper updates in rvm_tests.c, rvm_tests.cpp, regorus.hpp, plus CMake wiring. - C#: add Program/Rvm bindings, SafeHandle/PInvoke, tests, and example usage in Regorus, RvmProgramTests.cs, Program.cs, and README updates. - Go: add Program/Rvm bindings, tests, and examples in rvm.go, rvm_test.go, main.go. - Java: add Program/Rvm bindings, JNI glue, and examples in lib.rs, regorus, Test.java. - Python: add Program/Rvm bindings and examples in lib.rs, test.py. - WASM: add Program/Rvm bindings and examples in lib.rs, test.js. - Tooling: wire binding tests in xtask and ignore generated Java artifacts in .gitignore. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
0316ccd90c
commit
3f7a5496dc
@@ -100,4 +100,138 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("%s\n", output)
|
||||
|
||||
// RVM regular example (compile, serialize, execute)
|
||||
const regularPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
input.active == true
|
||||
}
|
||||
`
|
||||
const regularInput = `{"user":"alice","active":true}`
|
||||
|
||||
regularModules := []regorus.PolicyModule{{Id: "demo.rego", Content: regularPolicy}}
|
||||
regularEntryPoints := []string{"data.demo.allow"}
|
||||
regularProgram, err := regorus.CompileProgramFromModules("{}", regularModules, regularEntryPoints)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer regularProgram.Close()
|
||||
|
||||
listing, err := regularProgram.GenerateListing()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("RVM listing:\n%s\n", listing)
|
||||
|
||||
binary, err := regularProgram.SerializeBinary()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
regularProgram.Close()
|
||||
|
||||
rehydrated, isPartial, err := regorus.DeserializeProgram(binary)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if isPartial {
|
||||
fmt.Fprintf(os.Stderr, "error: program marked partial\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer rehydrated.Close()
|
||||
|
||||
regularVm, err := regorus.NewRvm()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer regularVm.Close()
|
||||
|
||||
if err := regularVm.LoadProgram(rehydrated); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := regularVm.SetInputJson(regularInput); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
regularResult, err := regularVm.Execute()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("RVM regular result: %s\n", regularResult)
|
||||
|
||||
// RVM HostAwait example
|
||||
const rvmPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.account.active == true
|
||||
details := __builtin_host_await(input.account.id, "account")
|
||||
details.tier == "gold"
|
||||
}
|
||||
`
|
||||
const rvmInput = `{"account":{"id":"acct-1","active":true}}`
|
||||
|
||||
modules := []regorus.PolicyModule{{Id: "demo.rego", Content: rvmPolicy}}
|
||||
entryPoints := []string{"data.demo.allow"}
|
||||
program, err := regorus.CompileProgramFromModules("{}", modules, entryPoints)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer program.Close()
|
||||
|
||||
vm, err := regorus.NewRvm()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer vm.Close()
|
||||
|
||||
if err := vm.SetExecutionMode(1); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := vm.LoadProgram(program); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := vm.SetInputJson(rvmInput); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if _, err := vm.Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
state, err := vm.GetExecutionState()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("HostAwait state: %s\n", state)
|
||||
|
||||
result, err := vm.Resume(`{"tier":"gold"}`, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("HostAwait result: %s\n", result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user