Created
April 19, 2017 01:09
-
-
Save rla/3239befc9575be1e9576a3a86f1786b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Local<Object> TermQuery(Local<Value> termString) { | |
Local<Object> variables = Nan::New<Object>(); | |
String::Utf8Value string(termString); | |
term_t t = PL_new_term_ref(); | |
if (!PL_chars_to_term(*string, t)) { | |
Nan::ThrowError("PL_chars_to_term failed."); | |
return variables; | |
} | |
functor_t f; | |
if (!PL_get_functor(t, &f)) { | |
Nan::ThrowError("PL_get_functor failed."); | |
return variables; | |
} | |
int arity = PL_functor_arity(f); | |
term_t args = PL_new_term_refs(arity); | |
for (int i = 1; i <= arity; ++i) { | |
if (!PL_get_arg(i, t, args + i - 1)) { | |
Nan::ThrowError("PL_get_arg failed."); | |
return variables; | |
} | |
int type = PL_term_type(args + i - 1); | |
char *msg; | |
if (type == PL_VARIABLE) { | |
if (!PL_get_chars(args + i - 1, &msg, CVT_VARIABLE)) { | |
Nan::ThrowError("PL_get_chars failed."); | |
return variables; | |
} | |
} | |
variables->Set(Nan::New<String>(msg).ToLocalChecked(), Nan::Null()); | |
} | |
predicate_t p = PL_pred(f, NULL); | |
int flags = PL_Q_NODEBUG | PL_Q_CATCH_EXCEPTION; | |
qid_t q = PL_open_query(NULL, flags, p, args); | |
if (!PL_next_solution(q)) { | |
Nan::ThrowError("PL_next_solution failed."); | |
return variables; | |
} | |
return variables; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment