Skip to content

Commit

Permalink
Use atom_hash* instead of keywords_hash in the code, atoms not used yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimZhestikov committed Oct 9, 2024
1 parent 7efc4f8 commit 0d49cc0
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 165 deletions.
1 change: 0 additions & 1 deletion auto/sources
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ NJS_LIB_SRCS=" \
src/njs_vm.c \
src/njs_vmcode.c \
src/njs_lexer.c \
src/njs_lexer_keyword.c \
src/njs_parser.c \
src/njs_variable.c \
src/njs_scope.c \
Expand Down
11 changes: 6 additions & 5 deletions src/njs_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ njs_builtin_objects_create(njs_vm_t *vm, njs_vm_t *vm_parent)

vm->shared = shared;

njs_lvlhsh_init(&shared->keywords_hash);

njs_lvlhsh_init(&shared->values_hash);

njs_atom_hash_init();

if (vm_parent == NULL) {
/* njs_lvlhsh_init(&vm->atom_hash_shared); // done by zalign */
/* njs_lvlhsh_init(&vm->atom_hash_shared); done by zalign. */

ret = njs_flathsh_alloc_copy(vm->mem_pool, &vm->atom_hash,
&njs_atom_hash);
Expand Down Expand Up @@ -765,10 +763,13 @@ njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *prop,
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
lhq.proto = &njs_lexer_hash_proto;

ret = njs_lvlhsh_find(&vm->shared->keywords_hash, &lhq);
ret = njs_lvlhsh_find(&vm->atom_hash, &lhq);

if (njs_slow_path(ret != NJS_OK || lhq.value == NULL)) {
return NJS_DECLINED;
ret = njs_lvlhsh_find(&vm->atom_hash_shared, &lhq);
if (njs_slow_path(ret != NJS_OK || lhq.value == NULL)) {
return NJS_DECLINED;
}
}

var_node.key = (uintptr_t) lhq.value;
Expand Down
2 changes: 1 addition & 1 deletion src/njs_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ njs_function_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
file = njs_str_value("runtime");

ret = njs_parser_init(vm, &parser, NULL, &file, str.start,
str.start + str.length, 1);
str.start + str.length);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
Expand Down
67 changes: 18 additions & 49 deletions src/njs_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,6 @@ njs_generate_var_statement_after(njs_vm_t *vm, njs_generator_t *generator,
njs_variable_t *var;
njs_parser_node_t *lvalue, *expr;
njs_vmcode_move_t *move;
const njs_lexer_entry_t *lex_entry;

lvalue = node->left;
expr = node->right;
Expand All @@ -1093,19 +1092,17 @@ njs_generate_var_statement_after(njs_vm_t *vm, njs_generator_t *generator,
node->index = expr->index;
node->temporary = expr->temporary;

if ((expr->token_type == NJS_TOKEN_FUNCTION_EXPRESSION
if (expr->token_type == NJS_TOKEN_FUNCTION_EXPRESSION
|| expr->token_type == NJS_TOKEN_ASYNC_FUNCTION_EXPRESSION)
&& njs_values_same(&expr->u.value.data.u.lambda->name, &njs_atom.vs_))
{
lex_entry = njs_lexer_entry(node->left->u.reference.unique_id);
if (njs_slow_path(lex_entry == NULL)) {
ret = njs_values_same(&expr->u.value.data.u.lambda->name,
(njs_value_t *)&njs_atom.vs_); /* empty string */
if (njs_slow_path(ret < 0)) {
return NJS_ERROR;
}

ret = njs_string_create(vm, &expr->u.value.data.u.lambda->name,
lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
if (ret) {
expr->u.value.data.u.lambda->name =
*((njs_value_t *)node->left->u.reference.unique_id);
}
}

Expand Down Expand Up @@ -3022,11 +3019,9 @@ static njs_int_t
njs_generate_global_property_set(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node_dst, njs_parser_node_t *node_src)
{
njs_int_t ret;
njs_value_t property;
njs_variable_t *var;
njs_vmcode_prop_set_t *prop_set;
const njs_lexer_entry_t *lex_entry;

var = njs_variable_reference(vm, node_dst);
if (var == NULL) {
Expand All @@ -3036,16 +3031,7 @@ njs_generate_global_property_set(njs_vm_t *vm, njs_generator_t *generator,
prop_set->value = node_dst->index;
prop_set->object = njs_scope_global_this_index();

lex_entry = njs_lexer_entry(node_dst->u.reference.unique_id);
if (njs_slow_path(lex_entry == NULL)) {
return NJS_ERROR;
}

ret = njs_string_create(vm, &property, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
property = *((njs_value_t *)node_dst->u.reference.unique_id);

prop_set->property = njs_scope_global_index(vm, &property,
generator->runtime);
Expand Down Expand Up @@ -3631,7 +3617,8 @@ njs_generate_function_expression(njs_vm_t *vm, njs_generator_t *generator,
njs_variable_t *var;
njs_function_lambda_t *lambda;
njs_vmcode_function_t *function;
const njs_lexer_entry_t *lex_entry;
njs_lexer_entry_t lex_entr;
njs_lexer_entry_t *lex_entry = &lex_entr;

var = njs_variable_reference(vm, node->left);
if (njs_slow_path(var == NULL)) {
Expand All @@ -3645,10 +3632,7 @@ njs_generate_function_expression(njs_vm_t *vm, njs_generator_t *generator,

lambda = node->u.value.data.u.lambda;

lex_entry = njs_lexer_entry(var->unique_id);
if (njs_slow_path(lex_entry == NULL)) {
return NJS_ERROR;
}
njs_lexer_entry(var->unique_id, lex_entry);

ret = njs_generate_function_scope(vm, generator, lambda, node,
&lex_entry->name);
Expand Down Expand Up @@ -4192,7 +4176,8 @@ njs_generate_function_declaration(njs_vm_t *vm, njs_generator_t *generator,
njs_variable_t *var;
njs_function_t *function;
njs_function_lambda_t *lambda;
const njs_lexer_entry_t *lex_entry;
njs_lexer_entry_t lex_entr;
njs_lexer_entry_t *lex_entry = &lex_entr;

var = njs_variable_reference(vm, node);
if (njs_slow_path(var == NULL)) {
Expand All @@ -4206,10 +4191,7 @@ njs_generate_function_declaration(njs_vm_t *vm, njs_generator_t *generator,

lambda = njs_variable_lambda(var);

lex_entry = njs_lexer_entry(node->u.reference.unique_id);
if (njs_slow_path(lex_entry == NULL)) {
return NJS_ERROR;
}
njs_lexer_entry(node->u.reference.unique_id, lex_entry);

ret = njs_string_create(vm, &lambda->name, lex_entry->name.start,
lex_entry->name.length);
Expand Down Expand Up @@ -5387,11 +5369,9 @@ 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;
const njs_lexer_entry_t *lex_entry;

index = njs_generate_temp_index_get(vm, generator, node);
if (njs_slow_path(index == NJS_INDEX_ERROR)) {
Expand All @@ -5409,16 +5389,7 @@ njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
return NJS_ERROR;
}

lex_entry = njs_lexer_entry(node->u.reference.unique_id);
if (njs_slow_path(lex_entry == NULL)) {
return NJS_ERROR;
}

ret = njs_string_create(vm, &property, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
property = *((njs_value_t *)node->u.reference.unique_id);

prop_get->property = njs_scope_global_index(vm, &property,
generator->runtime);
Expand All @@ -5441,7 +5412,8 @@ njs_generate_reference_error(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
njs_vmcode_error_t *ref_err;
const njs_lexer_entry_t *lex_entry;
njs_lexer_entry_t lex_entr;
njs_lexer_entry_t *lex_entry = &lex_entr;

if (njs_slow_path(!node->u.reference.not_defined)) {
njs_internal_error(vm, "variable is not defined but not_defined "
Expand All @@ -5453,10 +5425,7 @@ njs_generate_reference_error(njs_vm_t *vm, njs_generator_t *generator,
NULL);

ref_err->type = NJS_OBJ_TYPE_REF_ERROR;
lex_entry = njs_lexer_entry(node->u.reference.unique_id);
if (njs_slow_path(lex_entry == NULL)) {
return NJS_ERROR;
}
njs_lexer_entry(node->u.reference.unique_id, lex_entry);

return njs_name_copy(vm, &ref_err->u.name, &lex_entry->name);
}
Expand Down
88 changes: 45 additions & 43 deletions src/njs_lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,15 @@ static const njs_lexer_multi_t njs_assignment_token[] = {

njs_int_t
njs_lexer_init(njs_vm_t *vm, njs_lexer_t *lexer, njs_str_t *file,
u_char *start, u_char *end, njs_uint_t runtime,
njs_int_t init_lexer_memory)
u_char *start, u_char *end)
{
if (init_lexer_memory) {
njs_memzero(lexer, sizeof(njs_lexer_t));

}

lexer->file = *file;
lexer->start = start;
lexer->end = end;
lexer->line = 1;
lexer->keywords_hash = (runtime) ? &vm->keywords_hash
: &vm->shared->keywords_hash;
lexer->mem_pool = vm->mem_pool;

lexer->mem_pool = vm->atom_hash_mem_pool;
lexer->vm = vm;

njs_queue_init(&lexer->preread);

Expand Down Expand Up @@ -712,26 +706,33 @@ njs_lexer_make_token(njs_lexer_t *lexer, njs_lexer_token_t *token)
static njs_int_t
njs_lexer_hash_test(njs_lvlhsh_query_t *lhq, void *data)
{
njs_lexer_entry_t *entry;
u_char *start;
njs_value_t *name;

name = data;

entry = data;
/* string. */

if (entry->name.length == lhq->key.length
&& memcmp(entry->name.start, lhq->key.start, lhq->key.length) == 0)
{
if (lhq->key.length != name->string.data->size) {
return NJS_DECLINED;
}

start = name->string.data->start;

if (memcmp(start, lhq->key.start, lhq->key.length) == 0) {
return NJS_OK;
}

return NJS_DECLINED;
}


static njs_lexer_entry_t *
njs_lexer_keyword_find(njs_lexer_t *lexer, u_char *key, size_t length,
static njs_value_t *
njs_lexer_keyword_find(njs_vm_t *vm, u_char *key, size_t size, size_t length,
uint32_t hash)
{
njs_int_t ret;
njs_lexer_entry_t *entry;
njs_value_t *entry;
njs_lvlhsh_query_t lhq;

lhq.key.start = key;
Expand All @@ -740,30 +741,36 @@ njs_lexer_keyword_find(njs_lexer_t *lexer, u_char *key, size_t length,
lhq.key_hash = hash;
lhq.proto = &njs_lexer_hash_proto;

ret = njs_lvlhsh_find(lexer->keywords_hash, &lhq);
ret = njs_lvlhsh_find(&vm->atom_hash, &lhq);
if (ret == NJS_OK) {
return lhq.value;
}

ret = njs_lvlhsh_find(&vm->atom_hash_shared, &lhq);
if (ret == NJS_OK) {
return lhq.value;
}

entry = njs_mp_alloc(lexer->mem_pool, sizeof(njs_lexer_entry_t));
entry = njs_mp_alloc(vm->atom_hash_mem_pool, sizeof(njs_value_t));
if (njs_slow_path(entry == NULL)) {
return NULL;
}

entry->name.start = njs_mp_alloc(lexer->mem_pool, length + 1);
if (njs_slow_path(entry->name.start == NULL)) {
ret = njs_string_create(vm, entry, key, length);
if (njs_slow_path(ret != NJS_OK)) {
return NULL;
}

memcpy(entry->name.start, key, length);

entry->name.start[length] = '\0';
entry->name.length = length;
entry->string.atom_id = vm->atom_hash_atom_id++;
if (entry->string.atom_id == 0) {
return NULL;
}
entry->string.token_type = 0;

lhq.value = entry;
lhq.pool = lexer->mem_pool;
lhq.pool = vm->atom_hash_mem_pool;

ret = njs_lvlhsh_insert(lexer->keywords_hash, &lhq);
ret = njs_lvlhsh_insert(&vm->atom_hash, &lhq);
if (njs_slow_path(ret != NJS_OK)) {
return NULL;
}
Expand All @@ -777,8 +784,7 @@ njs_lexer_word(njs_lexer_t *lexer, njs_lexer_token_t *token)
{
u_char *p, c;
uint32_t hash_id;
const njs_lexer_entry_t *entry;
const njs_lexer_keyword_entry_t *key_entry;
const njs_value_t *entry;

/* TODO: UTF-8 */

Expand Down Expand Up @@ -817,24 +823,20 @@ njs_lexer_word(njs_lexer_t *lexer, njs_lexer_token_t *token)
token->text.length = p - token->text.start;
lexer->start = p;

key_entry = njs_lexer_keyword(token->text.start, token->text.length);

if (key_entry == NULL) {
entry = njs_lexer_keyword_find(lexer, token->text.start,
token->text.length, hash_id);
if (njs_slow_path(entry == NULL)) {
return NJS_ERROR;
}
entry = njs_lexer_keyword_find(lexer->vm, token->text.start,
token->text.length, token->text.length,
hash_id);
if (njs_slow_path(entry == NULL)) {
return NJS_ERROR;
}

if (entry->string.token_type == 0) {
token->type = NJS_TOKEN_NAME;
token->keyword_type = NJS_KEYWORD_TYPE_UNDEF;

} else {
entry = &key_entry->value->entry;
token->type = key_entry->value->type;

token->keyword_type = NJS_KEYWORD_TYPE_KEYWORD;
token->keyword_type |= key_entry->value->reserved;
token->type = entry->string.token_id;
token->keyword_type = entry->string.token_type;
}

token->unique_id = (uintptr_t) entry;
Expand Down
Loading

0 comments on commit 0d49cc0

Please sign in to comment.