newlisp datatypes translated to C?

Started by Alisaxy, July 28, 2015, 12:23:53 PM

Previous topic - Next topic

Alisaxy

Hello,



I'm trying to embed newlisp into the Unreal engine through a dynamic library.

I register functions like this in my C++ code: newlispCallback("<functionNameInNewLisp>", (long)<functionName>, NULL);

I'm having no troubles with calling my C functions from newlisp with primitives and null-terminated strings. I however am having problems with collections other than newlisp strings, those being lists and arrays.



I'm also wondering if there's a proper way to call a variadic function? I tried it, but it gave me a huge arguments lists with my two values being somewhere in the middle. My C/C++ skills are a bit rusty, so the latter problem may be due to that.



So I was mostly wondering what the lists and arrays translate to in C.





Thanks!



EDIT: figured out the varargs. Most of the problem solved. :)
void print(long arg...) {
va_list ap;
va_start(ap, arg);
for (int i = arg; i >= 0; i = va_arg(ap, int))
printf("HAHA: %d ", i);
va_end(ap);
putchar('n');
}


EDIT v2: Getting this literal: -858993460 at the end of my varargs. Why is that exactly I wonder?



EDIT v3: varargs for c_strings:

#define current_va_arg(list, type) (*(type *)(list - sizeof(type)))
#define increment_va_arg(list, type) (*(type *)(list += sizeof(type)))
void printStr(int varargLen, const char * args...) {
va_list vargs;
const char* buffer;
va_start(vargs, args);
for (int i = 0; i < varargLen; i++) {
buffer = current_va_arg(vargs, const char *);
cout << buffer << endl;
increment_va_arg(vargs, const char *);
}
va_end(vargs);
}


No idea why it works for integers, but has to be hacked for c_strings.

I should always give the length of the vararg list as the first parameter. I'll change my first example.

Hehe, not even a full-time C-programmer.



EDIT v4:



The latest version of integer varargs, same macros apply.

void print(int varargLen, const long args...) {
va_list vargs;
long buffer;
va_start(vargs, args);
for (int i = 0; i < varargLen; i++) {
buffer = current_va_arg(vargs, const long);
cout << "vararg " << buffer << endl;
increment_va_arg(vargs, const long);
}
va_end(vargs);
}

Now I have an idea why the second one was behaving weirdly under a shallow inspection. I'll keep on using those two macros I made instead of the vanilla method.

Chappuis

#1
Thanks for the info Cool Guy



https://gclub.gclubslot.com/gclub-download.html">Gclub Download
https://gclub.gclubslot.com\">Gclub newlisp datatypes