Skip to content

Instantly share code, notes, and snippets.

@xeioex
Created February 20, 2019 14:10
Show Gist options
  • Save xeioex/b763420e36210ee381a9d3d9fd3206b3 to your computer and use it in GitHub Desktop.
Save xeioex/b763420e36210ee381a9d3d9fd3206b3 to your computer and use it in GitHub Desktop.
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1550668590 -10800
# Wed Feb 20 16:16:30 2019 +0300
# Node ID 8250061df72a46278714165f835adc52f432056a
# Parent 8c422e42448e23eeacf85cad5f559941fd64a0ca
Introduced nxt_file_basename() and nxt_file_dirname().
diff --git a/njs/njs_shell.c b/njs/njs_shell.c
--- a/njs/njs_shell.c
+++ b/njs/njs_shell.c
@@ -216,7 +216,9 @@ main(int argc, char **argv)
if (!opts.quiet) {
if (opts.file != NULL) {
- nxt_file_name(&vm_options.file, opts.file);
+ vm_options.file.start = (u_char *) opts.file;
+ vm_options.file.length = strlen(opts.file);
+ nxt_file_basename(&vm_options.file, &vm_options.file);
} else {
vm_options.file = nxt_string_value("shell");
diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c
+++ b/njs/test/njs_unit_test.c
@@ -11994,6 +11994,97 @@ njs_vm_object_alloc_test(njs_vm_t * vm,
}
+static nxt_int_t
+nxt_file_basename_test(njs_vm_t * vm, nxt_bool_t disassemble,
+ nxt_bool_t verbose)
+{
+ nxt_str_t name;
+ nxt_bool_t success;
+ nxt_uint_t i;
+
+ static const struct {
+ nxt_str_t path;
+ nxt_str_t expected;
+ } tests[] = {
+ { nxt_string(""), nxt_string("") },
+ { nxt_string("/"), nxt_string("") },
+ { nxt_string("/a"), nxt_string("a") },
+ { nxt_string("///"), nxt_string("") },
+ { nxt_string("///a"), nxt_string("a") },
+ { nxt_string("///a/"), nxt_string("") },
+ { nxt_string("a"), nxt_string("a") },
+ { nxt_string("a/"), nxt_string("") },
+ { nxt_string("a//"), nxt_string("") },
+ { nxt_string("path/name"), nxt_string("name") },
+ { nxt_string("/path/name"), nxt_string("name") },
+ { nxt_string("/path/name/"), nxt_string("") },
+ };
+
+ for (i = 0; i < nxt_nitems(tests); i++) {
+ nxt_file_basename(&tests[i].path, &name);
+
+ success = nxt_strstr_eq(&tests[i].expected, &name);
+
+ if (!success) {
+ printf("nxt_file_basename_test(\"%.*s\"):\n"
+ "expected: \"%.*s\"\n got: \"%.*s\"\n",
+ (int) tests[i].path.length, tests[i].path.start,
+ (int) tests[i].expected.length, tests[i].expected.start,
+ (int) name.length, name.start);
+ return NXT_ERROR;
+ }
+ }
+
+ return NXT_OK;
+}
+
+
+static nxt_int_t
+nxt_file_dirname_test(njs_vm_t * vm, nxt_bool_t disassemble,
+ nxt_bool_t verbose)
+{
+ nxt_str_t name;
+ nxt_bool_t success;
+ nxt_uint_t i;
+
+ static const struct {
+ nxt_str_t path;
+ nxt_str_t expected;
+ } tests[] = {
+ { nxt_string(""), nxt_string("") },
+ { nxt_string("/"), nxt_string("/") },
+ { nxt_string("/a"), nxt_string("/") },
+ { nxt_string("///"), nxt_string("///") },
+ { nxt_string("///a"), nxt_string("///") },
+ { nxt_string("///a/"), nxt_string("///a") },
+ { nxt_string("a"), nxt_string("") },
+ { nxt_string("a/"), nxt_string("a") },
+ { nxt_string("a//"), nxt_string("a") },
+ { nxt_string("p1/p2/name"), nxt_string("p1/p2") },
+ { nxt_string("/p1/p2/name"), nxt_string("/p1/p2") },
+ { nxt_string("/p1/p2///name"), nxt_string("/p1/p2") },
+ { nxt_string("/p1/p2/name/"), nxt_string("/p1/p2/name") },
+ };
+
+ for (i = 0; i < nxt_nitems(tests); i++) {
+ nxt_file_dirname(&tests[i].path, &name);
+
+ success = nxt_strstr_eq(&tests[i].expected, &name);
+
+ if (!success) {
+ printf("nxt_file_dirname_test(\"%.*s\"):\n"
+ "expected: \"%.*s\"\n got: \"%.*s\"\n",
+ (int) tests[i].path.length, tests[i].path.start,
+ (int) tests[i].expected.length, tests[i].expected.start,
+ (int) name.length, name.start);
+ return NXT_ERROR;
+ }
+ }
+
+ return NXT_OK;
+}
+
+
typedef struct {
nxt_int_t (*test)(njs_vm_t *, nxt_bool_t, nxt_bool_t);
nxt_str_t name;
@@ -12009,10 +12100,13 @@ njs_api_test(nxt_bool_t disassemble, nxt
njs_vm_opt_t options;
njs_api_test_t *test;
- static njs_api_test_t njs_api_test[] =
- {
+ static njs_api_test_t njs_api_test[] = {
{ njs_vm_object_alloc_test,
- nxt_string("njs_vm_object_alloc_test") }
+ nxt_string("njs_vm_object_alloc_test") },
+ { nxt_file_basename_test,
+ nxt_string("nxt_file_basename_test") },
+ { nxt_file_dirname_test,
+ nxt_string("nxt_file_dirname_test") },
};
rc = NXT_ERROR;
diff --git a/nxt/nxt_file.c b/nxt/nxt_file.c
--- a/nxt/nxt_file.c
+++ b/nxt/nxt_file.c
@@ -14,20 +14,57 @@
void
-nxt_file_name(nxt_str_t *name, char *path)
+nxt_file_basename(const nxt_str_t *path, nxt_str_t *name)
{
- char *p;
- size_t length;
+ const u_char *p, *end;
- length = strlen(path);
+ end = path->start + path->length;
+ p = end - 1;
- for (p = path + length; p >= path; p--) {
- if (*p == '/') {
- p++;
- break;
- }
+ /* Stripping dir prefix. */
+
+ while (p >= path->start && *p != '/') { p--; }
+
+ p++;
+
+ name->start = (u_char *) p;
+ name->length = end - p;
+}
+
+
+void
+nxt_file_dirname(const nxt_str_t *path, nxt_str_t *name)
+{
+ const u_char *p, *end;
+
+ if (path->length == 0) {
+ *name = nxt_string_value("");
+ return;
}
- name->start = (u_char *) p;
- name->length = length - (p - path);
+ p = path->start + path->length - 1;
+
+ /* Stripping basename. */
+
+ while (p >= path->start && *p != '/') { p--; }
+
+ end = p + 1;
+
+ if (end == path->start) {
+ *name = nxt_string_value("");
+ return;
+ }
+
+ /* Stripping trailing slashes. */
+
+ while (p >= path->start && *p == '/') { p--; }
+
+ p++;
+
+ if (p == path->start) {
+ p = end;
+ }
+
+ name->start = path->start;
+ name->length = p - path->start;
}
diff --git a/nxt/nxt_file.h b/nxt/nxt_file.h
--- a/nxt/nxt_file.h
+++ b/nxt/nxt_file.h
@@ -8,7 +8,8 @@
#define _NXT_FILE_H_INCLUDED_
-void nxt_file_name(nxt_str_t *name, char *path);
+void nxt_file_basename(const nxt_str_t *path, nxt_str_t *name);
+void nxt_file_dirname(const nxt_str_t *path, nxt_str_t *name);
#endif /* _NXT_FILE_H_INCLUDED_ */
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID 64278ad1e14ccf9e6414d531fdea15d465d3167e
# Parent 8250061df72a46278714165f835adc52f432056a
Introduced njs_parser_global_scope().
diff --git a/njs/njs_parser.c b/njs/njs_parser.c
--- a/njs/njs_parser.c
+++ b/njs/njs_parser.c
@@ -2178,11 +2178,7 @@ njs_parser_builtin(njs_vm_t *vm, njs_par
njs_variable_t *var;
njs_parser_scope_t *scope;
- scope = parser->scope;
-
- while (scope->type != NJS_SCOPE_GLOBAL) {
- scope = scope->parent;
- }
+ scope = njs_parser_global_scope(vm);
var = njs_variable_add(vm, scope, name, hash, NJS_VARIABLE_VAR);
if (nxt_slow_path(var == NULL)) {
diff --git a/njs/njs_parser.h b/njs/njs_parser.h
--- a/njs/njs_parser.h
+++ b/njs/njs_parser.h
@@ -360,6 +360,21 @@ njs_parser_node_new(njs_vm_t *vm, njs_pa
}
+nxt_inline njs_parser_scope_t *
+njs_parser_global_scope(njs_vm_t *vm)
+{
+ njs_parser_scope_t *scope;
+
+ scope = vm->parser->scope;
+
+ while (scope->type != NJS_SCOPE_GLOBAL) {
+ scope = scope->parent;
+ }
+
+ return scope;
+}
+
+
extern const nxt_lvlhsh_proto_t njs_keyword_hash_proto;
diff --git a/njs/njs_variable.c b/njs/njs_variable.c
--- a/njs/njs_variable.c
+++ b/njs/njs_variable.c
@@ -356,7 +356,7 @@ njs_variable_reference_resolve(njs_vm_t
njs_parser_scope_t *node_scope)
{
nxt_lvlhsh_query_t lhq;
- njs_parser_scope_t *scope, *parent, *previous;
+ njs_parser_scope_t *scope, *previous;
lhq.key_hash = vr->hash;
lhq.key = vr->name;
@@ -395,9 +395,7 @@ njs_variable_reference_resolve(njs_vm_t
return NXT_OK;
}
- parent = scope->parent;
-
- if (parent == NULL) {
+ if (scope->parent == NULL) {
/* A global scope. */
vr->scope = scope;
@@ -405,7 +403,7 @@ njs_variable_reference_resolve(njs_vm_t
}
previous = scope;
- scope = parent;
+ scope = scope->parent;
}
}
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID b23c0a90e37f71c6a30921593c0d547aab90b4b3
# Parent 64278ad1e14ccf9e6414d531fdea15d465d3167e
Making njs_vm_invoke() public.
diff --git a/njs/njs.c b/njs/njs.c
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -10,8 +10,6 @@
static nxt_int_t njs_vm_init(njs_vm_t *vm);
-static nxt_int_t njs_vm_invoke(njs_vm_t *vm, njs_function_t *function,
- const njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
static nxt_int_t njs_vm_handle_events(njs_vm_t *vm);
@@ -458,11 +456,11 @@ nxt_int_t
njs_vm_call(njs_vm_t *vm, njs_function_t *function, const njs_value_t *args,
nxt_uint_t nargs)
{
- return njs_vm_invoke(vm, function, args, nargs, NJS_INDEX_GLOBAL_RETVAL);
+ return njs_vm_invoke(vm, function, args, nargs, NJS_INDEX_GLOBAL_RETVAL);
}
-static nxt_int_t
+nxt_int_t
njs_vm_invoke(njs_vm_t *vm, njs_function_t *function, const njs_value_t *args,
nxt_uint_t nargs, njs_index_t retval)
{
diff --git a/njs/njs.h b/njs/njs.h
--- a/njs/njs.h
+++ b/njs/njs.h
@@ -193,6 +193,8 @@ NXT_EXPORT nxt_int_t njs_vm_posted(njs_v
*/
NXT_EXPORT nxt_int_t njs_vm_call(njs_vm_t *vm, njs_function_t *function,
const njs_value_t *args, nxt_uint_t nargs);
+NXT_EXPORT nxt_int_t njs_vm_invoke(njs_vm_t *vm, njs_function_t *function,
+ const njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
/*
* Runs posted events.
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID 68e7119e97fe4ff75d52f6055d9e3a2b873d903d
# Parent b23c0a90e37f71c6a30921593c0d547aab90b4b3
Allocating njs_generator_t on stack.
diff --git a/njs/njs.c b/njs/njs.c
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -216,20 +216,20 @@ njs_vm_destroy(njs_vm_t *vm)
nxt_int_t
njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end)
{
- nxt_int_t ret;
+ nxt_int_t ret;
njs_lexer_t *lexer;
njs_parser_t *parser, *prev;
- njs_generator_t *generator;
+ njs_generator_t generator;
+
+ if (vm->parser != NULL && !vm->options.accumulative) {
+ return NJS_ERROR;
+ }
parser = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_parser_t));
if (nxt_slow_path(parser == NULL)) {
return NJS_ERROR;
}
- if (vm->parser != NULL && !vm->options.accumulative) {
- return NJS_ERROR;
- }
-
prev = vm->parser;
vm->parser = parser;
@@ -269,24 +269,16 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
*/
vm->code = NULL;
- generator = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
- sizeof(njs_generator_t));
+ nxt_memzero(&generator, sizeof(njs_generator_t));
- if (nxt_slow_path(generator == NULL)) {
- goto fail;
- }
-
- nxt_memzero(generator, sizeof(njs_generator_t));
-
- ret = njs_generate_scope(vm, generator, parser->scope);
+ ret = njs_generate_scope(vm, &generator, parser->scope);
if (nxt_slow_path(ret != NXT_OK)) {
goto fail;
}
- vm->current = generator->code_start;
-
- vm->global_scope = generator->local_scope;
- vm->scope_size = generator->scope_size;
+ vm->current = generator.code_start;
+ vm->global_scope = generator.local_scope;
+ vm->scope_size = generator.scope_size;
vm->variables_hash = parser->scope->variables;
diff --git a/njs/njs_generator.c b/njs/njs_generator.c
--- a/njs/njs_generator.c
+++ b/njs/njs_generator.c
@@ -2282,19 +2282,13 @@ njs_generate_function_scope(njs_vm_t *vm
size_t size;
nxt_int_t ret;
nxt_array_t *closure;
- njs_generator_t *generator;
-
- generator = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
- sizeof(njs_generator_t));
- if (nxt_slow_path(generator == NULL)) {
- return NXT_ERROR;
- }
-
- nxt_memzero(generator, sizeof(njs_generator_t));
+ njs_generator_t generator;
node = node->right;
- ret = njs_generate_scope(vm, generator, node->scope);
+ nxt_memzero(&generator, sizeof(njs_generator_t));
+
+ ret = njs_generate_scope(vm, &generator, node->scope);
if (nxt_fast_path(ret == NXT_OK)) {
size = 0;
@@ -2309,16 +2303,13 @@ njs_generate_function_scope(njs_vm_t *vm
lambda->closure_size = size;
lambda->nesting = node->scope->nesting;
- lambda->arguments_object = generator->arguments_object;
-
- lambda->local_size = generator->scope_size;
- lambda->local_scope = generator->local_scope;
-
- lambda->start = generator->code_start;
+ lambda->arguments_object = generator.arguments_object;
+
+ lambda->start = generator.code_start;
+ lambda->local_size = generator.scope_size;
+ lambda->local_scope = generator.local_scope;
}
- nxt_mp_free(vm->mem_pool, generator);
-
return ret;
}
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID 2101597aa18d30be530e98057ffb14550e1d6e52
# Parent 68e7119e97fe4ff75d52f6055d9e3a2b873d903d
Allocating njs_lexer_t on stack.
diff --git a/njs/njs.c b/njs/njs.c
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -217,7 +217,7 @@ nxt_int_t
njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end)
{
nxt_int_t ret;
- njs_lexer_t *lexer;
+ njs_lexer_t lexer;
njs_parser_t *parser, *prev;
njs_generator_t generator;
@@ -233,17 +233,15 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
prev = vm->parser;
vm->parser = parser;
- lexer = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_lexer_t));
- if (nxt_slow_path(lexer == NULL)) {
- return NJS_ERROR;
- }
+ nxt_memzero(&lexer, sizeof(njs_lexer_t));
- parser->lexer = lexer;
- lexer->start = *start;
- lexer->end = end;
- lexer->line = 1;
- lexer->file = vm->options.file;
- lexer->keywords_hash = vm->shared->keywords_hash;
+ lexer.start = *start;
+ lexer.end = end;
+ lexer.line = 1;
+ lexer.file = vm->options.file;
+ lexer.keywords_hash = vm->shared->keywords_hash;
+
+ parser->lexer = &lexer;
if (vm->backtrace != NULL) {
nxt_array_reset(vm->backtrace);
@@ -261,7 +259,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
goto fail;
}
- *start = parser->lexer->start;
+ *start = lexer.start;
/*
* Reset the code array to prevent it from being disassembled
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID 5db48d70db65e4734706a76767babda37a1ea74a
# Parent 2101597aa18d30be530e98057ffb14550e1d6e52
Improved njs_vm_compile().
diff --git a/njs/njs.c b/njs/njs.c
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -216,10 +216,11 @@ njs_vm_destroy(njs_vm_t *vm)
nxt_int_t
njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end)
{
- nxt_int_t ret;
- njs_lexer_t lexer;
- njs_parser_t *parser, *prev;
- njs_generator_t generator;
+ nxt_int_t ret;
+ njs_lexer_t lexer;
+ njs_parser_t *parser, *prev;
+ njs_generator_t generator;
+ njs_parser_scope_t *scope;
if (vm->parser != NULL && !vm->options.accumulative) {
return NJS_ERROR;
@@ -254,7 +255,9 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
goto fail;
}
- ret = njs_variables_scope_reference(vm, parser->scope);
+ scope = parser->scope;
+
+ ret = njs_variables_scope_reference(vm, scope);
if (nxt_slow_path(ret != NXT_OK)) {
goto fail;
}
@@ -269,7 +272,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
nxt_memzero(&generator, sizeof(njs_generator_t));
- ret = njs_generate_scope(vm, &generator, parser->scope);
+ ret = njs_generate_scope(vm, &generator, scope);
if (nxt_slow_path(ret != NXT_OK)) {
goto fail;
}
@@ -278,7 +281,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
vm->global_scope = generator.local_scope;
vm->scope_size = generator.scope_size;
- vm->variables_hash = parser->scope->variables;
+ vm->variables_hash = scope->variables;
if (vm->options.init) {
ret = njs_vm_init(vm);
# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1550330323 -28800
# Sat Feb 16 23:18:43 2019 +0800
# Node ID 82794813ce4cf58f4674daa2e93429e7410e3dbf
# Parent 5db48d70db65e4734706a76767babda37a1ea74a
Fixed fast paths.
diff --git a/njs/njs_variable.c b/njs/njs_variable.c
--- a/njs/njs_variable.c
+++ b/njs/njs_variable.c
@@ -144,7 +144,7 @@ njs_variable_reference(njs_vm_t *vm, njs
ret = nxt_lvlhsh_insert(&scope->references, &lhq);
- if (nxt_slow_path(ret != NXT_ERROR)) {
+ if (nxt_fast_path(ret != NXT_ERROR)) {
ret = NXT_OK;
}
}
@@ -493,7 +493,7 @@ njs_name_copy(njs_vm_t *vm, nxt_str_t *d
dst->start = nxt_mp_alloc(vm->mem_pool, src->length);
- if (nxt_slow_path(dst->start != NULL)) {
+ if (nxt_fast_path(dst->start != NULL)) {
(void) memcpy(dst->start, src->start, src->length);
return NXT_OK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment