Merge branch 'master' of ssh://ssh.dcc.fc.up.pt:31064/home/vsc/yap

This commit is contained in:
Vitor Santos Costa 2019-01-22 01:59:13 +00:00
commit daa86e9c34
5 changed files with 62 additions and 173 deletions

View File

@ -856,8 +856,8 @@ if (WITH_JAVA)
if (APPLE)
set(CMAKE_MACOSX_RPATH 1)
find_library (JLI jli ${JAVA_AWT_DIR}/jli)
find_library (JAL JavaApplicationLauncher FRAMEWORK ONLY PATH /System/Library/PrivateFrameworks)
find_library (JL JavaLaunching FRAMEWORK ONLY PATH /System/Library/PrivateFrameworks)
#find_library (JAL JavaApplicationLauncher FRAMEWORK ONLY PATH /System/Library/PrivateFrameworks)
#find_library (JL JavaLaunching FRAMEWORK ONLY PATH /System/Library/PrivateFrameworks)
list(APPEND CMAKE_INSTALL_RPATH ${JAVA_AWT_DIR}/jli)
list(APPEND JNI_LIBRARIES ${JLI};${JAL};${JL})
endif()

View File

@ -48,6 +48,8 @@ refactoring (trivial):
#define JPL_C_LIB_VERSION_PATCH 4
#define JPL_C_LIB_VERSION_STATUS "alpha"
#define JPL_DEBUG
#ifndef JPL_DEBUG
/*#define DEBUG(n, g) ((void)0) */
#define DEBUG_LEVEL 4
@ -640,7 +642,7 @@ static JNIEnv*
jni_env(void) /* economically gets a JNIEnv pointer, valid for this thread */
{ JNIEnv *env;
switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_8) )
switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_9) )
{ case JNI_OK:
return env;
case JNI_EDETACHED:
@ -1819,20 +1821,20 @@ jni_create_jvm_c(
char *cpoptp;
JavaVMOption opt[MAX_JVM_OPTIONS];
int r;
jint n;
jint n = 1;
int optn = 0;
JNIEnv *env;
JPL_DEBUG(1, Sdprintf( "[creating JVM with 'java.class.path=%s']\n", classpath));
vm_args.version = JNI_VERSION_1_6; /* "Java 1.2 please" */
vm_args.version = JNI_VERSION_1_6zzzz; /* "Java 1.2 please" */
if ( classpath )
{
cpoptp = (char *)malloc(strlen(classpath)+20);
strcpy( cpoptp, "-Djava.class.path="); /* was cpopt */
strcat( cpoptp, classpath); /* oughta check length... */
vm_args.options = opt;
opt[optn].optionString = cpoptp; /* was cpopt */
optn++;
cpoptp = (char *)malloc(strlen(classpath) + strlen("-Djava.class.path=")+1);
strcpy(cpoptp, "-Djava.class.path="); /* was cpopt */
strcat(cpoptp, classpath); /* oughta check length... */
vm_args.options = opt;
opt[optn].optionString = cpoptp; /* was cpopt */
optn++;
}
/* opt[optn++].optionString = "-Djava.compiler=NONE"; */
/* opt[optn].optionString = "exit"; // I don't understand this yet... */
@ -1841,10 +1843,12 @@ jni_create_jvm_c(
/* opt[optn++].extraInfo = jvm_abort; // this function has been moved to jpl_extras.c */
/* opt[optn++].optionString = "-Xcheck:jni"; // extra checking of JNI calls */
#if __YAP_PROLOG__
opt[optn++].optionString = "-Xmx512m"; // give java enough space
opt[optn].optionString = malloc(strlen("-Xmx512m")+1); // give java enough space
strcpy(opt[optn++].optionString,"-Xmx512m"); // give java enough space
#if defined(__APPLE__)
// I can't make jpl work with AWT graphics, without creating the extra thread.
opt[optn++].optionString = "-Djava.awt.headless=true";
// I can't make jpl work with AWT graphics, without creating the extra thread.
opt[optn].optionString = malloc(strlen("-Djava.awt.headless=true") + 1); // give java enough space
strcpy(opt[optn++].optionString, "-Djava.awt.headless=true"); // give java enough space
#endif
// opt[optn++].optionString = "-XstartOnFirstThread";
#endif
@ -1853,6 +1857,7 @@ jni_create_jvm_c(
/* opt[optn++].extraInfo = fprintf; // no O/P, then SEGV */
/* opt[optn++].extraInfo = xprintf; // one message, then SEGV */
/* opt[optn++].optionString = "-verbose:jni"; */
opt[optn].optionString = NULL;
if ( jvm_dia != NULL )
{

View File

@ -601,132 +601,6 @@ static long get_len_of_range(long lo, long hi, long step) {
{"A29", NULL}, {"A29", NULL}, {"A30", NULL}, {"A31", NULL}, {"A32", NULL},
{NULL, NULL}};
static PyObject *structseq_str(PyStructSequence *obj ) {
/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
bool removelast = false;
PyTypeObject *typ = Py_TYPE(obj);
const char *type_name = typ->tp_name;
Py_ssize_t len, i;
char buf[REPR_BUFFER_SIZE];
char *endofbuf, *pbuf = buf;
/* pointer to end of writeable buffer; safes space for "...)\0" */
endofbuf = &buf[REPR_BUFFER_SIZE - 5];
/* "typename(", limited to TYPE_MAXSIZE */
len =
strnlen(type_name, TYPE_MAXSIZE);
strncpy(pbuf, type_name, len);
pbuf += len;
*pbuf++ = '(';
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Str(val);
if (repr == NULL)
return Py_None;
crepr = PyUnicode_AsUTF8(repr);
if (crepr == NULL) {
Py_DECREF(repr);
return Py_None;
}
/* + 3: keep space for ", " */
len = strlen(crepr) + 2;
if ((pbuf + len) <= endofbuf) {
strcpy(pbuf, crepr);
pbuf += strlen(crepr);
*pbuf++ = ',';
*pbuf++ = ' ';
removelast = 1;
Py_DECREF(repr);
} else {
strcpy(pbuf, "...");
pbuf += 3;
removelast = 0;
Py_DECREF(repr);
break;
}
}
if (removelast) {
/* overwrite last ", " */
pbuf -= 2;
}
*pbuf++ = ')';
*pbuf = '\0';
return PyUnicode_FromString(buf);
}
static PyObject *structseq_repr(PyObject *iobj) {
/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
PyStructSequence *obj = (PyStructSequence *)iobj;
PyTypeObject *typ = Py_TYPE(obj);
const char *type_name = typ->tp_name;
bool removelast = false;
Py_ssize_t len, i;
char buf[REPR_BUFFER_SIZE];
char *endofbuf, *pbuf = buf;
/* pointer to end of writeable buffer; safes space for "...)\0" */
endofbuf = &buf[REPR_BUFFER_SIZE - 5];
/* "typename(", limited to TYPE_MAXSIZE */
len =
strnlen(type_name, TYPE_MAXSIZE);
strncpy(pbuf, type_name, len);
pbuf += len;
*pbuf++ = '(';
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Repr(val);
if (repr == NULL)
return NULL;
crepr = PyUnicode_AsUTF8(repr);
if (crepr == NULL) {
Py_DECREF(repr);
return NULL;
}
/* + 3: keep space for ", " */
len = strlen(crepr) + 2;
if ((pbuf + len) <= endofbuf) {
strcpy(pbuf, crepr);
pbuf += strlen(crepr);
*pbuf++ = ',';
*pbuf++ = ' ';
removelast = 1;
Py_DECREF(repr);
} else {
strcpy(pbuf, "...");
pbuf += 3;
removelast = 0;
Py_DECREF(repr);
break;
}
}
if (removelast) {
/* overwrite last ", " */
pbuf -= 2;
}
*pbuf++ = ')';
*pbuf = '\0';
return PyUnicode_FromString(buf);
}
#endif
static bool legal_symbol(const char *s) {
@ -761,8 +635,9 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
typp = (PyTypeObject *)d;
} else {
PyStructSequence_Desc *desc = PyMem_Calloc(sizeof(PyStructSequence_Desc), 1);
desc->name = PyMem_Malloc(strlen(s) + 1);
strcpy((char *)desc->name, s);
char *tnp;
desc->name = tnp = PyMem_Malloc(strlen(s) + 1);
strcpy(tnp, s);
desc->doc = "YAPTerm";
desc->fields = pnull;
desc->n_in_sequence = arity;
@ -773,7 +648,7 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
return NULL;
typp->tp_traverse = NULL;
typp->tp_flags |=
Py_TPFLAGS_TUPLE_SUBCLASS|
// Py_TPFLAGS_TUPLE_SUBCLASS|
Py_TPFLAGS_BASETYPE|
Py_TPFLAGS_HEAPTYPE;
// don't do this: we cannot add a type as an atribute.
@ -783,8 +658,6 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
Py_INCREF(key);
Py_INCREF(typp);
}
typp->tp_repr = structseq_repr;
typp->tp_str = structseq_str;
}
PyObject *o = PyStructSequence_New(typp);
Py_INCREF(typp);
@ -915,7 +788,14 @@ PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt) {
PyTuple_SET_ITEM(n, 1, out);
return n;
}
return term_to_nametuple(s, arity, out);
if (cvt)
return term_to_nametuple(s, arity, out);
else {
PyObject *rc = PyTuple_New(2);
PyTuple_SetItem(rc, 0, PyUnicode_FromString(s));
PyTuple_SetItem(rc, 1, out);
return rc;
}
}
}
@ -1146,7 +1026,13 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
// PyObject_Print(rc, stderr, 0);
// DebugPrintf("CallObject %p\n", rc);
} else {
if (cvt)
rc = term_to_nametuple(s, arity, pArgs);
else {
rc = PyTuple_New(2);
PyTuple_SetItem(rc, 0, ys);
PyTuple_SetItem(rc, 1, pArgs);
}
}
return rc;

