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

Add simpler mechanism for define new JS preprocessor functions/macros. #22510

Open
sbc100 opened this issue Sep 5, 2024 · 2 comments
Open

Add simpler mechanism for define new JS preprocessor functions/macros. #22510

sbc100 opened this issue Sep 5, 2024 · 2 comments

Comments

@sbc100
Copy link
Collaborator

sbc100 commented Sep 5, 2024

Currently we have a fixed set of processor macros/function that are mostly defined in parseTools.js

Adding new macros in JS libraries is possible, but its rather ugly syntax, and has some limitations:

{{{
globalThis.newMacro = () = { .. }
}}}

A major drawback (IIUC) of doing this is that the macros are not available during preprocessing since macros expansion happens after preprocessing. So to use these macros in a preprocessor directive one would need define them in a separate file that is loaded first. e.g. --js-library=my_macros.js --js-library=my_library.js. Here the macros defined in my_macros.js would be available in my_library.js.

One way to improve this would be to allow my_library.js to somehow #include "my_macros.js" .. however that isn't currently how the #include directive works. Currently #include files are placed directly into the output file and don't have macros expanded, so we would need somekind of new #include mechanism.

@adamscott
Copy link

adamscott commented Dec 16, 2024

Here's a my_macros.js file that enables semver checks in library files.

// my_macros.js
/* eslint-disable */
{{{
/* eslint-enable */
	globalThis.___EMSCRIPTEN_VERSION_PARSED = globalThis.EMSCRIPTEN_VERSION.split('.').map((n) => parseInt(n, 10));

	globalThis.___SEMVER_IS_GREATER_THAN = (a, b, { orEqual = false } = {}) => {
		const [aMajor, aMinor, aPatch] = a;
		const [bMajor, bMinor, bPatch] = b;
		if ((orEqual && aMajor >= bMajor) || aMajor > bMajor) {
			if ((orEqual && aMinor >= bMinor) || aMinor > bMinor) {
				if ((orEqual && aPatch >= bPatch) || aPatch > bPatch) {
					return true;
				}
			}
		}
		return false;
	};

	globalThis.EMSCRIPTEN_VERSION_IS_GREATER_THAN = (major, minor, patch) => {
		const isGreater = globalThis.___SEMVER_IS_GREATER_THAN(globalThis.___EMSCRIPTEN_VERSION_PARSED, [major, minor, patch]);
		return isGreater;
	};

	globalThis.EMSCRIPTEN_VERSION_IS_GREATER_THAN_OR_EQUAL = (major, minor, patch) => {
		const isGreaterOrEqual = globalThis.___SEMVER_IS_GREATER_THAN(globalThis.___EMSCRIPTEN_VERSION_PARSED, [major, minor, patch], { orEqual: true });
		return isGreaterOrEqual;
	};

	globalThis.EMSCRIPTEN_VERSION_IS_LESS_THAN = (major, minor, patch) => {
		const isGreaterOrEqual = globalThis.___SEMVER_IS_GREATER_THAN(globalThis.___EMSCRIPTEN_VERSION_PARSED, [major, minor, patch], { orEqual: true });
		return !isGreaterOrEqual;
	};

	globalThis.EMSCRIPTEN_VERSION_IS_LESS_THAN_OR_EQUAL = (major, minor, patch) => {
		const isGreater = globalThis.___SEMVER_IS_GREATER_THAN(globalThis.___EMSCRIPTEN_VERSION_PARSED, [major, minor, patch]);
		return !isGreater;
	};
/* eslint-disable */
}}}
/* eslint-enable */

Then, you can use the macros like this inside your library:

// library_mylib.js
const GodotWebXR = {
	$GodotWebXR__deps: [
#if EMSCRIPTEN_VERSION_IS_LESS_THAN(3,1,71)
		'$Browser',
#else 
		'$MainLoop',
#endif
		'$GL', 
		'$GodotRuntime', 
		'$runtimeKeepalivePush', 
		'$runtimeKeepalivePop'
	]
	$GodotWebXR: {
	}
}

@sbc100
Copy link
Collaborator Author

sbc100 commented Dec 16, 2024

Thats a good example of how things work today yes. This proposal is to make the mechanism official blessed and to enable library_mylib.js to reference my_macros.js internally so that users don't need to explicitly add both on the command line (in the right order).

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