details: https://hg.nginx.org/njs/rev/47cdd4680fc2 branches: changeset: 1195:47cdd4680fc2 user: Alexander Borisov <alexander.bori...@nginx.com> date: Tue Oct 22 19:58:52 2019 +0300 description: Removed argument prototypes for built-in functions.
Many JS functions do not have fixed prototypes as in C. For example String.prototype.replace() accepts RegExp or String as the first argument. diffstat: src/njs_array.c | 123 +++- src/njs_boolean.c | 4 +- src/njs_builtin.c | 89 +-- src/njs_crypto.c | 22 +- src/njs_date.c | 692 +++++++++++++++++++++++++----- src/njs_error.c | 21 +- src/njs_fs.c | 51 +- src/njs_function.c | 162 +------ src/njs_function.h | 11 - src/njs_json.c | 25 +- src/njs_math.c | 790 ++++++++++++++++++++------------- src/njs_module.c | 8 + src/njs_number.c | 253 ++++++---- src/njs_number.h | 2 +- src/njs_object.c | 82 +- src/njs_regexp.c | 39 +- src/njs_string.c | 915 +++++++++++++++++++++++++++++---------- src/njs_value.c | 37 - src/njs_value.h | 7 +- src/test/njs_interactive_test.c | 2 +- src/test/njs_unit_test.c | 18 + 21 files changed, 2161 insertions(+), 1192 deletions(-) diffs (truncated from 6563 to 1000 lines): diff -r 425cbc7d57b3 -r 47cdd4680fc2 src/njs_array.c --- a/src/njs_array.c Tue Oct 22 14:43:30 2019 +0300 +++ b/src/njs_array.c Tue Oct 22 19:58:52 2019 +0300 @@ -304,7 +304,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("isArray"), - .value = njs_native_function(njs_array_is_array, 1, 0), + .value = njs_native_function(njs_array_is_array, 1), .writable = 1, .configurable = 1, }, @@ -314,7 +314,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("of"), - .value = njs_native_function(njs_array_of, 0, 0), + .value = njs_native_function(njs_array_of, 0), .writable = 1, .configurable = 1, }, @@ -423,6 +423,11 @@ njs_array_prototype_slice(njs_vm_t *vm, uint32_t object_length; njs_int_t ret; + if (njs_slow_path(njs_is_null_or_undefined(njs_arg(args, nargs, 0)))) { + njs_type_error(vm, "cannot convert undefined to object"); + return NJS_ERROR; + } + ret = njs_object_length(vm, njs_arg(args, nargs, 0), &object_length); if (njs_slow_path(ret == NJS_ERROR)) { return ret; @@ -933,11 +938,17 @@ static njs_int_t njs_array_prototype_splice(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + int64_t n, start, length, items, delta, delete; njs_int_t ret; - njs_int_t n, start, length, items, delta, delete; njs_uint_t i; + njs_value_t *value; njs_array_t *array, *deleted; + if (njs_slow_path(njs_is_null_or_undefined(njs_arg(args, nargs, 0)))) { + njs_type_error(vm, "cannot convert undefined to object"); + return NJS_ERROR; + } + array = NULL; start = 0; delete = 0; @@ -947,7 +958,17 @@ njs_array_prototype_splice(njs_vm_t *vm, length = array->length; if (nargs > 1) { - start = njs_number(&args[1]); + value = njs_argument(args, 1); + + if (njs_slow_path(!njs_is_number(value))) { + ret = njs_value_to_integer(vm, value, &start); + if (njs_slow_path(ret != NJS_OK)) { + return ret; + } + + } else { + start = njs_number_to_integer(njs_number(value)); + } if (start < 0) { start += length; @@ -963,7 +984,17 @@ njs_array_prototype_splice(njs_vm_t *vm, delete = length - start; if (nargs > 2) { - n = njs_number(&args[2]); + value = njs_argument(args, 2); + + if (njs_slow_path(!njs_is_number(value))) { + ret = njs_value_to_integer(vm, value, &n); + if (njs_slow_path(ret != NJS_OK)) { + return ret; + } + + } else { + n = njs_number_to_integer(njs_number(value)); + } if (n < 0) { delete = 0; @@ -988,12 +1019,7 @@ njs_array_prototype_splice(njs_vm_t *vm, deleted->start[i] = array->start[n]; } - items = nargs - 3; - - if (items < 0) { - items = 0; - } - + items = (nargs > 3) ? nargs - 3: 0; delta = items - delete; if (delta != 0) { @@ -1037,6 +1063,11 @@ njs_array_prototype_reverse(njs_vm_t *vm njs_value_t value; njs_array_t *array; + if (njs_slow_path(njs_is_null_or_undefined(njs_arg(args, nargs, 0)))) { + njs_type_error(vm, "cannot convert undefined to object"); + return NJS_ERROR; + } + if (njs_is_array(&args[0])) { array = njs_array(&args[0]); length = array->length; @@ -1100,6 +1131,18 @@ njs_array_prototype_join(njs_vm_t *vm, n njs_value_t *value, *values; njs_string_prop_t separator, string; + if (njs_slow_path(njs_is_null_or_undefined(njs_arg(args, nargs, 0)))) { + njs_type_error(vm, "cannot convert undefined to object"); + return NJS_ERROR; + } + + if (nargs > 1 && !njs_is_string(&args[1])) { + ret = njs_value_to_string(vm, &args[1], &args[1]); + if (njs_slow_path(ret != NJS_OK)) { + return ret; + } + } + if (!njs_is_array(&args[0]) || njs_array_len(&args[0]) == 0) { vm->retval = njs_string_empty; return NJS_OK; @@ -2614,7 +2657,6 @@ njs_array_string_sort(njs_vm_t *vm, njs_ static const njs_function_t njs_array_string_sort_function = { .object = { .type = NJS_FUNCTION, .shared = 1, .extensible = 1 }, .native = 1, - .args_types = { NJS_SKIP_ARG, NJS_STRING_ARG, NJS_STRING_ARG }, .args_offset = 1, .u.native = njs_array_string_sort, }; @@ -2735,8 +2777,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("slice"), - .value = njs_native_function(njs_array_prototype_slice, 2, - NJS_OBJECT_ARG, NJS_INTEGER_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_array_prototype_slice, 2), .writable = 1, .configurable = 1, }, @@ -2744,7 +2785,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("push"), - .value = njs_native_function(njs_array_prototype_push, 1, 0), + .value = njs_native_function(njs_array_prototype_push, 1), .writable = 1, .configurable = 1, }, @@ -2752,7 +2793,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("pop"), - .value = njs_native_function(njs_array_prototype_pop, 0, 0), + .value = njs_native_function(njs_array_prototype_pop, 0), .writable = 1, .configurable = 1, }, @@ -2760,7 +2801,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("unshift"), - .value = njs_native_function(njs_array_prototype_unshift, 1, 0), + .value = njs_native_function(njs_array_prototype_unshift, 1), .writable = 1, .configurable = 1, }, @@ -2768,7 +2809,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("shift"), - .value = njs_native_function(njs_array_prototype_shift, 0, 0), + .value = njs_native_function(njs_array_prototype_shift, 0), .writable = 1, .configurable = 1, }, @@ -2776,8 +2817,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("splice"), - .value = njs_native_function(njs_array_prototype_splice, 2, - NJS_OBJECT_ARG, NJS_INTEGER_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_array_prototype_splice, 2), .writable = 1, .configurable = 1, }, @@ -2785,8 +2825,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("reverse"), - .value = njs_native_function(njs_array_prototype_reverse, 0, - NJS_OBJECT_ARG), + .value = njs_native_function(njs_array_prototype_reverse, 0), .writable = 1, .configurable = 1, }, @@ -2794,7 +2833,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("toString"), - .value = njs_native_function(njs_array_prototype_to_string, 0, 0), + .value = njs_native_function(njs_array_prototype_to_string, 0), .writable = 1, .configurable = 1, }, @@ -2802,8 +2841,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("join"), - .value = njs_native_function(njs_array_prototype_join, 1, - NJS_OBJECT_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_array_prototype_join, 1), .writable = 1, .configurable = 1, }, @@ -2811,7 +2849,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("concat"), - .value = njs_native_function(njs_array_prototype_concat, 1, 0), + .value = njs_native_function(njs_array_prototype_concat, 1), .writable = 1, .configurable = 1, }, @@ -2819,8 +2857,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("indexOf"), - .value = njs_native_function(njs_array_prototype_index_of, 1, - NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_array_prototype_index_of, 1), .writable = 1, .configurable = 1, }, @@ -2828,8 +2865,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("lastIndexOf"), - .value = njs_native_function(njs_array_prototype_last_index_of, 1, - NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_array_prototype_last_index_of, 1), .writable = 1, .configurable = 1, }, @@ -2838,8 +2874,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("includes"), - .value = njs_native_function(njs_array_prototype_includes, 1, - NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_array_prototype_includes, 1), .writable = 1, .configurable = 1, }, @@ -2847,7 +2882,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("forEach"), - .value = njs_native_function(njs_array_prototype_for_each, 1, 0), + .value = njs_native_function(njs_array_prototype_for_each, 1), .writable = 1, .configurable = 1, }, @@ -2855,7 +2890,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("some"), - .value = njs_native_function(njs_array_prototype_some, 1, 0), + .value = njs_native_function(njs_array_prototype_some, 1), .writable = 1, .configurable = 1, }, @@ -2863,7 +2898,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("every"), - .value = njs_native_function(njs_array_prototype_every, 1, 0), + .value = njs_native_function(njs_array_prototype_every, 1), .writable = 1, .configurable = 1, }, @@ -2872,9 +2907,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("fill"), - .value = njs_native_function(njs_array_prototype_fill, 1, - NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_NUMBER_ARG, - NJS_NUMBER_ARG), + .value = njs_native_function(njs_array_prototype_fill, 1), .writable = 1, .configurable = 1, }, @@ -2882,7 +2915,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("filter"), - .value = njs_native_function(njs_array_prototype_filter, 1, 0), + .value = njs_native_function(njs_array_prototype_filter, 1), .writable = 1, .configurable = 1, }, @@ -2891,7 +2924,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("find"), - .value = njs_native_function(njs_array_prototype_find, 1, 0), + .value = njs_native_function(njs_array_prototype_find, 1), .writable = 1, .configurable = 1, }, @@ -2900,7 +2933,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("findIndex"), - .value = njs_native_function(njs_array_prototype_find_index, 1, 0), + .value = njs_native_function(njs_array_prototype_find_index, 1), .writable = 1, .configurable = 1, }, @@ -2908,7 +2941,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("map"), - .value = njs_native_function(njs_array_prototype_map, 1, 0), + .value = njs_native_function(njs_array_prototype_map, 1), .writable = 1, .configurable = 1, }, @@ -2916,7 +2949,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("reduce"), - .value = njs_native_function(njs_array_prototype_reduce, 1, 0), + .value = njs_native_function(njs_array_prototype_reduce, 1), .writable = 1, .configurable = 1, }, @@ -2924,7 +2957,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("reduceRight"), - .value = njs_native_function(njs_array_prototype_reduce_right, 1, 0), + .value = njs_native_function(njs_array_prototype_reduce_right, 1), .writable = 1, .configurable = 1, }, @@ -2932,7 +2965,7 @@ static const njs_object_prop_t njs_arra { .type = NJS_PROPERTY, .name = njs_string("sort"), - .value = njs_native_function(njs_array_prototype_sort, 1, 0), + .value = njs_native_function(njs_array_prototype_sort, 1), .writable = 1, .configurable = 1, }, diff -r 425cbc7d57b3 -r 47cdd4680fc2 src/njs_boolean.c --- a/src/njs_boolean.c Tue Oct 22 14:43:30 2019 +0300 +++ b/src/njs_boolean.c Tue Oct 22 19:58:52 2019 +0300 @@ -145,7 +145,7 @@ static const njs_object_prop_t njs_bool { .type = NJS_PROPERTY, .name = njs_string("valueOf"), - .value = njs_native_function(njs_boolean_prototype_value_of, 0, 0), + .value = njs_native_function(njs_boolean_prototype_value_of, 0), .writable = 1, .configurable = 1, }, @@ -153,7 +153,7 @@ static const njs_object_prop_t njs_bool { .type = NJS_PROPERTY, .name = njs_string("toString"), - .value = njs_native_function(njs_boolean_prototype_to_string, 0, 0), + .value = njs_native_function(njs_boolean_prototype_to_string, 0), .writable = 1, .configurable = 1, }, diff -r 425cbc7d57b3 -r 47cdd4680fc2 src/njs_builtin.c --- a/src/njs_builtin.c Tue Oct 22 14:43:30 2019 +0300 +++ b/src/njs_builtin.c Tue Oct 22 19:58:52 2019 +0300 @@ -11,7 +11,6 @@ typedef struct { njs_function_native_t native; - uint8_t args_types[NJS_ARGS_TYPES_MAX]; } njs_function_init_t; @@ -95,29 +94,25 @@ const njs_object_init_t *njs_constructo const njs_function_init_t njs_native_constructors[] = { /* SunC does not allow empty array initialization. */ - { njs_object_constructor, { 0 } }, - { njs_array_constructor, { 0 } }, - { njs_boolean_constructor, { 0 } }, - { njs_number_constructor, { NJS_SKIP_ARG, NJS_NUMBER_ARG } }, - { njs_string_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_function_constructor, { 0 } }, - { njs_regexp_constructor, { 0 } }, - { njs_date_constructor, { 0 } }, - { njs_hash_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_hmac_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG, - NJS_STRING_ARG } }, - { njs_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_eval_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_internal_error_constructor, - { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_range_error_constructor, - { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_reference_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_syntax_error_constructor, - { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_type_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_uri_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, - { njs_memory_error_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG } }, + { njs_object_constructor }, + { njs_array_constructor }, + { njs_boolean_constructor }, + { njs_number_constructor }, + { njs_string_constructor }, + { njs_function_constructor}, + { njs_regexp_constructor }, + { njs_date_constructor }, + { njs_hash_constructor }, + { njs_hmac_constructor }, + { njs_error_constructor }, + { njs_eval_error_constructor }, + { njs_internal_error_constructor }, + { njs_range_error_constructor }, + { njs_reference_error_constructor }, + { njs_syntax_error_constructor }, + { njs_type_error_constructor }, + { njs_uri_error_constructor }, + { njs_memory_error_constructor }, }; @@ -331,8 +326,6 @@ njs_builtin_objects_create(njs_vm_t *vm) func->u.native = f->native; - memcpy(func->args_types, f->args_types, NJS_ARGS_TYPES_MAX); - ret = njs_object_hash_init(vm, &func->object.shared_hash, obj); if (njs_slow_path(ret != NJS_OK)) { return NJS_ERROR; @@ -1038,8 +1031,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("isFinite"), - .value = njs_native_function(njs_number_is_finite, 1, - NJS_SKIP_ARG, NJS_NUMBER_ARG), + .value = njs_native_function(njs_number_global_is_finite, 1), .writable = 1, .configurable = 1, }, @@ -1047,8 +1039,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("isNaN"), - .value = njs_native_function(njs_number_global_is_nan, 1, - NJS_SKIP_ARG, NJS_NUMBER_ARG), + .value = njs_native_function(njs_number_global_is_nan, 1), .writable = 1, .configurable = 1, }, @@ -1056,8 +1047,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("parseFloat"), - .value = njs_native_function(njs_number_parse_float, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_number_parse_float, 1), .writable = 1, .configurable = 1, }, @@ -1065,8 +1055,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("parseInt"), - .value = njs_native_function(njs_number_parse_int, 2, - NJS_SKIP_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG), + .value = njs_native_function(njs_number_parse_int, 2), .writable = 1, .configurable = 1, }, @@ -1074,7 +1063,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("toString"), - .value = njs_native_function(njs_object_prototype_to_string, 0, 0), + .value = njs_native_function(njs_object_prototype_to_string, 0), .writable = 1, .configurable = 1, }, @@ -1082,8 +1071,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("encodeURI"), - .value = njs_native_function(njs_string_encode_uri, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_string_encode_uri, 1), .writable = 1, .configurable = 1, }, @@ -1091,8 +1079,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_long_string("encodeURIComponent"), - .value = njs_native_function(njs_string_encode_uri_component, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_string_encode_uri_component, 1), .writable = 1, .configurable = 1, }, @@ -1100,8 +1087,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("decodeURI"), - .value = njs_native_function(njs_string_decode_uri, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_string_decode_uri, 1), .writable = 1, .configurable = 1, }, @@ -1109,8 +1095,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_long_string("decodeURIComponent"), - .value = njs_native_function(njs_string_decode_uri_component, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_string_decode_uri_component, 1), .writable = 1, .configurable = 1, }, @@ -1118,7 +1103,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("eval"), - .value = njs_native_function(njs_eval_function, 1, 0), + .value = njs_native_function(njs_eval_function, 1), .writable = 1, .configurable = 1, }, @@ -1126,9 +1111,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("setTimeout"), - .value = njs_native_function(njs_set_timeout, 2, - NJS_SKIP_ARG, NJS_FUNCTION_ARG, - NJS_NUMBER_ARG), + .value = njs_native_function(njs_set_timeout, 2), .writable = 1, .configurable = 1, }, @@ -1136,8 +1119,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("setImmediate"), - .value = njs_native_function(njs_set_immediate, 4, - NJS_SKIP_ARG, NJS_FUNCTION_ARG), + .value = njs_native_function(njs_set_immediate, 4), .writable = 1, .configurable = 1, }, @@ -1145,8 +1127,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("clearTimeout"), - .value = njs_native_function(njs_clear_timeout, 1, - NJS_SKIP_ARG, NJS_NUMBER_ARG), + .value = njs_native_function(njs_clear_timeout, 1), .writable = 1, .configurable = 1, }, @@ -1154,8 +1135,7 @@ static const njs_object_prop_t njs_glob { .type = NJS_PROPERTY, .name = njs_string("require"), - .value = njs_native_function(njs_module_require, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_module_require, 1), .writable = 1, .configurable = 1, }, @@ -1183,8 +1163,7 @@ static const njs_object_prop_t njs_njs_ { .type = NJS_PROPERTY, .name = njs_string("dump"), - .value = njs_native_function(njs_dump_value, 0, - NJS_SKIP_ARG, NJS_SKIP_ARG, NJS_NUMBER_ARG), + .value = njs_native_function(njs_dump_value, 0), .configurable = 1, }, }; diff -r 425cbc7d57b3 -r 47cdd4680fc2 src/njs_crypto.c --- a/src/njs_crypto.c Tue Oct 22 14:43:30 2019 +0300 +++ b/src/njs_crypto.c Tue Oct 22 19:58:52 2019 +0300 @@ -323,7 +323,7 @@ static const njs_object_prop_t njs_hash { .type = NJS_PROPERTY, .name = njs_string("toString"), - .value = njs_native_function(njs_hash_prototype_to_string, 0, 0), + .value = njs_native_function(njs_hash_prototype_to_string, 0), .writable = 1, .configurable = 1, }, @@ -331,8 +331,7 @@ static const njs_object_prop_t njs_hash { .type = NJS_PROPERTY, .name = njs_string("update"), - .value = njs_native_function(njs_hash_prototype_update, 0, - NJS_OBJECT_ARG, NJS_SKIP_ARG), + .value = njs_native_function(njs_hash_prototype_update, 0), .writable = 1, .configurable = 1, }, @@ -340,8 +339,7 @@ static const njs_object_prop_t njs_hash { .type = NJS_PROPERTY, .name = njs_string("digest"), - .value = njs_native_function(njs_hash_prototype_digest, 0, - NJS_OBJECT_ARG, NJS_SKIP_ARG), + .value = njs_native_function(njs_hash_prototype_digest, 0), .writable = 1, .configurable = 1, }, @@ -585,7 +583,7 @@ static const njs_object_prop_t njs_hmac { .type = NJS_PROPERTY, .name = njs_string("toString"), - .value = njs_native_function(njs_hmac_prototype_to_string, 0, 0), + .value = njs_native_function(njs_hmac_prototype_to_string, 0), .writable = 1, .configurable = 1, }, @@ -593,8 +591,7 @@ static const njs_object_prop_t njs_hmac { .type = NJS_PROPERTY, .name = njs_string("update"), - .value = njs_native_function(njs_hmac_prototype_update, 0, - NJS_OBJECT_ARG, NJS_SKIP_ARG), + .value = njs_native_function(njs_hmac_prototype_update, 0), .writable = 1, .configurable = 1, }, @@ -602,8 +599,7 @@ static const njs_object_prop_t njs_hmac { .type = NJS_PROPERTY, .name = njs_string("digest"), - .value = njs_native_function(njs_hmac_prototype_digest, 0, - NJS_OBJECT_ARG, NJS_SKIP_ARG), + .value = njs_native_function(njs_hmac_prototype_digest, 0), .writable = 1, .configurable = 1, }, @@ -650,8 +646,7 @@ static const njs_object_prop_t njs_cryp { .type = NJS_PROPERTY, .name = njs_string("createHash"), - .value = njs_native_function(njs_crypto_create_hash, 0, - NJS_SKIP_ARG), + .value = njs_native_function(njs_crypto_create_hash, 0), .writable = 1, .configurable = 1, }, @@ -659,8 +654,7 @@ static const njs_object_prop_t njs_cryp { .type = NJS_PROPERTY, .name = njs_string("createHmac"), - .value = njs_native_function(njs_crypto_create_hmac, 0, - NJS_SKIP_ARG), + .value = njs_native_function(njs_crypto_create_hmac, 0), .writable = 1, .configurable = 1, }, diff -r 425cbc7d57b3 -r 47cdd4680fc2 src/njs_date.c --- a/src/njs_date.c Tue Oct 22 14:43:30 2019 +0300 +++ b/src/njs_date.c Tue Oct 22 19:58:52 2019 +0300 @@ -389,9 +389,17 @@ static njs_int_t njs_date_parse(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { - double time; + double time; + njs_int_t ret; if (nargs > 1) { + if (njs_slow_path(!njs_is_string(&args[1]))) { + ret = njs_value_to_string(vm, &args[1], &args[1]); + if (njs_slow_path(ret != NJS_OK)) { + return ret; + } + } + time = njs_date_string_parse(&args[1]); } else { @@ -1016,7 +1024,7 @@ static const njs_object_prop_t njs_date { .type = NJS_PROPERTY, .name = njs_string("UTC"), - .value = njs_native_function(njs_date_utc, 7, 0), + .value = njs_native_function(njs_date_utc, 7), .writable = 1, .configurable = 1, }, @@ -1024,7 +1032,7 @@ static const njs_object_prop_t njs_date { .type = NJS_PROPERTY, .name = njs_string("now"), - .value = njs_native_function(njs_date_now, 0, 0), + .value = njs_native_function(njs_date_now, 0), .writable = 1, .configurable = 1, }, @@ -1032,8 +1040,7 @@ static const njs_object_prop_t njs_date { .type = NJS_PROPERTY, .name = njs_string("parse"), - .value = njs_native_function(njs_date_parse, 1, - NJS_SKIP_ARG, NJS_STRING_ARG), + .value = njs_native_function(njs_date_parse, 1), .writable = 1, .configurable = 1, }, @@ -1051,6 +1058,13 @@ static njs_int_t njs_date_prototype_value_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + njs_set_number(&vm->retval, njs_date(&args[0])->time); return NJS_OK; @@ -1061,6 +1075,13 @@ static njs_int_t njs_date_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + return njs_date_string(vm, "%a %b %d %Y %T GMT%z (%Z)", njs_date(&args[0])->time); } @@ -1070,6 +1091,13 @@ static njs_int_t njs_date_prototype_to_date_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + return njs_date_string(vm, "%a %b %d %Y", njs_date(&args[0])->time); } @@ -1078,6 +1106,13 @@ static njs_int_t njs_date_prototype_to_time_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + return njs_date_string(vm, "%T GMT%z (%Z)", njs_date(&args[0])->time); } @@ -1120,6 +1155,13 @@ njs_date_prototype_to_utc_string(njs_vm_ static const char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + time = njs_date(&args[0])->time; if (!isnan(time)) { @@ -1144,6 +1186,13 @@ static njs_int_t njs_date_prototype_to_iso_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + return njs_date_to_string(vm, &vm->retval, &args[0]); } @@ -1189,6 +1238,13 @@ njs_date_prototype_get_full_year(njs_vm_ time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1212,6 +1268,13 @@ njs_date_prototype_get_utc_full_year(njs time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1235,6 +1298,13 @@ njs_date_prototype_get_month(njs_vm_t *v time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1258,6 +1328,13 @@ njs_date_prototype_get_utc_month(njs_vm_ time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1282,6 +1359,13 @@ njs_date_prototype_get_date(njs_vm_t *vm time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1305,6 +1389,13 @@ njs_date_prototype_get_utc_date(njs_vm_t time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1328,6 +1419,13 @@ njs_date_prototype_get_day(njs_vm_t *vm, time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1351,6 +1449,13 @@ njs_date_prototype_get_utc_day(njs_vm_t time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1374,6 +1479,13 @@ njs_date_prototype_get_hours(njs_vm_t *v time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1398,6 +1510,13 @@ njs_date_prototype_get_utc_hours(njs_vm_ time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1421,6 +1540,13 @@ njs_date_prototype_get_minutes(njs_vm_t time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1445,6 +1571,13 @@ njs_date_prototype_get_utc_minutes(njs_v time_t clock; struct tm tm; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } + value = njs_date(&args[0])->time; if (njs_fast_path(!isnan(value))) { @@ -1466,6 +1599,13 @@ njs_date_prototype_get_seconds(njs_vm_t { double value; + if (njs_slow_path(!njs_is_date(&args[0]))) { + njs_type_error(vm, "cannot convert %s to date", + njs_type_string(args[0].type)); + + return NJS_ERROR; + } _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx-devel