View File

@ -31,7 +31,7 @@ static foreign_t python_represent( term_t name, term_t tobj) {
term_t stackp = python_acquire_GIL();
PyObject *e;
e = term_to_python(tobj, false, NULL, true);
e = term_to_python(tobj, false, NULL, false);
if (e == NULL) {
python_release_GIL(stackp);
pyErrorAndReturn(false);

View File

@ -74,48 +74,46 @@ argi(N,I,I1) :-
python_query( Caller, String ) :-
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Goal, VarNames, Status, Bindings),
query_to_answer( Goal, _, Status, VarNames, Bindings),
Caller.port := Status,
write_query_answer( Bindings ),
answer := {},
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
output(Caller, Bindings).
output( _, Bindings ) :-
write_query_answer( Bindings ),
fail.
output( Caller, Bindings ) :-
answer := {},
foldl(ground_dict(answer), Bindings, [], Ts),
term_variables( Ts, Hidden),
foldl(bv, Hidden , 0, _),
maplist(into_dict(answer),Ts),
Caller.answer := json.dumps(answer),
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S).
S := Caller.answer,
format(user_error, '~nor ~s~n~n',S),
fail.
output(_Caller, _Bindings).
bv(V,I,I1) :-
atomic_concat(['__',I],V),
I1 is I+1.
into_dict(D,V0=T) :-
D[V0] := T.
python_represents(D[V0], T).
/**
*
*/
ground_dict(_Dict, var([V,V]), I, I) :-
ground_dict(_Dict,var([_V]), I, I) :-
!.
ground_dict(Dict, nonvar([V0|Vs], T),I0, [V0=T| I0]) :-
ground_dict(_Dict,var([V,V]), I, I) :-
!.
ground_dict(Dict, nonvar([V0|Vs],T),I0, [V0=T| I0]) :-
!,
ground_dict( Dict, var([V0|Vs]), I0, I0).
ground_dict(Dict, var([V0,V|Vs]), I, I) :-
ground_dict(Dict, var([V0|Vs]),I0, I0).
ground_dict(Dict, var([V0,V1|Vs]), I, I) :-
!,
Dict[V]=V0,
ground_dict( Dict, var([V0|Vs]), I, I).
ground_dict(_, _, _, _).
Dict[V1] := V0,
ground_dict(Dict, var([V0|Vs]), I, I).
bound_dict(Dict, nonvar([V0|Vs], T)) :-
!,
Dict[V0] := T,
bound_dict( Dict, var([V0|Vs])).
bound_dict(Dict, var([V0,V|Vs])) :-
!,
Dict[V] := V0,
bound_dict( Dict, var([V0|Vs])).
bound_dict(_, _).