From: Igor Sysoev Date: Tue, 11 Oct 2016 14:44:05 +0000 (+0300) Subject: Accessing the global this object caused segfault. X-Git-Tag: 0.1.4~18 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=1e65edc8231dd91f80f31d730b77f22936c5c7b8;p=njs.git Accessing the global this object caused segfault. --- diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 23af28ac..a5f4ae7e 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -107,7 +107,8 @@ njs_builtin_objects_create(njs_vm_t *vm) }; static const njs_object_init_t *object_init[] = { - &njs_math_object_init, + NULL, /* global this */ + &njs_math_object_init, /* Math */ }; static const njs_object_init_t *function_init[] = { @@ -164,12 +165,14 @@ njs_builtin_objects_create(njs_vm_t *vm) objects = vm->shared->objects; - for (i = NJS_OBJECT_MATH; i < NJS_OBJECT_MAX; i++) { - ret = njs_object_hash_create(vm, &objects[i].shared_hash, - object_init[i]->properties, - object_init[i]->items); - if (nxt_slow_path(ret != NXT_OK)) { - return NXT_ERROR; + for (i = NJS_OBJECT_THIS; i < NJS_OBJECT_MAX; i++) { + if (object_init[i] != NULL) { + ret = njs_object_hash_create(vm, &objects[i].shared_hash, + object_init[i]->properties, + object_init[i]->items); + if (nxt_slow_path(ret != NXT_OK)) { + return NXT_ERROR; + } } objects[i].shared = 1; diff --git a/njs/njs_generator.c b/njs/njs_generator.c index 071ce3bd..abf348ac 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -289,6 +289,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node) case NJS_TOKEN_NAME: return njs_generate_name(vm, parser, node); + case NJS_TOKEN_GLOBAL_THIS: case NJS_TOKEN_MATH: case NJS_TOKEN_EVAL: case NJS_TOKEN_TO_STRING: diff --git a/njs/njs_parser.c b/njs/njs_parser.c index acde715f..5a7afa83 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -1644,8 +1644,14 @@ njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token) case NJS_TOKEN_THIS: nxt_thread_log_debug("JS: this"); - node->index = NJS_INDEX_THIS; - break; + if (parser->scope != NJS_SCOPE_GLOBAL) { + node->index = NJS_INDEX_THIS; + break; + } + + node->token = NJS_TOKEN_GLOBAL_THIS; + + /* Fall through. */ case NJS_TOKEN_MATH: return njs_parser_builtin_object(vm, parser, node); diff --git a/njs/njs_parser.h b/njs/njs_parser.h index a2beafa5..76d118fa 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -160,8 +160,9 @@ typedef enum { NJS_TOKEN_THIS, -#define NJS_TOKEN_FIRST_OBJECT NJS_TOKEN_MATH +#define NJS_TOKEN_FIRST_OBJECT NJS_TOKEN_GLOBAL_THIS + NJS_TOKEN_GLOBAL_THIS, NJS_TOKEN_MATH, NJS_TOKEN_OBJECT_CONSTRUCTOR, diff --git a/njs/njs_vm.h b/njs/njs_vm.h index cce7be7f..adb9da84 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -741,7 +741,8 @@ enum njs_constructor_e { enum njs_object_e { - NJS_OBJECT_MATH = 0, + NJS_OBJECT_THIS = 0, + NJS_OBJECT_MATH, #define NJS_OBJECT_MAX (NJS_OBJECT_MATH + 1) }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 5d9d6447..5da2cffa 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -4237,6 +4237,9 @@ static njs_unit_test_t njs_test[] = { nxt_string("/./ instanceof Object"), nxt_string("true") }, + { nxt_string("this"), + nxt_string("[object Object]") }, + { nxt_string("var o = Object(); o"), nxt_string("[object Object]") },