Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Addon: Re-format using clang-format -style=Google and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Jun 2, 2015
1 parent 460232a commit 561e217
Show file tree
Hide file tree
Showing 22 changed files with 1,110 additions and 1,214 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ If you wish to run the client one one machine and the server on another, make su
To run the tests, simply run ```npm test```. A script called ```dist.sh``` is also provided. It runs ```npm install```, runs the tests, then runs ```npm prune --production``` to remove all sources, headers, and build dependencies. After that, it copies relevant files to ```dist/``` and creates a tarball from them. This is followed by running ```npm install``` again to restore the development environment.

## Coding Style

Please follow the [jQuery](http://contribute.jquery.org/style-guide/js/) coding style, even for C++!
Please follow the [jQuery](http://contribute.jquery.org/style-guide/js/) coding style for the JavaScript files.

The C++ files can be formatted using ```clang-format -style=Google```:
```BASH
find src -type f | while read; do
clang-format -style=Google "$REPLY" > "$REPLY".new && mv "$REPLY".new "$REPLY"
done
```
117 changes: 51 additions & 66 deletions src/callback-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,65 @@ extern "C" {

#include "callback-info.h"

callback_info *callback_info_free( callback_info *info ) {
if ( info->closure ) {
ffi_closure_free( info->closure );
}
if ( info->remover ) {
info->remover( info->user_data );
}
free( info );
callback_info *callback_info_free(callback_info *info) {
if (info->closure) {
ffi_closure_free(info->closure);
}
if (info->remover) {
info->remover(info->user_data);
}
free(info);

return 0;
return 0;
}

callback_info *callback_info_new(
void *user_data,
UserDataRemover remover,
ffi_type *return_type,
Marshaller default_handler,
int argumentCount, ... ) {
callback_info *callback_info_new(void *user_data, UserDataRemover remover,
ffi_type *return_type,
Marshaller default_handler, int argumentCount,
...) {
// Allocate enough for the callback_info structure + argument types
callback_info *info = (callback_info *)malloc(
sizeof(callback_info) + argumentCount * sizeof(ffi_type *));
va_list va;
int index;

// Allocate enough for the callback_info structure + argument types
callback_info *info = ( callback_info * )malloc(
sizeof( callback_info ) +
argumentCount * sizeof( ffi_type * ) );
va_list va;
int index;
// Allocate info structure
if (info) {
memset(info, 0, sizeof(callback_info));

// Allocate info structure
if ( info ) {
memset( info, 0, sizeof( callback_info ) );
// Save user data info
info->remover = remover;
info->user_data = user_data;

// Save user data info
info->remover = remover;
info->user_data = user_data;
// Allocate closure
info->closure = (ffi_closure *)ffi_closure_alloc(
sizeof(ffi_closure), (void **)&(info->resultingFunction));
if (info->closure) {
// Populate argument type list
va_start(va, argumentCount);
for (index = 0; index < argumentCount; index++) {
info->argTypes[index] = va_arg(va, ffi_type *);
}
va_end(va);

// Allocate closure
info->closure = ( ffi_closure * )ffi_closure_alloc(
sizeof( ffi_closure ),
(void **)&( info->resultingFunction ) );
if ( info->closure ) {
// Set up argument description
if (ffi_prep_cif(&(info->cif), FFI_DEFAULT_ABI, argumentCount,
return_type, info->argTypes) == FFI_OK) {
// Set up closure
if (ffi_prep_closure_loc(info->closure, &(info->cif), default_handler,
user_data,
(void *)(info->resultingFunction)) != FFI_OK) {
info = callback_info_free(info);
}
} else {
info = callback_info_free(info);
}
} else {
info = callback_info_free(info);
}
}

// Populate argument type list
va_start( va, argumentCount );
for ( index = 0 ; index < argumentCount ; index++ ) {
info->argTypes[ index ] = va_arg( va, ffi_type * );
}
va_end( va );

// Set up argument description
if ( ffi_prep_cif(
&( info->cif ),
FFI_DEFAULT_ABI,
argumentCount,
return_type,
info->argTypes ) == FFI_OK ) {

// Set up closure
if ( ffi_prep_closure_loc(
info->closure,
&( info->cif ),
default_handler,
user_data,
(void *)( info->resultingFunction ) ) != FFI_OK ) {

info = callback_info_free( info );
}
} else {
info = callback_info_free( info );
}
} else {
info = callback_info_free( info );
}
}

return info;
return info;
}

#ifdef __cplusplus
Expand Down
42 changes: 21 additions & 21 deletions src/callback-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ extern "C" {

#include <ffi.h>

typedef void ( *Callback )();
typedef void ( *Marshaller )( ffi_cif*, void*, void**, void* );
typedef void ( *UserDataRemover )( void* );
typedef void (*Callback)();
typedef void (*Marshaller)(ffi_cif *, void *, void **, void *);
typedef void (*UserDataRemover)(void *);

typedef struct _callback_info {
ffi_cif cif;
Marshaller resultingFunction;
ffi_closure *closure;
void *user_data;
UserDataRemover remover;
ffi_type *argTypes[];
ffi_cif cif;
Marshaller resultingFunction;
ffi_closure *closure;
void *user_data;
UserDataRemover remover;
ffi_type *argTypes[];
} callback_info;

// Free a callback_info structure. Always returns NULL
callback_info *callback_info_free( callback_info *info );
callback_info *callback_info_free(callback_info *info);

// Create a new callback_info.
callback_info *callback_info_new(

// Extra parameter
void *user_data,
// Extra parameter
void *user_data,

// Function that frees the memory for the extra parameter
UserDataRemover remover,
// Function that frees the memory for the extra parameter
UserDataRemover remover,

// The signature of the original function:
// Return type
ffi_type *return_type,
// The signature of the original function:
// Return type
ffi_type *return_type,

// Location
Marshaller default_handler,
// Location
Marshaller default_handler,

// Argument count followed by list of argument types
int argumentCount, ... );
// Argument count followed by list of argument types
int argumentCount, ...);

#ifdef __cplusplus
}
Expand Down
12 changes: 6 additions & 6 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

using namespace v8;

Persistent<Function> *persistentJSCallback_new( Local<Function> callback ) {
Persistent<Function> *returnValue = new Persistent<Function>;
NanAssignPersistent( *returnValue, callback );
return returnValue;
Persistent<Function> *persistentJSCallback_new(Local<Function> callback) {
Persistent<Function> *returnValue = new Persistent<Function>;
NanAssignPersistent(*returnValue, callback);
return returnValue;
}

void persistentJSCallback_free( Persistent<Function> *callback ) {
delete callback;
void persistentJSCallback_free(Persistent<Function> *callback) {
delete callback;
}
60 changes: 32 additions & 28 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
#include <v8.h>
#include <nan.h>

#define VALIDATE_CALLBACK_RETURN_VALUE_TYPE( value, typecheck, message ) \
if ( !value->typecheck() ) { \
NanThrowTypeError( message " callback return value type must satisfy " #typecheck "()" ); \
}

#define VALIDATE_ARGUMENT_COUNT( args, length ) \
if ( (args).Length() < (length) ) { \
return NanThrowRangeError( "Need " #length " arguments" ); \
}

#define VALIDATE_ARGUMENT_TYPE( args, index, typecheck ) \
if ( !(args)[ (index) ]->typecheck() ) { \
return NanThrowTypeError( "Argument " #index " must satisfy " #typecheck "()" ); \
}

#define VALIDATE_VALUE_TYPE( value, typecheck, message, failReturn ) \
if ( !(value)->typecheck() ) { \
NanThrowTypeError( message " must satisfy " #typecheck "()" ); \
return failReturn; \
}

#define VALIDATE_ARGUMENT_TYPE_OR_NULL( args, index, typecheck ) \
if ( !( (args)[ (index) ]->typecheck() || (args)[ (index) ]->IsNull() ) ) { \
return NanThrowTypeError( "Argument " #index " must satisfy " #typecheck "() or IsNull()" ); \
}

v8::Persistent<v8::Function> *persistentJSCallback_new( v8::Local<v8::Function> callback );
void persistentJSCallback_free( v8::Persistent<v8::Function> *callback );
#define VALIDATE_CALLBACK_RETURN_VALUE_TYPE(value, typecheck, message) \
if (!value->typecheck()) { \
NanThrowTypeError( \
message " callback return value type must satisfy " #typecheck "()"); \
}

#define VALIDATE_ARGUMENT_COUNT(args, length) \
if ((args).Length() < (length)) { \
return NanThrowRangeError("Need " #length " arguments"); \
}

#define VALIDATE_ARGUMENT_TYPE(args, index, typecheck) \
if (!(args)[(index)]->typecheck()) { \
return NanThrowTypeError("Argument " #index " must satisfy " #typecheck \
"()"); \
}

#define VALIDATE_VALUE_TYPE(value, typecheck, message, failReturn) \
if (!(value)->typecheck()) { \
NanThrowTypeError(message " must satisfy " #typecheck "()"); \
return failReturn; \
}

#define VALIDATE_ARGUMENT_TYPE_OR_NULL(args, index, typecheck) \
if (!((args)[(index)]->typecheck() || (args)[(index)]->IsNull())) { \
return NanThrowTypeError("Argument " #index " must satisfy " #typecheck \
"() or IsNull()"); \
}

v8::Persistent<v8::Function> *persistentJSCallback_new(
v8::Local<v8::Function> callback);
void persistentJSCallback_free(v8::Persistent<v8::Function> *callback);

#endif /* __IOTIVITY_NODE_FUNCTIONS_INTERNAL_H__ */
Loading

0 comments on commit 561e217

Please sign in to comment.