Skip to content

Annotate return values

Philip Chimento requested to merge ptomato/gjs:annotate-return-values into master

The SpiderMonkey API has a convention for return values of many functions: with a true or nonzero return value, there must be no JS exception pending on the JSContext, and with a false or nullptr return value, there must be an exception pending (or else it indicates an "uncatchable" exception.)

There are many places where we don't follow this convention. We can't yet use the compiler to catch all of them, but this adds a GJS_JSAPI_RETURN_CONVENTION macro to annotate functions that are supposed to follow this convention. Currently this expands to GCC/Clang's "warn_unused_result" attribute, which just checks that we don't ignore the return value.

It should be possible in the future to change it to a custom attribute and write a compiler plugin that will check more strictly that we have thrown an exception where we are supposed to, and that we don't return true where we've thrown an exception.

This also adds a GJS_USE macro to annotate functions where the return value must be used, but does not adhere to the JSAPI return convention.

The combination of these two macros does catch a lot of cases where we were handling errors incorrectly.

Merge request reports