Skip to content

Commit

Permalink
Use njs_atom_atomize(), atoms not used yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimZhestikov committed Oct 16, 2024
1 parent 78fd258 commit b00efbb
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 50 deletions.
57 changes: 13 additions & 44 deletions src/njs_extern.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,7 @@ njs_external_add(njs_vm_t *vm, njs_arr_t *protos,
prop->enumerable = external->enumerable;

if (external->flags & NJS_EXTERN_SYMBOL) {
switch (external->name.symbol) {
case NJS_SYMBOL_ASYNC_ITERATOR:
prop->name = njs_atom.vw_asyncIterator;
break;
case NJS_SYMBOL_HAS_INSTANCE:
prop->name = njs_atom.vw_hasInstance;
break;
case NJS_SYMBOL_IS_CONCAT_SPREADABLE:
prop->name = njs_atom.vw_isConcatSpreadable;
break;
case NJS_SYMBOL_ITERATOR:
prop->name = njs_atom.vw_iterator;
break;
case NJS_SYMBOL_MATCH:
prop->name = njs_atom.vw_match;
break;
case NJS_SYMBOL_MATCH_ALL:
prop->name = njs_atom.vw_matchAll;
break;
case NJS_SYMBOL_REPLACE:
prop->name = njs_atom.vw_replace;
break;
case NJS_SYMBOL_SEARCH:
prop->name = njs_atom.vw_search;
break;
case NJS_SYMBOL_SPECIES:
prop->name = njs_atom.vw_species;
break;
case NJS_SYMBOL_SPLIT:
prop->name = njs_atom.vw_split;
break;
case NJS_SYMBOL_TO_PRIMITIVE:
prop->name = njs_atom.vw_toPrimitive;
break;
case NJS_SYMBOL_TO_STRING_TAG:
prop->name = njs_atom.vw_toStringTag;
break;
case NJS_SYMBOL_UNSCOPABLES:
prop->name = njs_atom.vw_unscopables;
break;
default:
return NJS_ERROR;
};

njs_set_symbol(&prop->name, external->name.symbol, NULL);
lhq.key_hash = external->name.symbol;

} else {
Expand All @@ -120,10 +77,17 @@ njs_external_add(njs_vm_t *vm, njs_arr_t *protos,
return NJS_ERROR;
}

ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return ret;
}

lhq.key = external->name.string;
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
}



lhq.value = prop;

switch (external->flags & NJS_EXTERN_TYPE_MASK) {
Expand Down Expand Up @@ -275,6 +239,11 @@ njs_external_prop_handler(njs_vm_t *vm, njs_object_prop_t *self,
prop->configurable = self->configurable;
prop->enumerable = self->enumerable;

ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return ret;
}

lhq.value = prop;
njs_string_get(&self->name, &lhq.key);
lhq.key_hash = njs_prop_magic32(self);
Expand Down
14 changes: 14 additions & 0 deletions src/njs_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ njs_function_name_set(njs_vm_t *vm, njs_function_t *function,

prop->configurable = 1;

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NJS_ERROR;
}
}

lhq.key_hash = NJS_NAME_HASH;
lhq.key = njs_str_value("name");
lhq.replace = 0;
Expand Down Expand Up @@ -917,6 +924,13 @@ njs_function_property_prototype_set(njs_vm_t *vm, njs_lvlhsh_t *hash,

prop->writable = 1;

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NULL;
}
}

lhq.value = prop;
lhq.key_hash = NJS_PROTOTYPE_HASH;
lhq.key = njs_str_value("prototype");
Expand Down
6 changes: 6 additions & 0 deletions src/njs_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5369,6 +5369,7 @@ static njs_int_t
njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_bool_t exception)
{
njs_int_t ret;
njs_index_t index;
njs_value_t property;
njs_vmcode_prop_get_t *prop_get;
Expand All @@ -5391,6 +5392,11 @@ njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,

property = *((njs_value_t *)node->u.reference.unique_id);

ret = njs_atom_atomize_key(vm, &property);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}

prop_get->property = njs_scope_global_index(vm, &property,
generator->runtime);
if (njs_slow_path(prop_get->property == NJS_INDEX_ERROR)) {
Expand Down
9 changes: 9 additions & 0 deletions src/njs_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,15 @@ njs_json_parse_object(njs_json_parse_ctx_t *ctx, njs_value_t *value,
goto memory_error;
}

prop->name.atom_id = 0;

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(ctx->vm, &prop->name);
if (ret != NJS_OK) {
return NULL;
}
}

njs_string_get(&prop_name, &lhq.key);
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
lhq.value = prop;
Expand Down
39 changes: 38 additions & 1 deletion src/njs_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ njs_object_hash_create(njs_vm_t *vm, njs_lvlhsh_t *hash,

prop->name = *prop->pname;
if (prop->type != NJS_ACCESSOR) {
prop->u.value = *prop->u.pvalue;
prop->u.value = *prop->u.pvalue;
}
}

