error.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2009 Piotr Caban
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
18

19 20 21 22
#include "config.h"
#include "wine/port.h"

#include <math.h>
23 24 25 26 27 28 29

#include "jscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

30
static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
31
static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
32
static const WCHAR nameW[] = {'n','a','m','e',0};
33
static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
34 35
static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};

36
/* ECMA-262 3rd Edition    15.11.4.4 */
37
static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
38
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
39
{
40
    jsdisp_t *jsthis;
41
    BSTR name = NULL, msg = NULL, ret = NULL;
42 43 44
    VARIANT v;
    HRESULT hres;

45
    static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
46 47 48

    TRACE("\n");

49 50
    jsthis = get_jsdisp(vthis);
    if(!jsthis || ctx->version < 2) {
51 52
        if(retv) {
            V_VT(retv) = VT_BSTR;
53
            V_BSTR(retv) = SysAllocString(object_errorW);
54 55 56 57 58 59
            if(!V_BSTR(retv))
                return E_OUTOFMEMORY;
        }
        return S_OK;
    }

60
    hres = jsdisp_propget_name(jsthis, nameW, &v, ei);
61 62 63
    if(FAILED(hres))
        return hres;

64 65 66 67 68 69 70 71 72 73
    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;
        }
    }
74

75
    hres = jsdisp_propget_name(jsthis, messageW, &v, ei);
76 77 78 79 80 81 82 83
    if(SUCCEEDED(hres)) {
        if(V_VT(&v) != VT_EMPTY) {
            hres = to_string(ctx, &v, ei, &msg);
            VariantClear(&v);
            if(SUCCEEDED(hres) && !*msg) {
                SysFreeString(msg);
                msg = NULL;
            }
84 85 86 87
        }
    }

    if(SUCCEEDED(hres)) {
88
        if(name && msg) {
89 90 91 92 93 94 95 96 97 98 99 100
            DWORD name_len, msg_len;

            name_len = SysStringLen(name);
            msg_len = SysStringLen(msg);

            ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
            if(ret) {
                memcpy(ret, name, name_len*sizeof(WCHAR));
                ret[name_len] = ':';
                ret[name_len+1] = ' ';
                memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
            }
101
        }else if(name) {
102 103
            ret = name;
            name = NULL;
104 105 106 107 108
        }else if(msg) {
            ret = msg;
            msg = NULL;
        }else {
            ret = SysAllocString(object_errorW);
109 110 111 112 113 114 115 116 117 118
        }
    }

    SysFreeString(msg);
    SysFreeString(name);
    if(FAILED(hres))
        return hres;
    if(!ret)
        return E_OUTOFMEMORY;

119 120
    if(retv) {
        V_VT(retv) = VT_BSTR;
121 122 123
        V_BSTR(retv) = ret;
    }else {
        SysFreeString(ret);
124 125 126
    }

    return S_OK;
127 128
}

129
static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
130
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
131
{
132 133 134 135
    TRACE("\n");

    switch(flags) {
    case INVOKE_FUNC:
136
        return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
137 138 139 140 141 142
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
143 144 145 146 147 148 149 150 151 152 153
}

static const builtin_prop_t Error_props[] = {
    {toStringW,                 Error_toString,                     PROPF_METHOD}
};

static const builtin_info_t Error_info = {
    JSCLASS_ERROR,
    {NULL, Error_value, 0},
    sizeof(Error_props)/sizeof(*Error_props),
    Error_props,
154
    NULL,
155 156 157 158 159 160
    NULL
};

static const builtin_info_t ErrorInst_info = {
    JSCLASS_ERROR,
    {NULL, Error_value, 0},
161 162
    0,
    NULL,
163
    NULL,
164 165 166
    NULL
};

167 168
static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
        jsdisp_t *constr, jsdisp_t **ret)
169
{
170
    jsdisp_t *err;
171 172
    HRESULT hres;

173
    err = heap_alloc_zero(sizeof(*err));
174 175 176
    if(!err)
        return E_OUTOFMEMORY;

177
    if(prototype)
178
        hres = init_dispex(err, ctx, &Error_info, prototype);
179
    else
180
        hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
181
            constr ? constr : ctx->error_constr);
182 183 184 185 186 187 188 189 190
    if(FAILED(hres)) {
        heap_free(err);
        return hres;
    }

    *ret = err;
    return S_OK;
}

191 192
static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
        UINT number, const WCHAR *msg, jsdisp_t **ret)
193
{
194
    jsdisp_t *err;
195
    VARIANT v;
196 197
    HRESULT hres;

198
    hres = alloc_error(ctx, NULL, constr, &err);
199 200 201
    if(FAILED(hres))
        return hres;

202
    num_set_int(&v, number);
203
    hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/);
204
    if(FAILED(hres)) {
205
        jsdisp_release(err);
206
        return hres;
207 208
    }

209 210 211 212
    V_VT(&v) = VT_BSTR;
    if(msg) V_BSTR(&v) = SysAllocString(msg);
    else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
    if(V_BSTR(&v)) {
213
        hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/);
214
        if(SUCCEEDED(hres))
215
            hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/);
216 217 218
        SysFreeString(V_BSTR(&v));
    }else {
        hres = E_OUTOFMEMORY;
219
    }
220
    if(FAILED(hres)) {
221
        jsdisp_release(err);
222 223 224
        return hres;
    }

