Welcome! Log In Create A New Profile

Advanced

[njs] Moving constructors to global object.

Dmitry Volyntsev
October 31, 2019 11:20AM
details: https://hg.nginx.org/njs/rev/c75a8fc6d534
branches:
changeset: 1211:c75a8fc6d534
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Thu Oct 31 18:17:31 2019 +0300
description:
Moving constructors to global object.

diffstat:

src/njs_array.c | 2 +-
src/njs_builtin.c | 240 ++++++++++++++++++++++++++++++++++++---
src/njs_crypto.c | 8 +-
src/njs_date.c | 2 +-
src/njs_error.c | 28 ++--
src/njs_error.h | 22 +-
src/njs_extern.c | 2 +-
src/njs_fs.c | 2 +-
src/njs_function.c | 4 +-
src/njs_generator.c | 19 +--
src/njs_lexer.h | 18 --
src/njs_lexer_keyword.c | 20 ---
src/njs_module.c | 2 +-
src/njs_object.c | 13 +-
src/njs_object_hash.h | 265 +++++++++++++++++++++++++++++++++++++++----
src/njs_parser.c | 6 +-
src/njs_parser.h | 8 +-
src/njs_parser_expression.c | 7 -
src/njs_parser_terminal.c | 68 -----------
src/njs_regexp.c | 2 +-
src/njs_vm.c | 4 +-
src/njs_vm.h | 121 +++++--------------
src/test/njs_benchmark.c | 23 +++
src/test/njs_unit_test.c | 29 ++++-
test/njs_expect_test.exp | 4 +-
25 files changed, 598 insertions(+), 321 deletions(-)

diffs (truncated from 1630 to 1000 lines):

diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_array.c
--- a/src/njs_array.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_array.c Thu Oct 31 18:17:31 2019 +0300
@@ -63,7 +63,7 @@ njs_array_alloc(njs_vm_t *vm, uint64_t l
array->start = array->data;
njs_lvlhsh_init(&array->object.hash);
array->object.shared_hash = vm->shared->array_instance_hash;
- array->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_ARRAY].object;
+ array->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_ARRAY].object;
array->object.type = NJS_ARRAY;
array->object.shared = 0;
array->object.extensible = 1;
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_builtin.c
--- a/src/njs_builtin.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_builtin.c Thu Oct 31 18:17:31 2019 +0300
@@ -39,8 +39,8 @@ const njs_object_init_t *njs_object_ini


const njs_object_init_t *njs_module_init[] = {
- &njs_fs_object_init, /* fs */
- &njs_crypto_object_init, /* crypto */
+ &njs_fs_object_init,
+ &njs_crypto_object_init,
NULL
};

@@ -64,6 +64,7 @@ const njs_object_init_t *njs_prototype_
&njs_syntax_error_prototype_init,
&njs_type_error_prototype_init,
&njs_uri_error_prototype_init,
+ &njs_internal_error_prototype_init,
NULL
};

@@ -324,7 +325,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
prototype++;
}

- shared->prototypes[NJS_PROTOTYPE_REGEXP].regexp.pattern =
+ shared->prototypes[NJS_OBJ_TYPE_REGEXP].regexp.pattern =
shared->empty_regexp_pattern;

