Skip to content

add transform functions for integer in jerryx/arg #1883

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

Merged
merged 1 commit into from
Jun 22, 2017

Conversation

jiangzidong
Copy link
Contributor

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]

@jiangzidong jiangzidong added the jerry-ext Related to the jerry-ext library label Jun 7, 2017
GENERATE_JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (uint16, 0, 65535)
GENERATE_JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (int16, -32767, 32767)
GENERATE_JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (uint32, 0, 4294967295)
GENERATE_JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (int32, -2147483648 , 2147483647)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the macros from stdint.h, (U)INT..._MAX/MIN ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, My concern was that the target may not have <stdint.h>, but now I realized that if so, there is also no such uint8_t things, and people should write their own stdint header.

if (!jerry_value_has_error_flag (rv)) \
{ \
tmp = jerryx_arg_clamp_double (tmp, min, max); \
*(type ## _t *) c_arg_p->dest = (type ## _t) tmp; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we round?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think? I think they both make sense if we tell the rules to user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah depends on the use case..
Maybe add a enum new argument to the jerryx_arg_(u)int... helpers, with JERRYX_ARG_ROUND / JERRYX_ARG_FLOOR (could provide ..._CEIL later if someone needs it...)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess some in use cases one may also not want the implicit clamping, but instead throw an error if the input value is out of the possible range.

So, to summarize we've got these "dimensions" when converting numbers:

  1. target storage type (width & signed-ness)
  2. rounding behavior (floor, round or ceil)
  3. out-of-range values: clamping vs throwing Range error

... and then the common dimensions that aren't specific to numbers:
4. optionality
5. strictness

I think it's important to be able to cater for 2 & 3.
But adding 2 & 3 as arguments jerryx_arg_(u)int... will make the signature quite long.
Maybe it makes sense to pass a "config" struct and choose default values such that passing {} as config is a reasonable thing to do?


typedef struct {
  jerryx_arg_coerce_t coercion;
  jerryx_arg_optional_t optionality;
  jerryx_arg_rounding_t rounding;
  jerryx_arg_clipping_t clipping;
} jerryx_arg_number_options_t;

//------

uint8_t d = 0;
jerryx_arg_t mapping[] = {
  // {} == "defaults": optional arg + coerce types + round + clip.
  jerryx_arg_uint8(&d, {})
};

//-------
uint8_t d = 0;
jerryx_arg_t mapping[] = {
  // {} == "defaults", all zero
  jerryx_arg_uint8(&d, (jerryx_arg_number_options_t) {
   .clipping = JERRYX_ARG_NO_CLIPPING,
   .coercion = JERRYX_ARG_NO_COERCE
  });
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 3 kinds of rounding policies and 2 kinds of out-of-range policies. What about define 3*2 = 6 enums. like JERRYX_ARG_INT_ROUND_ERROR (means will round and will throw error if out-of-range ), and passing the enum to the c_arg_p->extra_info.

Then in the validator funtion (e.g. jerryx_arg_transform_uint8_strict), we can choose different policy depends on the c_arg_p->extra_info.
In this way, we don't need to provide 24 functions for one int type, just 4 is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this way we can have just one internal transform function for all the int types.

Do you mean put "target storage type" , "optionality" and "strictness" into the jerryx_arg_number_options_t?
The reason why using multiple transform functions for different type is that some unused functions will not linked into the final exe file, so that the binary size can be saved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, yeah that makes sense.

Still, from a usage point of view, I think it's nice to have a struct where {} results in using reasonable "default" options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot just pass {} like jerryx_arg_uint8(&d, {})
In C, the braced list of initializers cannot be empty.

It should be jerryx_arg_uint8(&d, (jerryx_arg_int_option_t) {0})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot the cast. (jerryx_arg_int_option_t) {} should work too I think.

Copy link
Contributor Author

@jiangzidong jiangzidong Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, C doesn't support the empty initializer. C++ does.

http://en.cppreference.com/w/c/language/struct_initialization

In C, the braced list of initializers cannot be empty (note that C++ allows empty lists, and also note that a struct in C cannot be empty):

struct {int n;} s = {0}; // OK
struct {int n;} s = {}; // Error: initializer-list cannot be empty
struct {} s = {}; // Error: struct cannot be empty, initializer-list cannot be empty

const jerryx_arg_t *c_arg_p) /**< the native arg */
static jerry_value_t
jerryx_arg_transform_number_strict_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */
double *number_p) /**< OUT: the number in JS arg */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**< [out] the number in JS arg */

const jerryx_arg_t *c_arg_p) /**< the native arg */
static jerry_value_t
jerryx_arg_transform_number_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */
double *number_p) /**< OUT: the number in JS arg */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**< [out] the number in JS arg */


