From: Alexander Borisov Date: Tue, 3 Dec 2019 14:13:34 +0000 (+0300) Subject: Introduced SpeciesConstructor() from the specification. X-Git-Url: http://git.kaiwu.me/sitemap.xml?a=commitdiff_plain;h=cbda64d761488efc302bb8ecf47f1be6f805804f;p=njs.git Introduced SpeciesConstructor() from the specification. --- diff --git a/src/njs_value.c b/src/njs_value.c index c13ab92c..4abf3120 100644 --- a/src/njs_value.c +++ b/src/njs_value.c @@ -1259,3 +1259,56 @@ njs_symbol_conversion_failed(njs_vm_t *vm, njs_bool_t to_string) ? "Cannot convert a Symbol value to a string" : "Cannot convert a Symbol value to a number"); } + + +njs_int_t +njs_value_species_constructor(njs_vm_t *vm, njs_value_t *object, + njs_value_t *default_constructor, njs_value_t *dst) +{ + njs_int_t ret; + njs_value_t constructor, retval; + + static const njs_value_t string_constructor = njs_string("constructor"); + static const njs_value_t string_species = + njs_wellknown_symbol(NJS_SYMBOL_SPECIES); + + ret = njs_value_property(vm, object, njs_value_arg(&string_constructor), + &constructor); + if (njs_slow_path(ret == NJS_ERROR)) { + return NJS_ERROR; + } + + if (njs_is_undefined(&constructor)) { + goto default_сonstructor; + } + + if (njs_slow_path(!njs_is_object(&constructor))) { + njs_type_error(vm, "constructor is not object"); + return NJS_ERROR; + } + + ret = njs_value_property(vm, &constructor, njs_value_arg(&string_species), + &retval); + if (njs_slow_path(ret == NJS_ERROR)) { + return NJS_ERROR; + } + + if (njs_value_is_null_or_undefined(&retval)) { + goto default_сonstructor; + } + + if (!njs_is_function(&retval)) { + njs_type_error(vm, "object does not contain a constructor"); + return NJS_ERROR; + } + + *dst = retval; + + return NJS_OK; + +default_сonstructor: + + *dst = *default_constructor; + + return NJS_OK; +} diff --git a/src/njs_value.h b/src/njs_value.h index bf911488..6a4743d2 100644 --- a/src/njs_value.h +++ b/src/njs_value.h @@ -909,6 +909,9 @@ njs_int_t njs_value_to_object(njs_vm_t *vm, njs_value_t *value); void njs_symbol_conversion_failed(njs_vm_t *vm, njs_bool_t to_string); +njs_int_t njs_value_species_constructor(njs_vm_t *vm, njs_value_t *object, + njs_value_t *default_constructor, njs_value_t *dst); + #include "njs_number.h"