-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISSUE-2464: Make scryer prolog usable as a shared library for other programs #2464
Comments
One other bit of criticism -- I copied this loop in However, this would be rather brittle, since any change to the loop in |
One more question for anyone for more experienced rust specialists. I'm using a web development pattern here for a lot of these binding functions to return "ok/error/panic" via a string, i.e., https://github.com/mthom/scryer-prolog/pull/2465/files#diff-b1a35a68f14e696205874893c07fd24fdb88882b47c23cc0e0c80a30c7d53759R260-R280. Is this typical for rust / c interop code or is that considered overkill? |
I think Or, even better, make
This is very atypical for both Rust and C. In Rust you would usually use an enum like enum QueryResult {
Ok(QueryAnswer),
Err(QueryError),
Panic,
} In C you would usually indicate the error with a return value (like a NULL pointer or a non 0 // On success returns the result and sets error_msg to NULL.
// On error returns NULL and makes error_msg point to the error message.
char *function1(int arg1, int arg2, char **error_msg);
// On success returns 0, makes result point to the result and sets error_msg to NULL.
// On error returns a non-zero value that indicates what kind of error occurred,
// sets result to NULL and makes error_msg point to the error message.
int function2(int arg1, int arg2, char **result, char **error_msg);
// Because in function2 only one of result and error_msg are ever set, we can
// use just one out pointer to represent both, but this can be a bit confusing so
// needs to be properly documented.
// On success returns 0 and makes result point to the result.
// On error returns a non-zero value that indicates what kind of error occurred
// and makes result point to the error message.
int function3(int arg1, int arg2, char **result);
// Usage
int main() {
char *error_msg1 = NULL;
char *result1 = function1(1, 2, &error_msg1);
if (ret1 == NULL) {
// Now result1 is NULL and error_msg1 is the error message.
} else {
// Now result1 is the result and error_msg1 is still NULL.
}
char *error_msg2 = NULL;
char *result2 = NULL;
int err2 = function2(1, 2, &result2, &error_msg2);
if (err2 != 0) {
// Now result2 is NULL and error_msg2 is the error message.
// Can do a switch in the value of err2 to check what kind of
// error happened.
} else {
// Now result2 is the result and error_msg2 is still NULL.
}
char *result3 = NULL;
int err3 = functio3(1, 2, &result3);
if (err3 != 0) {
// Now result3 is the error message.
// Can do a switch in the value of err3 to check what kind of
// error happened.
} else {
// Now result3 is the result.
}
} In low level land where C lives it's in general very inconvenient to have to parse something like JSON just to get the results or even know if an error happened, because it involves third party libraries and probably a lot of allocation and memory management. It's a bit more acceptable in Rust because of the great ecosystem, but using better type-level representations is much preferred. |
I'd like to be able to use scryer as a shared library. Taking inspiration from the WASM code, I imagined something like
eval_code_c
andfree_c_string
.Note: I know absolutely nothing about rust, but from some hacking I have something that does... something.
Here is some test Python I'm using to work it:
I apologize for insulting anyone with my barbarian prolog and rust, pointers (no pun intended) would be appreciated.
The text was updated successfully, but these errors were encountered: