From: Igor Sysoev Date: Sun, 1 Jan 2017 17:45:59 +0000 (+0300) Subject: Style and small miscellaneous fixes. X-Git-Tag: 0.1.8~12 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=f786ed12d94a15a53e9248649cd7d6d1f9f65b80;p=njs.git Style and small miscellaneous fixes. --- diff --git a/njs/njs_array.c b/njs/njs_array.c index c25f6cd1..05c6f912 100644 --- a/njs/njs_array.c +++ b/njs/njs_array.c @@ -1882,7 +1882,7 @@ static const njs_object_prop_t njs_array_prototype_properties[] = NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG), }, - /* ES6. */ + /* ES7. */ { .type = NJS_METHOD, .name = njs_string("includes"), diff --git a/njs/njs_parser.h b/njs/njs_parser.h index 2f142bb2..902124ab 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -330,7 +330,6 @@ njs_token_t njs_lexer_keyword(njs_lexer_t *lexer); njs_extern_t *njs_parser_external(njs_vm_t *vm, njs_parser_t *parser); njs_parser_node_t *njs_parser(njs_vm_t *vm, njs_parser_t *parser); -njs_parser_node_t *njs_nonrecursive_parser(njs_vm_t *vm, njs_parser_t *parser); njs_token_t njs_parser_arguments(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *parent); njs_token_t njs_parser_expression(njs_vm_t *vm, njs_parser_t *parser, diff --git a/njs/njs_string.c b/njs/njs_string.c index 29a014c4..95a48cce 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -610,7 +610,6 @@ njs_string_prototype_concat(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, length &= mask; start = njs_string_alloc(vm, &vm->retval, size, length); - if (nxt_slow_path(start == NULL)) { return NXT_ERROR; } @@ -903,8 +902,8 @@ njs_string_prototype_substr(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, if (nargs > 1) { start = args[1].data.u.number; - if (start < length) { + if (start < length) { if (start < 0) { start += length; diff --git a/njs/njs_string.h b/njs/njs_string.h index f731837a..e8298533 100644 --- a/njs/njs_string.h +++ b/njs/njs_string.h @@ -12,8 +12,9 @@ /* * nJSVM supports two string variants: * - * 1) short strings which size is lesser than 14 bytes, these strings are - * stored inside njs_value_t (see njs_vm.h for details); + * 1) short strings which size is less than or equal to 14 (NJS_STRING_SHORT) + * bytes, these strings are stored inside njs_value_t (see njs_vm.h for + * details); * * 2) and long strings using additional njs_string_t structure. * This structure has the start field to support external strings. @@ -27,9 +28,10 @@ #define NJS_STRING_MAX_LENGTH 0x7fffffff /* - * Should be power of two to use shift and binary and operations instead of - * division and remainder operations but no less than 16 because the maximum - * length of short string inlined in njs_value_t is less than 16 bytes. + * NJS_STRING_MAP_STRIDE should be power of two to use shift and binary + * AND operations instead of division and remainder operations but no + * less than 16 because the maximum length of short string inlined in + * njs_value_t is less than 16 bytes. */ #define NJS_STRING_MAP_STRIDE 32 @@ -42,30 +44,31 @@ (((length - 1) / NJS_STRING_MAP_STRIDE) * sizeof(uint32_t)) /* - * The JavaScript standard states that strings are stored in UTF-16. - * nJSVM allows to store any byte sequences in strings. A size of the - * string in bytes is stored in the size field. If a byte sequence is - * valid UTF-8 string then its length is stored in the UTF-8 length field. - * Otherwise, the length field is zero. If a string is UTF-8 string then - * string functions work with UTF-8 characters positions and lengths. - * Othersise they work with byte positions and lengths. Using UTF-8 - * encoding does not allow to get quickly a character at specified position. - * To speed up this search a map of offsets is stored after the UTF-8 string. - * The map is aligned to uint32_t and contains byte positions of each - * NJS_STRING_MAP_STRIDE UTF-8 character except zero position. The map - * can be initialized on demand. If a string come outside JavaScript as - * byte sequnece just to be concatenated or to be used in regular expressions - * the offset map is not required. + * ECMAScript strings are stored in UTF-16. nJSVM however, allows to store + * any byte sequences in strings. A size of string in bytes is stored in the + * size field. If byte sequence is valid UTF-8 string then its length is + * stored in the UTF-8 length field. Otherwise, the length field is zero. + * If a string is UTF-8 string then string functions use UTF-8 characters + * positions and lengths. Otherwise they use with byte positions and lengths. + * Using UTF-8 encoding does not allow to get quickly a character at specified + * position. To speed up this search a map of offsets is stored after the + * UTF-8 string. The map is aligned to uint32_t and contains byte positions + * of each NJS_STRING_MAP_STRIDE UTF-8 character except zero position. The + * map can be initialized on demand. Unitialized map is marked with zero + * value in the first map element. If string comes outside JavaScript as + * byte string just to be concatenated or to match regular expressions the + * offset map is not required. * * The map is not allocated: - * 1) if the length is zero hence it is a byte string; - * 2) if the size and length are equal so the string contains only ASCII - * characters map is not required; - * 3) if the length is less than NJS_STRING_MAP_STRIDE. + * 1) if string length is zero hence string is a byte string; + * 2) if string size and length are equal so the string contains only + * ASCII characters and map is not required; + * 3) if string length is less than NJS_STRING_MAP_STRIDE. * * The current implementation does not support Unicode surrogate pairs. - * If offset in map points to surrogate pair then the previous offset - * should be used and so on until start of the string. + * It can be implemented later if it will be required using the following + * algorithm: if offset in map points to surrogate pair then the previous + * offset should be used and so on until start of the string. */ struct njs_string_s { diff --git a/njs/njs_vm.c b/njs/njs_vm.c index 72394c00..d8a5236e 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -3357,7 +3357,7 @@ njs_value_string_copy(njs_vm_t *vm, nxt_str_t *retval, njs_value_t *value, void -njs_vm_throw_exception(njs_vm_t *vm, u_char *buf, uint32_t size) +njs_vm_throw_exception(njs_vm_t *vm, const u_char *buf, uint32_t size) { int32_t length; njs_value_t *value; diff --git a/njs/njs_vm.h b/njs/njs_vm.h index f7bf38c4..ba65a43c 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -1016,7 +1016,7 @@ njs_ret_t njs_value_to_ext_string(njs_vm_t *vm, nxt_str_t *dst, const njs_value_t *src); void njs_number_set(njs_value_t *value, double num); -void njs_vm_throw_exception(njs_vm_t *vm, u_char *buf, uint32_t size); +void njs_vm_throw_exception(njs_vm_t *vm, const u_char *buf, uint32_t size); nxt_int_t njs_builtin_objects_create(njs_vm_t *vm); nxt_int_t njs_builtin_objects_clone(njs_vm_t *vm); diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index e4eb3b2f..6e84d1d9 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -626,6 +626,8 @@ static njs_unit_test_t njs_test[] = { nxt_string("x = '1'; +x + 2"), nxt_string("3") }, + /* Weird things. */ + { nxt_string("'3' -+-+-+ '1' + '1' / '3' * '6' + '2'"), nxt_string("42") }, @@ -641,6 +643,11 @@ static njs_unit_test_t njs_test[] = { nxt_string("!--[][1]"), nxt_string("true") }, + { nxt_string("[].concat[1,2,3]"), + nxt_string("undefined") }, + + /**/ + { nxt_string("'true' == true"), nxt_string("false") },