error.c 12.6 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

#include <math.h>
21 22 23 24 25 26 27

#include "jscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

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

34
/* ECMA-262 3rd Edition    15.11.4.4 */
35
static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
36
        unsigned argc, jsval_t *argv, jsval_t *r)
37
{
38
    jsdisp_t *jsthis;
39
    jsstr_t *name = NULL, *msg = NULL, *ret = NULL;
40
    jsval_t v;
41 42
    HRESULT hres;

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

    TRACE("\n");

47 48
    jsthis = get_jsdisp(vthis);
    if(!jsthis || ctx->version < 2) {
49
        if(r) {
50 51 52 53
            jsstr_t *str;

            str = jsstr_alloc(object_errorW);
            if(!str)
54
                return E_OUTOFMEMORY;
55
            *r = jsval_string(str);
56 57 58 59
        }
        return S_OK;
    }

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

64
    if(!is_undefined(v)) {
65
        hres = to_string(ctx, v, &name);
66
        jsval_release(v);
67 68 69
        if(FAILED(hres))
            return hres;
    }
70

71
    hres = jsdisp_propget_name(jsthis, messageW, &v);
72
    if(SUCCEEDED(hres)) {
73
        if(!is_undefined(v)) {
74
            hres = to_string(ctx, v, &msg);
75
            jsval_release(v);
76 77 78 79
        }
    }

    if(SUCCEEDED(hres)) {
80 81
        unsigned name_len = name ? jsstr_length(name) : 0;
        unsigned msg_len = msg ? jsstr_length(msg) : 0;
82

83
        if(name_len && msg_len) {
84 85
            WCHAR *ptr;

86 87
            ret = jsstr_alloc_buf(name_len + msg_len + 2, &ptr);
            if(ret) {
88 89 90 91 92 93
                jsstr_flush(name, ptr);
                ptr[name_len] = ':';
                ptr[name_len+1] = ' ';
                jsstr_flush(msg, ptr+name_len+2);
            }else {
                hres = E_OUTOFMEMORY;
94
            }
95
        }else if(name_len) {
96 97
            ret = name;
            name = NULL;
98
        }else if(msg_len) {
99 100 101
            ret = msg;
            msg = NULL;
        }else {
102
            ret = jsstr_alloc(object_errorW);
103 104 105
        }
    }

106 107 108 109
    if(msg)
        jsstr_release(msg);
    if(name)
        jsstr_release(name);
110 111 112 113 114
    if(FAILED(hres))
        return hres;
    if(!ret)
        return E_OUTOFMEMORY;

115 116 117
    if(r)
        *r = jsval_string(ret);
    else
118
        jsstr_release(ret);
119
    return S_OK;
120 121
}

122
static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
123
        unsigned argc, jsval_t *argv, jsval_t *r)
124
{
125 126 127 128
    TRACE("\n");

    switch(flags) {
    case INVOKE_FUNC:
129
        return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
130 131 132 133 134 135
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
136 137 138 139 140 141 142 143 144
}

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},
145
    ARRAY_SIZE(Error_props),
146
    Error_props,
147
    NULL,
148 149 150 151 152 153
    NULL
};

static const builtin_info_t ErrorInst_info = {
    JSCLASS_ERROR,
    {NULL, Error_value, 0},
154 155
    0,
    NULL,
156
    NULL,
157 158 159
    NULL
};

160 161
static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
        jsdisp_t *constr, jsdisp_t **ret)
162
{
163
    jsdisp_t *err;
164 165
    HRESULT hres;

166
    err = heap_alloc_zero(sizeof(*err));
167 168 169
    if(!err)
        return E_OUTOFMEMORY;

170
    if(prototype)
171
        hres = init_dispex(err, ctx, &Error_info, prototype);
172
    else
173
        hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
174
            constr ? constr : ctx->error_constr);
175 176 177 178 179 180 181 182 183
    if(FAILED(hres)) {
        heap_free(err);
        return hres;
    }

    *ret = err;
    return S_OK;
}

