Skip to content

Commit

Permalink
Array callbacks now preserve 'this'
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 31b71889b0e10a8a4b76532eba1aef7fdc9db4c2ce6e04ca93202139d2068773
  • Loading branch information
pmacdona committed Oct 20, 2020
1 parent d36c885 commit 9f8d022
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/jsiArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ static Jsi_RC jsi_ArrayMapCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_t
sthis = nthis = Jsi_ValueNewObj(interp, NULL);
Jsi_IncrRefCount(interp, sthis);
}
Jsi_IncrRefCount(interp, _this);
obj = _this->d.obj;
curlen = jsi_SizeOfArray(interp, obj);
Jsi_ObjListifyArray(interp, obj);
Expand Down Expand Up @@ -386,6 +387,7 @@ static Jsi_RC jsi_ArrayMapCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_t
Jsi_ObjSetLength(interp, nobj, i);

bail:
Jsi_DecrRefCount(interp, _this);
if (nthis)
Jsi_DecrRefCount(interp, nthis);
return rc;
Expand Down Expand Up @@ -508,6 +510,7 @@ static Jsi_RC jsi_ArrayForeachCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value
Jsi_ObjListifyArray(interp, obj);
Jsi_RC rc = JSI_OK;

Jsi_IncrRefCount(interp, _this);
Jsi_Value *vobjs[3];
Jsi_Func *fptr = func->d.obj->d.fobj->func;
int maa = (fptr->argnames?fptr->argnames->argCnt:0);
Expand All @@ -523,6 +526,7 @@ static Jsi_RC jsi_ArrayForeachCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value
rc = Jsi_FunctionInvoke(interp, func, vpargs, ret, sthis);
Jsi_DecrRefCount(interp, vpargs);
}
Jsi_DecrRefCount(interp, _this);
if (nthis)
Jsi_DecrRefCount(interp, nthis);
return rc;
Expand All @@ -545,6 +549,7 @@ static Jsi_RC jsi_ArrayFindSubCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value
Jsi_IncrRefCount(interp, sthis);
}

Jsi_IncrRefCount(interp, _this);
obj = _this->d.obj;
Jsi_ObjListifyArray(interp, obj);
int fval = 0;
Expand Down Expand Up @@ -580,6 +585,7 @@ static Jsi_RC jsi_ArrayFindSubCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value
else if (op == 4)
Jsi_ValueMakeNumber(interp, ret, (Jsi_Number)(fval?(int)i:-1));
}
Jsi_DecrRefCount(interp, _this);
if (nthis)
Jsi_DecrRefCount(interp, nthis);
Jsi_DecrRefCount(interp, nrPtr);
Expand Down Expand Up @@ -609,6 +615,7 @@ static Jsi_RC jsi_ArrayReduceSubCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Val
if (maa>4 || fptr->type == FC_BUILDIN)
maa = 4;

Jsi_IncrRefCount(interp, _this);
for (n = 0, i = (rev?obj->arrCnt-1:0); (rev?i>=0:i < (int)obj->arrCnt) && rc == JSI_OK; n++, i = (rev?i-1:i+1)) {
if (!obj->arr[i]) continue;
if (n==0 && !ini) {
Expand All @@ -630,6 +637,7 @@ static Jsi_RC jsi_ArrayReduceSubCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Val
}
if (rc == JSI_OK && ini)
Jsi_ValueCopy(interp, *ret, ini);
Jsi_DecrRefCount(interp, _this);
Jsi_DecrRefCount(interp, nrPtr);
return rc;

Expand Down

0 comments on commit 9f8d022

Please sign in to comment.