Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Piotr Caban |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 | */ |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 18 | #include "config.h" |
| 19 | #include "wine/port.h" |
| 20 | |
| 21 | #include <math.h> |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 22 | |
| 23 | #include "jscript.h" |
| 24 | |
| 25 | #include "wine/debug.h" |
| 26 | |
| 27 | WINE_DEFAULT_DEBUG_CHANNEL(jscript); |
| 28 | |
| 29 | typedef struct { |
| 30 | DispatchEx dispex; |
| 31 | |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 32 | VARIANT number; |
| 33 | VARIANT description; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 34 | VARIANT message; |
| 35 | } ErrorInstance; |
| 36 | |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 37 | static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 38 | static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0}; |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 39 | static const WCHAR numberW[] = {'n','u','m','b','e','r',0}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 40 | static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 41 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 42 | static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp) |
| 43 | { |
| 44 | return (ErrorInstance*)vdisp->u.jsdisp; |
| 45 | } |
| 46 | |
| 47 | static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 48 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 49 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 50 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 51 | |
| 52 | TRACE("\n"); |
| 53 | |
| 54 | switch(flags) { |
| 55 | case DISPATCH_PROPERTYGET: |
| 56 | return VariantCopy(retv, &This->number); |
| 57 | case DISPATCH_PROPERTYPUT: |
| 58 | return VariantCopy(&This->number, get_arg(dp, 0)); |
| 59 | default: |
| 60 | FIXME("unimplemented flags %x\n", flags); |
| 61 | return E_NOTIMPL; |
| 62 | } |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 65 | static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 66 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 67 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 68 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 69 | |
| 70 | TRACE("\n"); |
| 71 | |
| 72 | switch(flags) { |
| 73 | case DISPATCH_PROPERTYGET: |
| 74 | return VariantCopy(retv, &This->description); |
| 75 | case DISPATCH_PROPERTYPUT: |
| 76 | return VariantCopy(&This->description, get_arg(dp, 0)); |
| 77 | default: |
| 78 | FIXME("unimplemented flags %x\n", flags); |
| 79 | return E_NOTIMPL; |
| 80 | } |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 83 | /* ECMA-262 3rd Edition 15.11.4.3 */ |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 84 | static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 85 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 86 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 87 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f17b1f6 | 2009-07-20 18:17:54 +0200 | [diff] [blame] | 88 | |
| 89 | TRACE("\n"); |
| 90 | |
| 91 | switch(flags) { |
| 92 | case DISPATCH_PROPERTYGET: |
| 93 | return VariantCopy(retv, &This->message); |
| 94 | case DISPATCH_PROPERTYPUT: |
| 95 | return VariantCopy(&This->message, get_arg(dp, 0)); |
| 96 | default: |
| 97 | FIXME("unimplemented flags %x\n", flags); |
| 98 | return E_NOTIMPL; |
| 99 | } |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 102 | /* ECMA-262 3rd Edition 15.11.4.4 */ |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 103 | static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 104 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 105 | { |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 106 | static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0}; |
| 107 | |
| 108 | TRACE("\n"); |
| 109 | |
| 110 | if(retv) { |
| 111 | V_VT(retv) = VT_BSTR; |
| 112 | V_BSTR(retv) = SysAllocString(str); |
| 113 | if(!V_BSTR(retv)) |
| 114 | return E_OUTOFMEMORY; |
| 115 | } |
| 116 | |
| 117 | return S_OK; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 120 | static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 121 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 122 | { |
Piotr Caban | 8dd1d9b | 2009-07-22 13:02:30 +0200 | [diff] [blame] | 123 | TRACE("\n"); |
| 124 | |
| 125 | switch(flags) { |
| 126 | case INVOKE_FUNC: |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 127 | return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL); |
Piotr Caban | 8dd1d9b | 2009-07-22 13:02:30 +0200 | [diff] [blame] | 128 | default: |
| 129 | FIXME("unimplemented flags %x\n", flags); |
| 130 | return E_NOTIMPL; |
| 131 | } |
| 132 | |
| 133 | return S_OK; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void Error_destructor(DispatchEx *dispex) |
| 137 | { |
| 138 | ErrorInstance *This = (ErrorInstance*)dispex; |
| 139 | |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 140 | VariantClear(&This->number); |
| 141 | VariantClear(&This->description); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 142 | VariantClear(&This->message); |
| 143 | heap_free(This); |
| 144 | } |
| 145 | |
| 146 | static const builtin_prop_t Error_props[] = { |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 147 | {descriptionW, Error_description, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 148 | {messageW, Error_message, 0}, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 149 | {numberW, Error_number, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 150 | {toStringW, Error_toString, PROPF_METHOD} |
| 151 | }; |
| 152 | |
| 153 | static const builtin_info_t Error_info = { |
| 154 | JSCLASS_ERROR, |
| 155 | {NULL, Error_value, 0}, |
| 156 | sizeof(Error_props)/sizeof(*Error_props), |
| 157 | Error_props, |
| 158 | Error_destructor, |
| 159 | NULL |
| 160 | }; |
| 161 | |
| 162 | static const builtin_prop_t ErrorInst_props[] = { |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 163 | {descriptionW, Error_description, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 164 | {messageW, Error_message, 0}, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 165 | {numberW, Error_number, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static const builtin_info_t ErrorInst_info = { |
| 169 | JSCLASS_ERROR, |
| 170 | {NULL, Error_value, 0}, |
| 171 | sizeof(ErrorInst_props)/sizeof(*ErrorInst_props), |
| 172 | ErrorInst_props, |
| 173 | Error_destructor, |
| 174 | NULL |
| 175 | }; |
| 176 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 177 | static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 178 | DispatchEx *constr, ErrorInstance **ret) |
| 179 | { |
| 180 | ErrorInstance *err; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 181 | HRESULT hres; |
| 182 | |
| 183 | err = heap_alloc_zero(sizeof(ErrorInstance)); |
| 184 | if(!err) |
| 185 | return E_OUTOFMEMORY; |
| 186 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 187 | if(prototype) |
| 188 | hres = init_dispex(&err->dispex, ctx, &Error_info, prototype); |
| 189 | else |
| 190 | hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info, |
| 191 | constr ? constr : ctx->error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 192 | if(FAILED(hres)) { |
| 193 | heap_free(err); |
| 194 | return hres; |
| 195 | } |
| 196 | |
| 197 | *ret = err; |
| 198 | return S_OK; |
| 199 | } |
| 200 | |
| 201 | static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr, |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 202 | const UINT *number, const WCHAR *msg, DispatchEx **ret) |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 203 | { |
| 204 | ErrorInstance *err; |
| 205 | HRESULT hres; |
| 206 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 207 | hres = alloc_error(ctx, NULL, constr, &err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 208 | if(FAILED(hres)) |
| 209 | return hres; |
| 210 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 211 | if(number) { |
| 212 | V_VT(&err->number) = VT_I4; |
| 213 | V_I4(&err->number) = *number; |
| 214 | } |
| 215 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 216 | V_VT(&err->message) = VT_BSTR; |
| 217 | if(msg) V_BSTR(&err->message) = SysAllocString(msg); |
| 218 | else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0); |
| 219 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 220 | VariantCopy(&err->description, &err->message); |
| 221 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 222 | if(!V_BSTR(&err->message)) { |
| 223 | heap_free(err); |
| 224 | return E_OUTOFMEMORY; |
| 225 | } |
| 226 | |
| 227 | *ret = &err->dispex; |
| 228 | return S_OK; |
| 229 | } |
| 230 | |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 231 | static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 232 | VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) { |
| 233 | DispatchEx *err; |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 234 | VARIANT numv; |
| 235 | UINT num; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 236 | BSTR msg = NULL; |
| 237 | HRESULT hres; |
| 238 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 239 | V_VT(&numv) = VT_NULL; |
| 240 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 241 | if(arg_cnt(dp)) { |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 242 | hres = to_number(ctx, get_arg(dp, 0), ei, &numv); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 243 | if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv)))) |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 244 | hres = to_string(ctx, get_arg(dp, 0), ei, &msg); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 245 | else if(V_VT(&numv) == VT_I4) |
| 246 | num = V_I4(&numv); |
| 247 | else |
| 248 | num = V_R8(&numv); |
| 249 | |
| 250 | if(FAILED(hres)) |
| 251 | return hres; |
| 252 | } |
| 253 | |
| 254 | if(arg_cnt(dp)>1 && !msg) { |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 255 | hres = to_string(ctx, get_arg(dp, 1), ei, &msg); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 256 | if(FAILED(hres)) |
| 257 | return hres; |
| 258 | } |
| 259 | |
| 260 | switch(flags) { |
| 261 | case INVOKE_FUNC: |
| 262 | case DISPATCH_CONSTRUCT: |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 263 | if(V_VT(&numv) == VT_NULL) |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 264 | hres = create_error(ctx, constr, NULL, msg, &err); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 265 | else |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 266 | hres = create_error(ctx, constr, &num, msg, &err); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 267 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 268 | if(FAILED(hres)) |
| 269 | return hres; |
| 270 | |
| 271 | if(retv) { |
| 272 | V_VT(retv) = VT_DISPATCH; |
| 273 | V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err); |
| 274 | } |
| 275 | else |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 276 | jsdisp_release(err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 277 | |
| 278 | return S_OK; |
| 279 | |
| 280 | default: |
| 281 | FIXME("unimplemented flags %x\n", flags); |
| 282 | return E_NOTIMPL; |
| 283 | } |
| 284 | } |
| 285 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 286 | static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 287 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 288 | { |
| 289 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 290 | return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 293 | static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 294 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 295 | { |
| 296 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 297 | return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 300 | static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 301 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 302 | { |
| 303 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 304 | return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 307 | static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 308 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 309 | { |
| 310 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 311 | return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 314 | static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 315 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 316 | { |
| 317 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 318 | return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr); |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 321 | static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 322 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 323 | { |
| 324 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 325 | return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 328 | static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 329 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 330 | { |
| 331 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 332 | return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 335 | static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 336 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 337 | { |
| 338 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 339 | return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 342 | HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype) |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 343 | { |
| 344 | static const WCHAR nameW[] = {'n','a','m','e',0}; |
| 345 | static const WCHAR ErrorW[] = {'E','r','r','o','r',0}; |
| 346 | static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0}; |
| 347 | static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0}; |
| 348 | static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0}; |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 349 | static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 350 | static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0}; |
| 351 | static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0}; |
| 352 | static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0}; |
| 353 | static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 354 | ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 355 | DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 356 | &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 357 | &ctx->syntax_error_constr, &ctx->type_error_constr, |
| 358 | &ctx->uri_error_constr}; |
| 359 | static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 360 | RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value, |
| 361 | SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 362 | |
| 363 | ErrorInstance *err; |
| 364 | INT i; |
| 365 | VARIANT v; |
| 366 | HRESULT hres; |
| 367 | |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 368 | for(i=0; i < sizeof(names)/sizeof(names[0]); i++) { |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 369 | hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 370 | if(FAILED(hres)) |
| 371 | return hres; |
| 372 | |
| 373 | V_VT(&v) = VT_BSTR; |
| 374 | V_BSTR(&v) = SysAllocString(names[i]); |
| 375 | if(!V_BSTR(&v)) { |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 376 | jsdisp_release(&err->dispex); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 377 | return E_OUTOFMEMORY; |
| 378 | } |
| 379 | |
Jacek Caban | fadfab5 | 2009-09-23 16:11:11 +0200 | [diff] [blame] | 380 | hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 381 | |
| 382 | if(SUCCEEDED(hres)) |
Jacek Caban | d918a18 | 2009-09-17 01:04:44 +0200 | [diff] [blame] | 383 | hres = create_builtin_function(ctx, constr_val[i], names[i], NULL, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 384 | PROPF_CONSTR, &err->dispex, constr_addr[i]); |
| 385 | |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 386 | jsdisp_release(&err->dispex); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 387 | VariantClear(&v); |
| 388 | if(FAILED(hres)) |
| 389 | return hres; |
| 390 | } |
| 391 | |
| 392 | return S_OK; |
| 393 | } |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 394 | |
| 395 | static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr) |
| 396 | { |
| 397 | WCHAR buf[1024], *pos = NULL; |
| 398 | DispatchEx *err; |
| 399 | HRESULT hres; |
| 400 | |
Piotr Caban | 98223b9 | 2009-07-24 09:35:56 +0200 | [diff] [blame] | 401 | buf[0] = '\0'; |
| 402 | LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR)); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 403 | |
| 404 | if(str) pos = strchrW(buf, '|'); |
| 405 | if(pos) { |
| 406 | int len = strlenW(str); |
Piotr Caban | d8e841c | 2009-07-22 13:01:59 +0200 | [diff] [blame] | 407 | memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR)); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 408 | memcpy(pos, str, len*sizeof(WCHAR)); |
| 409 | } |
| 410 | |
Piotr Caban | d8e841c | 2009-07-22 13:01:59 +0200 | [diff] [blame] | 411 | WARN("%s\n", debugstr_w(buf)); |
| 412 | |
Piotr Caban | 98223b9 | 2009-07-24 09:35:56 +0200 | [diff] [blame] | 413 | id |= JSCRIPT_ERROR; |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 414 | hres = create_error(ctx, constr, &id, buf, &err); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 415 | if(FAILED(hres)) |
| 416 | return hres; |
| 417 | |
| 418 | if(!ei) |
| 419 | return id; |
| 420 | |
| 421 | V_VT(&ei->var) = VT_DISPATCH; |
| 422 | V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err); |
| 423 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 424 | return id; |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 428 | { |
| 429 | return throw_error(ctx, ei, id, str, ctx->eval_error_constr); |
| 430 | } |
| 431 | |
Jacek Caban | 6d4533a | 2009-09-30 14:34:47 +0200 | [diff] [blame^] | 432 | HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 433 | { |
| 434 | return throw_error(ctx, ei, id, str, ctx->error_constr); |
| 435 | } |
| 436 | |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 437 | HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 438 | { |
| 439 | return throw_error(ctx, ei, id, str, ctx->range_error_constr); |
| 440 | } |
| 441 | |
| 442 | HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 443 | { |
| 444 | return throw_error(ctx, ei, id, str, ctx->reference_error_constr); |
| 445 | } |
| 446 | |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 447 | HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 448 | { |
| 449 | return throw_error(ctx, ei, id, str, ctx->regexp_error_constr); |
| 450 | } |
| 451 | |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 452 | HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 453 | { |
| 454 | return throw_error(ctx, ei, id, str, ctx->syntax_error_constr); |
| 455 | } |
| 456 | |
| 457 | HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 458 | { |
| 459 | return throw_error(ctx, ei, id, str, ctx->type_error_constr); |
| 460 | } |
| 461 | |
| 462 | HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 463 | { |
| 464 | return throw_error(ctx, ei, id, str, ctx->uri_error_constr); |
| 465 | } |