Expand Down Expand Up @@ -1352,6 +1352,13 @@ njs_object_define_property(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
value = njs_argument(args, 1);
name = njs_lvalue_arg(&lvalue, args, nargs, 2);

if (!name->atom_id) {
ret = njs_atom_atomize_key(vm, name);
if (ret != NJS_OK) {
return NJS_ERROR;
}
}

ret = njs_object_prop_define(vm, value, name, desc,
NJS_OBJECT_PROP_DESCRIPTOR, 0);
if (njs_slow_path(ret != NJS_OK)) {
Expand Down Expand Up @@ -1415,6 +1422,13 @@ njs_object_define_properties(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
goto done;
}

if (!(&keys->start[i])->atom_id) {
ret = njs_atom_atomize_key(vm, &keys->start[i]);
if (ret != NJS_OK) {
goto done;
}
}

ret = njs_object_prop_define(vm, value, &keys->start[i], &desc,
NJS_OBJECT_PROP_DESCRIPTOR, 0);
if (njs_slow_path(ret != NJS_OK)) {
Expand Down Expand Up @@ -1494,6 +1508,15 @@ njs_object_get_own_property_descriptors(njs_vm_t *vm, njs_value_t *args,

for (i = 0; i < length; i++) {
key = &names->start[i];
if (!key->atom_id) {

ret = njs_atom_atomize_key(vm, key);
if (ret != NJS_OK) {
ret = NJS_ERROR;
goto done;
}
}

ret = njs_object_prop_descriptor(vm, &descriptor, value, key);
if (njs_slow_path(ret != NJS_OK)) {
ret = NJS_ERROR;
Expand Down Expand Up @@ -1975,6 +1998,13 @@ njs_property_prototype_create(njs_vm_t *vm, njs_lvlhsh_t *hash,
return NULL;
}

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NULL;
}
}

njs_set_type_object(njs_prop_value(prop), prototype, prototype->type);

lhq.value = prop;
Expand Down Expand Up @@ -2234,6 +2264,13 @@ njs_property_constructor_set(njs_vm_t *vm, njs_lvlhsh_t *hash,
njs_value_assign(njs_prop_value(prop), constructor);
prop->enumerable = 0;

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NULL;
}
}

lhq.value = prop;
lhq.key_hash = NJS_CONSTRUCTOR_HASH;
lhq.key = njs_str_value("constructor");
Expand Down
21 changes: 21 additions & 0 deletions src/njs_object_prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ njs_object_property_add(njs_vm_t *vm, njs_value_t *object, njs_value_t *key,
return NULL;
}

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NULL;
}
}

lhq.proto = &njs_object_hash_proto;
njs_string_get(&key_value, &lhq.key);
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
Expand Down Expand Up @@ -332,6 +339,13 @@ njs_object_prop_define(njs_vm_t *vm, njs_value_t *object,

} else {

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return NJS_ERROR;
}
}

if ((flags & NJS_OBJECT_PROP_CREATE)) {
ret = njs_primitive_value_to_key(vm, &pq.key, name);
if (njs_slow_path(ret != NJS_OK)) {
Expand Down Expand Up @@ -1096,6 +1110,13 @@ njs_object_prop_init(njs_vm_t *vm, const njs_object_init_t* init,
prop->type = NJS_PROPERTY;
njs_set_object(njs_prop_value(prop), object);

if (!prop->name.atom_id) {
ret = njs_atom_atomize_key(vm, &prop->name);
if (ret != NJS_OK) {
return ret;
}
}

lhq.proto = &njs_object_hash_proto;
njs_string_get(&prop->name, &lhq.key);
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
Expand Down
15 changes: 13 additions & 2 deletions src/njs_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ njs_parser(njs_vm_t *vm, njs_parser_t *parser)
parser->ret = NJS_OK;
}

parser->undefined_id = (uintptr_t) &njs_atom.vs_undefined; //??? do we need this field?
parser->undefined_id = (uintptr_t) &njs_atom.vs_undefined;

njs_queue_init(&parser->stack);

Expand Down Expand Up @@ -6677,7 +6677,7 @@ njs_parser_labelled_statement_after(njs_parser_t *parser,
njs_lexer_token_t *token, njs_queue_link_t *current)
{
njs_int_t ret;
njs_str_t str; //?? remove it
njs_str_t str;
uintptr_t unique_id;
njs_parser_node_t *node;
const njs_value_t *entry;
Expand Down Expand Up @@ -8680,6 +8680,7 @@ njs_parser_string_create(njs_vm_t *vm, njs_lexer_token_t *token,
njs_value_t *value)
{
size_t length;
njs_int_t ret;
njs_str_t dst;

length = njs_decode_utf8_length(&token->text, &dst.length);
Expand All @@ -8695,6 +8696,11 @@ njs_parser_string_create(njs_vm_t *vm, njs_lexer_token_t *token,
dst.length);
}

ret = njs_atom_atomize_key(vm, value);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_TOKEN_ERROR;
}

return NJS_OK;
}

Expand Down Expand Up @@ -8939,6 +8945,11 @@ njs_parser_escape_string_create(njs_parser_t *parser, njs_lexer_token_t *token,
njs_string_utf8_offset_map_init(start, size);
}

ret = njs_atom_atomize_key(parser->vm, value);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_TOKEN_ERROR;
}

return NJS_TOKEN_STRING;
}

Expand Down
5 changes: 5 additions & 0 deletions src/njs_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,11 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_value_t *r, njs_utf8_t utf8,
goto fail;
}

ret = njs_atom_atomize_key(vm, &prop->name);
if (njs_slow_path(ret != NJS_OK)) {
goto fail;
}

prop = njs_object_prop_alloc(vm, &name,
&array->start[group->capture], 1);
if (njs_slow_path(prop == NULL)) {
Expand Down
Loading

0 comments on commit b00efbb

Please sign in to comment.