string_object = &shared->string_object;
@@ -453,7 +454,6 @@ njs_builtin_objects_clone(njs_vm_t *vm,
{
size_t size;
njs_uint_t i;
- njs_value_t *values;
njs_object_t *object_prototype, *function_prototype, *error_prototype,
*error_constructor;

@@ -461,36 +461,32 @@ njs_builtin_objects_clone(njs_vm_t *vm,
* Copy both prototypes and constructors arrays by one memcpy()
* because they are stored together.
*/
- size = NJS_PROTOTYPE_MAX * sizeof(njs_object_prototype_t)
- + NJS_CONSTRUCTOR_MAX * sizeof(njs_function_t);
+ size = (sizeof(njs_object_prototype_t) + sizeof(njs_function_t))
+ * NJS_OBJ_TYPE_MAX;

memcpy(vm->prototypes, vm->shared->prototypes, size);

- object_prototype = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
+ object_prototype = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;

- for (i = NJS_PROTOTYPE_ARRAY; i < NJS_PROTOTYPE_EVAL_ERROR; i++) {
+ for (i = NJS_OBJ_TYPE_ARRAY; i < NJS_OBJ_TYPE_EVAL_ERROR; i++) {
vm->prototypes[i].object.__proto__ = object_prototype;
}

- error_prototype = &vm->prototypes[NJS_PROTOTYPE_ERROR].object;
+ error_prototype = &vm->prototypes[NJS_OBJ_TYPE_ERROR].object;

- for (i = NJS_PROTOTYPE_EVAL_ERROR; i < NJS_PROTOTYPE_MAX; i++) {
+ for (i = NJS_OBJ_TYPE_EVAL_ERROR; i < NJS_OBJ_TYPE_MAX; i++) {
vm->prototypes[i].object.__proto__ = error_prototype;
}

- values = vm->scopes[NJS_SCOPE_GLOBAL];
+ function_prototype = &vm->prototypes[NJS_OBJ_TYPE_FUNCTION].object;

- function_prototype = &vm->prototypes[NJS_PROTOTYPE_FUNCTION].object;
-
- for (i = NJS_CONSTRUCTOR_OBJECT; i < NJS_CONSTRUCTOR_EVAL_ERROR; i++) {
- njs_set_function(&values[i], &vm->constructors[i]);
+ for (i = NJS_OBJ_TYPE_OBJECT; i < NJS_OBJ_TYPE_EVAL_ERROR; i++) {
vm->constructors[i].object.__proto__ = function_prototype;
}

- error_constructor = &vm->constructors[NJS_CONSTRUCTOR_ERROR].object;
+ error_constructor = &vm->constructors[NJS_OBJ_TYPE_ERROR].object;

- for (i = NJS_CONSTRUCTOR_EVAL_ERROR; i < NJS_CONSTRUCTOR_MAX; i++) {
- njs_set_function(&values[i], &vm->constructors[i]);
+ for (i = NJS_OBJ_TYPE_EVAL_ERROR; i < NJS_OBJ_TYPE_MAX; i++) {
vm->constructors[i].object.__proto__ = error_constructor;
}

@@ -501,7 +497,7 @@ njs_builtin_objects_clone(njs_vm_t *vm,
njs_set_object(global, &vm->global_object);

vm->string_object = vm->shared->string_object;
- vm->string_object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_STRING].object;
+ vm->string_object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_STRING].object;

return NJS_OK;
}
@@ -1090,6 +1086,51 @@ njs_top_level_object(njs_vm_t *vm, njs_o
}


+static njs_int_t
+njs_top_level_constructor(njs_vm_t *vm, njs_object_prop_t *self,
+ njs_value_t *global, njs_value_t *setval, njs_value_t *retval)
+{
+ njs_int_t ret;
+ njs_function_t *ctor;
+ njs_object_prop_t *prop;
+ njs_lvlhsh_query_t lhq;
+
+ if (njs_slow_path(setval != NULL)) {
+ *retval = *setval;
+
+ } else {
+ ctor = &vm->constructors[self->value.data.magic16];
+
+ njs_set_function(retval, ctor);
+ }
+
+ prop = njs_object_prop_alloc(vm, &self->name, retval, 1);
+ if (njs_slow_path(prop == NULL)) {
+ return NJS_ERROR;
+ }
+
+ /* GC */
+
+ prop->value = *retval;
+ prop->enumerable = 0;
+
+ lhq.value = prop;
+ njs_string_get(&self->name, &lhq.key);
+ lhq.key_hash = self->value.data.magic32;
+ lhq.replace = 1;
+ lhq.pool = vm->mem_pool;
+ lhq.proto = &njs_object_hash_proto;
+
+ ret = njs_lvlhsh_insert(njs_object_hash(global), &lhq);
+ if (njs_slow_path(ret != NJS_OK)) {
+ njs_internal_error(vm, "lvlhsh insert/replace failed");
+ return NJS_ERROR;
+ }
+
+ return NJS_OK;
+}
+
+
static const njs_object_prop_t njs_global_this_object_properties[] =
{
/* Global constants. */
@@ -1264,6 +1305,167 @@ static const njs_object_prop_t njs_glob
.configurable = 1,
},

+ /* Global constructors. */
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Object"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_OBJECT, NJS_OBJECT_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Array"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_ARRAY, NJS_ARRAY_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Boolean"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_BOOLEAN, NJS_BOOLEAN_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Number"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_NUMBER, NJS_NUMBER_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("String"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_STRING, NJS_STRING_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Function"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_FUNCTION, NJS_FUNCTION_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("RegExp"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_REGEXP, NJS_REGEXP_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Date"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_DATE, NJS_DATE_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("Error"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_ERROR, NJS_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("EvalError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_EVAL_ERROR,
+ NJS_EVAL_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("InternalError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_INTERNAL_ERROR,
+ NJS_INTERNAL_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("RangeError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_RANGE_ERROR,
+ NJS_RANGE_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("ReferenceError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_REF_ERROR, NJS_REF_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("SyntaxError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_SYNTAX_ERROR,
+ NJS_SYNTAX_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("TypeError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_TYPE_ERROR,
+ NJS_TYPE_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("URIError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_URI_ERROR,
+ NJS_URI_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
+
+ {
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("MemoryError"),
+ .value = njs_prop_handler2(njs_top_level_constructor,
+ NJS_OBJ_TYPE_MEMORY_ERROR,
+ NJS_MEMORY_ERROR_HASH),
+ .writable = 1,
+ .configurable = 1,
+ },
};


diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_crypto.c
--- a/src/njs_crypto.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_crypto.c Thu Oct 31 18:17:31 2019 +0300
@@ -122,7 +122,7 @@ static njs_crypto_enc_t *njs_crypto_enco


static njs_object_value_t *
-njs_crypto_object_value_alloc(njs_vm_t *vm, njs_uint_t proto)
+njs_crypto_object_value_alloc(njs_vm_t *vm, njs_object_type_t type)
{
njs_object_value_t *ov;

@@ -135,7 +135,7 @@ njs_crypto_object_value_alloc(njs_vm_t *
ov->object.shared = 0;
ov->object.extensible = 1;

- ov->object.__proto__ = &vm->prototypes[proto].object;
+ ov->object.__proto__ = &vm->prototypes[type].object;
return ov;
}

@@ -166,7 +166,7 @@ njs_crypto_create_hash(njs_vm_t *vm, njs
return NJS_ERROR;
}

- hash = njs_crypto_object_value_alloc(vm, NJS_PROTOTYPE_CRYPTO_HASH);
+ hash = njs_crypto_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HASH);
if (njs_slow_path(hash == NULL)) {
return NJS_ERROR;
}
@@ -431,7 +431,7 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
alg->init(&ctx->u);
alg->update(&ctx->u, key_buf, 64);

- hmac = njs_crypto_object_value_alloc(vm, NJS_PROTOTYPE_CRYPTO_HMAC);
+ hmac = njs_crypto_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HMAC);
if (njs_slow_path(hmac == NULL)) {
return NJS_ERROR;
}
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_date.c
--- a/src/njs_date.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_date.c Thu Oct 31 18:17:31 2019 +0300
@@ -257,7 +257,7 @@ njs_date_constructor(njs_vm_t *vm, njs_v
date->object.type = NJS_DATE;
date->object.shared = 0;
date->object.extensible = 1;
- date->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_DATE].object;
+ date->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_DATE].object;

date->time = njs_timeclip(time);

diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_error.c
--- a/src/njs_error.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_error.c Thu Oct 31 18:17:31 2019 +0300
@@ -13,7 +13,7 @@ static const njs_value_t njs_error_name


void
-njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_prototype_t type,
+njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
u_char *start, size_t size)
{
ssize_t length;
@@ -41,7 +41,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t


void
-njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_prototype_t type,
+njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
const char *fmt, ...)
{
va_list args;
@@ -60,7 +60,7 @@ njs_error_fmt_new(njs_vm_t *vm, njs_valu


njs_object_t *
-njs_error_alloc(njs_vm_t *vm, njs_prototype_t type, const njs_value_t *name,
+njs_error_alloc(njs_vm_t *vm, njs_object_type_t type, const njs_value_t *name,
const njs_value_t *message)
{
njs_int_t ret;
@@ -136,7 +136,7 @@ memory_error:

static njs_int_t
njs_error_create(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
- njs_prototype_t type)
+ njs_object_type_t type)
{
njs_int_t ret;
njs_value_t *value;
@@ -169,7 +169,7 @@ njs_int_t
njs_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_ERROR);
}


@@ -211,7 +211,7 @@ njs_int_t
njs_eval_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_EVAL_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_EVAL_ERROR);
}


@@ -253,7 +253,7 @@ njs_int_t
njs_internal_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_INTERNAL_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_INTERNAL_ERROR);
}


@@ -295,7 +295,7 @@ njs_int_t
njs_range_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_RANGE_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_RANGE_ERROR);
}


@@ -337,7 +337,7 @@ njs_int_t
njs_reference_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_REF_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_REF_ERROR);
}


@@ -379,7 +379,7 @@ njs_int_t
njs_syntax_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_SYNTAX_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_SYNTAX_ERROR);
}


@@ -421,7 +421,7 @@ njs_int_t
njs_type_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_TYPE_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_TYPE_ERROR);
}


