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,9 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import com.microsoft.regorus.Engine;
|
||||
import com.microsoft.regorus.PolicyModule;
|
||||
import com.microsoft.regorus.Program;
|
||||
import com.microsoft.regorus.Rvm;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -44,5 +47,70 @@ public class Test {
|
||||
"package world\nx { true }"
|
||||
);
|
||||
}
|
||||
|
||||
String regularPolicy = String.join("\n",
|
||||
"package demo",
|
||||
"import rego.v1",
|
||||
"",
|
||||
"default allow := false",
|
||||
"",
|
||||
"allow if {",
|
||||
" input.user == \"alice\"",
|
||||
" input.active == true",
|
||||
"}"
|
||||
);
|
||||
String regularInput = "{\"user\":\"alice\",\"active\":true}";
|
||||
|
||||
{
|
||||
PolicyModule module = new PolicyModule("demo.rego", regularPolicy);
|
||||
Program program = Program.compileFromModules("{}", new PolicyModule[]{module}, new String[]{"data.demo.allow"});
|
||||
System.out.println("RVM listing:\n" + program.generateListing());
|
||||
|
||||
byte[] binary = program.serializeBinary();
|
||||
program.close();
|
||||
|
||||
boolean[] isPartial = new boolean[1];
|
||||
Program rehydrated = Program.deserializeBinary(binary, isPartial);
|
||||
if (isPartial[0]) {
|
||||
throw new IllegalStateException("Deserialized program marked partial");
|
||||
}
|
||||
|
||||
try (Rvm vm = new Rvm()) {
|
||||
vm.loadProgram(rehydrated);
|
||||
vm.setInputJson(regularInput);
|
||||
String result = vm.execute();
|
||||
System.out.println("RVM regular result: " + result);
|
||||
}
|
||||
rehydrated.close();
|
||||
}
|
||||
|
||||
String awaitPolicy = String.join("\n",
|
||||
"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\"",
|
||||
"}"
|
||||
);
|
||||
String awaitInput = "{\"account\":{\"id\":\"acct-1\",\"active\":true}}";
|
||||
|
||||
{
|
||||
PolicyModule module = new PolicyModule("await.rego", awaitPolicy);
|
||||
Program program = Program.compileFromModules("{}", new PolicyModule[]{module}, new String[]{"data.demo.allow"});
|
||||
try (Rvm vm = new Rvm()) {
|
||||
vm.setExecutionMode((byte) 1);
|
||||
vm.loadProgram(program);
|
||||
vm.setInputJson(awaitInput);
|
||||
vm.execute();
|
||||
System.out.println("HostAwait state: " + vm.getExecutionState());
|
||||
String resumed = vm.resume("{\"tier\":\"gold\"}");
|
||||
System.out.println("HostAwait result: " + resumed);
|
||||
}
|
||||
program.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user