Skip to content

Update preprocessor macros to use do-while loop #243

@cb1kenobi

Description

@cb1kenobi

Several of the macros define a block scope for local variables, but that doesn't force semicolons at build time. A common pattern is to use a do-while loop which does require a semicolon while preserving the block scope. The overhead of the do-while loop is complied out anyways.

For example, take:

NAPI_STATUS_RETURN(::napi_typeof(env, argv[0], &type))

This builds just fine with the trailing semicolon, however this can lead to confusing code as we're using NAPI_* macros like they are functions.

To clean things up, we need to change macros like:

#define NAPI_STATUS_RETURN(call) \
	{ \
		napi_status status = (call); \
		if (status != napi_ok) { \
			return status; \
		} \
	}

to:

#define NAPI_STATUS_RETURN(call) \
	do { \
		napi_status status = (call); \
		if (status != napi_ok) { \
			return status; \
		} \
	} while (0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions