Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support checkbuiltin function #239

Open
hemebond opened this issue Jan 4, 2025 · 9 comments
Open

Support checkbuiltin function #239

hemebond opened this issue Jan 4, 2025 · 9 comments

Comments

@hemebond
Copy link
Contributor

hemebond commented Jan 4, 2025

The checkbuiltin function allows for testing whether or not a builtin function is available, allowing the QuakeC code to support multiple engines, e.g.,

float can_parse_json = checkbuiltin(json_parse); 
@Baker7
Copy link

Baker7 commented Jan 4, 2025

Doesn't isfunction do that?

Update: looks like only does progs functions unlike command "prvm_printfunction"

Update2: Nevermind, it does builtins ...

Sample code + output ...

.// Throw this in CSQC_Init or worldspawn 
	#define printline(...)						print(__VA_ARGS__, "\n")

	string s;
	float result;
	s = "buf_sort";
	result = isfunction(s);
	printline (s, " isfunction?  result is ", ftos(result));

	s = "buf_sort2";
	result = isfunction(s);
	printline (s, " isfunction?  result is ", ftos(result));

Output:

buf_sort isfunction? result is 1
buf_sort2 isfunction? result is 0

@hemebond
Copy link
Contributor Author

hemebond commented Jan 4, 2025

You're right! I'd completely forgotten about that one. Thanks @Baker7

@hemebond hemebond closed this as completed Jan 4, 2025
@hemebond hemebond reopened this Jan 4, 2025
@hemebond
Copy link
Contributor Author

hemebond commented Jan 4, 2025

Actually, it's a little different. Similar to checkextension, if I want the code to branch based on whether or not the engine supports something, I need checkbuiltin.

For example, using a builtin json_parse function when available, or falling back to a QuakeC implementation.

@Baker7
Copy link

Baker7 commented Jan 4, 2025

You can't have a QuakeC function with the same name as a builtin and use them both -- I don't think.

So if you have a builtin myfunc and a QuakeC function myfunc, how are you going to distinguish which one to call if they both have the same name?

So give the QuakeC implementation a different name? But then you don't need to do anything because existing isfunction can handle that (?)

Optionally you could do something like:

void (float a, float b) myfunc;

void CSQC_Init ()
{
if (isfunction (json_parse))
myfunc = json_parse;
else
myfunc = json_parse_quakec;

myfunc (a, b);
}

/My raw thoughts

@hemebond
Copy link
Contributor Author

hemebond commented Jan 4, 2025

The fallback wouldn't have the same name, just the same task. Even if the builtin hasn't been implemented, you still need a definition (declaration?) of the builtin in order to compile.

@Baker7
Copy link

Baker7 commented Jan 4, 2025

Even if the builtin hasn't been implemented, you still need a definition (declaration?) of the builtin in order to compile.

When you compile there are statements like buf_sort = #292 // or whatever the number is

At run-time, if builtin 292 doesn't exist, it kicks an error when it runs the statement calling builtin 292.

This is why checkextension works because the code ALWAYS supports functions that do not exist, but lets your QuakeC decide whether to call them (and you checkextension or -- in your case issfunction --- to never call them if they aren't supported).

@hemebond
Copy link
Contributor Author

hemebond commented Jan 4, 2025

If I defined jsonnode json_parse(string) = #0:json_parse; and compile, isfunction("json_parse"); returns true, even though it's not implemented.

@Baker7
Copy link

Baker7 commented Jan 4, 2025

I see what you are saying, since it is defined in the csqcdefs.h or such, the QuakeC thinks it exists and only when it runs and fails will it error, making it not useful for an if statement, etc.

An alternative point of view: Shouldn't you be using checkextension("DP_JSON_EXT") or whatever.

What you propose on the surface seems like something that should be performed by checkextension.

@Baker7
Copy link

Baker7 commented Jan 5, 2025

Note: I was confused about checkbuiltin vs. isfunction in my first post -- and I got caught up in that and never quite got out of that tunnel completely.

This checkbuiltin function would be great and I read the fte implementation.

The extension system is "ok" but having a more fine-tuned checkbuiltin would make certain things easier, especially engine version upgrades where some new builtins were added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants