Skip to content

Commit ba48645

Browse files
committed
Verbose errors also for return shorthand
1 parent 84f462f commit ba48645

18 files changed

Lines changed: 116 additions & 112 deletions

src-input/duk_api_stack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,11 +4062,11 @@ DUK_EXTERNAL void duk_push_buffer_object(duk_context *ctx, duk_idx_t idx_buffer,
40624062
return;
40634063

40644064
range_error:
4065-
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS);
4065+
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
40664066
return; /* not reached */
40674067

40684068
arg_error:
4069-
DUK_ERROR_TYPE(thr, DUK_STR_INVALID_CALL_ARGS);
4069+
DUK_ERROR_TYPE(thr, DUK_STR_INVALID_ARGS);
40704070
return; /* not reached */
40714071
}
40724072
#else /* DUK_USE_BUFFEROBJECT_SUPPORT */

src-input/duk_bi_array.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_context *ctx) {
7676
*/
7777
duk_uint32_t ret = duk__push_this_obj_len_u32(ctx);
7878
if (DUK_UNLIKELY(ret >= 0x80000000UL)) {
79-
DUK_ERROR_RANGE((duk_hthread *) ctx, DUK_STR_ARRAY_LENGTH_OVER_2G);
79+
DUK_ERROR_RANGE_INVALID_LENGTH((duk_hthread *) ctx);
8080
}
8181
return ret;
8282
}
@@ -157,7 +157,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_context *ctx) {
157157
d = duk_get_number(ctx, 0);
158158
len = duk_to_uint32(ctx, 0);
159159
if (((duk_double_t) len) != d) {
160-
return DUK_RET_RANGE_ERROR;
160+
DUK_DCERROR_RANGE_INVALID_LENGTH((duk_hthread *) ctx);
161161
}
162162

163163
/* For small lengths create a dense preallocated array.
@@ -481,7 +481,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx) {
481481
}
482482

483483
#if defined(DUK_USE_ARRAY_FASTPATH)
484-
DUK_LOCAL duk_bool_t duk__array_push_fastpath(duk_context *ctx, duk_harray *h_arr) {
484+
DUK_LOCAL duk_ret_t duk__array_push_fastpath(duk_context *ctx, duk_harray *h_arr) {
485485
duk_hthread *thr;
486486
duk_tval *tv_arraypart;
487487
duk_tval *tv_src;
@@ -497,7 +497,7 @@ DUK_LOCAL duk_bool_t duk__array_push_fastpath(duk_context *ctx, duk_harray *h_ar
497497
n = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
498498
if (DUK_UNLIKELY(len + n < len)) {
499499
DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
500-
return DUK_RET_RANGE_ERROR;
500+
DUK_DCERROR_RANGE_INVALID_LENGTH(thr); /* != 0 return value returned as is by caller */
501501
}
502502
if (len + n > DUK_HOBJECT_GET_ASIZE((duk_hobject *) h_arr)) {
503503
/* Array part would need to be extended. Rely on slow path
@@ -569,7 +569,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx) {
569569

570570
if (len + (duk_uint32_t) n < len) {
571571
DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
572-
return DUK_RET_RANGE_ERROR;
572+
DUK_DCERROR_RANGE_INVALID_LENGTH((duk_hthread *) ctx);
573573
}
574574

575575
for (i = 0; i < n; i++) {
@@ -962,7 +962,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx) {
962962
/* For now, restrict result array into 32-bit length range. */
963963
if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) {
964964
DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw"));
965-
return DUK_RET_RANGE_ERROR;
965+
DUK_DCERROR_RANGE_INVALID_LENGTH((duk_hthread *) ctx);
966966
}
967967

968968
duk_push_array(ctx);
@@ -1248,7 +1248,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx) {
12481248

12491249
if (len + (duk_uint32_t) nargs < len) {
12501250
DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw"));
1251-
return DUK_RET_RANGE_ERROR;
1251+
DUK_DCERROR_RANGE_INVALID_LENGTH((duk_hthread *) ctx);
12521252
}
12531253

12541254
i = len;
@@ -1536,9 +1536,7 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) {
15361536

15371537
duk_set_top(ctx, 2);
15381538
len = duk__push_this_obj_len_u32(ctx);
1539-
if (!duk_is_callable(ctx, 0)) {
1540-
goto type_error;
1541-
}
1539+
duk_require_callable(ctx, 0);
15421540

15431541
/* stack[0] = callback fn
15441542
* stack[1] = initialValue
@@ -1600,14 +1598,11 @@ DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx) {
16001598
}
16011599

16021600
if (!have_acc) {
1603-
goto type_error;
1601+
DUK_DCERROR_TYPE_INVALID_ARGS((duk_hthread *) ctx);
16041602
}
16051603

16061604
DUK_ASSERT_TOP(ctx, 5);
16071605
return 1;
1608-
1609-
type_error:
1610-
return DUK_RET_TYPE_ERROR;
16111606
}
16121607

16131608
#endif /* DUK_USE_ARRAY_BUILTIN */

src-input/duk_bi_boolean.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx
3636
}
3737
}
3838