@@ -463,7 +463,7 @@ njs_int_t
njs_uri_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
- return njs_error_create(vm, args, nargs, NJS_PROTOTYPE_URI_ERROR);
+ return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_URI_ERROR);
}


@@ -512,7 +512,7 @@ njs_memory_error_set(njs_vm_t *vm, njs_v

njs_lvlhsh_init(&object->hash);
njs_lvlhsh_init(&object->shared_hash);
- object->__proto__ = &prototypes[NJS_PROTOTYPE_INTERNAL_ERROR].object;
+ object->__proto__ = &prototypes[NJS_OBJ_TYPE_INTERNAL_ERROR].object;
object->type = NJS_OBJECT;
object->shared = 1;

@@ -554,7 +554,7 @@ njs_memory_error_prototype_create(njs_vm

/* MemoryError has no its own prototype. */

- index = NJS_PROTOTYPE_INTERNAL_ERROR;
+ index = NJS_OBJ_TYPE_INTERNAL_ERROR;

function = njs_function(value);
proto = njs_property_prototype_create(vm, &function->object.hash,
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_error.h
--- a/src/njs_error.h Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_error.h Thu Oct 31 18:17:31 2019 +0300
@@ -9,38 +9,38 @@


#define njs_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_ERROR, fmt, ##__VA_ARGS__)
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_ERROR, fmt, ##__VA_ARGS__)
#define njs_eval_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_EVAL_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_EVAL_ERROR, fmt, \
##__VA_ARGS__)
#define njs_internal_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_INTERNAL_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_INTERNAL_ERROR, fmt, \
##__VA_ARGS__)
#define njs_range_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_RANGE_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_RANGE_ERROR, fmt, \
##__VA_ARGS__)
#define njs_reference_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_REF_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_REF_ERROR, fmt, \
##__VA_ARGS__)
#define njs_syntax_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_SYNTAX_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_SYNTAX_ERROR, fmt, \
##__VA_ARGS__)
#define njs_type_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_TYPE_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_TYPE_ERROR, fmt, \
##__VA_ARGS__)
#define njs_uri_error(vm, fmt, ...) \
- njs_error_fmt_new(vm, &vm->retval, NJS_PROTOTYPE_URI_ERROR, fmt, \
+ njs_error_fmt_new(vm, &vm->retval, NJS_OBJ_TYPE_URI_ERROR, fmt, \
##__VA_ARGS__)

-void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_prototype_t type,
+void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type,
u_char *start, size_t size);
void njs_noinline njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst,
- njs_prototype_t type, const char *fmt, ...);
+ njs_object_type_t type, const char *fmt, ...);

void njs_memory_error(njs_vm_t *vm);
void njs_memory_error_set(njs_vm_t *vm, njs_value_t *value);

-njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_prototype_t type,
+njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_object_type_t type,
const njs_value_t *name, const njs_value_t *message);
njs_int_t njs_error_constructor(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused);
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_extern.c
--- a/src/njs_extern.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_extern.c Thu Oct 31 18:17:31 2019 +0300
@@ -106,7 +106,7 @@ njs_vm_external_add(njs_vm_t *vm, njs_lv
*/

function->object.__proto__ =
- &vm->prototypes[NJS_CONSTRUCTOR_FUNCTION].object;
+ &vm->prototypes[NJS_OBJ_TYPE_FUNCTION].object;
function->object.shared_hash = vm->shared->arrow_instance_hash;
function->object.type = NJS_FUNCTION;
function->object.shared = 1;
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_fs.c
--- a/src/njs_fs.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_fs.c Thu Oct 31 18:17:31 2019 +0300
@@ -1028,7 +1028,7 @@ njs_fs_error(njs_vm_t *vm, const char *s
return NJS_ERROR;
}

- error = njs_error_alloc(vm, NJS_PROTOTYPE_ERROR, NULL, &string);
+ error = njs_error_alloc(vm, NJS_OBJ_TYPE_ERROR, NULL, &string);
if (njs_slow_path(error == NULL)) {
return NJS_ERROR;
}
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_function.c
--- a/src/njs_function.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_function.c Thu Oct 31 18:17:31 2019 +0300
@@ -46,7 +46,7 @@ njs_function_alloc(njs_vm_t *vm, njs_fun
function->object.shared_hash = vm->shared->arrow_instance_hash;
}

- function->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_FUNCTION].object;
+ function->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_FUNCTION].object;
function->object.type = NJS_FUNCTION;
function->object.shared = shared;
function->object.extensible = 1;
@@ -122,7 +122,7 @@ njs_function_copy(njs_vm_t *vm, njs_func
}

