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}; |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 39 | static const WCHAR nameW[] = {'n','a','m','e',0}; |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 40 | static const WCHAR numberW[] = {'n','u','m','b','e','r',0}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 41 | 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] | 42 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 43 | static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp) |
| 44 | { |
| 45 | return (ErrorInstance*)vdisp->u.jsdisp; |
| 46 | } |
| 47 | |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 48 | static inline ErrorInstance *error_this(vdisp_t *jsthis) |
| 49 | { |
| 50 | return is_vclass(jsthis, JSCLASS_ERROR) ? error_from_vdisp(jsthis) : NULL; |
| 51 | } |
| 52 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 53 | 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] | 54 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 55 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 56 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 57 | |
| 58 | TRACE("\n"); |
| 59 | |
| 60 | switch(flags) { |
| 61 | case DISPATCH_PROPERTYGET: |
| 62 | return VariantCopy(retv, &This->number); |
| 63 | case DISPATCH_PROPERTYPUT: |
| 64 | return VariantCopy(&This->number, get_arg(dp, 0)); |
| 65 | default: |
| 66 | FIXME("unimplemented flags %x\n", flags); |
| 67 | return E_NOTIMPL; |
| 68 | } |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 71 | 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] | 72 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 73 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 74 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 75 | |
| 76 | TRACE("\n"); |
| 77 | |
| 78 | switch(flags) { |
| 79 | case DISPATCH_PROPERTYGET: |
| 80 | return VariantCopy(retv, &This->description); |
| 81 | case DISPATCH_PROPERTYPUT: |
| 82 | return VariantCopy(&This->description, get_arg(dp, 0)); |
| 83 | default: |
| 84 | FIXME("unimplemented flags %x\n", flags); |
| 85 | return E_NOTIMPL; |
| 86 | } |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 89 | /* ECMA-262 3rd Edition 15.11.4.3 */ |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 90 | 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] | 91 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 92 | { |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 93 | ErrorInstance *This = error_from_vdisp(jsthis); |
Piotr Caban | f17b1f6 | 2009-07-20 18:17:54 +0200 | [diff] [blame] | 94 | |
| 95 | TRACE("\n"); |
| 96 | |
| 97 | switch(flags) { |
| 98 | case DISPATCH_PROPERTYGET: |
| 99 | return VariantCopy(retv, &This->message); |
| 100 | case DISPATCH_PROPERTYPUT: |
| 101 | return VariantCopy(&This->message, get_arg(dp, 0)); |
| 102 | default: |
| 103 | FIXME("unimplemented flags %x\n", flags); |
| 104 | return E_NOTIMPL; |
| 105 | } |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 108 | /* ECMA-262 3rd Edition 15.11.4.4 */ |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 109 | static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 110 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller) |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 111 | { |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 112 | ErrorInstance *error; |
| 113 | BSTR name, msg = NULL, ret = NULL; |
| 114 | VARIANT v; |
| 115 | HRESULT hres; |
| 116 | |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 117 | static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0}; |
| 118 | |
| 119 | TRACE("\n"); |
| 120 | |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 121 | error = error_this(jsthis); |
| 122 | if(ctx->version < 2 || !error) { |
| 123 | if(retv) { |
| 124 | V_VT(retv) = VT_BSTR; |
| 125 | V_BSTR(retv) = SysAllocString(str); |
| 126 | if(!V_BSTR(retv)) |
| 127 | return E_OUTOFMEMORY; |
| 128 | } |
| 129 | return S_OK; |
| 130 | } |
| 131 | |
| 132 | hres = jsdisp_propget_name(&error->dispex, nameW, &v, ei, caller); |
| 133 | if(FAILED(hres)) |
| 134 | return hres; |
| 135 | |
| 136 | hres = to_string(ctx, &v, ei, &name); |
| 137 | VariantClear(&v); |
| 138 | if(FAILED(hres)) |
| 139 | return hres; |
| 140 | |
| 141 | if(V_VT(&error->message) != VT_EMPTY) { |
| 142 | hres = to_string(ctx, &error->message, ei, &msg); |
| 143 | if(SUCCEEDED(hres) && !*msg) { |
| 144 | SysFreeString(msg); |
| 145 | msg = NULL; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if(SUCCEEDED(hres)) { |
| 150 | if(msg) { |
| 151 | DWORD name_len, msg_len; |
| 152 | |
| 153 | name_len = SysStringLen(name); |
| 154 | msg_len = SysStringLen(msg); |
| 155 | |
| 156 | ret = SysAllocStringLen(NULL, name_len + msg_len + 2); |
| 157 | if(ret) { |
| 158 | memcpy(ret, name, name_len*sizeof(WCHAR)); |
| 159 | ret[name_len] = ':'; |
| 160 | ret[name_len+1] = ' '; |
| 161 | memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR)); |
| 162 | } |
| 163 | }else { |
| 164 | ret = name; |
| 165 | name = NULL; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | SysFreeString(msg); |
| 170 | SysFreeString(name); |
| 171 | if(FAILED(hres)) |
| 172 | return hres; |
| 173 | if(!ret) |
| 174 | return E_OUTOFMEMORY; |
| 175 | |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 176 | if(retv) { |
| 177 | V_VT(retv) = VT_BSTR; |
Jacek Caban | 86e7bea | 2009-10-19 20:41:02 +0200 | [diff] [blame^] | 178 | V_BSTR(retv) = ret; |
| 179 | }else { |
| 180 | SysFreeString(ret); |
Piotr Caban | 2d71dac | 2009-07-20 18:17:57 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return S_OK; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 186 | 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] | 187 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 188 | { |
Piotr Caban | 8dd1d9b | 2009-07-22 13:02:30 +0200 | [diff] [blame] | 189 | TRACE("\n"); |
| 190 | |
| 191 | switch(flags) { |
| 192 | case INVOKE_FUNC: |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 193 | return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL); |
Piotr Caban | 8dd1d9b | 2009-07-22 13:02:30 +0200 | [diff] [blame] | 194 | default: |
| 195 | FIXME("unimplemented flags %x\n", flags); |
| 196 | return E_NOTIMPL; |
| 197 | } |
| 198 | |
| 199 | return S_OK; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void Error_destructor(DispatchEx *dispex) |
| 203 | { |
| 204 | ErrorInstance *This = (ErrorInstance*)dispex; |
| 205 | |
Piotr Caban | f3eef0d | 2009-07-20 18:18:19 +0200 | [diff] [blame] | 206 | VariantClear(&This->number); |
| 207 | VariantClear(&This->description); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 208 | VariantClear(&This->message); |
| 209 | heap_free(This); |
| 210 | } |
| 211 | |
| 212 | static const builtin_prop_t Error_props[] = { |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 213 | {descriptionW, Error_description, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 214 | {messageW, Error_message, 0}, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 215 | {numberW, Error_number, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 216 | {toStringW, Error_toString, PROPF_METHOD} |
| 217 | }; |
| 218 | |
| 219 | static const builtin_info_t Error_info = { |
| 220 | JSCLASS_ERROR, |
| 221 | {NULL, Error_value, 0}, |
| 222 | sizeof(Error_props)/sizeof(*Error_props), |
| 223 | Error_props, |
| 224 | Error_destructor, |
| 225 | NULL |
| 226 | }; |
| 227 | |
| 228 | static const builtin_prop_t ErrorInst_props[] = { |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 229 | {descriptionW, Error_description, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 230 | {messageW, Error_message, 0}, |
Piotr Caban | 7c0a702 | 2009-07-20 18:18:17 +0200 | [diff] [blame] | 231 | {numberW, Error_number, 0}, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static const builtin_info_t ErrorInst_info = { |
| 235 | JSCLASS_ERROR, |
| 236 | {NULL, Error_value, 0}, |
| 237 | sizeof(ErrorInst_props)/sizeof(*ErrorInst_props), |
| 238 | ErrorInst_props, |
| 239 | Error_destructor, |
| 240 | NULL |
| 241 | }; |
| 242 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 243 | static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 244 | DispatchEx *constr, ErrorInstance **ret) |
| 245 | { |
| 246 | ErrorInstance *err; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 247 | HRESULT hres; |
| 248 | |
| 249 | err = heap_alloc_zero(sizeof(ErrorInstance)); |
| 250 | if(!err) |
| 251 | return E_OUTOFMEMORY; |
| 252 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 253 | if(prototype) |
| 254 | hres = init_dispex(&err->dispex, ctx, &Error_info, prototype); |
| 255 | else |
| 256 | hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info, |
| 257 | constr ? constr : ctx->error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 258 | if(FAILED(hres)) { |
| 259 | heap_free(err); |
| 260 | return hres; |
| 261 | } |
| 262 | |
| 263 | *ret = err; |
| 264 | return S_OK; |
| 265 | } |
| 266 | |
| 267 | static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr, |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 268 | const UINT *number, const WCHAR *msg, DispatchEx **ret) |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 269 | { |
| 270 | ErrorInstance *err; |
| 271 | HRESULT hres; |
| 272 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 273 | hres = alloc_error(ctx, NULL, constr, &err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 274 | if(FAILED(hres)) |
| 275 | return hres; |
| 276 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 277 | if(number) { |
| 278 | V_VT(&err->number) = VT_I4; |
| 279 | V_I4(&err->number) = *number; |
| 280 | } |
| 281 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 282 | V_VT(&err->message) = VT_BSTR; |
| 283 | if(msg) V_BSTR(&err->message) = SysAllocString(msg); |
| 284 | else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0); |
| 285 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 286 | VariantCopy(&err->description, &err->message); |
| 287 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 288 | if(!V_BSTR(&err->message)) { |
| 289 | heap_free(err); |
| 290 | return E_OUTOFMEMORY; |
| 291 | } |
| 292 | |
| 293 | *ret = &err->dispex; |
| 294 | return S_OK; |
| 295 | } |
| 296 | |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 297 | static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 298 | VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) { |
| 299 | DispatchEx *err; |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 300 | VARIANT numv; |
| 301 | UINT num; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 302 | BSTR msg = NULL; |
| 303 | HRESULT hres; |
| 304 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 305 | V_VT(&numv) = VT_NULL; |
| 306 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 307 | if(arg_cnt(dp)) { |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 308 | hres = to_number(ctx, get_arg(dp, 0), ei, &numv); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 309 | if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv)))) |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 310 | hres = to_string(ctx, get_arg(dp, 0), ei, &msg); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 311 | else if(V_VT(&numv) == VT_I4) |
| 312 | num = V_I4(&numv); |
| 313 | else |
| 314 | num = V_R8(&numv); |
| 315 | |
| 316 | if(FAILED(hres)) |
| 317 | return hres; |
| 318 | } |
| 319 | |
| 320 | if(arg_cnt(dp)>1 && !msg) { |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 321 | hres = to_string(ctx, get_arg(dp, 1), ei, &msg); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 322 | if(FAILED(hres)) |
| 323 | return hres; |
| 324 | } |
| 325 | |
| 326 | switch(flags) { |
| 327 | case INVOKE_FUNC: |
| 328 | case DISPATCH_CONSTRUCT: |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 329 | if(V_VT(&numv) == VT_NULL) |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 330 | hres = create_error(ctx, constr, NULL, msg, &err); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 331 | else |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 332 | hres = create_error(ctx, constr, &num, msg, &err); |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 333 | |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 334 | if(FAILED(hres)) |
| 335 | return hres; |
| 336 | |
| 337 | if(retv) { |
| 338 | V_VT(retv) = VT_DISPATCH; |
| 339 | V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err); |
| 340 | } |
| 341 | else |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 342 | jsdisp_release(err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 343 | |
| 344 | return S_OK; |
| 345 | |
| 346 | default: |
| 347 | FIXME("unimplemented flags %x\n", flags); |
| 348 | return E_NOTIMPL; |
| 349 | } |
| 350 | } |
| 351 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 352 | 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] | 353 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 354 | { |
| 355 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 356 | return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 359 | 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] | 360 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 361 | { |
| 362 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 363 | return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 366 | 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] | 367 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 368 | { |
| 369 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 370 | return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 373 | 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] | 374 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 375 | { |
| 376 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 377 | return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 380 | 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] | 381 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 382 | { |
| 383 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 384 | return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr); |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 387 | 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] | 388 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 389 | { |
| 390 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 391 | return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 394 | 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] | 395 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 396 | { |
| 397 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 398 | return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Jacek Caban | f8c2b42 | 2009-09-23 16:18:39 +0200 | [diff] [blame] | 401 | 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] | 402 | DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) |
| 403 | { |
| 404 | TRACE("\n"); |
Jacek Caban | 5dcd182 | 2009-09-23 16:17:17 +0200 | [diff] [blame] | 405 | return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 408 | HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype) |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 409 | { |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 410 | static const WCHAR ErrorW[] = {'E','r','r','o','r',0}; |
| 411 | static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0}; |
| 412 | static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0}; |
| 413 | 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] | 414 | 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] | 415 | static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0}; |
| 416 | static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0}; |
| 417 | static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0}; |
| 418 | static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 419 | ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 420 | DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 421 | &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr, |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 422 | &ctx->syntax_error_constr, &ctx->type_error_constr, |
| 423 | &ctx->uri_error_constr}; |
| 424 | static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value, |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 425 | RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value, |
| 426 | SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value}; |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 427 | |
| 428 | ErrorInstance *err; |
| 429 | INT i; |
| 430 | VARIANT v; |
| 431 | HRESULT hres; |
| 432 | |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 433 | for(i=0; i < sizeof(names)/sizeof(names[0]); i++) { |
Piotr Caban | 28734e3 | 2009-08-14 11:58:10 +0200 | [diff] [blame] | 434 | hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 435 | if(FAILED(hres)) |
| 436 | return hres; |
| 437 | |
| 438 | V_VT(&v) = VT_BSTR; |
| 439 | V_BSTR(&v) = SysAllocString(names[i]); |
| 440 | if(!V_BSTR(&v)) { |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 441 | jsdisp_release(&err->dispex); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 442 | return E_OUTOFMEMORY; |
| 443 | } |
| 444 | |
Jacek Caban | fadfab5 | 2009-09-23 16:11:11 +0200 | [diff] [blame] | 445 | hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 446 | |
| 447 | if(SUCCEEDED(hres)) |
Jacek Caban | d918a18 | 2009-09-17 01:04:44 +0200 | [diff] [blame] | 448 | hres = create_builtin_function(ctx, constr_val[i], names[i], NULL, |
Piotr Caban | 662a852 | 2009-10-13 22:08:43 +0200 | [diff] [blame] | 449 | PROPF_CONSTR|1, &err->dispex, constr_addr[i]); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 450 | |
Jacek Caban | c444a49 | 2009-09-01 13:26:08 +0200 | [diff] [blame] | 451 | jsdisp_release(&err->dispex); |
Piotr Caban | f33f5c9 | 2009-07-20 18:17:51 +0200 | [diff] [blame] | 452 | VariantClear(&v); |
| 453 | if(FAILED(hres)) |
| 454 | return hres; |
| 455 | } |
| 456 | |
| 457 | return S_OK; |
| 458 | } |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 459 | |
| 460 | static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr) |
| 461 | { |
| 462 | WCHAR buf[1024], *pos = NULL; |
| 463 | DispatchEx *err; |
| 464 | HRESULT hres; |
| 465 | |
Piotr Caban | 98223b9 | 2009-07-24 09:35:56 +0200 | [diff] [blame] | 466 | buf[0] = '\0'; |
| 467 | LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR)); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 468 | |
| 469 | if(str) pos = strchrW(buf, '|'); |
| 470 | if(pos) { |
| 471 | int len = strlenW(str); |
Piotr Caban | d8e841c | 2009-07-22 13:01:59 +0200 | [diff] [blame] | 472 | memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR)); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 473 | memcpy(pos, str, len*sizeof(WCHAR)); |
| 474 | } |
| 475 | |
Piotr Caban | d8e841c | 2009-07-22 13:01:59 +0200 | [diff] [blame] | 476 | WARN("%s\n", debugstr_w(buf)); |
| 477 | |
Piotr Caban | 98223b9 | 2009-07-24 09:35:56 +0200 | [diff] [blame] | 478 | id |= JSCRIPT_ERROR; |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 479 | hres = create_error(ctx, constr, &id, buf, &err); |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 480 | if(FAILED(hres)) |
| 481 | return hres; |
| 482 | |
| 483 | if(!ei) |
| 484 | return id; |
| 485 | |
| 486 | V_VT(&ei->var) = VT_DISPATCH; |
| 487 | V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err); |
| 488 | |
Piotr Caban | a77e369 | 2009-07-20 18:18:22 +0200 | [diff] [blame] | 489 | return id; |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 493 | { |
| 494 | return throw_error(ctx, ei, id, str, ctx->eval_error_constr); |
| 495 | } |
| 496 | |
Jacek Caban | 6d4533a | 2009-09-30 14:34:47 +0200 | [diff] [blame] | 497 | HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 498 | { |
| 499 | return throw_error(ctx, ei, id, str, ctx->error_constr); |
| 500 | } |
| 501 | |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 502 | HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 503 | { |
| 504 | return throw_error(ctx, ei, id, str, ctx->range_error_constr); |
| 505 | } |
| 506 | |
| 507 | HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 508 | { |
| 509 | return throw_error(ctx, ei, id, str, ctx->reference_error_constr); |
| 510 | } |
| 511 | |
Jacek Caban | 9e523c6 | 2009-09-23 16:09:22 +0200 | [diff] [blame] | 512 | HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 513 | { |
| 514 | return throw_error(ctx, ei, id, str, ctx->regexp_error_constr); |
| 515 | } |
| 516 | |
Piotr Caban | 469b597 | 2009-07-20 18:18:00 +0200 | [diff] [blame] | 517 | HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 518 | { |
| 519 | return throw_error(ctx, ei, id, str, ctx->syntax_error_constr); |
| 520 | } |
| 521 | |
| 522 | HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 523 | { |
| 524 | return throw_error(ctx, ei, id, str, ctx->type_error_constr); |
| 525 | } |
| 526 | |
| 527 | HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str) |
| 528 | { |
| 529 | return throw_error(ctx, ei, id, str, ctx->uri_error_constr); |
| 530 | } |