/**
* clamp a double by given min and max value
* @return clamped double value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline before @return

} /* jerryx_arg_transform_number_common */

/**
* clamp a double by given min and max value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clamp a double by given min and max value.

@jiangzidong
Copy link
Contributor Author

updated @LaszloLango @martijnthe

@jiangzidong
Copy link
Contributor Author

jiangzidong commented Jun 8, 2017

CI reports

error: type of bit-field ‘round’ is a GCC extension [-Werror=pedantic]
   jerryx_arg_round_t round:2; /**< rounding policy */
   ^

But **in my local machine, it can pass all the tests ** (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4))

In order to solve this "pedantic" error, I use packed enum instead of bit-field.

{
jerryx_arg_round_t round; /**< rounding policy */
jerryx_arg_clamp_t clamp; /**< clamping policy */
} jerryx_arg_int_option_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes more sense to add the packed to the struct type.
AFAIK packed isn't applied to "parent" types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jiangzidong
Copy link
Contributor Author

@LaszloLango Furthur comments?

@@ -98,6 +98,68 @@ Enum that indicates whether an argument is optional or required.
- [jerryx_arg_function](#jerryx_arg_function)
- [jerryx_arg_native_pointer](#jerryx_arg_native_pointer)

## jerryx_arg_round_t

Enum that indicates the rounding policy which will be choose to transform a integer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an integer

**Summary**

The structure indicates the options used to transform integer argument.
It will be passed into jerryx_arg_t's extra_info filed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field

const jerryx_arg_t *c_arg_p) /**< the native arg */
static jerry_value_t
jerryx_arg_transform_number_strict_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */
double *number_p) /**< [out]: the number in JS arg */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colon is not needed after [out]

const jerryx_arg_t *c_arg_p) /**< the native arg */
static jerry_value_t
jerryx_arg_transform_number_common (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */
double *number_p) /**< [out]: the number in JS arg */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

} /* jerryx_arg_transform_number */

/**
* clamp a double by given min and max value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clamp

/**
* round a double
*
* @return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the description

} /* jerryx_arg_helper_round */

static jerry_value_t
jerryx_arg_helper_process_double (double *d,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comments

@jiangzidong
Copy link
Contributor Author

@LaszloLango updated


- JERRYX_ARG_CLAMP - it will try to clamp the number into the range.
- JERRYX_ARG_NO_CLAMP - it will throw range error.
**See also**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line

* jerry error: the transformer fails.
*/
static jerry_value_t
jerryx_arg_helper_process_double (double *d, /**< [inout] the number to be processed */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[in, out]

Copy link
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@akosthekiss akosthekiss added the enhancement An improvement label Jun 15, 2017
Copy link
Member

@akosthekiss akosthekiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First set of thoughts

@@ -98,6 +98,69 @@ Enum that indicates whether an argument is optional or required.
- [jerryx_arg_function](#jerryx_arg_function)
- [jerryx_arg_native_pointer](#jerryx_arg_native_pointer)

## jerryx_arg_round_t

Enum that indicates the rounding policy which will be choose to transform an integer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chosen


**Summary**

All above jerryx_arg_intX(uintX) are used to Create a validation/transformation step
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • That (uintX) is misleading as it looks like a function argument. Alternatives: jerryx_arg_[u]intX (or the same but with parentheses instead of brackets), or jerryx_arg_intX and jerryx_arg_uintX (written out in full).
  • I'd add the word "functions" after _intX (whichever form is used), like: All the above xxxxxx functions
  • Unnecessary capitalization of Create

jerry_value_t jerryx_arg_transform_ ## type (jerryx_arg_js_iterator_t *js_arg_iter_p, \
const jerryx_arg_t *c_arg_p); \
jerry_value_t jerryx_arg_transform_ ## type ## _optional (jerryx_arg_js_iterator_t *js_arg_iter_p, \
const jerryx_arg_t *c_arg_p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent the body of the define


#define DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT(type) \
DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type) \
DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type ## _strict)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent the body of the define

JERRYX_STATIC_ASSERT (sizeof (jerryx_arg_int_option_t) <= sizeof (((jerryx_arg_t *) 0)->extra_info),
jerryx_arg_number_options_t_must_fit_into_extra_info);

#undef JERRYX_STATIC_ASSERT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static assertions should usually not go in (public) headers but in sources (as in jerry-core)

jerry_value_t jerryx_arg_transform_native_pointer_optional (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);

#define DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the DECLARE_ prefix. why not without it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I use the prefix DECLARE_ to indicate it is function declaration.And I use GENERATE_ to indicate it is function impl./defination

jerry_value_t jerryx_arg_transform_ ## type ## _optional (jerryx_arg_js_iterator_t *js_arg_iter_p, \
const jerryx_arg_t *c_arg_p);

#define DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT(type) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop DECLARE_ prefix?

jerry_value_t jerryx_arg_transform_ignore (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);

/**
* The macro used to generate jerryx_arg_xxx for int type.
*/
#define GENERATE_JERRYX_ARG_INT(type) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop GENERATE_ prefix?

DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (string)
DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (boolean)
DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (function)
DECLARE_JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (native_pointer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macros not undef'd after the last use. Note that this header is included by users of jerry-ext (even if it is an "impl.h"), so whatever is (left) defined here, will clutter the namespace of the user after the include of arg.h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the #undef in the end of the usage. Thanks for reminding.

/**
* Indicates the rounding policy which will be choose to transform a integer.
*/
typedef enum __attribute__((packed))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The need for an attribute signals that something is non-standard here. Why is this needed? I guess it is to ensure that jerryx_arg_int_option_t fits into extra_info. But that can be solved in other ways. BTW, is it necessary to expose jerryx_arg_int_option_t to the user? Why not have the jerryx_arg_(u)intX functions accept both a jerryx_arg_round_t and a jerryx_arg_clamp_t argument and let those functions encode (pack) the enums into extra_info? E.g., by casting the enum values to integers and shifting-bitmasking them together into a single int value. As a lib user, you could still create mappings easily but without a need for an extra (public) type, e.g.,

jerryx_arg_int8 (&num0, 0, 0, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED)
jerryx_arg_int8 (&num0, JERRYX_ARG_ROUND, 0, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED)
jerryx_arg_int8 (&num0, 0, JERRYX_ARG_NO_CLAMP, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED)
jerryx_arg_int8 (&num0, JERRYX_ARG_ROUND, JERRYX_ARG_NO_CLAMP, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the discussion with @martijnthe is in this thread #1883 (comment)

The main purpose is that

  • we don't want the signature of the jerryx_arg_[u]intX is too long
  • jerryx_arg_int_option_t indicates that these are int-specific options from normal number. And people can just pass a empty one to use the default options
  • we avoid the extra computation (like encode (pack) the enums into extra_info) in jerryx_arg_[u]intX since they are inlined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having read the discussion, I'm still not convinced about the need for a public option type.

  • In the discussion, the option type was planned to contain rounding, clamping, optionality, requiredness - but eventually it just remained with rounding and clamping. Optionality and requiredness are still passed as regular args. So, the functions already take 4 arguments. How much longer is a 5-args signature, when does it count as too long?
  • The empty initializer is not C but C++, as also mentioned in the discussion. How better is {0} than 0, 0? (Provided that you'd like to use 0-based defaults instead of writing out the enum values explicitly.)
  • I'm not sure that accessing fields of a struct of packed enums is in any ways different from doing the shifting-masking manually (at the machine code level). But if we want to avoid bit ops, we can still have a non-public struct of two uint8_t fields that contain the enum values. Then, at implementation source code level, we only have to write out casting explicitly (to enum and uint8_t), and the compiler will hide the packing.

@jiangzidong
Copy link
Contributor Author

@akosthekiss updated
About the prefix DECLARE_ ,
it is used to indicate the following part is function declaration instead of the definition. And I use GENERATE_ to indicate it is function impl./defination

const jerryx_arg_t *c_arg_p) /**< the native arg */
{
return jerryx_arg_transform_number_strict_common (js_arg_iter_p,
c_arg_p->dest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) I see no reason for breaking this function call into multiple lines. we are still within line length limits.

const jerryx_arg_t *c_arg_p) /**< the native arg */
{
return jerryx_arg_transform_number_common (js_arg_iter_p,
c_arg_p->dest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) no need to break this call either

* @return the rounded double
*/
static double
jerryx_arg_helper_round (double x) /**< the number need to round */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to round -> to be rounded ?

(jerry_char_t *) "The number is out of range.");
}
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are using an enum with potentially multiple values (not necessarily just a boolean yes/no), we should get future-proof. There are three alternatives:

  • if (x == VALUE1) { ... } else { assert (x == VALUE2); ... }
  • if (x == VALUE1) { ... } else if (x == VALUE2) { ... } else { assert (false); }
  • switch (x) { case VALUE1: ... break; case VALUE2: ... break; default: assert (false); }

Perhaps the last one is the best (after fleshing the sketch out).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to perfect the above code after we impl the assert/unreachable/ndebug such things in jerryx?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, leave it for now

{
*d = floor (*d);
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here as above, for safely covering all enum values (especially since we really have more than two possible values in this case)

{
const double t = d < min ? min : d;
return t > max ? max : t;
} /* jerryx_arg_helper_clamp */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any reason to have the clamp and round helpers in separate functions? both are called from jerryx_arg_helper_process_double only, and each only once.

out of the target type.

- JERRYX_ARG_CLAMP - it will try to clamp the number into the range.
- JERRYX_ARG_NO_CLAMP - it will throw range error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(doc) the descriptions are not word by word identical to the comments in the sources. it might be easier to maintain documentation if they would always be kept in sync. (btw, why is "try to clamp"? can clamping not succeed?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clamping will not fail. I will change that


/**
* The structure indicates the options used to transform integer argument.
* It will be passed into jerryx_arg_t's extra_info filed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filed -> field (only if this struct survives)

test_validator_int1_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
const jerry_value_t this_val __attribute__((unused)), /**< this value */
const jerry_value_t args_p[], /**< arguments list */
const jerry_length_t args_cnt) /**< arguments length */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is off by one space

jerryx_arg_int8 (&num5, opt_ceil, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_int8 (&num6, opt_default, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),
jerryx_arg_int8 (&num7, opt_no_clamp, JERRYX_ARG_COERCE, JERRYX_ARG_REQUIRED),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this empty line within the array?

Copy link
Member

@akosthekiss akosthekiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One extra request. There is a bad style leftover from old: the body of JERRYX_ARG_TRANSFORM_OPTIONAL was not indented originally. Would it be possible to fix it in this PR?

* Use the macro to define two transform functions for int type.
* One is non-strict version and the other is the strict version.
*/
#define JERRYX_ARG_TRANSFORM_FUNC_FOR_INT(type, min, max) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this macro still contains two function implementations which are copy-paste clones of each other except for "_strict". the macro should contain a single function (template) only, and strictness should be an additional macro argument


Indicates the clamping policy which will be chosen to transform a integer.
If the policy is NO_CLAMP, and the number is out of range,
then the transformer will throw an range error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an range -> a range


## jerryx_arg_clamp_t

Indicates the clamping policy which will be chosen to transform a integer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a integer -> an integer

@@ -90,13 +90,52 @@ typedef enum
JERRYX_ARG_REQUIRED
} jerryx_arg_optional_t;

/**
* Indicates the rounding policy which will be chosen to transform a integer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a integer -> an integer

} jerryx_arg_round_t;

/**
* Indicates the clamping policy which will be chosen to transform a integer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a integer -> an integer

/**
* Indicates the clamping policy which will be chosen to transform a integer.
* If the policy is NO_CLAMP, and the number is out of range,
* then the transformer will throw an range error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an range -> a range

{ \
.func = func, \
.dest = (void *) dest, \
.extra_info = *extra_info_p \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't this be simply .extra_info = *(uintptr_t *) &int_option; without an extra extra_info_p variable?

(jerry_char_t *) "The number is out of range.");
}
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, leave it for now

@jiangzidong
Copy link
Contributor Author

fixed @akosthekiss

Copy link
Member

@akosthekiss akosthekiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly style issues (strange that style checker did not catch them)

note: the patch will need to be rebased (and conflicts resolved) once the object properties PR lands because of some changes to arg.impl.h .


/**
* Use the macro to define two transform functions for int type.
* One is non-strict version and the other is the strict version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment is outdated

*/
#define JERRYX_ARG_TRANSFORM_FUNC_FOR_INT_TEMPLATE(type, suffix, min, max) \
jerry_value_t jerryx_arg_transform_ ## type ## suffix (jerryx_arg_js_iterator_t *js_arg_iter_p, \
const jerryx_arg_t *c_arg_p) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is off by one space

} \
*(type ## _t *) c_arg_p->dest = (type ## _t) tmp; \
return rv; \
} \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing backslash not needed

JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (uint16, 0, UINT16_MAX)
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (int16, INT16_MIN, INT16_MAX)
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (uint32, 0, UINT32_MAX)
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (int32, INT32_MIN , INT32_MAX)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra spacing (INT32_MIN , INT32_MAX)

jerryx_arg_round_t round_flag, \
jerryx_arg_clamp_t clamp_flag, \
jerryx_arg_coerce_t coerce_flag, \
jerryx_arg_optional_t opt_flag);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation off by one space

