jscript: Fixed Error.toString implementation for non-Error this.

This commit is contained in:
Jacek Caban 2010-08-04 15:32:59 +02:00 committed by Alexandre Julliard
parent 04819e9066
commit ae8cb5a1e6
2 changed files with 61 additions and 15 deletions

View File

@ -47,39 +47,45 @@ static inline ErrorInstance *error_this(vdisp_t *jsthis)
}
/* ECMA-262 3rd Edition 15.11.4.4 */
static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
{
ErrorInstance *error;
BSTR name, msg = NULL, ret = NULL;
DispatchEx *jsthis;
BSTR name = NULL, msg = NULL, ret = NULL;
VARIANT v;
HRESULT hres;
static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
TRACE("\n");
error = error_this(jsthis);
if(ctx->version < 2 || !error) {
jsthis = get_jsdisp(vthis);
if(!jsthis || ctx->version < 2) {
if(retv) {
V_VT(retv) = VT_BSTR;
V_BSTR(retv) = SysAllocString(str);
V_BSTR(retv) = SysAllocString(object_errorW);
if(!V_BSTR(retv))
return E_OUTOFMEMORY;
}
return S_OK;
}
hres = jsdisp_propget_name(&error->dispex, nameW, &v, ei, caller);
hres = jsdisp_propget_name(jsthis, nameW, &v, ei, caller);
if(FAILED(hres))
return hres;
hres = to_string(ctx, &v, ei, &name);
VariantClear(&v);
if(FAILED(hres))
return hres;
if(V_VT(&v) != VT_EMPTY) {
hres = to_string(ctx, &v, ei, &name);
VariantClear(&v);
if(FAILED(hres))
return hres;
if(!*name) {
SysFreeString(name);
name = NULL;
}
}
hres = jsdisp_propget_name(&error->dispex, messageW, &v, ei, caller);
hres = jsdisp_propget_name(jsthis, messageW, &v, ei, caller);
if(SUCCEEDED(hres)) {
if(V_VT(&v) != VT_EMPTY) {
hres = to_string(ctx, &v, ei, &msg);
@ -92,7 +98,7 @@ static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
}
if(SUCCEEDED(hres)) {
if(msg) {
if(name && msg) {
DWORD name_len, msg_len;
name_len = SysStringLen(name);
@ -105,9 +111,16 @@ static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
ret[name_len+1] = ' ';
memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
}
}else {
}else if(name) {
ret = name;
name = NULL;
}else if(msg) {
ret = msg;
msg = NULL;
}else {
ret = SysAllocString(object_errorW);
if(!V_BSTR(retv))
hres = E_OUTOFMEMORY;
}
}

View File

@ -1780,6 +1780,12 @@ ok(err.message === "message", "err.message = " + err.message);
ok(err.description === "message", "err.description = " + err.description);
err = new Error();
ok(err.number === 0, "err.number = " + err.number);
ok(err.description === "", "err.description = " + err.description);
err.description = 5;
ok(err.description === 5, "err.description = " + err.description);
ok(err.message === "", "err.message = " + err.message);
err.message = 4;
ok(err.message === 4, "err.message = " + err.message);
ok(!("number" in Error), "number is in Error");
@ -1789,6 +1795,33 @@ tmp.toString = function() { return "test"; };
tmp = Error.prototype.toString.call(tmp);
ok(tmp === "[object Error]", "Error.prototype.toString.call(tmp) = " + tmp);
if(invokeVersion >= 2) {
obj = new Object();
obj.name = "test";
tmp = Error.prototype.toString.call(obj);
ok(tmp === "test", "Error.prototype.toString.call(obj) = " + tmp);
obj = new Object();
obj.name = 6;
obj.message = false;
tmp = Error.prototype.toString.call(obj);
ok(tmp === "6: false", "Error.prototype.toString.call(obj) = " + tmp);
obj = new Object();
obj.message = "test";
tmp = Error.prototype.toString.call(obj);
ok(tmp === "test", "Error.prototype.toString.call(obj) = " + tmp);
obj = new Object();
obj.name = "";
obj.message = "test";
tmp = Error.prototype.toString.call(obj);
ok(tmp === "test", "Error.prototype.toString.call(obj) = " + tmp);
}
tmp = Error.prototype.toString.call(testObj);
ok(tmp === "[object Error]", "Error.prototype.toString.call(testObj) = " + tmp);
err = new Error();
err.name = null;
ok(err.name === null, "err.name = " + err.name + " expected null");