Browse Source

rework Array built-in indexOf/lastIndexOf to use a shared native helper + magic

pull/1/head
Sami Vaarala 11 years ago
parent
commit
c66093f9dd
  1. 13
      src/duk_builtin_array.c
  2. 3
      src/duk_builtin_protos.h
  3. 4
      src/genbuiltins.py

13
src/duk_builtin_array.c

@ -931,12 +931,13 @@ int duk_builtin_array_prototype_unshift(duk_context *ctx) {
* indexOf(), lastIndexOf()
*/
/* idx_step == 1 is indexOf, idx_step == -1 is lastIndexOf */
static int array_indexof_helper(duk_context *ctx, int idx_step) {
int duk_builtin_array_prototype_indexof_shared(duk_context *ctx) {
/* FIXME: types, ensure loop below works when fixed (i must be able to go negative right now) */
int nargs;
int i, len;
int fromIndex;
int idx_step = duk_get_magic(ctx) - 1; /* 0 -> -1, 2 -> +1 */
/* idx_step is +1 for indexOf, -1 for lastIndexOf */
/* lastIndexOf() needs to be a vararg function because we must distinguish
* between an undefined fromIndex and a "not given" fromIndex; indexOf() is
@ -1019,14 +1020,6 @@ static int array_indexof_helper(duk_context *ctx, int idx_step) {
return 1;
}
int duk_builtin_array_prototype_index_of(duk_context *ctx) {
return array_indexof_helper(ctx, 1 /*idx_step*/);
}
int duk_builtin_array_prototype_last_index_of(duk_context *ctx) {
return array_indexof_helper(ctx, -1 /*idx_step*/);
}
/*
* every(), some(), forEach(), map(), filter()
*/

3
src/duk_builtin_protos.h

@ -19,8 +19,7 @@ int duk_builtin_array_prototype_slice(duk_context *ctx);
int duk_builtin_array_prototype_sort(duk_context *ctx);
int duk_builtin_array_prototype_splice(duk_context *ctx);
int duk_builtin_array_prototype_unshift(duk_context *ctx);
int duk_builtin_array_prototype_index_of(duk_context *ctx);
int duk_builtin_array_prototype_last_index_of(duk_context *ctx);
int duk_builtin_array_prototype_indexof_shared(duk_context *ctx);
int duk_builtin_array_prototype_iter_shared(duk_context *ctx);
int duk_builtin_array_prototype_reduce_shared(duk_context *ctx);

4
src/genbuiltins.py

@ -416,8 +416,8 @@ bi_array_prototype = {
{ 'name': 'sort', 'native': 'duk_builtin_array_prototype_sort', 'length': 1 },
{ 'name': 'splice', 'native': 'duk_builtin_array_prototype_splice', 'length': 2, 'varargs': True },
{ 'name': 'unshift', 'native': 'duk_builtin_array_prototype_unshift', 'length': 1, 'varargs': True },
{ 'name': 'indexOf', 'native': 'duk_builtin_array_prototype_index_of', 'length': 1, 'varargs': True },
{ 'name': 'lastIndexOf', 'native': 'duk_builtin_array_prototype_last_index_of', 'length': 1, 'varargs': True },
{ 'name': 'indexOf', 'native': 'duk_builtin_array_prototype_indexof_shared', 'length': 1, 'varargs': True, 'magic': { 'type': 'plain', 'value': 2 } }, # magic = 2 -> idx_step = 2 - 1 = +1
{ 'name': 'lastIndexOf', 'native': 'duk_builtin_array_prototype_indexof_shared', 'length': 1, 'varargs': True, 'magic': { 'type': 'plain', 'value': 0 } }, # magic = 0 -> idx_step = 0 - 1 = -1
{ 'name': 'every', 'native': 'duk_builtin_array_prototype_iter_shared', 'length': 1, 'nargs': 2, 'magic': { 'type': 'plain', 'value': BI_ARRAY_ITER_EVERY } },
{ 'name': 'some', 'native': 'duk_builtin_array_prototype_iter_shared', 'length': 1, 'nargs': 2, 'magic': { 'type': 'plain', 'value': BI_ARRAY_ITER_SOME } },
{ 'name': 'forEach', 'native': 'duk_builtin_array_prototype_iter_shared', 'length': 1, 'nargs': 2, 'magic': { 'type': 'plain', 'value': BI_ARRAY_ITER_FOREACH } },

Loading…
Cancel
Save