Skip to content

Commit

Permalink
Slightly cleaner C-API for V1 interpreters
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Johnson <matjoh@microsoft.com>
  • Loading branch information
matajoh committed Sep 26, 2024
1 parent 99836d8 commit fc99e92
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/c/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main(void)
regoEnum err;
int rc = EXIT_SUCCESS;
regoOutput* output = NULL;
regoInterpreter* rego = regoNew(false);
regoInterpreter* rego = regoNew();

err = regoAddModuleFile(rego, "examples/objects.rego");
if (err != REGO_OK)
Expand Down
2 changes: 1 addition & 1 deletion examples/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int main(int argc, char** argv)
}
}

rego = regoNew(false);
rego = regoNew();

for (data_index = 0; data_index < config.data_files_count; data_index++)
{
Expand Down
12 changes: 10 additions & 2 deletions include/rego/rego_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,18 @@ extern "C"
*
* The caller is responsible for freeing the interpreter with regoFree.
*
* @param v1_compatible Whether the interpreter should be v1 compatible.
* @return A pointer to the new interpreter.
*/
regoInterpreter* regoNew(regoBoolean v1_compatible);
regoInterpreter* regoNew();

/**
* Allocates and initializes a new V1 Rego interpreter.
*
* The caller is responsible for freeing the interpreter with regoFree.
*
* @return A pointer to the new interpreter.
*/
regoInterpreter* regoNewV1();

/**
* Frees a Rego interpreter.
Expand Down
12 changes: 9 additions & 3 deletions src/rego_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ extern "C"
}
}

regoInterpreter* regoNew(regoBoolean v1_compatible)
regoInterpreter* regoNew()
{
auto ptr =
reinterpret_cast<regoInterpreter*>(new rego::Interpreter(v1_compatible));
auto ptr = reinterpret_cast<regoInterpreter*>(new rego::Interpreter(false));
logging::Debug() << "regoNew: " << ptr;
return ptr;
}

regoInterpreter* regoNewV1()
{
auto ptr = reinterpret_cast<regoInterpreter*>(new rego::Interpreter(true));
logging::Debug() << "regoNewV1: " << ptr;
return ptr;
}

void regoFree(regoInterpreter* rego)
{
logging::Debug() << "regoFree: " << rego;
Expand Down
2 changes: 1 addition & 1 deletion tests/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int main()
int rc = REGO_OK;
regoOutput* output = NULL;
regoNode* node = NULL;
regoInterpreter* rego = regoNew(false);
regoInterpreter* rego = regoNew();
regoSize size = 0;
char* buf = NULL;

Expand Down
6 changes: 2 additions & 4 deletions wrappers/python/src/_regopy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ PYBIND11_MODULE(_regopy, m)
"level"_a);
m.def(
"regoSetTZDataPath", &regoSetTZDataPath, "Sets the TZData path.", "path"_a);
m.def("regoNew", &regoNew, "Returns a pointer to a new rego instance.");
m.def(
"regoNew",
&regoNew,
"Returns a pointer to a new rego instance.",
"v1_compatible"_a);
"regoNewV1", &regoNewV1, "Returns a pointer to a new V1 rego instance.");
m.def("regoFree", &regoFree, "Deletes a rego instance.");
m.def(
"regoAddModuleFile",
Expand Down
3 changes: 2 additions & 1 deletion wrappers/python/src/regopy/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
regoGetStrictBuiltInErrors,
regoGetWellFormedChecksEnabled,
regoNew,
regoNewV1,
regoQuery,
regoSetDebugEnabled,
regoSetDebugPath,
Expand Down Expand Up @@ -106,7 +107,7 @@ class Interpreter:

def __init__(self, v1_compatible=False):
"""Initializer."""
self._impl = regoNew(v1_compatible)
self._impl = regoNewV1() if v1_compatible else regoNew()

def __del__(self):
"""Destructor."""
Expand Down
4 changes: 2 additions & 2 deletions wrappers/rust/regorust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,14 @@ pub fn set_tzdata_path(path: &str, download: bool) -> Result<(), &'static str> {
impl Interpreter {
/// Creates a new Rego interpreter.
pub fn new() -> Self {
let interpreter_ptr = unsafe { regoNew(0) };
let interpreter_ptr = unsafe { regoNew() };
Self {
c_ptr: interpreter_ptr,
}
}

pub fn new_v1() -> Self {
let interpreter_ptr = unsafe { regoNew(1) };
let interpreter_ptr = unsafe { regoNewV1() };
Self {
c_ptr: interpreter_ptr,
}
Expand Down

0 comments on commit fc99e92

Please sign in to comment.