newlisp datatypes translated to C?

Q&A's, tips, howto's
Post Reply
Alisaxy
Posts: 3
Joined: Tue Jul 28, 2015 7:12 pm

newlisp datatypes translated to C?

Post by 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:

Code: Select all

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. :)

Code: Select all

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:

Code: Select all

-858993460
at the end of my varargs. Why is that exactly I wonder?

EDIT v3: varargs for c_strings:

Code: Select all

#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.

Code: Select all

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
Posts: 2
Joined: Thu Jan 26, 2017 8:53 am

Re: newlisp datatypes translated to C?

Post by Chappuis »

Thanks for the info Cool Guy

Gclub Download
Gclub newlisp datatypes

Post Reply