*copy = *function;
- copy->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_FUNCTION].object;
+ copy->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_FUNCTION].object;
copy->object.shared = 0;

if (nesting == 0) {
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_generator.c
--- a/src/njs_generator.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_generator.c Thu Oct 31 18:17:31 2019 +0300
@@ -247,7 +247,7 @@ static njs_int_t njs_generate_function_d


#define njs_generate_syntax_error(vm, node, fmt, ...) \
- njs_parser_node_error(vm, node, NJS_PROTOTYPE_SYNTAX_ERROR, fmt, \
+ njs_parser_node_error(vm, node, NJS_OBJ_TYPE_SYNTAX_ERROR, fmt, \
##__VA_ARGS__)


@@ -417,23 +417,6 @@ njs_generate(njs_vm_t *vm, njs_generator
return njs_generate_template_literal(vm, generator, node);

case NJS_TOKEN_THIS:
- case NJS_TOKEN_OBJECT_CONSTRUCTOR:
- case NJS_TOKEN_ARRAY_CONSTRUCTOR:
- case NJS_TOKEN_NUMBER_CONSTRUCTOR:
- case NJS_TOKEN_BOOLEAN_CONSTRUCTOR:
- case NJS_TOKEN_STRING_CONSTRUCTOR:
- case NJS_TOKEN_FUNCTION_CONSTRUCTOR:
- case NJS_TOKEN_REGEXP_CONSTRUCTOR:
- case NJS_TOKEN_DATE_CONSTRUCTOR:
- case NJS_TOKEN_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_EVAL_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_INTERNAL_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_RANGE_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_REF_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_SYNTAX_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_TYPE_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_URI_ERROR_CONSTRUCTOR:
- case NJS_TOKEN_MEMORY_ERROR_CONSTRUCTOR:
case NJS_TOKEN_EXTERNAL:
return NJS_OK;

diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_lexer.h
--- a/src/njs_lexer.h Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_lexer.h Thu Oct 31 18:17:31 2019 +0300
@@ -171,24 +171,6 @@ typedef enum {
NJS_TOKEN_ARGUMENTS,
NJS_TOKEN_EVAL,

- NJS_TOKEN_OBJECT_CONSTRUCTOR,
- NJS_TOKEN_ARRAY_CONSTRUCTOR,
- NJS_TOKEN_BOOLEAN_CONSTRUCTOR,
- NJS_TOKEN_NUMBER_CONSTRUCTOR,
- NJS_TOKEN_STRING_CONSTRUCTOR,
- NJS_TOKEN_FUNCTION_CONSTRUCTOR,
- NJS_TOKEN_REGEXP_CONSTRUCTOR,
- NJS_TOKEN_DATE_CONSTRUCTOR,
- NJS_TOKEN_ERROR_CONSTRUCTOR,
- NJS_TOKEN_EVAL_ERROR_CONSTRUCTOR,
- NJS_TOKEN_INTERNAL_ERROR_CONSTRUCTOR,
- NJS_TOKEN_RANGE_ERROR_CONSTRUCTOR,
- NJS_TOKEN_REF_ERROR_CONSTRUCTOR,
- NJS_TOKEN_SYNTAX_ERROR_CONSTRUCTOR,
- NJS_TOKEN_TYPE_ERROR_CONSTRUCTOR,
- NJS_TOKEN_URI_ERROR_CONSTRUCTOR,
- NJS_TOKEN_MEMORY_ERROR_CONSTRUCTOR,
-
NJS_TOKEN_IMPORT,
NJS_TOKEN_EXPORT,

diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_lexer_keyword.c
--- a/src/njs_lexer_keyword.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_lexer_keyword.c Thu Oct 31 18:17:31 2019 +0300
@@ -47,26 +47,6 @@ static const njs_keyword_t njs_keywords
{ njs_str("finally"), NJS_TOKEN_FINALLY, 0 },
{ njs_str("throw"), NJS_TOKEN_THROW, 0 },

- /* Builtin functions. */
-
- { njs_str("Object"), NJS_TOKEN_OBJECT_CONSTRUCTOR, 0 },
- { njs_str("Array"), NJS_TOKEN_ARRAY_CONSTRUCTOR, 0 },
- { njs_str("Boolean"), NJS_TOKEN_BOOLEAN_CONSTRUCTOR, 0 },
- { njs_str("Number"), NJS_TOKEN_NUMBER_CONSTRUCTOR, 0 },
- { njs_str("String"), NJS_TOKEN_STRING_CONSTRUCTOR, 0 },
- { njs_str("Function"), NJS_TOKEN_FUNCTION_CONSTRUCTOR, 0 },
- { njs_str("RegExp"), NJS_TOKEN_REGEXP_CONSTRUCTOR, 0 },
- { njs_str("Date"), NJS_TOKEN_DATE_CONSTRUCTOR, 0 },
- { njs_str("Error"), NJS_TOKEN_ERROR_CONSTRUCTOR, 0 },
- { njs_str("EvalError"), NJS_TOKEN_EVAL_ERROR_CONSTRUCTOR, 0 },
- { njs_str("InternalError"), NJS_TOKEN_INTERNAL_ERROR_CONSTRUCTOR, 0 },
- { njs_str("RangeError"), NJS_TOKEN_RANGE_ERROR_CONSTRUCTOR, 0 },
- { njs_str("ReferenceError"), NJS_TOKEN_REF_ERROR_CONSTRUCTOR, 0 },
- { njs_str("SyntaxError"), NJS_TOKEN_SYNTAX_ERROR_CONSTRUCTOR, 0 },
- { njs_str("TypeError"), NJS_TOKEN_TYPE_ERROR_CONSTRUCTOR, 0 },
- { njs_str("URIError"), NJS_TOKEN_URI_ERROR_CONSTRUCTOR, 0 },
- { njs_str("MemoryError"), NJS_TOKEN_MEMORY_ERROR_CONSTRUCTOR, 0 },
-
/* Module. */

{ njs_str("import"), NJS_TOKEN_IMPORT, 0 },
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_module.c
--- a/src/njs_module.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_module.c Thu Oct 31 18:17:31 2019 +0300
@@ -537,7 +537,7 @@ njs_module_require(njs_vm_t *vm, njs_val
}

*object = module->object;
- object->__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
+ object->__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
object->shared = 0;
object->error_data = 0;

diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_object.c
--- a/src/njs_object.c Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_object.c Thu Oct 31 18:17:31 2019 +0300
@@ -39,7 +39,7 @@ njs_object_alloc(njs_vm_t *vm)
if (njs_fast_path(object != NULL)) {
njs_lvlhsh_init(&object->hash);
njs_lvlhsh_init(&object->shared_hash);
- object->__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
+ object->__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
object->type = NJS_OBJECT;
object->shared = 0;
object->extensible = 1;
@@ -68,7 +68,7 @@ njs_object_value_copy(njs_vm_t *vm, njs_

if (njs_fast_path(object != NULL)) {
*object = *njs_object(value);
- object->__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
+ object->__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
object->shared = 0;
value->data.u.object = object;
return object;
@@ -1605,7 +1605,7 @@ njs_object_prototype_create(njs_vm_t *vm
function = njs_function(value);
index = function - vm->constructors;

- if (index >= 0 && index < NJS_PROTOTYPE_MAX) {
+ if (index >= 0 && index < NJS_OBJ_TYPE_MAX) {
proto = njs_property_prototype_create(vm, &function->object.hash,
&vm->prototypes[index].object);
}
@@ -1928,7 +1928,7 @@ njs_object_prototype_create_constructor(
njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
{
int32_t index;
- njs_value_t *cons;
+ njs_value_t *cons, constructor;
njs_object_t *object;
njs_object_prototype_t *prototype;

@@ -1939,7 +1939,7 @@ njs_object_prototype_create_constructor(
prototype = (njs_object_prototype_t *) object;
index = prototype - vm->prototypes;

- if (index >= 0 && index < NJS_PROTOTYPE_MAX) {
+ if (index >= 0 && index < NJS_OBJ_TYPE_MAX) {
goto found;
}

@@ -1959,7 +1959,8 @@ njs_object_prototype_create_constructor(
found:

if (setval == NULL) {
- setval = &vm->scopes[NJS_SCOPE_GLOBAL][index];
+ njs_set_function(&constructor, &vm->constructors[index]);
+ setval = &constructor;
}

cons = njs_property_constructor_create(vm, &prototype->object.hash, setval);
diff -r ba6f17a75b3a -r c75a8fc6d534 src/njs_object_hash.h
--- a/src/njs_object_hash.h Thu Oct 31 18:17:31 2019 +0300
+++ b/src/njs_object_hash.h Thu Oct 31 18:17:31 2019 +0300
@@ -21,6 +21,15 @@
'_'), '_'), 'p'), 'r'), 'o'), 't'), 'o'), '_'), '_')


+#define NJS_ARRAY_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'A'), 'r'), 'r'), 'a'), 'y')
+
+
#define NJS_ARGV_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -29,6 +38,17 @@
'a'), 'r'), 'g'), 'v')


+#define NJS_BOOLEAN_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'B'), 'o'), 'o'), 'l'), 'e'), 'a'), 'n')
+
+
#define NJS_CONFIGURABLE_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -60,6 +80,14 @@
'c'), 'o'), 'n'), 's'), 't'), 'r'), 'u'), 'c'), 't'), 'o'), 'r')


+#define NJS_DATE_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'D'), 'a'), 't'), 'e')
+
+
#define NJS_ENUMERABLE_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -83,6 +111,15 @@
'e'), 'r'), 'r'), 'n'), 'o')


+#define NJS_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'E'), 'r'), 'r'), 'o'), 'r')
+
+
#define NJS_ENCODING_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -102,6 +139,19 @@
'e'), 'n'), 'v')


+#define NJS_EVAL_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'E'), 'v'), 'a'), 'l'), 'E'), 'r'), 'r'), 'o'), 'r')
+
+
#define NJS_FLAG_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -110,6 +160,25 @@
'f'), 'l'), 'a'), 'g')


+#define NJS_GET_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'g'), 'e'), 't')
+
+
+#define NJS_FUNCTION_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'F'), 'u'), 'n'), 'c'), 't'), 'i'), 'o'), 'n')
+
+
#define NJS_INDEX_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -128,6 +197,24 @@
'i'), 'n'), 'p'), 'u'), 't')


+#define NJS_INTERNAL_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'I'), 'n'), 't'), 'e'), 'r'), 'n'), 'a'), 'l'), \
+ 'E'), 'r'), 'r'), 'o'), 'r')
+
+
#define NJS_GROUPS_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -179,6 +266,16 @@
'n'), 'j'), 's')


