2019-05-31 23:59:45 +00:00
|
|
|
|
// Copyright 2019 Google LLC
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
#include <njs_main.h>
|
|
|
|
|
#include <njs_value.h>
|
|
|
|
|
|
2019-05-31 23:59:45 +00:00
|
|
|
|
#include <njs_builtin.h>
|
|
|
|
|
|
|
|
|
|
// The vast majority of the code was copied from njs/njs_shell.c.
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uint8_t disassemble;
|
|
|
|
|
uint8_t interactive;
|
|
|
|
|
uint8_t module;
|
|
|
|
|
uint8_t quiet;
|
|
|
|
|
uint8_t sandbox;
|
|
|
|
|
uint8_t version;
|
|
|
|
|
|
|
|
|
|
char *file;
|
|
|
|
|
char *command;
|
|
|
|
|
size_t n_paths;
|
|
|
|
|
char **paths;
|
|
|
|
|
} njs_opts_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
size_t index;
|
|
|
|
|
size_t length;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_array_t *completions;
|
|
|
|
|
njs_array_t *suffix_completions;
|
|
|
|
|
njs_lvlhsh_each_t lhe;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
NJS_COMPLETION_VAR = 0,
|
|
|
|
|
NJS_COMPLETION_SUFFIX,
|
|
|
|
|
NJS_COMPLETION_GLOBAL
|
|
|
|
|
} phase;
|
|
|
|
|
} njs_completion_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
njs_vm_event_t vm_event;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_queue_link_t link;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
} njs_ev_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
njs_vm_t *vm;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_lvlhsh_t events; /* njs_ev_t * */
|
|
|
|
|
njs_queue_t posted_events;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
uint64_t time;
|
|
|
|
|
|
|
|
|
|
njs_completion_t completion;
|
|
|
|
|
} njs_console_t;
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t njs_console_init(njs_vm_t *vm, njs_console_t *console);
|
|
|
|
|
static njs_int_t njs_externals_init(njs_vm_t *vm, njs_console_t *console);
|
|
|
|
|
static njs_int_t njs_interactive_shell(njs_opts_t *opts,
|
|
|
|
|
njs_vm_opt_t *vm_options, njs_str_t *line);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
static njs_vm_t *njs_create_vm(njs_opts_t *opts, njs_vm_opt_t *vm_options);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t njs_process_script(njs_console_t *console, njs_opts_t *opts,
|
|
|
|
|
const njs_str_t *script);
|
|
|
|
|
|
|
|
|
|
static njs_int_t njs_ext_console_log(njs_vm_t *vm, njs_value_t *args,
|
|
|
|
|
njs_uint_t nargs, njs_index_t unused);
|
|
|
|
|
static njs_int_t njs_ext_console_dump(njs_vm_t *vm, njs_value_t *args,
|
|
|
|
|
njs_uint_t nargs, njs_index_t unused);
|
|
|
|
|
static njs_int_t njs_ext_console_help(njs_vm_t *vm, njs_value_t *args,
|
|
|
|
|
njs_uint_t nargs, njs_index_t unused);
|
|
|
|
|
static njs_int_t njs_ext_console_time(njs_vm_t *vm, njs_value_t *args,
|
|
|
|
|
njs_uint_t nargs, njs_index_t unused);
|
|
|
|
|
static njs_int_t njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args,
|
|
|
|
|
njs_uint_t nargs, njs_index_t unused);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
static njs_host_event_t njs_console_set_timer(njs_external_ptr_t external,
|
|
|
|
|
uint64_t delay, njs_vm_event_t vm_event);
|
|
|
|
|
static void njs_console_clear_timer(njs_external_ptr_t external,
|
|
|
|
|
njs_host_event_t event);
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t lvlhsh_key_test(njs_lvlhsh_query_t *lhq, void *data);
|
|
|
|
|
static void *lvlhsh_pool_alloc(void *pool, size_t size, njs_uint_t nalloc);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
static void lvlhsh_pool_free(void *pool, void *p, size_t size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static njs_external_t njs_ext_console[] = {
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("log"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_METHOD,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
njs_ext_console_log,
|
|
|
|
|
0 },
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("dump"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_METHOD,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
njs_ext_console_dump,
|
|
|
|
|
0 },
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("help"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_METHOD,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
njs_ext_console_help,
|
|
|
|
|
0 },
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("time"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_METHOD,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
njs_ext_console_time,
|
|
|
|
|
0 },
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("timeEnd"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_METHOD,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
njs_ext_console_time_end,
|
|
|
|
|
0 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static njs_external_t njs_externals[] = {
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
{ njs_str("console"),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NJS_EXTERN_OBJECT,
|
|
|
|
|
njs_ext_console,
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_nitems(njs_ext_console),
|
2019-05-31 23:59:45 +00:00
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
0 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static const njs_lvlhsh_proto_t lvlhsh_proto njs_aligned(64) = {
|
|
|
|
|
NJS_LVLHSH_LARGE_SLAB,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
0,
|
|
|
|
|
lvlhsh_key_test,
|
|
|
|
|
lvlhsh_pool_alloc,
|
|
|
|
|
lvlhsh_pool_free,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static njs_vm_ops_t njs_console_ops = {
|
|
|
|
|
njs_console_set_timer,
|
|
|
|
|
njs_console_clear_timer
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static njs_console_t njs_console;
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_console_init(njs_vm_t *vm, njs_console_t *console)
|
|
|
|
|
{
|
|
|
|
|
console->vm = vm;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_lvlhsh_init(&console->events);
|
|
|
|
|
njs_queue_init(&console->posted_events);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
console->time = UINT64_MAX;
|
|
|
|
|
|
|
|
|
|
console->completion.completions = njs_vm_completions(vm, NULL);
|
|
|
|
|
if (console->completion.completions == NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_OK;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_externals_init(njs_vm_t *vm, njs_console_t *console)
|
|
|
|
|
{
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_uint_t ret;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_value_t *value;
|
|
|
|
|
const njs_extern_t *proto;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static const njs_str_t name = njs_str("console");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
proto = njs_vm_external_prototype(vm, &njs_externals[0]);
|
|
|
|
|
if (proto == NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_stderror("failed to add console proto\n");
|
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
value = njs_mp_zalloc(vm->mem_pool, sizeof(njs_opaque_value_t));
|
2019-05-31 23:59:45 +00:00
|
|
|
|
if (value == NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_vm_external_create(vm, value, proto, console);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_vm_external_bind(vm, &name, value);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_console_init(vm, console);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_OK;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_interactive_shell(njs_opts_t *opts, njs_vm_opt_t *vm_options, njs_str_t *line)
|
2019-05-31 23:59:45 +00:00
|
|
|
|
{
|
|
|
|
|
njs_vm_t *vm;
|
|
|
|
|
|
|
|
|
|
vm = njs_create_vm(opts, vm_options);
|
|
|
|
|
if (vm == NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_ERROR;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
njs_process_script(vm_options->external, opts, line);
|
|
|
|
|
njs_vm_destroy(vm);
|
|
|
|
|
vm = NULL;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_OK;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static njs_vm_t *
|
|
|
|
|
njs_create_vm(njs_opts_t *opts, njs_vm_opt_t *vm_options)
|
|
|
|
|
{
|
|
|
|
|
u_char *p, *start;
|
|
|
|
|
njs_vm_t *vm;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_int_t ret;
|
|
|
|
|
njs_str_t path;
|
|
|
|
|
njs_uint_t i;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
vm = njs_vm_create(vm_options);
|
|
|
|
|
if (vm == NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_stderror("failed to create vm\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (njs_externals_init(vm, vm_options->external) != NJS_OK) {
|
|
|
|
|
njs_stderror("failed to add external protos\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < opts->n_paths; i++) {
|
|
|
|
|
path.start = (u_char *) opts->paths[i];
|
2019-08-01 23:03:49 +00:00
|
|
|
|
path.length = njs_strlen(opts->paths[i]);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
ret = njs_vm_add_path(vm, &path);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
njs_stderror("failed to add path\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = (u_char *) getenv("NJS_PATH");
|
|
|
|
|
if (start == NULL) {
|
|
|
|
|
return vm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
p = njs_strchr(start, ':');
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
path.start = start;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
path.length = (p != NULL) ? (size_t) (p - start) : njs_strlen(start);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
ret = njs_vm_add_path(vm, &path);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
njs_stderror("failed to add path\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = p + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return vm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_process_events(njs_console_t *console, njs_opts_t *opts)
|
|
|
|
|
{
|
|
|
|
|
njs_ev_t *ev;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_queue_t *events;
|
|
|
|
|
njs_queue_link_t *link;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
events = &console->posted_events;
|
|
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
link = njs_queue_first(events);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (link == njs_queue_tail(events)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
ev = njs_queue_link_data(link, njs_ev_t, link);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_queue_remove(&ev->link);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
ev->link.prev = NULL;
|
|
|
|
|
ev->link.next = NULL;
|
|
|
|
|
|
|
|
|
|
njs_vm_post_event(console->vm, ev->vm_event, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_OK;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_process_script(njs_console_t *console, njs_opts_t *opts,
|
2019-08-01 23:03:49 +00:00
|
|
|
|
const njs_str_t *script)
|
2019-05-31 23:59:45 +00:00
|
|
|
|
{
|
|
|
|
|
u_char *start;
|
|
|
|
|
njs_vm_t *vm;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_int_t ret;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
vm = console->vm;
|
|
|
|
|
start = script->start;
|
|
|
|
|
|
|
|
|
|
ret = njs_vm_compile(vm, &start, start + script->length);
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret == NJS_OK) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
if (opts->disassemble) {
|
|
|
|
|
njs_disassembler(vm);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_vm_start(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
if (!njs_vm_pending(vm)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_process_events(console, opts);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (njs_slow_path(ret != NJS_OK)) {
|
|
|
|
|
njs_stderror("njs_process_events() failed\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
ret = NJS_ERROR;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (njs_vm_waiting(vm) && !njs_vm_posted(vm)) {
|
|
|
|
|
/*TODO: async events. */
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_stderror("njs_process_script(): async events unsupported\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
ret = NJS_ERROR;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = njs_vm_run(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_ext_console_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_index_t unused)
|
|
|
|
|
{
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_str_t msg;
|
|
|
|
|
njs_uint_t n;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
|
|
while (n < nargs) {
|
|
|
|
|
if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1, 0)
|
|
|
|
|
== NJS_ERROR)
|
|
|
|
|
{
|
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("%s", (n != 1) ? " " : "");
|
|
|
|
|
njs_print(msg.start, msg.length);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nargs > 1) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vm->retval = njs_value_undefined;
|
|
|
|
|
|
|
|
|
|
return NJS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_ext_console_dump(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_index_t unused)
|
|
|
|
|
{
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_str_t msg;
|
|
|
|
|
njs_uint_t n;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
|
|
while (n < nargs) {
|
|
|
|
|
if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1, 1)
|
|
|
|
|
== NJS_ERROR)
|
|
|
|
|
{
|
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("%s", (n != 1) ? " " : "");
|
|
|
|
|
njs_print(msg.start, msg.length);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nargs > 1) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vm->retval = njs_value_undefined;
|
|
|
|
|
|
|
|
|
|
return NJS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_ext_console_help(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_index_t unused)
|
|
|
|
|
{
|
|
|
|
|
const njs_object_init_t *obj, **objpp;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("VM built-in objects:\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
for (objpp = njs_constructor_init; *objpp != NULL; objpp++) {
|
|
|
|
|
obj = *objpp;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf(" %V\n", &obj->name);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (objpp = njs_object_init; *objpp != NULL; objpp++) {
|
|
|
|
|
obj = *objpp;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf(" %V\n", &obj->name);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("\nEmbedded objects:\n");
|
|
|
|
|
njs_printf(" console\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
vm->retval = njs_value_undefined;
|
|
|
|
|
|
|
|
|
|
return NJS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_ext_console_time(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_index_t unused)
|
|
|
|
|
{
|
|
|
|
|
njs_console_t *console;
|
|
|
|
|
|
|
|
|
|
if (!njs_value_is_undefined(njs_arg(args, nargs, 1))) {
|
|
|
|
|
njs_vm_error(vm, "labels not implemented");
|
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console = njs_vm_external(vm, njs_arg(args, nargs, 0));
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (njs_slow_path(console == NULL)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
console->time = njs_time();
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
vm->retval = njs_value_undefined;
|
|
|
|
|
|
|
|
|
|
return NJS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_index_t unused)
|
|
|
|
|
{
|
|
|
|
|
uint64_t ns, ms;
|
|
|
|
|
njs_console_t *console;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
ns = njs_time();
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
if (!njs_value_is_undefined(njs_arg(args, nargs, 1))) {
|
|
|
|
|
njs_vm_error(vm, "labels not implemented");
|
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console = njs_vm_external(vm, njs_arg(args, nargs, 0));
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (njs_slow_path(console == NULL)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NJS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (njs_fast_path(console->time != UINT64_MAX)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
ns = ns - console->time;
|
|
|
|
|
|
|
|
|
|
ms = ns / 1000000;
|
|
|
|
|
ns = ns % 1000000;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("default: %uL.%06uLms\n", ms, ns);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
console->time = UINT64_MAX;
|
|
|
|
|
|
|
|
|
|
} else {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_printf("Timer \"default\" doesn’t exist.\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vm->retval = njs_value_undefined;
|
|
|
|
|
|
|
|
|
|
return NJS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static njs_host_event_t
|
|
|
|
|
njs_console_set_timer(njs_external_ptr_t external, uint64_t delay,
|
|
|
|
|
njs_vm_event_t vm_event)
|
|
|
|
|
{
|
|
|
|
|
njs_ev_t *ev;
|
|
|
|
|
njs_vm_t *vm;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_int_t ret;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_console_t *console;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_lvlhsh_query_t lhq;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
if (delay != 0) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_stderror("njs_console_set_timer(): async timers unsupported\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console = external;
|
|
|
|
|
vm = console->vm;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
ev = njs_mp_alloc(vm->mem_pool, sizeof(njs_ev_t));
|
|
|
|
|
if (njs_slow_path(ev == NULL)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ev->vm_event = vm_event;
|
|
|
|
|
|
|
|
|
|
lhq.key.start = (u_char *) &ev->vm_event;
|
|
|
|
|
lhq.key.length = sizeof(njs_vm_event_t);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
lhq.replace = 0;
|
|
|
|
|
lhq.value = ev;
|
|
|
|
|
lhq.proto = &lvlhsh_proto;
|
|
|
|
|
lhq.pool = vm->mem_pool;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
ret = njs_lvlhsh_insert(&console->events, &lhq);
|
|
|
|
|
if (njs_slow_path(ret != NJS_OK)) {
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_queue_insert_tail(&console->posted_events, &ev->link);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
return (njs_host_event_t) ev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
njs_console_clear_timer(njs_external_ptr_t external, njs_host_event_t event)
|
|
|
|
|
{
|
|
|
|
|
njs_vm_t *vm;
|
|
|
|
|
njs_ev_t *ev;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_int_t ret;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_console_t *console;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_lvlhsh_query_t lhq;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
ev = event;
|
|
|
|
|
console = external;
|
|
|
|
|
vm = console->vm;
|
|
|
|
|
|
|
|
|
|
lhq.key.start = (u_char *) &ev->vm_event;
|
|
|
|
|
lhq.key.length = sizeof(njs_vm_event_t);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
lhq.proto = &lvlhsh_proto;
|
|
|
|
|
lhq.pool = vm->mem_pool;
|
|
|
|
|
|
|
|
|
|
if (ev->link.prev != NULL) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_queue_remove(&ev->link);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
ret = njs_lvlhsh_delete(&console->events, &lhq);
|
|
|
|
|
if (ret != NJS_OK) {
|
|
|
|
|
njs_stderror("njs_lvlhsh_delete() failed\n");
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_mp_free(vm->mem_pool, ev);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
static njs_int_t
|
|
|
|
|
lvlhsh_key_test(njs_lvlhsh_query_t *lhq, void *data)
|
2019-05-31 23:59:45 +00:00
|
|
|
|
{
|
|
|
|
|
njs_ev_t *ev;
|
|
|
|
|
|
|
|
|
|
ev = data;
|
|
|
|
|
|
|
|
|
|
if (memcmp(&ev->vm_event, lhq->key.start, sizeof(njs_vm_event_t)) == 0) {
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_OK;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return NJS_DECLINED;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
2019-08-01 23:03:49 +00:00
|
|
|
|
lvlhsh_pool_alloc(void *pool, size_t size, njs_uint_t nalloc)
|
2019-05-31 23:59:45 +00:00
|
|
|
|
{
|
2019-08-01 23:03:49 +00:00
|
|
|
|
return njs_mp_align(pool, size, size);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
lvlhsh_pool_free(void *pool, void *p, size_t size)
|
|
|
|
|
{
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_mp_free(pool, p);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|
|
|
|
if (size == 0) return 0;
|
|
|
|
|
|
2019-06-01 04:40:00 +00:00
|
|
|
|
char* input = malloc(size);
|
2019-05-31 23:59:45 +00:00
|
|
|
|
memcpy(input, data, size);
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_str_t line = {size, input};
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
njs_vm_t *vm;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_int_t ret;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_opts_t opts;
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_str_t command;
|
2019-05-31 23:59:45 +00:00
|
|
|
|
njs_vm_opt_t vm_options;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_memzero(&opts, sizeof(njs_opts_t));
|
2019-05-31 23:59:45 +00:00
|
|
|
|
opts.interactive = 1;
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
njs_memzero(&vm_options, sizeof(njs_vm_opt_t));
|
2019-05-31 23:59:45 +00:00
|
|
|
|
|
|
|
|
|
vm_options.init = !opts.interactive;
|
|
|
|
|
vm_options.accumulative = opts.interactive;
|
|
|
|
|
vm_options.backtrace = 1;
|
|
|
|
|
vm_options.quiet = opts.quiet;
|
|
|
|
|
vm_options.sandbox = opts.sandbox;
|
|
|
|
|
vm_options.module = opts.module;
|
|
|
|
|
|
|
|
|
|
vm_options.ops = &njs_console_ops;
|
|
|
|
|
vm_options.external = &njs_console;
|
|
|
|
|
|
|
|
|
|
ret = njs_interactive_shell(&opts, &vm_options, &line);
|
|
|
|
|
free(input);
|
|
|
|
|
|
2019-08-01 23:03:49 +00:00
|
|
|
|
if (ret != NJS_OK)
|
2019-05-31 23:59:45 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|