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
@@ -2,6 +2,10 @@
|
||||
# Licensed under the MIT License.
|
||||
|
||||
import regorus
|
||||
import sys
|
||||
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
|
||||
# Create engine
|
||||
engine = regorus.Engine()
|
||||
@@ -94,3 +98,68 @@ engine1.set_gather_prints(True)
|
||||
engine1.eval_query('print("Hello")')
|
||||
ps = engine1.take_prints()
|
||||
print(ps)
|
||||
|
||||
# RVM regular example
|
||||
policy = """
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
input.active == true
|
||||
}
|
||||
"""
|
||||
def run_regular_example():
|
||||
module = ("demo.rego", policy)
|
||||
program = regorus.Program.compile_from_modules(
|
||||
"{}",
|
||||
[module],
|
||||
["data.demo.allow"],
|
||||
)
|
||||
|
||||
print(program.generate_listing())
|
||||
|
||||
binary = program.serialize_binary()
|
||||
program, is_partial = regorus.Program.deserialize_binary(binary)
|
||||
if is_partial:
|
||||
raise RuntimeError("Deserialized program marked partial")
|
||||
|
||||
vm = regorus.Rvm()
|
||||
vm.load_program(program)
|
||||
vm.set_input_json('{"user":"alice","active":true}')
|
||||
print(vm.execute())
|
||||
|
||||
run_regular_example()
|
||||
|
||||
# RVM HostAwait example
|
||||
policy = """
|
||||
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"
|
||||
}
|
||||
"""
|
||||
def run_host_await_example():
|
||||
module = ("await.rego", policy)
|
||||
program = regorus.Program.compile_from_modules(
|
||||
"{}",
|
||||
[module],
|
||||
["data.demo.allow"],
|
||||
)
|
||||
|
||||
vm = regorus.Rvm()
|
||||
vm.set_execution_mode(1)
|
||||
vm.load_program(program)
|
||||
vm.set_input_json('{"account":{"id":"acct-1","active":true}}')
|
||||
vm.execute()
|
||||
print(vm.get_execution_state())
|
||||
print(vm.resume('{"tier":"gold"}'))
|
||||
|
||||
run_host_await_example()
|
||||
|
||||
Reference in New Issue
Block a user