39-
return DUK_RET_TYPE_ERROR;
39+
DUK_DCERROR_TYPE_INVALID_ARGS((duk_hthread *) ctx);
40+
/* never here */
4041

4142
type_ok:
4243
if (coerce_tostring) {

src-input/duk_bi_buffer.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ DUK_LOCAL void duk__resolve_offset_opt_length(duk_context *ctx,
314314
return;
315315

316316
fail_range:
317-
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_CALL_ARGS);
317+
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
318318
}
319319

320320
/* Shared lenient buffer length clamping helper. No negative indices, no
@@ -618,7 +618,7 @@ DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) {
618618

619619
/* XXX: function flag to make this automatic? */
620620
if (!duk_is_constructor_call(ctx)) {
621-
return DUK_RET_TYPE_ERROR;
621+
DUK_DCERROR_TYPE_INVALID_ARGS(thr); /* reworked in another branch... */
622622
}
623623

624624
len = duk_to_int(ctx, 0);
@@ -650,7 +650,7 @@ DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx) {
650650
return 1;
651651

652652
fail_length:
653-
return DUK_RET_RANGE_ERROR;
653+
DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
654654
}
655655
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
656656

@@ -692,7 +692,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
692692

693693
/* XXX: function flag to make this automatic? */
694694
if (!duk_is_constructor_call(ctx)) {
695-
return DUK_RET_TYPE_ERROR;
695+
DUK_DCERROR_TYPE_INVALID_ARGS(thr); /* reworked in another branch... */
696696
}
697697

698698
/* We could fit built-in index into magic but that'd make the magic
@@ -795,7 +795,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
795795
proto_bidx);
796796
h_val = h_bufarg->buf;
797797
if (h_val == NULL) {
798-
return DUK_RET_TYPE_ERROR;
798+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
799799
}
800800
h_bufobj->buf = h_val;
801801
DUK_HBUFFER_INCREF(thr, h_val);
@@ -821,7 +821,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
821821
DUK_ASSERT_HBUFOBJ_VALID(h_bufarg);
822822
elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift);
823823
if (h_bufarg->buf == NULL) {
824-
return DUK_RET_TYPE_ERROR;
824+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
825825
}
826826

827827
/* Select copy mode. Must take into account element
@@ -1026,7 +1026,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx) {
10261026
return 1;
10271027

10281028
fail_arguments:
1029-
return DUK_RET_RANGE_ERROR;
1029+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
10301030
}
10311031
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
10321032

@@ -1040,7 +1040,7 @@ DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) {
10401040

10411041
/* XXX: function flag to make this automatic? */
10421042
if (!duk_is_constructor_call(ctx)) {
1043-
return DUK_RET_TYPE_ERROR;
1043+
DUK_DCERROR_TYPE_INVALID_ARGS((duk_hthread *) ctx); /* reworked in another branch... */
10441044
}
10451045

10461046
h_bufarg = duk__require_bufobj_value(ctx, 0);
@@ -1058,7 +1058,7 @@ DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx) {
10581058

10591059
h_val = h_bufarg->buf;
10601060
if (h_val == NULL) {
1061-
return DUK_RET_TYPE_ERROR;
1061+
DUK_DCERROR_TYPE_INVALID_ARGS((duk_hthread *) ctx);
10621062
}
10631063
h_bufobj->buf = h_val;
10641064
DUK_HBUFFER_INCREF(thr, h_val);
@@ -1183,7 +1183,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) {
11831183
DUK_ASSERT(buf_slice != NULL);
11841184

11851185
if (h_this->buf == NULL) {
1186-
goto type_error;
1186+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
11871187
}
11881188

11891189
/* XXX: it'd be tempting to duk_push_lstring() the data, but technically
@@ -1210,9 +1210,6 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx) {
12101210
*/
12111211
(void) duk_buffer_to_string(ctx, -1);
12121212
return 1;
1213-
1214-
type_error:
1215-
return DUK_RET_TYPE_ERROR;
12161213
}
12171214
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
12181215

@@ -1345,7 +1342,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx) {
13451342
h_this = duk__require_bufobj_this(ctx);
13461343
DUK_ASSERT(h_this != NULL);
13471344
if (h_this->buf == NULL) {
1348-
return DUK_RET_TYPE_ERROR;
1345+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
13491346
}
13501347

13511348
/* [ value offset end ] */
@@ -1553,7 +1550,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx) {
15531550
return 1;
15541551

15551552
fail_bounds:
1556-
return DUK_RET_RANGE_ERROR;
1553+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
15571554
}
15581555
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
15591556

@@ -1625,21 +1622,22 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
16251622
*/
16261623
offset_signed = duk_to_int(ctx, 1);
16271624
if (offset_signed < 0) {
1628-
return DUK_RET_TYPE_ERROR;
1625+
/* For some reason this is a TypeError (at least in V8). */
1626+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
16291627
}
16301628
offset_elems = (duk_uint_t) offset_signed;
16311629
offset_bytes = offset_elems << h_this->shift;
16321630
if ((offset_bytes >> h_this->shift) != offset_elems) {
16331631
/* Byte length would overflow. */
16341632
/* XXX: easier check with less code? */
1635-
return DUK_RET_RANGE_ERROR;
1633+
goto fail_args;
16361634
}
16371635
if (offset_bytes > h_this->length) {
16381636
/* Equality may be OK but >length not. Checking
16391637
* this explicitly avoids some overflow cases
16401638
* below.
16411639
*/
1642-
return DUK_RET_RANGE_ERROR;
1640+
goto fail_args;
16431641
}
16441642
DUK_ASSERT(offset_bytes <= h_this->length);
16451643

@@ -1677,7 +1675,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
16771675
if ((dst_length >> h_this->shift) != dst_length_elems) {
16781676
/* Byte length would overflow. */
16791677
/* XXX: easier check with less code? */
1680-
return DUK_RET_RANGE_ERROR;
1678+
goto fail_args;
16811679
}
16821680
DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld",
16831681
(long) src_length, (long) dst_length));
@@ -1687,7 +1685,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
16871685
* side and guaranteed to be >= 0.
16881686
*/
16891687
DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
1690-
return DUK_RET_RANGE_ERROR;
1688+
goto fail_args;
16911689
}
16921690
if (!DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) {
16931691
DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore"));
@@ -1830,7 +1828,7 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
18301828
* side and guaranteed to be >= 0.
18311829
*/
18321830
DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
1833-
return DUK_RET_RANGE_ERROR;
1831+
goto fail_args;
18341832
}
18351833

18361834
/* There's no need to check for buffer validity status for the
@@ -1850,6 +1848,9 @@ DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_context *ctx) {
18501848
}
18511849

18521850
return 0;
1851+
1852+
fail_args:
1853+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
18531854
}
18541855
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
18551856

@@ -1950,13 +1951,8 @@ DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) {
19501951
* buffer (handled automatically by duk__require_bufobj_this()).
19511952
*/
19521953

1953-
#if 1
19541954
DUK_DDD(DUK_DDDPRINT("slice() doesn't handle view into plain buffer, coerce 'this' to ArrayBuffer object"));
19551955
/* fall through */
1956-
#else
1957-
DUK_DDD(DUK_DDDPRINT("slice() doesn't handle view into plain buffer, refuse slice"));
1958-
return DUK_RET_TYPE_ERROR;
1959-
#endif
19601956
}
19611957
}
19621958
tv = NULL; /* No longer valid nor needed. */
@@ -2016,7 +2012,7 @@ DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx) {
20162012

20172013
h_val = h_this->buf;
20182014
if (h_val == NULL) {
2019-
return DUK_RET_TYPE_ERROR;
2015+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
20202016
}
20212017

20222018
if (magic & 0x02) {
@@ -2169,7 +2165,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) {
21692165
/* Node.js accepts only actual Arrays. */
21702166
h_arg = duk_require_hobject(ctx, 0);
21712167
if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) {
2172-
return DUK_RET_TYPE_ERROR;
2168+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
21732169
}
21742170

21752171
/* Compute result length and validate argument buffers. */
@@ -2201,7 +2197,7 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx) {
22012197
total_length = duk_to_int(ctx, 1);
22022198
}
22032199
if (total_length < 0) {
2204-
return DUK_RET_RANGE_ERROR;
2200+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
22052201
}
22062202

22072203
h_bufres = duk_push_bufobj_raw(ctx,
@@ -2533,8 +2529,7 @@ DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx) {
25332529
duk_push_nan(ctx);
25342530
return 1;
25352531
}
2536-
2537-
return DUK_RET_RANGE_ERROR;
2532+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
25382533
}
25392534
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
25402535

@@ -2812,6 +2807,6 @@ DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx) {
28122807
duk_push_uint(ctx, offset + nbytes);
28132808
return 1;
28142809
}
2815-
return DUK_RET_RANGE_ERROR;
2810+
DUK_DCERROR_RANGE_INVALID_ARGS(thr);
28162811
}
28172812
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */

src-input/duk_bi_duktape.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx) {
269269
DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
270270
#endif
271271
} else {
272-
return DUK_RET_TYPE_ERROR;
272+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
273273
}
274274
return 1;
275275
}
@@ -311,7 +311,7 @@ DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx) {
311311
DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/);
312312
#endif
313313
} else {
314-
return DUK_RET_TYPE_ERROR;
314+
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
315315
}
316316
return 1;
317317
}

0 commit comments

Comments
 (0)