-
Notifications
You must be signed in to change notification settings - Fork 684
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
#include <math.h> | ||
|
||
#include "jerryscript-ext/arg.h" | ||
#include "jerryscript.h" | ||
|
||
|
@@ -39,14 +41,15 @@ jerryx_arg_transform_optional (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< ava | |
} /* jerryx_arg_transform_optional */ | ||
|
||
/** | ||
* Tranform a JS argument to a double. Type coercion is not allowed. | ||
* The common part in transforming a JS argument to a number (double or certain int) type. | ||
* Type coercion is not allowed. | ||
* | ||
* @return jerry undefined: the transformer passes, | ||
* jerry error: the transformer fails. | ||
*/ | ||
jerry_value_t | ||
jerryx_arg_transform_number_strict (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */ | ||
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 */ | ||
{ | ||
jerry_value_t js_arg = jerryx_arg_js_iterator_pop (js_arg_iter_p); | ||
|
||
|
@@ -56,21 +59,21 @@ jerryx_arg_transform_number_strict (jerryx_arg_js_iterator_t *js_arg_iter_p, /** | |
(jerry_char_t *) "It is not a number."); | ||
} | ||
|
||
double *dest = c_arg_p->dest; | ||
*dest = jerry_get_number_value (js_arg); | ||
*number_p = jerry_get_number_value (js_arg); | ||
|
||
return jerry_create_undefined (); | ||
} /* jerryx_arg_transform_number_strict */ | ||
} /* jerryx_arg_transform_number_strict_common */ | ||
|
||
/** | ||
* Transform a JS argument to a double. Type coercion is allowed. | ||
* The common part in transforming a JS argument to a number (double or certain int) type. | ||
* Type coercion is allowed. | ||
* | ||
* @return jerry undefined: the transformer passes, | ||
* jerry error: the transformer fails. | ||
*/ | ||
jerry_value_t | ||
jerryx_arg_transform_number (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */ | ||
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 */ | ||
{ | ||
jerry_value_t js_arg = jerryx_arg_js_iterator_pop (js_arg_iter_p); | ||
|
||
|
@@ -84,13 +87,118 @@ jerryx_arg_transform_number (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< avail | |
(jerry_char_t *) "It can not be converted to a number."); | ||
} | ||
|
||
double *dest = c_arg_p->dest; | ||
*dest = jerry_get_number_value (to_number); | ||
*number_p = jerry_get_number_value (to_number); | ||
jerry_release_value (to_number); | ||
|
||
return jerry_create_undefined (); | ||
} /* jerryx_arg_transform_number_common */ | ||
|
||
/** | ||
* Transform a JS argument to a double. Type coercion is not allowed. | ||
* | ||
* @return jerry undefined: the transformer passes, | ||
* jerry error: the transformer fails. | ||
*/ | ||
jerry_value_t | ||
jerryx_arg_transform_number_strict (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */ | ||
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); | ||
} /* jerryx_arg_transform_number_strict */ | ||
|
||
/** | ||
* Tranform a JS argument to a double. Type coercion is allowed. | ||
* | ||
* @return jerry undefined: the transformer passes, | ||
* jerry error: the transformer fails. | ||
*/ | ||
jerry_value_t | ||
jerryx_arg_transform_number (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< available JS args */ | ||
const jerryx_arg_t *c_arg_p) /**< the native arg */ | ||
{ | ||
return jerryx_arg_transform_number_common (js_arg_iter_p, c_arg_p->dest); | ||
} /* jerryx_arg_transform_number */ | ||
|
||
/** | ||
* Helper function to process a double number before converting it | ||
* to an integer. | ||
* | ||
* @return jerry undefined: the transformer passes, | ||
* jerry error: the transformer fails. | ||
*/ | ||
static jerry_value_t | ||
jerryx_arg_helper_process_double (double *d, /**< [in, out] the number to be processed */ | ||
double min, /**< the min value for clamping */ | ||
double max, /**< the max value for clamping */ | ||
jerryx_arg_int_option_t option) /**< the converting policies */ | ||
{ | ||
if (option.clamp == JERRYX_ARG_NO_CLAMP) | ||
{ | ||
if (*d > max || *d < min) | ||
{ | ||
return jerry_create_error (JERRY_ERROR_TYPE, | ||
(jerry_char_t *) "The number is out of range."); | ||
} | ||
} | ||
else | ||
{ | ||
*d = *d < min ? min : *d; | ||
*d = *d > max ? max : *d; | ||
} | ||
|
||
if (option.round == JERRYX_ARG_ROUND) | ||
{ | ||
*d = (*d >= 0.0) ? floor (*d + 0.5) : ceil (*d - 0.5); | ||
} | ||
else if (option.round == JERRYX_ARG_FLOOR) | ||
{ | ||
*d = floor (*d); | ||
} | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
{ | ||
*d = ceil (*d); | ||
} | ||
|
||
return jerry_create_undefined (); | ||
} /* jerryx_arg_helper_process_double */ | ||
|
||
/** | ||
* Use the macro to define thr transform functions for int type. | ||
*/ | ||
#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) \ | ||
{ \ | ||
double tmp; \ | ||
jerry_value_t rv = jerryx_arg_transform_number ## suffix ## _common (js_arg_iter_p, &tmp); \ | ||
if (jerry_value_has_error_flag (rv)) \ | ||
{ \ | ||
return rv; \ | ||
} \ | ||
jerry_release_value (rv); \ | ||
jerryx_arg_int_option_t *options_p = (jerryx_arg_int_option_t *) &c_arg_p->extra_info; \ | ||
rv = jerryx_arg_helper_process_double (&tmp, min, max, *options_p); \ | ||
if (jerry_value_has_error_flag (rv)) \ | ||
{ \ | ||
return rv; \ | ||
} \ | ||
*(type ## _t *) c_arg_p->dest = (type ## _t) tmp; \ | ||
return rv; \ | ||
} | ||
|
||
#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) | ||
|
||
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (uint8, 0, UINT8_MAX) | ||
JERRYX_ARG_TRANSFORM_FUNC_FOR_INT (int8, INT8_MIN, INT8_MAX) | ||
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) | ||
|
||
#undef JERRYX_ARG_TRANSFORM_FUNC_FOR_INT_TEMPLATE | ||
#undef JERRYX_ARG_TRANSFORM_FUNC_FOR_INT | ||
/** | ||
* Tranform a JS argument to a boolean. Type coercion is not allowed. | ||
* | ||
|
@@ -294,12 +402,12 @@ jerryx_arg_transform_object_props (jerryx_arg_js_iterator_t *js_arg_iter_p, /**< | |
* Define transformer for optional argument. | ||
*/ | ||
#define JERRYX_ARG_TRANSFORM_OPTIONAL(type) \ | ||
jerry_value_t \ | ||
jerryx_arg_transform_ ## type ## _optional (jerryx_arg_js_iterator_t *js_arg_iter_p, \ | ||
const jerryx_arg_t *c_arg_p) \ | ||
{ \ | ||
return jerryx_arg_transform_optional (js_arg_iter_p, c_arg_p, jerryx_arg_transform_ ## type); \ | ||
} | ||
jerry_value_t \ | ||
jerryx_arg_transform_ ## type ## _optional (jerryx_arg_js_iterator_t *js_arg_iter_p, \ | ||
const jerryx_arg_t *c_arg_p) \ | ||
{ \ | ||
return jerryx_arg_transform_optional (js_arg_iter_p, c_arg_p, jerryx_arg_transform_ ## type); \ | ||
} | ||
|
||
JERRYX_ARG_TRANSFORM_OPTIONAL (number) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (number_strict) | ||
|
@@ -311,6 +419,21 @@ JERRYX_ARG_TRANSFORM_OPTIONAL (function) | |
JERRYX_ARG_TRANSFORM_OPTIONAL (native_pointer) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (object_props) | ||
|
||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint8) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint16) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint32) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int8) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int16) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int32) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int8_strict) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int16_strict) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (int32_strict) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint8_strict) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint16_strict) | ||
JERRYX_ARG_TRANSFORM_OPTIONAL (uint32_strict) | ||
|
||
#undef JERRYX_ARG_TRANSFORM_OPTIONAL | ||
|
||
/** | ||
* Ignore the JS argument. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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