184
static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
185
        UINT number, jsstr_t *msg, jsdisp_t **ret)
186
{
187
    jsdisp_t *err;
188 189
    HRESULT hres;

190
    hres = alloc_error(ctx, NULL, constr, &err);
191 192 193
    if(FAILED(hres))
        return hres;

194 195
    hres = jsdisp_define_data_property(err, numberW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
                                       jsval_number((INT)number));
196
    if(FAILED(hres)) {
197
        jsdisp_release(err);
198
        return hres;
199 200
    }

201 202 203
    hres = jsdisp_define_data_property(err, messageW,
                                       PROPF_WRITABLE | PROPF_ENUMERABLE | PROPF_CONFIGURABLE,
                                       jsval_string(msg));
204
    if(SUCCEEDED(hres))
205 206
        hres = jsdisp_define_data_property(err, descriptionW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
                                           jsval_string(msg));
207
    if(FAILED(hres)) {
208
        jsdisp_release(err);
209 210 211
        return hres;
    }

212
    *ret = err;
213 214 215
    return S_OK;
}

216
static HRESULT error_constr(script_ctx_t *ctx, WORD flags, unsigned argc, jsval_t *argv,
217
        jsval_t *r, jsdisp_t *constr) {
218
    jsdisp_t *err;
219
    UINT num = 0;
220
    jsstr_t *msg = NULL;
221 222
    HRESULT hres;

223
    if(argc) {
224
        double n;
225

226
        hres = to_number(ctx, argv[0], &n);
227
        if(FAILED(hres)) /* FIXME: really? */
228
            n = NAN;
229
        if(isnan(n))
230
            hres = to_string(ctx, argv[0], &msg);
231 232
        if(FAILED(hres))
            return hres;
233
        num = n;
234 235
    }

236 237 238 239 240 241 242 243
    if(!msg) {
        if(argc > 1) {
            hres = to_string(ctx, argv[1], &msg);
            if(FAILED(hres))
                return hres;
        }else {
            msg = jsstr_empty();
        }
244 245 246 247 248
    }

    switch(flags) {
    case INVOKE_FUNC:
    case DISPATCH_CONSTRUCT:
249 250
        hres = create_error(ctx, constr, num, msg, &err);
        jsstr_release(msg);
251 252 253
        if(FAILED(hres))
            return hres;

254 255
        if(r)
            *r = jsval_obj(err);
256
        else
Jacek Caban's avatar
Jacek Caban committed
257
            jsdisp_release(err);
258 259 260
        return S_OK;

    default:
261 262
        if(msg)
            jsstr_release(msg);
263 264 265 266 267
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }
}

268
static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
269
        unsigned argc, jsval_t *argv, jsval_t *r)
270 271
{
    TRACE("\n");
272
    return error_constr(ctx, flags, argc, argv, r, ctx->error_constr);
273 274
}

275
static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
276
        unsigned argc, jsval_t *argv, jsval_t *r)
277 278
{
    TRACE("\n");
279
    return error_constr(ctx, flags, argc, argv, r, ctx->eval_error_constr);
280 281
}

282
static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
283
        unsigned argc, jsval_t *argv, jsval_t *r)
284 285
{
    TRACE("\n");
286
    return error_constr(ctx, flags, argc, argv, r, ctx->range_error_constr);
287 288
}

289
static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
290
        unsigned argc, jsval_t *argv, jsval_t *r)
291 292
{
    TRACE("\n");
293
    return error_constr(ctx, flags, argc, argv, r, ctx->reference_error_constr);
294 295
}

296
static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
297
        unsigned argc, jsval_t *argv, jsval_t *r)
298 299
{
    TRACE("\n");
300
    return error_constr(ctx, flags, argc, argv, r, ctx->regexp_error_constr);
301 302
}

303
static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
304
        unsigned argc, jsval_t *argv, jsval_t *r)
305 306
{
    TRACE("\n");
307
    return error_constr(ctx, flags, argc, argv, r, ctx->syntax_error_constr);
308 309
}

310
static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
311
        unsigned argc, jsval_t *argv, jsval_t *r)
312 313
{
    TRACE("\n");
314
    return error_constr(ctx, flags, argc, argv, r, ctx->type_error_constr);
315 316
}

317
static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
318
        unsigned argc, jsval_t *argv, jsval_t *r)
319 320
{
    TRACE("\n");
321
    return error_constr(ctx, flags, argc, argv, r, ctx->uri_error_constr);
322 323
}

324
HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
325 326 327 328 329
{
    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};
330
    static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
331 332 333 334
    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,
335
        ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
336
    jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
337
        &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
338 339 340
        &ctx->syntax_error_constr, &ctx->type_error_constr,
        &ctx->uri_error_constr};
    static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
341 342
        RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
        SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
343

344
    jsdisp_t *err;
345
    unsigned int i;
346
    jsstr_t *str;
347 348
    HRESULT hres;

349
    for(i=0; i < ARRAY_SIZE(names); i++) {
350
        hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
351 352 353
        if(FAILED(hres))
            return hres;

354
        str = jsstr_alloc(names[i]);
355
        if(!str) {
356
            jsdisp_release(err);
357 358 359
            return E_OUTOFMEMORY;
        }

360 361
        hres = jsdisp_define_data_property(err, nameW, PROPF_WRITABLE | PROPF_CONFIGURABLE,
                                           jsval_string(str));
362
        jsstr_release(str);
363
        if(SUCCEEDED(hres))
364
            hres = create_builtin_constructor(ctx, constr_val[i], names[i], NULL,
365
                    PROPF_CONSTR|1, err, constr_addr[i]);
366

367
        jsdisp_release(err);
368 369 370 371 372 373
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}
374

375
static HRESULT throw_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str, jsdisp_t *constr)
376 377
{
    WCHAR buf[1024], *pos = NULL;
378
    jsdisp_t *err;
379
    jsstr_t *msg;
380 381
    HRESULT hres;

382 383 384
    if(!is_jscript_error(error))
        return error;

385
    buf[0] = '\0';
386
    LoadStringW(jscript_hinstance, HRESULT_CODE(error), buf, ARRAY_SIZE(buf));
387

388
    if(str) pos = wcschr(buf, '|');
389
    if(pos) {
390 391
        int len = lstrlenW(str);
        memmove(pos+len, pos+1, (lstrlenW(pos+1)+1)*sizeof(WCHAR));
392 393 394
        memcpy(pos, str, len*sizeof(WCHAR));
    }

395 396
    WARN("%s\n", debugstr_w(buf));

397 398 399 400 401 402
    msg = jsstr_alloc(buf);
    if(!msg)
        return E_OUTOFMEMORY;

    hres = create_error(ctx, constr, error, msg, &err);
    jsstr_release(msg);
403 404 405
    if(FAILED(hres))
        return hres;

406 407
    jsval_release(ctx->ei.val);
    ctx->ei.val = jsval_obj(err);
408
    return error;
409 410
}

411
HRESULT throw_generic_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
412
{
413
    return throw_error(ctx, error, str, ctx->error_constr);
414 415
}

416
HRESULT throw_range_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
417
{
418
    return throw_error(ctx, error, str, ctx->range_error_constr);
419 420
}

421
HRESULT throw_reference_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
422
{
423
    return throw_error(ctx, error, str, ctx->reference_error_constr);
424 425
}

426
HRESULT throw_regexp_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
427
{
428
    return throw_error(ctx, error, str, ctx->regexp_error_constr);
429 430
}

431
HRESULT throw_syntax_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
432
{
433
    return throw_error(ctx, error, str, ctx->syntax_error_constr);
434 435
}

436
HRESULT throw_type_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
437
{
438
    return throw_error(ctx, error, str, ctx->type_error_constr);
439 440
}

441
HRESULT throw_uri_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
442
{
443
    return throw_error(ctx, error, str, ctx->uri_error_constr);
444
}