static njs_ret_t
-njs_array_prototype_length(njs_vm_t *vm, njs_value_t *array)
+njs_array_prototype_length(njs_vm_t *vm, njs_value_t *array,
+ njs_value_t *retval)
{
- njs_value_number_set(&vm->retval, array->data.u.array->length);
+ njs_value_number_set(retval, array->data.u.array->length);
njs_release(vm, array);
static njs_ret_t
-njs_memory_error_prototype_create(njs_vm_t *vm, njs_value_t *value)
+njs_memory_error_prototype_create(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
int32_t index;
njs_value_t *proto;
proto = (njs_value_t *) &njs_value_void;
}
- vm->retval = *proto;
+ *retval = *proto;
return NXT_OK;
}
*/
njs_ret_t
-njs_function_prototype_create(njs_vm_t *vm, njs_value_t *value)
+njs_function_prototype_create(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
njs_value_t *proto;
proto = njs_function_property_prototype_create(vm, value);
if (nxt_fast_path(proto != NULL)) {
- vm->retval = *proto;
+ *retval = *proto;
return NXT_OK;
}
njs_function_t *njs_function_alloc(njs_vm_t *vm);
njs_function_t *njs_function_value_copy(njs_vm_t *vm, njs_value_t *value);
njs_native_frame_t *njs_function_frame_alloc(njs_vm_t *vm, size_t size);
-njs_ret_t njs_function_prototype_create(njs_vm_t *vm, njs_value_t *value);
+njs_ret_t njs_function_prototype_create(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval);
njs_value_t *njs_function_property_prototype_create(njs_vm_t *vm,
njs_value_t *value);
njs_ret_t njs_function_constructor(njs_vm_t *vm, njs_value_t *args,
njs_index_t unused)
{
if (nargs > 1 && njs_is_object(&args[1])) {
- njs_object_prototype_get_proto(vm, &args[1]);
+ njs_object_prototype_get_proto(vm, &args[1], &vm->retval);
return NXT_OK;
}
*/
njs_ret_t
-njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value)
+njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
nxt_uint_t index;
njs_object_t *proto;
proto = &vm->prototypes[index].object;
}
- vm->retval.data.u.object = proto;
- vm->retval.type = proto->type;
- vm->retval.data.truth = 1;
+ retval->data.u.object = proto;
+ retval->type = proto->type;
+ retval->data.truth = 1;
return NXT_OK;
}
*/
njs_ret_t
-njs_object_prototype_create(njs_vm_t *vm, njs_value_t *value)
+njs_object_prototype_create(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
int32_t index;
njs_value_t *proto;
proto = (njs_value_t *) &njs_value_void;
}
- vm->retval = *proto;
+ *retval = *proto;
return NXT_OK;
}
njs_ret_t
-njs_object_prototype_get_proto(njs_vm_t *vm, njs_value_t *value)
+njs_object_prototype_get_proto(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
njs_object_t *proto;
proto = value->data.u.object->__proto__;
if (nxt_fast_path(proto != NULL)) {
- vm->retval.data.u.object = proto;
- vm->retval.type = proto->type;
- vm->retval.data.truth = 1;
+ retval->data.u.object = proto;
+ retval->type = proto->type;
+ retval->data.truth = 1;
} else {
- vm->retval = njs_value_null;
+ *retval = njs_value_null;
}
return NXT_OK;
*/
static njs_ret_t
-njs_object_prototype_create_constructor(njs_vm_t *vm, njs_value_t *value)
+njs_object_prototype_create_constructor(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
int32_t index;
njs_value_t *cons;
cons = njs_property_constructor_create(vm, &prototype->object.hash,
&vm->scopes[NJS_SCOPE_GLOBAL][index]);
if (nxt_fast_path(cons != NULL)) {
- vm->retval = *cons;
+ *retval = *cons;
return NXT_OK;
}
nxt_uint_t nargs, njs_index_t unused);
njs_object_prop_t *njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name,
const njs_value_t *value, uint8_t attributes);
-njs_ret_t njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value);
-njs_ret_t njs_object_prototype_create(njs_vm_t *vm, njs_value_t *value);
+njs_ret_t njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval);
+njs_ret_t njs_object_prototype_create(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval);
njs_value_t *njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
njs_object_t *prototype);
-njs_ret_t njs_object_prototype_get_proto(njs_vm_t *vm, njs_value_t *value);
+njs_ret_t njs_object_prototype_get_proto(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval);
njs_value_t *njs_property_constructor_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
njs_value_t *constructor);
njs_ret_t njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
static njs_ret_t
-njs_regexp_prototype_last_index(njs_vm_t *vm, njs_value_t *value)
+njs_regexp_prototype_last_index(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
uint32_t index;
njs_regexp_t *regexp;
(void) njs_string_prop(&string, ®exp->string);
index = njs_string_index(&string, regexp->last_index);
- njs_value_number_set(&vm->retval, index);
+ njs_value_number_set(retval, index);
return NXT_OK;
}
static njs_ret_t
-njs_regexp_prototype_global(njs_vm_t *vm, njs_value_t *value)
+njs_regexp_prototype_global(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
njs_regexp_pattern_t *pattern;
pattern = value->data.u.regexp->pattern;
- vm->retval = pattern->global ? njs_value_true : njs_value_false;
+ *retval = pattern->global ? njs_value_true : njs_value_false;
njs_release(vm, value);
return NXT_OK;
static njs_ret_t
-njs_regexp_prototype_ignore_case(njs_vm_t *vm, njs_value_t *value)
+njs_regexp_prototype_ignore_case(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
njs_regexp_pattern_t *pattern;
pattern = value->data.u.regexp->pattern;
- vm->retval = pattern->ignore_case ? njs_value_true : njs_value_false;
+ *retval = pattern->ignore_case ? njs_value_true : njs_value_false;
njs_release(vm, value);
return NXT_OK;
static njs_ret_t
-njs_regexp_prototype_multiline(njs_vm_t *vm, njs_value_t *value)
+njs_regexp_prototype_multiline(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
njs_regexp_pattern_t *pattern;
pattern = value->data.u.regexp->pattern;
- vm->retval = pattern->multiline ? njs_value_true : njs_value_false;
+ *retval = pattern->multiline ? njs_value_true : njs_value_false;
njs_release(vm, value);
return NXT_OK;
static njs_ret_t
-njs_regexp_prototype_source(njs_vm_t *vm, njs_value_t *value)
+njs_regexp_prototype_source(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
u_char *source;
int32_t length;
size = strlen((char *) source) - pattern->flags;
length = nxt_utf8_length(source, size);
- return njs_regexp_string_create(vm, &vm->retval, source, size, length);
+ return njs_regexp_string_create(vm, retval, source, size, length);
}
static njs_ret_t
-njs_string_prototype_length(njs_vm_t *vm, njs_value_t *value)
+njs_string_prototype_length(njs_vm_t *vm, njs_value_t *value,
+ njs_value_t *retval)
{
size_t size;
uintptr_t length;
length = (length == 0) ? size : length;
}
- njs_value_number_set(&vm->retval, length);
+ njs_value_number_set(retval, length);
njs_release(vm, value);
typedef struct {
nxt_lvlhsh_query_t lhq;
+
+ /* scratch is used to get the value of an NJS_NATIVE_GETTER property. */
+ njs_object_prop_t scratch;
+
njs_value_t value;
njs_object_t *prototype;
uint8_t query;
retval = &prop->value;
break;
- case NJS_NATIVE_GETTER:
- ret = prop->value.data.u.getter(vm, object);
-
- if (nxt_fast_path(ret == NXT_OK)) {
- return sizeof(njs_vmcode_prop_get_t);
- }
-
- return ret;
-
default:
nxt_thread_log_alert("invalid property get type:%d", prop->type);
if (prop->type != NJS_WHITEOUT) {
pq->shared = 0;
+
return ret;
}
if (ret == NXT_OK) {
pq->shared = 1;
+ prop = pq->lhq.value;
- if (pq->query == NJS_PROPERTY_QUERY_IN) {
+ switch (pq->query) {
+ case NJS_PROPERTY_QUERY_GET:
+ if (prop->type == NJS_NATIVE_GETTER) {
+ pq->scratch = *prop;
+ prop = &pq->scratch;
+ ret = prop->value.data.u.getter(vm, value, &prop->value);
+
+ if (nxt_fast_path(ret == NXT_OK)) {
+ prop->type = NJS_PROPERTY;
+ pq->lhq.value = prop;
+ }
+ }
+
+ break;
+
+ case NJS_PROPERTY_QUERY_IN:
prop = pq->lhq.value;
if (prop->type == NJS_WHITEOUT) {
return NXT_DECLINED;
}
+
+ break;
}
return ret;
njs_vmcode_instance_of(njs_vm_t *vm, njs_value_t *object,
njs_value_t *constructor)
{
- nxt_int_t ret;
- njs_value_t *value;
- njs_object_t *prototype, *proto;
- njs_object_prop_t *prop;
- const njs_value_t *retval;
- nxt_lvlhsh_query_t lhq;
+ nxt_int_t ret;
+ njs_value_t *value;
+ njs_object_t *prototype, *proto;
+ njs_object_prop_t *prop;
+ const njs_value_t *retval;
+ njs_property_query_t pq;
+
+ static njs_value_t prototype_string = njs_string("prototype");
if (!njs_is_function(constructor)) {
njs_exception_type_error(vm, "right argument is not a function", NULL);
retval = &njs_value_false;
if (njs_is_object(object)) {
+ pq.query = NJS_PROPERTY_QUERY_GET;
- lhq.key_hash = NJS_PROTOTYPE_HASH;
- lhq.key = nxt_string_value("prototype");
+ ret = njs_property_query(vm, &pq, constructor, &prototype_string);
- prop = njs_object_property(vm, constructor->data.u.object, &lhq);
-
- if (prop != NULL) {
+ if (nxt_fast_path(ret == NXT_OK)) {
+ prop = pq.lhq.value;
value = &prop->value;
- if (prop->type == NJS_NATIVE_GETTER) {
- /*
- * STUB: getter should be called by some njs_object_property()
- */
- ret = prop->value.data.u.getter(vm, constructor);
-
- if (nxt_slow_path(ret != NXT_OK)) {
- return ret;
- }
-
- value = &vm->retval;
- }
-
/* TODO: test prop->value is object. */
prototype = value->data.u.object;
typedef struct njs_parser_s njs_parser_t;
-typedef njs_ret_t (*njs_getter_t) (njs_vm_t *vm, njs_value_t *obj);
+typedef njs_ret_t (*njs_getter_t) (njs_vm_t *vm, njs_value_t *obj,
+ njs_value_t *retval);
typedef njs_ret_t (*njs_function_native_t) (njs_vm_t *vm, njs_value_t *args,
nxt_uint_t nargs, njs_index_t retval);
{ nxt_string("({}).constructor === Object"),
nxt_string("true") },
+ { nxt_string("({}).constructor()"),
+ nxt_string("[object Object]") },
+
{ nxt_string("var a = Object.__proto__; a()"),
nxt_string("undefined") },
+ { nxt_string("Object.__proto__()"),
+ nxt_string("undefined") },
+
{ nxt_string("var a = Array(3); a"),
nxt_string(",,") },
{ nxt_string("[].constructor === Array"),
nxt_string("true") },
+ { nxt_string("([]).constructor()"),
+ nxt_string("") },
+
{ nxt_string("Boolean()"),
nxt_string("false") },