jerry_value_t jerryx_arg_transform_native_pointer_optional (jerryx_arg_js_iterator_t *js_arg_iter_p,

#define JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type) \
jerry_value_t jerryx_arg_transform_ ## type (jerryx_arg_js_iterator_t *js_arg_iter_p, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra spacing (type ()


#define JERRYX_ARG_TRANSFORM_FUNC_FOR_INT(type, min, max) \
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT_TEMPLATE(type, _strict, min, max) \
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT_TEMPLATE(type, , min, max)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space at TEMPLATE( (between end of macro name and opening parenthesis)


#define JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT(type) \
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type) \
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL(type ## _strict)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space at OPTIONAL(

@jiangzidong
Copy link
Contributor Author

fixed and rebased to the latest master @akosthekiss

*/
#define JERRYX_ARG_TRANSFORM_FUNC_FOR_INT_TEMPLATE(type, suffix, min, max) \
jerry_value_t jerryx_arg_transform_ ## type ## suffix (jerryx_arg_js_iterator_t *js_arg_iter_p, \
const jerryx_arg_t *c_arg_p) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation still off by one space

JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (string)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (boolean)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (function)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL (native_pointer)
jerry_value_t jerryx_arg_transform_ignore (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);
jerry_value_t jerryx_arg_transform_object_props (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);
jerry_value_t jerryx_arg_transform_object_props_optional (jerryx_arg_js_iterator_t *js_arg_iter_p,
const jerryx_arg_t *c_arg_p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why haven't these been refactored to use JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL? (that's what I was referring to in my previous review)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mainly focused on the conflict after rebasing to the integer-check patch, and missed this one.. Fixed

TEST_ASSERT (num7 == 123);

jerry_release_value (is_ok);
validator_int_count ++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like extra spacing

TEST_ASSERT (num11 == -2147483647);

jerry_release_value (is_ok);
validator_int_count ++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like extra spacing

@jiangzidong
Copy link
Contributor Author

What about the current update? @akosthekiss


- return value - the created `jerryx_arg_t` instance.
- `dest` - pointer to the `int32_t` where the result should be stored.
- `round_flag` - the roudning policy.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roudning -> rounding

JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (number)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (string)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (boolean)
JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT (object_props)
Copy link
Member

@akosthekiss akosthekiss Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not right. JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL_AND_STRICT will declare

jerryx_arg_transform_object_props
jerryx_arg_transform_object_props_optional
jerryx_arg_transform_object_props_strict
jerryx_arg_transform_object_props_strict_optional

but the latter two do not exist. JERRYX_ARG_TRANSFORM_FUNC_WITH_OPTIONAL should be used instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. Stupid mistake...

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
Copy link
Member

@akosthekiss akosthekiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jiangzidong jiangzidong merged commit b153475 into jerryscript-project:master Jun 22, 2017
@jiangzidong jiangzidong deleted the arg-int branch June 22, 2017 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement jerry-ext Related to the jerry-ext library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants