From f1c8ada97c13b09ce3fd2e2bb71739c087d00935 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 31 Oct 2019 18:17:33 +0300 Subject: [PATCH] Refactored object type initialization. --- src/njs_array.c | 10 +- src/njs_array.h | 6 +- src/njs_boolean.c | 11 +- src/njs_boolean.h | 6 +- src/njs_builtin.c | 298 +++++++++------------------------------------ src/njs_builtin.h | 17 --- src/njs_crypto.c | 22 +++- src/njs_crypto.h | 12 +- src/njs_date.c | 10 +- src/njs_date.h | 6 +- src/njs_error.c | 90 ++++++++++++-- src/njs_error.h | 46 ++----- src/njs_fs.h | 1 + src/njs_function.c | 35 +++--- src/njs_function.h | 13 +- src/njs_main.h | 1 - src/njs_number.c | 32 ++--- src/njs_number.h | 5 +- src/njs_object.c | 10 +- src/njs_object.h | 5 +- src/njs_regexp.c | 10 +- src/njs_regexp.h | 6 +- src/njs_string.c | 11 +- src/njs_string.h | 5 +- src/njs_value.h | 8 ++ src/njs_vm.h | 2 + 26 files changed, 270 insertions(+), 408 deletions(-) delete mode 100644 src/njs_builtin.h diff --git a/src/njs_array.c b/src/njs_array.c index 3fd1cb36..c7f9cbd6 100644 --- a/src/njs_array.c +++ b/src/njs_array.c @@ -177,7 +177,7 @@ memory_error: } -njs_int_t +static njs_int_t njs_array_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -3006,3 +3006,11 @@ const njs_object_init_t njs_array_instance_init = { njs_array_instance_properties, njs_nitems(njs_array_instance_properties), }; + + +const njs_object_type_init_t njs_array_type_init = { + .constructor = njs_array_constructor, + .prototype_props = &njs_array_prototype_init, + .constructor_props = &njs_array_constructor_init, + .value = { .object = { .type = NJS_ARRAY } }, +}; diff --git a/src/njs_array.h b/src/njs_array.h index 242430c8..1fe18472 100644 --- a/src/njs_array.h +++ b/src/njs_array.h @@ -21,12 +21,10 @@ njs_int_t njs_array_string_add(njs_vm_t *vm, njs_array_t *array, const u_char *start, size_t size, size_t length); njs_int_t njs_array_expand(njs_vm_t *vm, njs_array_t *array, uint32_t prepend, uint32_t append); -njs_int_t njs_array_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -extern const njs_object_init_t njs_array_constructor_init; -extern const njs_object_init_t njs_array_prototype_init; + extern const njs_object_init_t njs_array_instance_init; +extern const njs_object_type_init_t njs_array_type_init; #endif /* _NJS_ARRAY_H_INCLUDED_ */ diff --git a/src/njs_boolean.c b/src/njs_boolean.c index 0dc4860e..a9e6e99a 100644 --- a/src/njs_boolean.c +++ b/src/njs_boolean.c @@ -8,7 +8,7 @@ #include -njs_int_t +static njs_int_t njs_boolean_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -163,3 +163,12 @@ const njs_object_init_t njs_boolean_prototype_init = { njs_boolean_prototype_properties, njs_nitems(njs_boolean_prototype_properties), }; + + +const njs_object_type_init_t njs_boolean_type_init = { + .constructor = njs_boolean_constructor, + .prototype_props = &njs_boolean_prototype_init, + .constructor_props = &njs_boolean_constructor_init, + .value = { .object_value = { .value = njs_value(NJS_BOOLEAN, 0, 0.0), + .object = { .type = NJS_OBJECT_BOOLEAN } } }, +}; diff --git a/src/njs_boolean.h b/src/njs_boolean.h index a936706b..26181d98 100644 --- a/src/njs_boolean.h +++ b/src/njs_boolean.h @@ -8,11 +8,7 @@ #define _NJS_BOOLEAN_H_INCLUDED_ -njs_int_t njs_boolean_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); - -extern const njs_object_init_t njs_boolean_constructor_init; -extern const njs_object_init_t njs_boolean_prototype_init; +extern const njs_object_type_init_t njs_boolean_type_init; #endif /* _NJS_BOOLEAN_H_INCLUDED_ */ diff --git a/src/njs_builtin.c b/src/njs_builtin.c index 4dd710fd..d08c4201 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -9,11 +9,6 @@ #include -typedef struct { - njs_function_native_t native; -} njs_function_init_t; - - typedef struct { enum { NJS_BUILTIN_TRAVERSE_KEYS, @@ -27,8 +22,6 @@ typedef struct { } njs_builtin_traverse_t; -static njs_int_t njs_prototype_function(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); static njs_arr_t *njs_vm_expression_completions(njs_vm_t *vm, njs_str_t *expression); static njs_arr_t *njs_object_completions(njs_vm_t *vm, njs_object_t *object); @@ -36,12 +29,12 @@ static njs_int_t njs_env_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, char **environment); -const njs_object_init_t njs_global_this_init; -const njs_object_init_t njs_njs_object_init; -const njs_object_init_t njs_process_object_init; +static const njs_object_init_t njs_global_this_init; +static const njs_object_init_t njs_njs_object_init; +static const njs_object_init_t njs_process_object_init; -const njs_object_init_t *njs_object_init[] = { +static const njs_object_init_t *njs_object_init[] = { &njs_global_this_init, &njs_njs_object_init, &njs_process_object_init, @@ -51,129 +44,43 @@ const njs_object_init_t *njs_object_init[] = { }; -const njs_object_init_t *njs_module_init[] = { +static const njs_object_init_t *njs_module_init[] = { &njs_fs_object_init, &njs_crypto_object_init, NULL }; -const njs_object_init_t *njs_prototype_init[] = { - &njs_object_prototype_init, - &njs_array_prototype_init, - &njs_boolean_prototype_init, - &njs_number_prototype_init, - &njs_string_prototype_init, - &njs_function_prototype_init, - &njs_regexp_prototype_init, - &njs_date_prototype_init, - &njs_hash_prototype_init, - &njs_hmac_prototype_init, - &njs_error_prototype_init, - &njs_eval_error_prototype_init, - &njs_internal_error_prototype_init, - &njs_range_error_prototype_init, - &njs_reference_error_prototype_init, - &njs_syntax_error_prototype_init, - &njs_type_error_prototype_init, - &njs_uri_error_prototype_init, - &njs_internal_error_prototype_init, - NULL -}; - - -const njs_object_init_t *njs_constructor_init[] = { - &njs_object_constructor_init, - &njs_array_constructor_init, - &njs_boolean_constructor_init, - &njs_number_constructor_init, - &njs_string_constructor_init, - &njs_function_constructor_init, - &njs_regexp_constructor_init, - &njs_date_constructor_init, - &njs_hash_constructor_init, - &njs_hmac_constructor_init, - &njs_error_constructor_init, - &njs_eval_error_constructor_init, - &njs_internal_error_constructor_init, - &njs_range_error_constructor_init, - &njs_reference_error_constructor_init, - &njs_syntax_error_constructor_init, - &njs_type_error_constructor_init, - &njs_uri_error_constructor_init, - &njs_memory_error_constructor_init, - NULL -}; - - -const njs_function_init_t njs_native_constructors[] = { - /* SunC does not allow empty array initialization. */ - { njs_object_constructor }, - { njs_array_constructor }, - { njs_boolean_constructor }, - { njs_number_constructor }, - { njs_string_constructor }, - { njs_function_constructor}, - { njs_regexp_constructor }, - { njs_date_constructor }, - { njs_hash_constructor }, - { njs_hmac_constructor }, - { njs_error_constructor }, - { njs_eval_error_constructor }, - { njs_internal_error_constructor }, - { njs_range_error_constructor }, - { njs_reference_error_constructor }, - { njs_syntax_error_constructor }, - { njs_type_error_constructor }, - { njs_uri_error_constructor }, - { njs_memory_error_constructor }, -}; - - -const njs_object_prototype_t njs_prototype_values[] = { - /* - * GCC 4 complains about uninitialized .shared field, - * if the .type field is initialized as .object.type. - */ - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_ARRAY } }, - - /* - * The .object.type field must be initialzed after the .value field, - * otherwise SunC 5.9 treats the .value as .object.value or so. - */ - { .object_value = { .value = njs_value(NJS_BOOLEAN, 0, 0.0), - .object = { .type = NJS_OBJECT_BOOLEAN } } }, - - { .object_value = { .value = njs_value(NJS_NUMBER, 0, 0.0), - .object = { .type = NJS_OBJECT_NUMBER } } }, - - { .object_value = { .value = njs_string(""), - .object = { .type = NJS_OBJECT_STRING } } }, - - { .function = { .native = 1, - .args_offset = 1, - .u.native = njs_prototype_function, - .object = { .type = NJS_FUNCTION } } }, - - { .object = { .type = NJS_REGEXP } }, - - { .object = { .type = NJS_OBJECT } }, - - { .object_value = { .value = njs_value(NJS_DATA, 0, 0.0), - .object = { .type = NJS_OBJECT } } }, - - { .object_value = { .value = njs_value(NJS_DATA, 0, 0.0), - .object = { .type = NJS_OBJECT } } }, - - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, - { .object = { .type = NJS_OBJECT } }, +static const njs_object_type_init_t *const + njs_object_type_init[NJS_OBJ_TYPE_MAX] = +{ + /* Global types. */ + + &njs_obj_type_init, + &njs_array_type_init, + &njs_boolean_type_init, + &njs_number_type_init, + &njs_string_type_init, + &njs_function_type_init, + &njs_regexp_type_init, + &njs_date_type_init, + + /* Hidden types. */ + + &njs_hash_type_init, + &njs_hmac_type_init, + + /* Error types. */ + + &njs_error_type_init, + &njs_eval_error_type_init, + &njs_internal_error_type_init, + &njs_range_error_type_init, + &njs_reference_error_type_init, + &njs_syntax_error_type_init, + &njs_type_error_type_init, + &njs_uri_error_type_init, + &njs_memory_error_type_init, }; @@ -192,16 +99,16 @@ njs_int_t njs_builtin_objects_create(njs_vm_t *vm) { njs_int_t ret; + njs_uint_t i; njs_module_t *module; njs_object_t *object, *string_object; - njs_function_t *func; + njs_function_t *constructor; njs_vm_shared_t *shared; njs_lvlhsh_query_t lhq; njs_regexp_pattern_t *pattern; njs_object_prototype_t *prototype; const njs_object_prop_t *prop; const njs_object_init_t *obj, **p; - const njs_function_init_t *f; static const njs_str_t sandbox_key = njs_str("sandbox"); static const njs_str_t name_key = njs_str("name"); @@ -336,19 +243,17 @@ njs_builtin_objects_create(njs_vm_t *vm) } prototype = shared->prototypes; - memcpy(prototype, njs_prototype_values, sizeof(njs_prototype_values)); - for (p = njs_prototype_init; *p != NULL; p++) { - obj = *p; + for (i = NJS_OBJ_TYPE_OBJECT; i < NJS_OBJ_TYPE_MAX; i++) { + prototype[i] = njs_object_type_init[i]->value; - ret = njs_object_hash_init(vm, &prototype->object.shared_hash, obj); + ret = njs_object_hash_init(vm, &prototype[i].object.shared_hash, + njs_object_type_init[i]->prototype_props); if (njs_slow_path(ret != NJS_OK)) { return NJS_ERROR; } - prototype->object.extensible = 1; - - prototype++; + prototype[i].object.extensible = 1; } shared->prototypes[NJS_OBJ_TYPE_REGEXP].regexp.pattern = @@ -361,28 +266,23 @@ njs_builtin_objects_create(njs_vm_t *vm) string_object->shared = 1; string_object->extensible = 0; - f = njs_native_constructors; - func = shared->constructors; + constructor = shared->constructors; - for (p = njs_constructor_init; *p != NULL; p++) { - obj = *p; + for (i = NJS_OBJ_TYPE_OBJECT; i < NJS_OBJ_TYPE_MAX; i++) { + constructor[i].object.type = NJS_FUNCTION; + constructor[i].object.shared = 0; + constructor[i].object.extensible = 1; + constructor[i].native = 1; + constructor[i].ctor = 1; + constructor[i].args_offset = 1; - func->object.type = NJS_FUNCTION; - func->object.shared = 0; - func->object.extensible = 1; - func->native = 1; - func->ctor = 1; - func->args_offset = 1; + constructor[i].u.native = njs_object_type_init[i]->constructor; - func->u.native = f->native; - - ret = njs_object_hash_init(vm, &func->object.shared_hash, obj); + ret = njs_object_hash_init(vm, &constructor[i].object.shared_hash, + njs_object_type_init[i]->constructor_props); if (njs_slow_path(ret != NJS_OK)) { return NJS_ERROR; } - - f++; - func++; } vm->shared = shared; @@ -391,90 +291,6 @@ njs_builtin_objects_create(njs_vm_t *vm) } -static njs_int_t -njs_prototype_function(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, - njs_index_t unused) -{ - njs_set_undefined(&vm->retval); - - return NJS_OK; -} - - -/* - * Object(), - * Object.__proto__ -> Function.prototype, - * Object.prototype.__proto__ -> null, - * the null value is handled by njs_object_prototype_proto(), - * - * Array(), - * Array.__proto__ -> Function.prototype, - * Array.prototype.__proto__ -> Object.prototype, - * - * Boolean(), - * Boolean.__proto__ -> Function.prototype, - * Boolean.prototype.__proto__ -> Object.prototype, - * - * Number(), - * Number.__proto__ -> Function.prototype, - * Number.prototype.__proto__ -> Object.prototype, - * - * String(), - * String.__proto__ -> Function.prototype, - * String.prototype.__proto__ -> Object.prototype, - * - * Function(), - * Function.__proto__ -> Function.prototype, - * Function.prototype.__proto__ -> Object.prototype, - * - * RegExp(), - * RegExp.__proto__ -> Function.prototype, - * RegExp.prototype.__proto__ -> Object.prototype, - * - * Date(), - * Date.__proto__ -> Function.prototype, - * Date.prototype.__proto__ -> Object.prototype, - * - * Error(), - * Error.__proto__ -> Function.prototype, - * Error.prototype.__proto__ -> Object.prototype, - * - * EvalError(), - * EvalError.__proto__ -> Error, - * EvalError.prototype.__proto__ -> Error.prototype, - * - * InternalError(), - * InternalError.__proto__ -> Error, - * InternalError.prototype.__proto__ -> Error.prototype, - * - * RangeError(), - * RangeError.__proto__ -> Error, - * RangeError.prototype.__proto__ -> Error.prototype, - * - * ReferenceError(), - * ReferenceError.__proto__ -> Error, - * ReferenceError.prototype.__proto__ -> Error.prototype, - * - * SyntaxError(), - * SyntaxError.__proto__ -> Error, - * SyntaxError.prototype.__proto__ -> Error.prototype, - * - * TypeError(), - * TypeError.__proto__ -> Error, - * TypeError.prototype.__proto__ -> Error.prototype, - * - * URIError(), - * URIError.__proto__ -> Error, - * URIError.prototype.__proto__ -> Error.prototype, - * - * MemoryError(), - * MemoryError.__proto__ -> Error, - * MemoryError.prototype.__proto__ -> Error.prototype, - * - * eval(), - * eval.__proto__ -> Function.prototype. - */ - njs_int_t njs_builtin_objects_clone(njs_vm_t *vm, njs_value_t *global) { @@ -972,7 +788,7 @@ njs_builtin_match_native_function(njs_vm_t *vm, njs_function_t *function, /* Constructor from built-in modules (not-mapped to global object). */ - for (i = NJS_OBJ_TYPE_CRYPTO_HASH; i < NJS_OBJ_TYPE_ERROR; i++) { + for (i = NJS_OBJ_TYPE_HIDDEN_MIN; i < NJS_OBJ_TYPE_HIDDEN_MAX; i++) { njs_set_object(&value, &vm->constructors[i].object); ret = njs_value_property(vm, &value, njs_value_arg(&njs_string_name), @@ -1475,7 +1291,7 @@ static const njs_object_prop_t njs_global_this_object_properties[] = }; -const njs_object_init_t njs_global_this_init = { +static const njs_object_init_t njs_global_this_init = { njs_global_this_object_properties, njs_nitems(njs_global_this_object_properties) }; @@ -1500,7 +1316,7 @@ static const njs_object_prop_t njs_njs_object_properties[] = }; -const njs_object_init_t njs_njs_object_init = { +static const njs_object_init_t njs_njs_object_init = { njs_njs_object_properties, njs_nitems(njs_njs_object_properties), }; @@ -1708,7 +1524,7 @@ static const njs_object_prop_t njs_process_object_properties[] = }; -const njs_object_init_t njs_process_object_init = { +static const njs_object_init_t njs_process_object_init = { njs_process_object_properties, njs_nitems(njs_process_object_properties), }; diff --git a/src/njs_builtin.h b/src/njs_builtin.h deleted file mode 100644 index 8c169f22..00000000 --- a/src/njs_builtin.h +++ /dev/null @@ -1,17 +0,0 @@ - -/* - * Copyright (C) Dmitry Volyntsev - * Copyright (C) NGINX, Inc. - */ - -#ifndef _NJS_BUILTIN_H_INCLUDED_ -#define _NJS_BUILTIN_H_INCLUDED_ - - -extern const njs_object_init_t *njs_object_init[]; -extern const njs_object_init_t *njs_module_init[]; -extern const njs_object_init_t *njs_prototype_init[]; -extern const njs_object_init_t *njs_constructor_init[]; - - -#endif /* _NJS_BUILTIN_H_INCLUDED_ */ diff --git a/src/njs_crypto.c b/src/njs_crypto.c index 1b75ee3e..61c8802f 100644 --- a/src/njs_crypto.c +++ b/src/njs_crypto.c @@ -352,7 +352,7 @@ const njs_object_init_t njs_hash_prototype_init = { }; -njs_int_t +static njs_int_t njs_hash_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -390,6 +390,15 @@ const njs_object_init_t njs_hash_constructor_init = { }; +const njs_object_type_init_t njs_hash_type_init = { + .constructor = njs_hash_constructor, + .prototype_props = &njs_hash_prototype_init, + .constructor_props = &njs_hash_constructor_init, + .value = { .object_value = { .value = njs_value(NJS_DATA, 0, 0.0), + .object = { .type = NJS_OBJECT } } }, +}; + + static njs_int_t njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) @@ -634,7 +643,7 @@ const njs_object_init_t njs_hmac_prototype_init = { }; -njs_int_t +static njs_int_t njs_hmac_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -712,6 +721,15 @@ const njs_object_init_t njs_crypto_object_init = { }; +const njs_object_type_init_t njs_hmac_type_init = { + .constructor = njs_hmac_constructor, + .prototype_props = &njs_hmac_prototype_init, + .constructor_props = &njs_hmac_constructor_init, + .value = { .object_value = { .value = njs_value(NJS_DATA, 0, 0.0), + .object = { .type = NJS_OBJECT } } }, +}; + + static njs_hash_alg_t * njs_crypto_alg(njs_vm_t *vm, const njs_str_t *name) { diff --git a/src/njs_crypto.h b/src/njs_crypto.h index ce00d3a3..27472f1d 100644 --- a/src/njs_crypto.h +++ b/src/njs_crypto.h @@ -7,18 +7,10 @@ #ifndef _NJS_CRYPTO_H_INCLUDED_ #define _NJS_CRYPTO_H_INCLUDED_ -njs_int_t njs_hash_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_hmac_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); - extern const njs_object_init_t njs_crypto_object_init; -extern const njs_object_init_t njs_hash_prototype_init; -extern const njs_object_init_t njs_hmac_prototype_init; - -extern const njs_object_init_t njs_hash_constructor_init; -extern const njs_object_init_t njs_hmac_constructor_init; +extern const njs_object_type_init_t njs_hash_type_init; +extern const njs_object_type_init_t njs_hmac_type_init; #endif /* _NJS_CRYPTO_H_INCLUDED_ */ diff --git a/src/njs_date.c b/src/njs_date.c index 4ee83943..43e9d8c7 100644 --- a/src/njs_date.c +++ b/src/njs_date.c @@ -168,7 +168,7 @@ njs_make_date(int64_t days, int64_t time, njs_bool_t local) } -njs_int_t +static njs_int_t njs_date_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -2890,3 +2890,11 @@ const njs_object_init_t njs_date_prototype_init = { njs_date_prototype_properties, njs_nitems(njs_date_prototype_properties), }; + + +const njs_object_type_init_t njs_date_type_init = { + .constructor = njs_date_constructor, + .prototype_props = &njs_date_prototype_init, + .constructor_props = &njs_date_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; diff --git a/src/njs_date.h b/src/njs_date.h index 93263b22..d4fbb062 100644 --- a/src/njs_date.h +++ b/src/njs_date.h @@ -8,15 +8,11 @@ #define _NJS_DATE_H_INCLUDED_ -njs_int_t njs_date_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); - njs_int_t njs_date_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *date); -extern const njs_object_init_t njs_date_constructor_init; -extern const njs_object_init_t njs_date_prototype_init; +extern const njs_object_type_init_t njs_date_type_init; #endif /* _NJS_DATE_H_INCLUDED_ */ diff --git a/src/njs_error.c b/src/njs_error.c index e0184556..e84e8f40 100644 --- a/src/njs_error.c +++ b/src/njs_error.c @@ -165,7 +165,7 @@ njs_error_create(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } -njs_int_t +static njs_int_t njs_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -206,7 +206,7 @@ const njs_object_init_t njs_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_eval_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -247,7 +247,7 @@ const njs_object_init_t njs_eval_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_internal_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -288,7 +288,7 @@ const njs_object_init_t njs_internal_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_range_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -329,7 +329,7 @@ const njs_object_init_t njs_range_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_reference_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -370,7 +370,7 @@ const njs_object_init_t njs_reference_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_syntax_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -411,7 +411,7 @@ const njs_object_init_t njs_syntax_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_type_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -452,7 +452,7 @@ const njs_object_init_t njs_type_error_constructor_init = { }; -njs_int_t +static njs_int_t njs_uri_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -526,7 +526,7 @@ njs_memory_error(njs_vm_t *vm) } -njs_int_t +static njs_int_t njs_memory_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -758,6 +758,14 @@ const njs_object_init_t njs_error_prototype_init = { }; +const njs_object_type_init_t njs_error_type_init = { + .constructor = njs_error_constructor, + .prototype_props = &njs_error_prototype_init, + .constructor_props = &njs_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_eval_error_prototype_properties[] = { { @@ -792,6 +800,14 @@ const njs_object_init_t njs_eval_error_prototype_init = { }; +const njs_object_type_init_t njs_eval_error_type_init = { + .constructor = njs_eval_error_constructor, + .prototype_props = &njs_eval_error_prototype_init, + .constructor_props = &njs_eval_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static njs_int_t njs_internal_error_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) @@ -846,6 +862,22 @@ const njs_object_init_t njs_internal_error_prototype_init = { }; +const njs_object_type_init_t njs_internal_error_type_init = { + .constructor = njs_internal_error_constructor, + .prototype_props = &njs_internal_error_prototype_init, + .constructor_props = &njs_internal_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + +const njs_object_type_init_t njs_memory_error_type_init = { + .constructor = njs_memory_error_constructor, + .prototype_props = &njs_internal_error_prototype_init, + .constructor_props = &njs_memory_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_range_error_prototype_properties[] = { { @@ -880,6 +912,14 @@ const njs_object_init_t njs_range_error_prototype_init = { }; +const njs_object_type_init_t njs_range_error_type_init = { + .constructor = njs_range_error_constructor, + .prototype_props = &njs_range_error_prototype_init, + .constructor_props = &njs_range_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_reference_error_prototype_properties[] = { { @@ -914,6 +954,14 @@ const njs_object_init_t njs_reference_error_prototype_init = { }; +const njs_object_type_init_t njs_reference_error_type_init = { + .constructor = njs_reference_error_constructor, + .prototype_props = &njs_reference_error_prototype_init, + .constructor_props = &njs_reference_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_syntax_error_prototype_properties[] = { { @@ -948,6 +996,14 @@ const njs_object_init_t njs_syntax_error_prototype_init = { }; +const njs_object_type_init_t njs_syntax_error_type_init = { + .constructor = njs_syntax_error_constructor, + .prototype_props = &njs_syntax_error_prototype_init, + .constructor_props = &njs_syntax_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_type_error_prototype_properties[] = { { @@ -982,6 +1038,14 @@ const njs_object_init_t njs_type_error_prototype_init = { }; +const njs_object_type_init_t njs_type_error_type_init = { + .constructor = njs_type_error_constructor, + .prototype_props = &njs_type_error_prototype_init, + .constructor_props = &njs_type_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; + + static const njs_object_prop_t njs_uri_error_prototype_properties[] = { { @@ -1014,3 +1078,11 @@ const njs_object_init_t njs_uri_error_prototype_init = { njs_uri_error_prototype_properties, njs_nitems(njs_uri_error_prototype_properties), }; + + +const njs_object_type_init_t njs_uri_error_type_init = { + .constructor = njs_uri_error_constructor, + .prototype_props = &njs_uri_error_prototype_init, + .constructor_props = &njs_uri_error_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; diff --git a/src/njs_error.h b/src/njs_error.h index 8ef7d5f7..0d5f0e0a 100644 --- a/src/njs_error.h +++ b/src/njs_error.h @@ -42,47 +42,19 @@ void njs_memory_error_set(njs_vm_t *vm, njs_value_t *value); 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); -njs_int_t njs_eval_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_internal_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_range_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_reference_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_syntax_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_type_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_uri_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); -njs_int_t njs_memory_error_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); - njs_int_t njs_error_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *error); -extern const njs_object_init_t njs_error_constructor_init; -extern const njs_object_init_t njs_eval_error_constructor_init; -extern const njs_object_init_t njs_internal_error_constructor_init; -extern const njs_object_init_t njs_range_error_constructor_init; -extern const njs_object_init_t njs_reference_error_constructor_init; -extern const njs_object_init_t njs_syntax_error_constructor_init; -extern const njs_object_init_t njs_type_error_constructor_init; -extern const njs_object_init_t njs_uri_error_constructor_init; -extern const njs_object_init_t njs_memory_error_constructor_init; - -extern const njs_object_init_t njs_error_prototype_init; -extern const njs_object_init_t njs_eval_error_prototype_init; -extern const njs_object_init_t njs_internal_error_prototype_init; -extern const njs_object_init_t njs_range_error_prototype_init; -extern const njs_object_init_t njs_reference_error_prototype_init; -extern const njs_object_init_t njs_syntax_error_prototype_init; -extern const njs_object_init_t njs_type_error_prototype_init; -extern const njs_object_init_t njs_uri_error_prototype_init; +extern const njs_object_type_init_t njs_error_type_init; +extern const njs_object_type_init_t njs_eval_error_type_init; +extern const njs_object_type_init_t njs_internal_error_type_init; +extern const njs_object_type_init_t njs_range_error_type_init; +extern const njs_object_type_init_t njs_reference_error_type_init; +extern const njs_object_type_init_t njs_syntax_error_type_init; +extern const njs_object_type_init_t njs_type_error_type_init; +extern const njs_object_type_init_t njs_uri_error_type_init; +extern const njs_object_type_init_t njs_memory_error_type_init; #endif /* _NJS_BOOLEAN_H_INCLUDED_ */ diff --git a/src/njs_fs.h b/src/njs_fs.h index 89e7fa61..db6a72e0 100644 --- a/src/njs_fs.h +++ b/src/njs_fs.h @@ -7,6 +7,7 @@ #ifndef _NJS_FS_H_INCLUDED_ #define _NJS_FS_H_INCLUDED_ + extern const njs_object_init_t njs_fs_object_init; diff --git a/src/njs_function.c b/src/njs_function.c index 5b6dbc15..d43bfb28 100644 --- a/src/njs_function.c +++ b/src/njs_function.c @@ -723,7 +723,7 @@ njs_function_prototype_create(njs_vm_t *vm, njs_object_prop_t *prop, } -njs_int_t +static njs_int_t njs_function_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -1211,27 +1211,22 @@ njs_eval_function(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } -static const njs_object_prop_t njs_eval_function_properties[] = +static njs_int_t +njs_prototype_function(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, + njs_index_t unused) { - /* eval.name == "eval". */ - { - .type = NJS_PROPERTY, - .name = njs_string("name"), - .value = njs_string("eval"), - .configurable = 1, - }, + njs_set_undefined(&vm->retval); - /* eval.length == 1. */ - { - .type = NJS_PROPERTY, - .name = njs_string("length"), - .value = njs_value(NJS_NUMBER, 1, 1.0), - .configurable = 1, - }, -}; + return NJS_OK; +} -const njs_object_init_t njs_eval_function_init = { - njs_eval_function_properties, - njs_nitems(njs_eval_function_properties), +const njs_object_type_init_t njs_function_type_init = { + .constructor = njs_function_constructor, + .prototype_props = &njs_function_prototype_init, + .constructor_props = &njs_function_constructor_init, + .value = { .function = { .native = 1, + .args_offset = 1, + .u.native = njs_prototype_function, + .object = { .type = NJS_FUNCTION } } }, }; diff --git a/src/njs_function.h b/src/njs_function.h index 82ca3a3b..9c80fb5e 100644 --- a/src/njs_function.h +++ b/src/njs_function.h @@ -107,8 +107,8 @@ njs_int_t njs_function_rest_parameters_init(njs_vm_t *vm, njs_native_frame_t *frame); njs_int_t njs_function_prototype_create(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); -njs_int_t njs_function_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); +njs_int_t njs_eval_function(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, + njs_index_t unused); njs_int_t njs_function_native_frame(njs_vm_t *vm, njs_function_t *function, const njs_value_t *this, const njs_value_t *args, njs_uint_t nargs, njs_bool_t ctor); @@ -194,16 +194,9 @@ njs_function_apply(njs_vm_t *vm, njs_function_t *function, } -extern const njs_object_init_t njs_function_constructor_init; -extern const njs_object_init_t njs_function_prototype_init; +extern const njs_object_type_init_t njs_function_type_init; extern const njs_object_init_t njs_function_instance_init; extern const njs_object_init_t njs_arrow_instance_init; extern const njs_object_init_t njs_arguments_object_instance_init; -njs_int_t njs_eval_function(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, - njs_index_t unused); - -extern const njs_object_init_t njs_eval_function_init; - - #endif /* _NJS_FUNCTION_H_INCLUDED_ */ diff --git a/src/njs_main.h b/src/njs_main.h index 4ee8748a..cd2c9663 100644 --- a/src/njs_main.h +++ b/src/njs_main.h @@ -70,7 +70,6 @@ #include #include -#include #include #include #include diff --git a/src/njs_number.c b/src/njs_number.c index ef3a55ea..48183b70 100644 --- a/src/njs_number.c +++ b/src/njs_number.c @@ -237,7 +237,7 @@ njs_number_to_string(njs_vm_t *vm, njs_value_t *string, } -njs_int_t +static njs_int_t njs_number_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -1123,28 +1123,10 @@ njs_number_parse_float(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } -static const njs_object_prop_t njs_is_finite_function_properties[] = -{ - /* isFinite.name == "isFinite". */ - { - .type = NJS_PROPERTY, - .name = njs_string("name"), - .value = njs_string("isFinite"), - .configurable = 1, - }, - - /* isFinite.length == 1. */ - { - .type = NJS_PROPERTY, - .name = njs_string("length"), - .value = njs_value(NJS_NUMBER, 1, 1.0), - .configurable = 1, - }, +const njs_object_type_init_t njs_number_type_init = { + .constructor = njs_number_constructor, + .prototype_props = &njs_number_prototype_init, + .constructor_props = &njs_number_constructor_init, + .value = { .object_value = { .value = njs_value(NJS_NUMBER, 0, 0.0), + .object = { .type = NJS_OBJECT_NUMBER } } }, }; - - -const njs_object_init_t njs_is_finite_function_init = { - njs_is_finite_function_properties, - njs_nitems(njs_is_finite_function_properties), -}; - diff --git a/src/njs_number.h b/src/njs_number.h index 3fcc5c0b..dcce83fc 100644 --- a/src/njs_number.h +++ b/src/njs_number.h @@ -17,8 +17,6 @@ int64_t njs_number_radix_parse(const u_char **start, const u_char *end, uint8_t radix); njs_int_t njs_number_to_string(njs_vm_t *vm, njs_value_t *string, const njs_value_t *number); -njs_int_t njs_number_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); njs_int_t njs_number_global_is_nan(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused); njs_int_t njs_number_global_is_finite(njs_vm_t *vm, njs_value_t *args, @@ -154,8 +152,7 @@ njs_uint32_to_string(njs_value_t *value, uint32_t u32) } -extern const njs_object_init_t njs_number_constructor_init; -extern const njs_object_init_t njs_number_prototype_init; +extern const njs_object_type_init_t njs_number_type_init; #endif /* _NJS_NUMBER_H_INCLUDED_ */ diff --git a/src/njs_object.c b/src/njs_object.c index fd27472c..26c051de 100644 --- a/src/njs_object.c +++ b/src/njs_object.c @@ -190,7 +190,7 @@ njs_object_hash_test(njs_lvlhsh_query_t *lhq, void *data) } -njs_int_t +static njs_int_t njs_object_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -2388,3 +2388,11 @@ njs_object_length(njs_vm_t *vm, njs_value_t *value, uint32_t *length) return njs_value_to_length(vm, &value_length, length); } + + +const njs_object_type_init_t njs_obj_type_init = { + .constructor = njs_object_constructor, + .prototype_props = &njs_object_prototype_init, + .constructor_props = &njs_object_constructor_init, + .value = { .object = { .type = NJS_OBJECT } }, +}; diff --git a/src/njs_object.h b/src/njs_object.h index f8936bdf..4943077a 100644 --- a/src/njs_object.h +++ b/src/njs_object.h @@ -72,8 +72,6 @@ njs_int_t njs_object_traverse(njs_vm_t *vm, njs_object_t *object, void *ctx, njs_object_traverse_cb_t cb); njs_int_t njs_object_hash_create(njs_vm_t *vm, njs_lvlhsh_t *hash, const njs_object_prop_t *prop, njs_uint_t n); -njs_int_t njs_object_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); njs_int_t njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); @@ -103,8 +101,7 @@ njs_int_t njs_object_prop_descriptor(njs_vm_t *vm, njs_value_t *dest, njs_value_t *value, njs_value_t *setval); const char *njs_prop_type_string(njs_object_prop_type_t type); -extern const njs_object_init_t njs_object_constructor_init; -extern const njs_object_init_t njs_object_prototype_init; +extern const njs_object_type_init_t njs_obj_type_init; njs_inline njs_int_t diff --git a/src/njs_regexp.c b/src/njs_regexp.c index cf8465e6..4234ac21 100644 --- a/src/njs_regexp.c +++ b/src/njs_regexp.c @@ -96,7 +96,7 @@ njs_regexp_value_flags(njs_vm_t *vm, const njs_value_t *regexp) } -njs_int_t +static njs_int_t njs_regexp_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -1268,3 +1268,11 @@ const njs_object_init_t njs_regexp_prototype_init = { njs_regexp_prototype_properties, njs_nitems(njs_regexp_prototype_properties), }; + + +const njs_object_type_init_t njs_regexp_type_init = { + .constructor = njs_regexp_constructor, + .prototype_props = &njs_regexp_prototype_init, + .constructor_props = &njs_regexp_constructor_init, + .value = { .object = { .type = NJS_REGEXP } }, +}; diff --git a/src/njs_regexp.h b/src/njs_regexp.h index 12f57981..0873d713 100644 --- a/src/njs_regexp.h +++ b/src/njs_regexp.h @@ -17,8 +17,6 @@ typedef enum { njs_int_t njs_regexp_init(njs_vm_t *vm); -njs_int_t njs_regexp_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); njs_int_t njs_regexp_create(njs_vm_t *vm, njs_value_t *value, u_char *start, size_t length, njs_regexp_flags_t flags); njs_token_t njs_regexp_literal(njs_vm_t *vm, njs_parser_t *parser, @@ -34,8 +32,8 @@ njs_int_t njs_regexp_prototype_exec(njs_vm_t *vm, njs_value_t *args, njs_int_t njs_regexp_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *regexp); -extern const njs_object_init_t njs_regexp_constructor_init; -extern const njs_object_init_t njs_regexp_prototype_init; + +extern const njs_object_type_init_t njs_regexp_type_init; #endif /* _NJS_REGEXP_H_INCLUDED_ */ diff --git a/src/njs_string.c b/src/njs_string.c index f5e0ba2b..698c3450 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -537,7 +537,7 @@ njs_string_prop(njs_string_prop_t *string, const njs_value_t *value) } -njs_int_t +static njs_int_t njs_string_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused) { @@ -5265,3 +5265,12 @@ njs_value_index(njs_vm_t *vm, const njs_value_t *src, njs_uint_t runtime) return (njs_index_t) value; } + + +const njs_object_type_init_t njs_string_type_init = { + .constructor = njs_string_constructor, + .prototype_props = &njs_string_prototype_init, + .constructor_props = &njs_string_constructor_init, + .value = { .object_value = { .value = njs_string(""), + .object = { .type = NJS_OBJECT_STRING } } }, +}; diff --git a/src/njs_string.h b/src/njs_string.h index ef88a1ab..edf78f4f 100644 --- a/src/njs_string.h +++ b/src/njs_string.h @@ -165,8 +165,6 @@ void njs_string_copy(njs_value_t *dst, njs_value_t *src); njs_int_t njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string, njs_value_t *value); size_t njs_string_prop(njs_string_prop_t *string, const njs_value_t *value); -njs_int_t njs_string_constructor(njs_vm_t *vm, njs_value_t *args, - njs_uint_t nargs, njs_index_t unused); njs_int_t njs_string_cmp(const njs_value_t *val1, const njs_value_t *val2); void njs_string_slice_string_prop(njs_string_prop_t *dst, const njs_string_prop_t *string, const njs_slice_prop_t *slice); @@ -194,9 +192,8 @@ njs_int_t njs_string_prototype_concat(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused); -extern const njs_object_init_t njs_string_constructor_init; -extern const njs_object_init_t njs_string_prototype_init; extern const njs_object_init_t njs_string_instance_init; +extern const njs_object_type_init_t njs_string_type_init; #endif /* _NJS_STRING_H_INCLUDED_ */ diff --git a/src/njs_value.h b/src/njs_value.h index 60f87175..75a1ec2e 100644 --- a/src/njs_value.h +++ b/src/njs_value.h @@ -284,6 +284,14 @@ typedef union { } njs_object_prototype_t; +typedef struct { + njs_function_native_t constructor; + const njs_object_init_t *prototype_props; + const njs_object_init_t *constructor_props; + njs_object_prototype_t value; +} njs_object_type_init_t; + + typedef enum { NJS_ENUM_KEYS, NJS_ENUM_VALUES, diff --git a/src/njs_vm.h b/src/njs_vm.h index 5475b515..63f3059e 100644 --- a/src/njs_vm.h +++ b/src/njs_vm.h @@ -89,7 +89,9 @@ typedef enum { NJS_OBJ_TYPE_REGEXP, NJS_OBJ_TYPE_DATE, NJS_OBJ_TYPE_CRYPTO_HASH, +#define NJS_OBJ_TYPE_HIDDEN_MIN (NJS_OBJ_TYPE_CRYPTO_HASH) NJS_OBJ_TYPE_CRYPTO_HMAC, +#define NJS_OBJ_TYPE_HIDDEN_MAX (NJS_OBJ_TYPE_CRYPTO_HMAC + 1) NJS_OBJ_TYPE_ERROR, NJS_OBJ_TYPE_EVAL_ERROR, NJS_OBJ_TYPE_INTERNAL_ERROR, -- 2.47.3