+#define NJS_NUMBER_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'N'), 'u'), 'm'), 'b'), 'e'), 'r')
+
+
#define NJS_MATH_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -187,6 +284,22 @@
'M'), 'a'), 't'), 'h')


+#define NJS_MEMORY_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'M'), 'e'), 'm'), 'o'), 'r'), 'y'), \
+ 'E'), 'r'), 'r'), 'o'), 'r')
+
+
#define NJS_MESSAGE_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
@@ -206,15 +319,14 @@
'm'), 'o'), 'd'), 'e')


-#define NJS_SYSCALL_HASH \
- njs_djb_hash_add( \
+#define NJS_OBJECT_HASH \
njs_djb_hash_add( \
njs_djb_hash_add( \
njs_djb_hash_add( \
njs_djb_hash_add( \
njs_djb_hash_add( \
njs_djb_hash_add(NJS_DJB_HASH_INIT, \
- 's'), 'y'), 's'), 'c'), 'a'), 'l'), 'l')
+ 'O'), 'b'), 'j'), 'e'), 'c'), 't')


#define NJS_PATH_HASH \
@@ -249,6 +361,93 @@
'p'), 'r'), 'o'), 't'), 'o'), 't'), 'y'), 'p'), 'e')


+#define NJS_RANGE_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'R'), 'a'), 'n'), 'g'), 'e'), 'E'), 'r'), 'r'), 'o'), 'r')
+
+
+#define NJS_REF_ERROR_HASH \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add( \
+ njs_djb_hash_add(NJS_DJB_HASH_INIT, \
+ 'R'), 'e'), 'f'), 'e'), 'r'), 'e'), 'n'), 'c'), 'e'), \
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Moving constructors to global object.

Dmitry Volyntsev 50 October 31, 2019 11:20AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 91
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready