fix copy_term

This commit is contained in:
Vitor Santos Costa 2018-06-27 16:38:17 +01:00
parent f2d6c99c8a
commit cdd892c012
16 changed files with 138 additions and 122 deletions

View File

@ -2167,7 +2167,7 @@ X_API FILE *YAP_TermToStream(Term t) {
X_API void YAP_EndConsult(int sno, int *osnop, const char *full) {
BACKUP_MACHINE_REGS();
Yap_CloseStream(sno);
Yap_ChDir(full);
Yap_ChDir(dirname(full));
if (osnop >= 0)
Yap_AddAlias(AtomLoopStream, *osnop);
Yap_end_consult();

View File

@ -355,8 +355,10 @@ static int copy_complex_term(register CELL *pt0, register CELL *pt0_end,
int share, int copy_att_vars, CELL *ptf,
CELL *HLow USES_REGS) {
struct cp_frame *to_visit0,
*to_visit = (struct cp_frame *)Yap_PreAllocCodeSpace();
int lvl = push_text_stack();
struct cp_frame *to_visit0, *to_visit = Malloc(1024*sizeof(struct cp_frame));
struct cp_frame *to_visit_max;
CELL *HB0 = HB;
tr_fr_ptr TR0 = TR;
int ground = TRUE;
@ -382,7 +384,7 @@ loop:
*ptf = AbsPair(HR);
ptf++;
#ifdef RATIONAL_TREES
if (to_visit + 1 >= (struct cp_frame *)AuxSp) {
if (to_visit >= to_visit_max) {
goto heap_overflow;
}
to_visit->start_cp = pt0;
@ -493,7 +495,7 @@ loop:
ptf++;
/* store the terms to visit */
#ifdef RATIONAL_TREES
if (to_visit + 1 >= (struct cp_frame *)AuxSp) {
if (to_visit + 1 >= to_visit_max) {
goto heap_overflow;
}
to_visit->start_cp = pt0;
@ -593,6 +595,7 @@ loop:
HB = HB0;
clean_dirty_tr(TR0 PASS_REGS);
/* follow chain of multi-assigned variables */
pop_text_stack(lvl);
return 0;
overflow:
@ -611,6 +614,7 @@ overflow:
}
#endif
reset_trail(TR0);
pop_text_stack(lvl);
return -1;
heap_overflow:
@ -629,6 +633,7 @@ heap_overflow:
}
#endif
reset_trail(TR0);
pop_text_stack(lvl);
return -2;
trail_overflow:
@ -647,6 +652,7 @@ trail_overflow:
}
#endif
reset_trail(TR0);
pop_text_stack(lvl);
return -4;
}

View File

@ -217,8 +217,7 @@ void *Yap_InitTextAllocator(void) {
static size_t MaxTmp(USES_REGS1) {
return ((char *)LOCAL_TextBuffer->buf + LOCAL_TextBuffer->sz) -
(char *)LOCAL_TextBuffer->ptr;
return 1025;
}
static Term Globalize(Term v USES_REGS) {
@ -282,7 +281,7 @@ static void *codes2buf(Term t0, void *b0, bool *get_codes USES_REGS) {
return NULL;
}
if (!IsAtomTerm(hd)) {
Yap_ThrowError(TYPE_ERROR_CHARACTER, hd, "scanning list of atoms");
Yap_ThrowError(TYPE_ERROR_CHARACTER, hd, "scanning list of texts");
return NULL;
}
const char *code = RepAtom(AtomOfTerm(hd))->StrOfAE;
@ -441,7 +440,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
LOCAL_ActiveError->errorRawTerm = inp->val.t;
}
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
pop_text_stack(lvl);
Yap_ThrowError(LOCAL_Error_TYPE, inp->val.t, "Converting to text from term ");
return NULL;
}
@ -508,7 +507,6 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
s = Malloc(2 * MaxTmp(PASS_REGS1));
if (snprintf(s, MaxTmp(PASS_REGS1) - 1, Int_FORMAT,
IntegerOfTerm(inp->val.t)) < 0) {
pop_text_stack(lvl);
AUX_ERROR(inp->val.t, 2 * MaxTmp(PASS_REGS1), s, char);
}
return pop_output_text_stack(lvl, s);
@ -528,7 +526,6 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
char *s;
s = Malloc(MaxTmp());
if (!Yap_mpz_to_string(Yap_BigIntOfTerm(inp->val.t), s, MaxTmp() - 1, 10)) {
pop_text_stack(lvl);
AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char);
}
return inp->val.uc = pop_output_text_stack(lvl, s);

View File

@ -182,8 +182,6 @@ static bool consult(const char *b_file USES_REGS) {
CACHE_REGS
YAP_Reset(YAP_FULL_RESET, false);
Yap_StartSlots();
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "read %s <%d>", b_file,
GLOBAL_Stream[c_stream].linecount);
Term vs = MkVarTerm(), pos = MkVarTerm();
t = YAP_ReadClauseFromStream(c_stream, vs, pos);
// Yap_GetNèwSlot(t);

View File

@ -161,6 +161,7 @@ public:
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
// Yap_DebugPlWrite(out);
if (tout == 0L) {
return;
throw YAPError();
}
ap = getPred(tout, nts);

View File

@ -289,7 +289,7 @@ public:
/**
* @brief Compound Term
*/
class X_API YAPApplTerm : public YAPTerm {
class X_API YAPApplTerm : public YAPTerm {
friend class YAPTerm;
public:

View File

@ -56,14 +56,20 @@ opportunity. Initial value is 10,000. May be changed. A value of 0
~~~
*/
/**< `answer_format` how to present answers, default is `~p`. */
YAP_FLAG(ANSWER_FORMAT_FLAG, "answer_format", true, isatom, "~p", NULL),
#if __APPLE__
YAP_FLAG(APPLE_FLAG, "apple", false, booleanFlag, "true", NULL), /**<
read-only boolean, a machine running an Apple Operating System */
#endif
/**< `answer_format` how to present answers, default is `~p`. */
YAP_FLAG(ANSWER_FORMAT_FLAG, "answer_format", true, isatom, "~p", NULL),
#if __ANDROID__
YAP_FLAG(ANDROID_FLAG, "android", false, booleanFlag, "true", NULL), /**<
read-only boolean, a machine running an Google's Android version of the Linux Operating System */
#endif
YAP_FLAG(ARCH_FLAG, "arch", false, isatom, YAP_ARCH, NULL), /**<
`apple`: read-only atom, it describes the ISA used in this version of YAP.
Available from YAP_AEH.

View File

@ -114,6 +114,7 @@ INLINE_ONLY Term Yap_ensure_atom__(const char *fu, const char *fi,
#define AUX_ERROR(t, n, s, TYPE) \
if (s + (n + 1) > (TYPE *)AuxSp) { \
pop_text_stack(lvl); \
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; \
LOCAL_Error_Size = n * sizeof(TYPE); \
return NULL; \

View File

@ -215,6 +215,8 @@ static bool set_cwd(VFS_t *me, const char *dirName) {
if (GLOBAL_cwd) {
free(GLOBAL_cwd);
}
if (!is_dir_a(me,dirName))
dirName = dirname(dirName);
GLOBAL_cwd = malloc(strlen(dirName)+1);
strcpy(GLOBAL_cwd, dirName);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "chdir %s", GLOBAL_cwd);

View File

@ -639,8 +639,22 @@ static Int file_directory_name(USES_REGS1) { /* file_directory_name(Stream,N) */
static Int list_directory(USES_REGS1) {
Term tf = MkAtomTerm(Yap_LookupAtom("[]"));
yhandle_t sl = Yap_InitSlot(tf);
VFS_t *vfsp;
char *buf = (char *)AtomName(AtomOfTerm(ARG1));
if ((vfsp = vfs_owner(buf))) {
void *de;
const char *dp;
if ((de = vfsp->opendir(vfsp, buf)) == NULL) {
PlIOError(PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in list_directory",
strerror(errno));
}
while ((dp = vfsp->nextdir( de))) {
YAP_Term ti = MkAtomTerm(Yap_LookupAtom(dp));
Yap_PutInHandle(sl, MkPairTerm(ti, Yap_GetFromHandle(sl)));
}
vfsp->closedir( de);
} else {
#if defined(__MINGW32__) || _MSC_VER
struct _finddata_t c_file;
char bs[BUF_SIZE];
@ -667,26 +681,7 @@ static Int list_directory(USES_REGS1) {
YAP_PutInSlot(sl, YAP_MkPairTerm(ti, YAP_GetFromSlot(sl)));
}
_findclose(hFile);
#else
#if __ANDROID__
{
const char *dirName = buf + strlen("/assets/");
AAssetManager *mgr = GLOBAL_VFS->priv;
AAssetDir *de;
const char *dp;
if ((de = AAssetManager_openDir(mgr, dirName)) == NULL) {
PlIOError(PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in list_directory",
strerror(errno));
}
while ((dp = AAssetDir_getNextFileName(de))) {
YAP_Term ti = MkAtomTerm(Yap_LookupAtom(dp));
Yap_PutInHandle(sl, MkPairTerm(ti, Yap_GetFromHandle(sl)));
}
AAssetDir_close(de);
}
#endif
#if HAVE_OPENDIR
#elif HAVE_OPENDIR
{
DIR *de;
struct dirent *dp;
@ -704,7 +699,7 @@ static Int list_directory(USES_REGS1) {
closedir(de);
}
#endif /* HAVE_OPENDIR */
#endif
}
tf = Yap_GetFromSlot(sl);
return Yap_unify(ARG2, tf);
}

View File

@ -1398,7 +1398,7 @@ Term Yap_BufferToTerm(const char *s, Term opts) {
Term rval;
int sno;
encoding_t l = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, strlen((const char *)s), &l,
sno = Yap_open_buf_read_stream((char *)s, strlen((const char *)s)+1, &l,
MEM_BUF_USER);
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
@ -1544,17 +1544,11 @@ static Int read_term_from_string(USES_REGS1) {
static Int atomic_to_term(USES_REGS1) {
Term t1 = Deref(ARG1);
if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3");
return (FALSE);
} else if (!IsAtomicTerm(t1)) {
Yap_Error(TYPE_ERROR_ATOMIC, t1, "read_term_from_atomic/3");
return (FALSE);
} else {
Term t = Yap_AtomicToString(t1 PASS_REGS);
const unsigned char *us = UStringOfTerm(t);
return Yap_UBufferToTerm(us, add_output(ARG2, add_names(ARG3, TermNil)));
}
int l = push_text_stack();
const char *s = Yap_TextToUTF8Buffer(t1 PASS_REGS);
Int rc = Yap_BufferToTerm(s, add_output(ARG2, add_names(ARG3, TermNil)));
pop_text_stack(l);
return rc;
}
static Int atom_to_term(USES_REGS1) {

View File

@ -92,6 +92,14 @@ bool Yap_isDirectory(const char *FileName) {
#endif
}
Int exists_directory(USES_REGS1) {
int lvl = push_text_stack();
const char *path = Yap_AbsoluteFile(Yap_TextTermToText(Deref(ARG1) PASS_REGS),true);
bool rc = Yap_isDirectory(path);
pop_text_stack(lvl);
return rc;
}
bool Yap_Exists(const char *f) {
VFS_t *vfs;
f = Yap_VFAlloc(f);
@ -346,14 +354,14 @@ bool Yap_ChDir(const char *path) {
bool rc = false;
int lvl = push_text_stack();
const char *qpath = Yap_AbsoluteFile(path, true);
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "chdir %s", path);
VFS_t *v;
if ((v = vfs_owner(path))) {
rc = v->chdir(v, path);
if ((v = vfs_owner(qpath))) {
rc = v->chdir(v, (qpath));
pop_text_stack(lvl);
return rc;
}
const char *qpath = Yap_AbsoluteFile(path, true);
#if _WIN32
rc = true;
if (qpath != NULL && qpath[0] &&
@ -615,6 +623,8 @@ do_glob(const char *spec, bool glob_vs_wordexp) {
}
return tf;
}
#elif __ANDROID__
return MkPairTerm(MkAtomTerm(Yap_LookupAtom(spec)), TermNil);
#elif HAVE_WORDEXP || HAVE_GLOB
char u[YAP_FILENAME_MAX + 1];
const char *espec = u;
@ -1867,6 +1877,7 @@ void Yap_InitSysPreds(void) {
Yap_InitCPred("$dir_separator", 1, p_dir_sp, SafePredFlag);
Yap_InitCPred("libraries_directories", 2, libraries_directories, 0);
Yap_InitCPred("system_library", 1, system_library, 0);
Yap_InitCPred("exists_directory", 1, exists_directory, 0);
Yap_InitCPred("commons_library", 1, commons_library, 0);
Yap_InitCPred("$getenv", 2, p_getenv, SafePredFlag);
Yap_InitCPred("$putenv", 2, p_putenv, SafePredFlag | SyncPredFlag);

View File

@ -72,7 +72,7 @@ foreach (driver ${MYDDAS_DBMS})
cpp_driver(MYDDAS_YAP ${driver} myddas_driver.ypp)
endforeach ()
#list (APPEND MYDDAS_YAP ${CMAKE_CURRENT_SOURCE_DIR}/../sqlite3/test.yap ${CMAKE_CURRENT_SOURCE_DIR}/../sqlite3/chinook.db)
list (APPEND MYDDAS_YAP ${CMAKE_CURRENT_SOURCE_DIR}/../sqlite3/test.yap ${CMAKE_CURRENT_SOURCE_DIR}/../sqlite3/chinook.db)
add_to_group(MYDDAS_YAP pl_library )
add_custom_target(plmyddas ALL DEPENDS ${MYDDAS_YAP} )

View File

@ -1,5 +1,6 @@
:- compile(library(myddas)).
:- compile(library(maplist)).
:- use_module(library(myddas)).
main :-
init,
@ -10,20 +11,23 @@ main_ :-
fail.
main_ .
init :-
%init :-
db_open(sqlite3, '/data/user/0/pt.up.yap.yapdroid/files/Yap/chinook.db', _, _),
db_open(sqlite3, '/assets/Yap/chinook.db', _, _),
writeln('chinook has landed'),
db_import('Artist', artist),
writeln('Artist -> artist'),
db_import('Album', album),
writeln('Album -> album'),
db_import('Track', track).
writeln('Track -> track').
go :-
db_get_attributes_types(album,Als),
format('~w -> ~w~n'.[album,Als]),
db_get_attributes_types(track,Ts),
format('~w -> ~w~n'.[track,Ts]),
db_get_attributes_types(artist,As),
writeln(As:Als:Ts).
format('~w -> ~w~n'.[artist,As]).
go :-
db_number_of_fields(album,Als),
db_number_of_fields(track,Ts),
@ -31,11 +35,11 @@ go :-
writeln(As:Als:Ts).
go :-
db_describe(album, Desc), writeln(Desc) ;
db_describe(track, Desc), writeln(Desc) ;
db_describe(artist, Desc), writeln(Desc).
db_describe(album, Desc), writeln(album:Desc) ;
db_describe(track, Desc), writeln(track:Desc) ;
db_describe(artist, Desc), writeln(artist:Desc).
go :-
db_show_tables(Desc), writeln(Desc).
db_show_tables(Desc), writeln(tables:Desc).
go :-
db_show_tables(table(T)),
db_describe(T,tableinfo(FieldID,Type,Null,Primary,Default,'')),

View File

@ -61,6 +61,7 @@ and_put(int sno, int ch) {
buff0 += ch;
if (ch=='\n' || buff0.length() == 128) { //buff0+= '\0';
streamerInstance->display(buff0);
buff0.clear();
}
return ch;
}

View File

@ -14,6 +14,14 @@
%include std_string.i
%include std_vector.i
#if __ANDROID__
%include "arrays_java.i";
// %inline %{
static YAPTerm Temp[1];
// %}
// %typemap(javapackage) std::vector<YAPTerm> "pt.up.yap.YAPTerm"
//%template(VectorOfTerm) std::vector<YAPTerm>;
#endif
%feature("novaluewrapper") std::vector<Term>;
@ -34,10 +42,6 @@ class YAPEngine;
#if __ANDROID__
#endif
#ifdef SWIGPYTHON
#include <py4yap.h>
@ -59,10 +63,6 @@ class YAPEngine;
// no way to translate
return NULL;
}
#endif
#ifdef SWIGJAVA0
%typemap(javapackage) std::vector<YAPTerm> "pt.up.yap.YAPTerm"
%template(VectorOfTerm) std::vector<YAPTerm>;
#endif
}
%}