Skip to content

Commit

Permalink
Reserved atom_id in njs_value_t.
Browse files Browse the repository at this point in the history
To have enough space in njs_value_t this patch removed built-in short string.
The short strings were an optimization which will be less useful when
atomic strings are introduced.
  • Loading branch information
VadimZhestikov committed Sep 17, 2024
1 parent 2871981 commit 8249156
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 672 deletions.
32 changes: 21 additions & 11 deletions src/njs_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ njs_int_t
njs_array_convert_to_slow_array(njs_vm_t *vm, njs_array_t *array)
{
uint32_t i, length;
njs_int_t ret;
njs_value_t index, value;
njs_object_prop_t *prop;

Expand All @@ -153,7 +154,10 @@ njs_array_convert_to_slow_array(njs_vm_t *vm, njs_array_t *array)

for (i = 0; i < length; i++) {
if (njs_is_valid(&array->start[i])) {
njs_uint32_to_string(&index, i);
ret = njs_uint32_to_string(vm, &index, i);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
prop = njs_object_property_add(vm, &value, &index, 0);
if (njs_slow_path(prop == NULL)) {
return NJS_ERROR;
Expand All @@ -176,8 +180,6 @@ njs_array_length_redefine(njs_vm_t *vm, njs_value_t *value, uint32_t length,
{
njs_object_prop_t *prop;

static const njs_value_t string_length = njs_string("length");

if (njs_slow_path(!njs_is_array(value))) {
njs_internal_error(vm, "njs_array_length_redefine() "
"applied to non-array");
Expand Down Expand Up @@ -787,8 +789,8 @@ static njs_int_t
njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
int64_t start, int64_t length, njs_value_t *retval)
{
u_char *c, buf[4];
size_t size;
u_char *dst;
uint32_t n;
njs_int_t ret;
njs_array_t *array, *keys;
Expand Down Expand Up @@ -828,10 +830,14 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,

do {
value = &array->start[n++];
dst = njs_string_short_start(value);
dst = njs_utf8_copy(dst, &src, end);
size = dst - njs_string_short_start(value);
njs_string_short_set(value, size, 1);
c = buf;
c = njs_utf8_copy(c, &src, end);
size = c - buf;

ret = njs_string_new(vm, value, buf, size, 1);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}

length--;
} while (length != 0);
Expand Down Expand Up @@ -1603,6 +1609,9 @@ njs_array_prototype_to_reversed(njs_vm_t *vm, njs_value_t *args,
}


static const njs_value_t join_string = njs_string("join");


njs_int_t
njs_array_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
Expand All @@ -1611,8 +1620,6 @@ njs_array_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_value_t value;
njs_lvlhsh_query_t lhq;

static const njs_value_t join_string = njs_string("join");

if (njs_is_object(njs_argument(args, 0))) {
njs_object_property_init(&lhq, &join_string, NJS_JOIN_HASH);

Expand Down Expand Up @@ -2776,7 +2783,10 @@ njs_sort_indexed_properties(njs_vm_t *vm, njs_value_t *obj, int64_t length,
njs_value_assign(&p->value, &start[i]);

} else {
njs_uint32_to_string(&key, i);
ret = njs_uint32_to_string(vm, &key, i);
if (njs_slow_path(ret != NJS_OK)) {
goto exception;
}
ret = njs_value_property(vm, obj, &key, &p->value);
if (njs_slow_path(ret == NJS_ERROR)) {
goto exception;
Expand Down
19 changes: 10 additions & 9 deletions src/njs_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ njs_buffer_from(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}


static const njs_str_t str_buffer = njs_str("Buffer");


static njs_int_t
njs_buffer_from_object(njs_vm_t *vm, njs_value_t *value, njs_value_t *retval)
{
Expand All @@ -349,9 +352,6 @@ njs_buffer_from_object(njs_vm_t *vm, njs_value_t *value, njs_value_t *retval)
njs_value_t val, data, length;
njs_typed_array_t *buffer;

static const njs_value_t string_length = njs_string("length");
static const njs_str_t str_buffer = njs_str("Buffer");

next:

ret = njs_value_property(vm, value, njs_value_arg(&string_length),
Expand Down Expand Up @@ -2358,6 +2358,9 @@ njs_buffer_prototype_swap(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}


static const njs_value_t string_buffer = njs_string("Buffer");


static njs_int_t
njs_buffer_prototype_to_json(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
Expand All @@ -2371,8 +2374,6 @@ njs_buffer_prototype_to_json(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_typed_array_t *ta;
njs_array_buffer_t *buffer;

static const njs_value_t string_buffer = njs_string("Buffer");

ta = njs_buffer_slot(vm, njs_argument(args, 0), "this");
if (njs_slow_path(ta == NULL)) {
return NJS_ERROR;
Expand Down Expand Up @@ -2660,7 +2661,7 @@ static const njs_object_prop_t njs_buffer_constructor_properties[] =

NJS_DECLARE_PROP_NATIVE("allocUnsafe", njs_buffer_alloc_safe, 0, 0),

NJS_DECLARE_PROP_LNATIVE("allocUnsafeSlow", njs_buffer_alloc_safe, 1, 0),
NJS_DECLARE_PROP_NATIVE("allocUnsafeSlow", njs_buffer_alloc_safe, 1, 0),

NJS_DECLARE_PROP_NATIVE("byteLength", njs_buffer_byte_length, 1, 0),

Expand Down Expand Up @@ -2695,9 +2696,9 @@ static const njs_object_prop_t njs_buffer_constants_properties[] =
NJS_DECLARE_PROP_VALUE("MAX_LENGTH", njs_value(NJS_NUMBER, 1, INT32_MAX),
NJS_OBJECT_PROP_VALUE_E),

NJS_DECLARE_PROP_LVALUE("MAX_STRING_LENGTH",
njs_value(NJS_NUMBER, 1, NJS_STRING_MAX_LENGTH),
NJS_OBJECT_PROP_VALUE_E),
NJS_DECLARE_PROP_VALUE("MAX_STRING_LENGTH",
njs_value(NJS_NUMBER, 1, NJS_STRING_MAX_LENGTH),
NJS_OBJECT_PROP_VALUE_E),
};


Expand Down
43 changes: 23 additions & 20 deletions src/njs_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ njs_ext_on(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
}


static const njs_value_t size_string = njs_string("size");
static const njs_value_t nblocks_string = njs_string("nblocks");
static const njs_value_t page_string = njs_string("page_size");
static const njs_value_t cluster_string = njs_string("cluster_size");


static njs_int_t
njs_ext_memory_stats(njs_vm_t *vm, njs_object_prop_t *prop,
njs_value_t *unused, njs_value_t *unused2, njs_value_t *retval)
Expand All @@ -659,11 +665,6 @@ njs_ext_memory_stats(njs_vm_t *vm, njs_object_prop_t *prop,
njs_object_t *stat;
njs_mp_stat_t mp_stat;

static const njs_value_t size_string = njs_string("size");
static const njs_value_t nblocks_string = njs_string("nblocks");
static const njs_value_t page_string = njs_string("page_size");
static const njs_value_t cluster_string = njs_string("cluster_size");

stat = njs_object_alloc(vm);
if (njs_slow_path(stat == NULL)) {
return NJS_ERROR;
Expand Down Expand Up @@ -729,7 +730,12 @@ njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *prop,
return NJS_DECLINED;
}

if (njs_slow_path(prop->name.type == NJS_SYMBOL)) {
return NJS_DECLINED;
}

njs_string_get(&prop->name, &lhq.key);

lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
lhq.proto = &njs_lexer_hash_proto;

Expand Down Expand Up @@ -954,11 +960,11 @@ static const njs_object_prop_t njs_global_this_object_properties[] =

NJS_DECLARE_PROP_NATIVE("encodeURI", njs_string_encode_uri, 1, 0),

NJS_DECLARE_PROP_LNATIVE("encodeURIComponent", njs_string_encode_uri, 1, 1),
NJS_DECLARE_PROP_NATIVE("encodeURIComponent", njs_string_encode_uri, 1, 1),

NJS_DECLARE_PROP_NATIVE("decodeURI", njs_string_decode_uri, 1, 0),

NJS_DECLARE_PROP_LNATIVE("decodeURIComponent", njs_string_decode_uri, 1, 1),
NJS_DECLARE_PROP_NATIVE("decodeURIComponent", njs_string_decode_uri, 1, 1),

NJS_DECLARE_PROP_NATIVE("atob", njs_string_atob, 1, 0),

Expand Down Expand Up @@ -1054,15 +1060,10 @@ static const njs_object_prop_t njs_global_this_object_properties[] =
NJS_OBJ_TYPE_FLOAT64_ARRAY, NJS_FLOAT64ARRAY_HASH,
NJS_OBJECT_PROP_VALUE_CW),

{
.type = NJS_PROPERTY_HANDLER,
.name = njs_long_string("Uint8ClampedArray"),
.u.value = njs_prop_handler2(njs_top_level_constructor,
NJS_OBJ_TYPE_UINT8_CLAMPED_ARRAY,
NJS_UINT8CLAMPEDARRAY_HASH),
.writable = 1,
.configurable = 1,
},
NJS_DECLARE_PROP_HANDLER("Uint8ClampedArray", njs_top_level_constructor,
NJS_OBJ_TYPE_UINT8_CLAMPED_ARRAY,
NJS_UINT8CLAMPEDARRAY_HASH,
NJS_OBJECT_PROP_VALUE_CW),

NJS_DECLARE_PROP_HANDLER("Boolean", njs_top_level_constructor,
NJS_OBJ_TYPE_BOOLEAN, NJS_BOOLEAN_HASH,
Expand Down Expand Up @@ -1181,6 +1182,9 @@ static const njs_object_init_t njs_njs_object_init = {
};


static const njs_value_t argv_string = njs_string("argv");


static njs_int_t
njs_process_object_argv(njs_vm_t *vm, njs_object_prop_t *pr,
njs_value_t *process, njs_value_t *unused, njs_value_t *retval)
Expand All @@ -1192,8 +1196,6 @@ njs_process_object_argv(njs_vm_t *vm, njs_object_prop_t *pr,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

static const njs_value_t argv_string = njs_string("argv");

argv = njs_array_alloc(vm, 1, vm->options.argc, 0);
if (njs_slow_path(argv == NULL)) {
return NJS_ERROR;
Expand Down Expand Up @@ -1315,6 +1317,9 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, char **environment)
}


static const njs_value_t env_string = njs_string("env");


static njs_int_t
njs_process_object_env(njs_vm_t *vm, njs_object_prop_t *pr,
njs_value_t *process, njs_value_t *unused, njs_value_t *retval)
Expand All @@ -1324,8 +1329,6 @@ njs_process_object_env(njs_vm_t *vm, njs_object_prop_t *pr,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

static const njs_value_t env_string = njs_string("env");

env = njs_object_alloc(vm);
if (njs_slow_path(env == NULL)) {
return NJS_ERROR;
Expand Down
45 changes: 23 additions & 22 deletions src/njs_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,9 @@ njs_date_prototype_set_fields(njs_vm_t *vm, njs_value_t *args,
}


static const njs_value_t to_iso_string = njs_string("toISOString");


static njs_int_t
njs_date_prototype_to_json(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
Expand All @@ -1447,8 +1450,6 @@ njs_date_prototype_to_json(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_value_t value;
njs_lvlhsh_query_t lhq;

static const njs_value_t to_iso_string = njs_string("toISOString");

if (njs_is_object(njs_argument(args, 0))) {
njs_object_property_init(&lhq, &to_iso_string, NJS_TO_ISO_STRING_HASH);

Expand Down Expand Up @@ -1494,13 +1495,13 @@ static const njs_object_prop_t njs_date_prototype_properties[] =
NJS_DECLARE_PROP_NATIVE("toLocaleString", njs_date_prototype_to_string, 0,
NJS_DATE_FMT_TO_STRING),

NJS_DECLARE_PROP_LNATIVE("toLocaleDateString",
njs_date_prototype_to_string, 0,
NJS_DATE_FMT_TO_DATE_STRING),
NJS_DECLARE_PROP_NATIVE("toLocaleDateString",
njs_date_prototype_to_string, 0,
NJS_DATE_FMT_TO_DATE_STRING),

NJS_DECLARE_PROP_LNATIVE("toLocaleTimeString",
njs_date_prototype_to_string, 0,
NJS_DATE_FMT_TO_TIME_STRING),
NJS_DECLARE_PROP_NATIVE("toLocaleTimeString",
njs_date_prototype_to_string, 0,
NJS_DATE_FMT_TO_TIME_STRING),

NJS_DECLARE_PROP_NATIVE("toUTCString",
njs_date_prototype_to_string, 0,
Expand Down Expand Up @@ -1570,26 +1571,26 @@ static const njs_object_prop_t njs_date_prototype_properties[] =
njs_date_prototype_get_field, 0,
njs_date_magic(NJS_DATE_SEC, 0)),

NJS_DECLARE_PROP_LNATIVE("getMilliseconds",
njs_date_prototype_get_field, 0,
njs_date_magic(NJS_DATE_MSEC, 1)),
NJS_DECLARE_PROP_NATIVE("getMilliseconds",
njs_date_prototype_get_field, 0,
njs_date_magic(NJS_DATE_MSEC, 1)),

NJS_DECLARE_PROP_LNATIVE("getUTCMilliseconds",
njs_date_prototype_get_field, 0,
njs_date_magic(NJS_DATE_MSEC, 0)),
NJS_DECLARE_PROP_NATIVE("getUTCMilliseconds",
njs_date_prototype_get_field, 0,
njs_date_magic(NJS_DATE_MSEC, 0)),

NJS_DECLARE_PROP_LNATIVE("getTimezoneOffset",
njs_date_prototype_get_timezone_offset, 0, 0),
NJS_DECLARE_PROP_NATIVE("getTimezoneOffset",
njs_date_prototype_get_timezone_offset, 0, 0),

NJS_DECLARE_PROP_NATIVE("setTime", njs_date_prototype_set_time, 1, 0),

NJS_DECLARE_PROP_LNATIVE("setMilliseconds",
njs_date_prototype_set_fields, 1,
njs_date_magic2(NJS_DATE_MSEC, 1, 1)),
NJS_DECLARE_PROP_NATIVE("setMilliseconds",
njs_date_prototype_set_fields, 1,
njs_date_magic2(NJS_DATE_MSEC, 1, 1)),

NJS_DECLARE_PROP_LNATIVE("setUTCMilliseconds",
njs_date_prototype_set_fields, 1,
njs_date_magic2(NJS_DATE_MSEC, 1, 0)),
NJS_DECLARE_PROP_NATIVE("setUTCMilliseconds",
njs_date_prototype_set_fields, 1,
njs_date_magic2(NJS_DATE_MSEC, 1, 0)),

NJS_DECLARE_PROP_NATIVE("setSeconds",
njs_date_prototype_set_fields, 2,
Expand Down
Loading

0 comments on commit 8249156

Please sign in to comment.