225
    *ret = err;
226 227 228
    return S_OK;
}

229
static HRESULT error_constr(script_ctx_t *ctx, WORD flags, unsigned argc, VARIANT *argv,
230 231
        VARIANT *retv, jsexcept_t *ei, jsdisp_t *constr) {
    jsdisp_t *err;
232
    UINT num = 0;
233 234 235
    BSTR msg = NULL;
    HRESULT hres;

236
    if(argc) {
237
        double n;
238

239
        hres = to_number(ctx, argv, ei, &n);
240
        if(FAILED(hres)) /* FIXME: really? */
241
            n = NAN;
242
        if(isnan(n))
243
            hres = to_string(ctx, argv, ei, &msg);
244 245
        if(FAILED(hres))
            return hres;
246
        num = n;
247 248
    }

249 250
    if(argc>1 && !msg) {
        hres = to_string(ctx, argv+1, ei, &msg);
251 252 253 254 255 256 257
        if(FAILED(hres))
            return hres;
    }

    switch(flags) {
    case INVOKE_FUNC:
    case DISPATCH_CONSTRUCT:
258
        hres = create_error(ctx, constr, num, msg, &err);
259
        SysFreeString(msg);
260

261 262 263
        if(FAILED(hres))
            return hres;

264 265
        if(retv)
            var_set_jsdisp(retv, err);
266
        else
Jacek Caban's avatar
Jacek Caban committed
267
            jsdisp_release(err);
268 269 270 271 272 273 274 275 276

        return S_OK;

    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }
}

277
static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
278
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
279 280
{
    TRACE("\n");
281
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->error_constr);
282 283
}

284
static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
285
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
286 287
{
    TRACE("\n");
288
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->eval_error_constr);
289 290
}

291
static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
292
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
293 294
{
    TRACE("\n");
295
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->range_error_constr);
296 297
}

298
static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
299
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
300 301
{
    TRACE("\n");
302
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->reference_error_constr);
303 304
}

305
static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
306
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
307 308
{
    TRACE("\n");
309
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->regexp_error_constr);
310 311
}

312
static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
313
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
314 315
{
    TRACE("\n");
316
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->syntax_error_constr);
317 318
}

319
static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
320
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
321 322
{
    TRACE("\n");
323
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->type_error_constr);
324 325
}

326
static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
327
        unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
328 329
{
    TRACE("\n");
330
    return error_constr(ctx, flags, argc, argv, retv, ei, ctx->uri_error_constr);
331 332
}

333
HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
334 335 336 337 338
{
    static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
    static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
    static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
    static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
339
    static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
340 341 342 343
    static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
    static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
    static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
    static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
344
        ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
345
    jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
346
        &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
347 348 349
        &ctx->syntax_error_constr, &ctx->type_error_constr,
        &ctx->uri_error_constr};
    static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
350 351
        RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
        SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
352

353
    jsdisp_t *err;
354 355 356 357
    INT i;
    VARIANT v;
    HRESULT hres;

358
    for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
359
        hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
360 361 362 363 364 365
        if(FAILED(hres))
            return hres;

        V_VT(&v) = VT_BSTR;
        V_BSTR(&v) = SysAllocString(names[i]);
        if(!V_BSTR(&v)) {
366
            jsdisp_release(err);
367 368 369
            return E_OUTOFMEMORY;
        }

370
        hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/);
371 372

        if(SUCCEEDED(hres))
373
            hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
374
                    PROPF_CONSTR|1, err, constr_addr[i]);
375

376
        jsdisp_release(err);
377 378 379 380 381 382 383
        VariantClear(&v);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}
384

385
static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str, jsdisp_t *constr)
386 387
{
    WCHAR buf[1024], *pos = NULL;
388
    jsdisp_t *err;
389 390
    HRESULT hres;

391 392 393
    if(!is_jscript_error(error))
        return error;

394
    buf[0] = '\0';
395
    LoadStringW(jscript_hinstance, HRESULT_CODE(error),  buf, sizeof(buf)/sizeof(WCHAR));
396 397 398 399

    if(str) pos = strchrW(buf, '|');
    if(pos) {
        int len = strlenW(str);
400
        memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
401 402 403
        memcpy(pos, str, len*sizeof(WCHAR));
    }

404 405
    WARN("%s\n", debugstr_w(buf));

406
    hres = create_error(ctx, constr, error, buf, &err);
407 408 409
    if(FAILED(hres))
        return hres;

410 411
    if(ei)
        var_set_jsdisp(&ei->var, err);
412
    return error;
413 414
}

415
HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
416
{
417
    return throw_error(ctx, ei, error, str, ctx->error_constr);
418 419
}

420
HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
421
{
422
    return throw_error(ctx, ei, error, str, ctx->range_error_constr);
423 424
}

425
HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
426
{
427
    return throw_error(ctx, ei, error, str, ctx->reference_error_constr);
428 429
}

430
HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
431
{
432
    return throw_error(ctx, ei, error, str, ctx->regexp_error_constr);
433 434
}

435
HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
436
{
437
    return throw_error(ctx, ei, error, str, ctx->syntax_error_constr);
438 439
}

440
HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
441
{
442
    return throw_error(ctx, ei, error, str, ctx->type_error_constr);
443 444
}

445
HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
446
{
447
    return throw_error(ctx, ei, error, str, ctx->uri_error_constr);
448
}