jscript: Make Array.pop generic.

This commit is contained in:
Piotr Caban 2010-01-20 17:12:38 +01:00 committed by Alexandre Julliard
parent 9a9c075ee9
commit 91dcc245f9
2 changed files with 29 additions and 26 deletions

View File

@ -372,52 +372,42 @@ static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPAR
return hres; return hres;
} }
static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp, static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller) VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
{ {
DispatchEx *jsthis;
VARIANT val; VARIANT val;
DWORD length; DWORD length;
WCHAR buf[14];
DISPID id;
HRESULT hres; HRESULT hres;
static const WCHAR formatW[] = {'%','d',0};
TRACE("\n"); TRACE("\n");
if(is_vclass(jsthis, JSCLASS_ARRAY)) { hres = get_length(ctx, vthis, ei, &jsthis, &length);
ArrayInstance *array = array_from_vdisp(jsthis); if(FAILED(hres))
length = array->length; return hres;
}else {
FIXME("not Array this\n");
return E_NOTIMPL;
}
if(!length) { if(!length) {
hres = set_length(jsthis, ei, 0);
if(FAILED(hres))
return hres;
if(retv) if(retv)
V_VT(retv) = VT_EMPTY; V_VT(retv) = VT_EMPTY;
return S_OK; return S_OK;
} }
sprintfW(buf, formatW, --length); length--;
hres = jsdisp_get_id(jsthis->u.jsdisp, buf, 0, &id); hres = jsdisp_propget_idx(jsthis, length, &val, ei, caller);
if(SUCCEEDED(hres)) { if(SUCCEEDED(hres)) {
hres = jsdisp_propget(jsthis->u.jsdisp, id, &val, ei, caller); hres = jsdisp_delete_idx(jsthis, length);
if(FAILED(hres)) } else if(hres == DISP_E_UNKNOWNNAME) {
return hres;
hres = IDispatchEx_DeleteMemberByDispID(jsthis->u.dispex, id);
}else if(hres == DISP_E_UNKNOWNNAME) {
V_VT(&val) = VT_EMPTY; V_VT(&val) = VT_EMPTY;
hres = S_OK; hres = S_OK;
}else { } else
return hres; return hres;
}
if(SUCCEEDED(hres)) { if(SUCCEEDED(hres))
ArrayInstance *array = array_from_vdisp(jsthis); hres = set_length(jsthis, ei, length);
array->length = length;
}
if(FAILED(hres)) { if(FAILED(hres)) {
VariantClear(&val); VariantClear(&val);
@ -428,6 +418,7 @@ static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPAR
*retv = val; *retv = val;
else else
VariantClear(&val); VariantClear(&val);
return S_OK; return S_OK;
} }

View File

@ -618,6 +618,12 @@ ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
ok(arr[8] === "b", "arr[8] != 'b'"); ok(arr[8] === "b", "arr[8] != 'b'");
ok(arr.length === 10, "arr.length != 10"); ok(arr.length === 10, "arr.length != 10");
arr.pop = Array.prototype.pop;
ok(arr.pop() === false, "arr.pop() !== false");
ok(arr[8] === "b", "arr[8] !== 'b'");
ok(arr.pop() === 'b', "arr.pop() !== 'b'");
ok(arr[8] === undefined, "arr[8] !== undefined");
arr = [3,4,5]; arr = [3,4,5];
tmp = arr.pop(); tmp = arr.pop();
ok(arr.length === 2, "arr.length = " + arr.length); ok(arr.length === 2, "arr.length = " + arr.length);
@ -633,6 +639,11 @@ for(tmp in arr)
tmp = arr.pop(); tmp = arr.pop();
ok(arr.length === 0, "arr.length = " + arr.length); ok(arr.length === 0, "arr.length = " + arr.length);
ok(tmp === undefined, "tmp = " + tmp); ok(tmp === undefined, "tmp = " + tmp);
arr = new Object();
arr.pop = Array.prototype.pop;
tmp = arr.pop();
ok(arr.length === 0, "arr.length = " + arr.length);
ok(tmp === undefined, "tmp = " + tmp);
arr = [,,,,,]; arr = [,,,,,];
tmp = arr.pop(); tmp = arr.pop();
ok(arr.length === 5, "arr.length = " + arr.length); ok(arr.length === 5, "arr.length = " + arr.length);
@ -1891,6 +1902,7 @@ testArrayHostThis("splice");
testArrayHostThis("unshift"); testArrayHostThis("unshift");
testArrayHostThis("reverse"); testArrayHostThis("reverse");
testArrayHostThis("join"); testArrayHostThis("join");
testArrayHostThis("pop");
function testObjectInherit(obj, constr, ts, tls, vo) { function testObjectInherit(obj, constr, ts, tls, vo) {
ok(obj instanceof Object, "obj is not instance of Object"); ok(obj instanceof Object, "obj is not instance of Object");