error.c 12.3 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, jsval_t *argv, jsval_t *r)
39
{
40
    jsdisp_t *jsthis;
41
    jsstr_t *name = NULL, *msg = NULL, *ret = NULL;
42
    jsval_t v;
43 44
    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
        if(r) {
52 53 54 55
            jsstr_t *str;

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

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

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

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

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

85
        if(name_len && msg_len) {
86 87 88 89 90 91 92 93 94 95
            WCHAR *ptr;

            ptr = jsstr_alloc_buf(name_len + msg_len + 2, &ret);
            if(ptr) {
                jsstr_flush(name, ptr);
                ptr[name_len] = ':';
                ptr[name_len+1] = ' ';
                jsstr_flush(msg, ptr+name_len+2);
            }else {
                hres = E_OUTOFMEMORY;
96
            }
97
        }else if(name_len) {
98 99
            ret = name;
            name = NULL;
100
        }else if(msg_len) {
101 102 103
            ret = msg;
            msg = NULL;
        }else {
104
            ret = jsstr_alloc(object_errorW);
105 106 107
        }
    }

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

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

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

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

    return S_OK;
138 139 140 141 142 143 144 145 146 147 148
}

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,
149
    NULL,
150 151 152 153 154 155
    NULL
};

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

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

168
    err = heap_alloc_zero(sizeof(*err));
169 170 171
    if(!err)
        return E_OUTOFMEMORY;

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

    *ret = err;
    return S_OK;
}

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

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

196
    hres = jsdisp_propput_dontenum(err, numberW, jsval_number((INT)number));
197
    if(FAILED(hres)) {
198
        jsdisp_release(err);
199
        return hres;
200 201
    }

202 203 204
    hres = jsdisp_propput_name(err, messageW, jsval_string(msg));
    if(SUCCEEDED(hres))
        hres = jsdisp_propput_dontenum(err, descriptionW, jsval_string(msg));
205
    if(FAILED(hres)) {
206
        jsdisp_release(err);
207 208 209
        return hres;
    }

210
    *ret = err;
211 212 213
    return S_OK;
}

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

221
    if(argc) {
222
        double n;
223

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

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

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

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

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

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

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

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

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

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

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

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

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

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

342
    jsdisp_t *err;
343
    unsigned int i;
344
    jsstr_t *str;
345 346
    HRESULT hres;

347
    for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
348
        hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
349 350 351
        if(FAILED(hres))
            return hres;

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

358
        hres = jsdisp_propput_dontenum(err, nameW, jsval_string(str));
359
        jsstr_release(str);
360
        if(SUCCEEDED(hres))
361
            hres = create_builtin_constructor(ctx, constr_val[i], names[i], NULL,
362
                    PROPF_CONSTR|1, err, constr_addr[i]);
363

364
        jsdisp_release(err);
365 366 367 368 369 370
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}
371

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

379 380 381
    if(!is_jscript_error(error))
        return error;

382
    buf[0] = '\0';
383
    LoadStringW(jscript_hinstance, HRESULT_CODE(error),  buf, sizeof(buf)/sizeof(WCHAR));
384 385 386 387

    if(str) pos = strchrW(buf, '|');
    if(pos) {
        int len = strlenW(str);
388
        memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
389 390 391
        memcpy(pos, str, len*sizeof(WCHAR));
    }

392 393
    WARN("%s\n", debugstr_w(buf));

394 395 396 397 398 399
    msg = jsstr_alloc(buf);
    if(!msg)
        return E_OUTOFMEMORY;

    hres = create_error(ctx, constr, error, msg, &err);
    jsstr_release(msg);
400 401 402
    if(FAILED(hres))
        return hres;

403 404
    jsval_release(ctx->ei.val);
    ctx->ei.val = jsval_obj(err);
405
    return error;
406 407
}

408
HRESULT throw_generic_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
409
{
410
    return throw_error(ctx, error, str, ctx->error_constr);
411 412
}

413
HRESULT throw_range_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
414
{
415
    return throw_error(ctx, error, str, ctx->range_error_constr);
416 417
}

418
HRESULT throw_reference_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
419
{
420
    return throw_error(ctx, error, str, ctx->reference_error_constr);
421 422
}

423
HRESULT throw_regexp_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
424
{
425
    return throw_error(ctx, error, str, ctx->regexp_error_constr);
426 427
}

428
HRESULT throw_syntax_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
429
{
430
    return throw_error(ctx, error, str, ctx->syntax_error_constr);
431 432
}

433
HRESULT throw_type_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
434
{
435
    return throw_error(ctx, error, str, ctx->type_error_constr);
436 437
}

438
HRESULT throw_uri_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
439
{
440
    return throw_error(ctx, error, str, ctx->uri_error_constr);
441
}