jscript: Added String.split implementation for non-regexp arguments.
[wine] / dlls / jscript / global.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
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  */
18
19 #include "jscript.h"
20 #include "engine.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25
26 static const WCHAR NaNW[] = {'N','a','N',0};
27 static const WCHAR InfinityW[] = {'I','n','f','i','n','i','t','y',0};
28 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
29 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
30 static const WCHAR DateW[] = {'D','a','t','e',0};
31 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
32 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
33 static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0};
34 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
35 static const WCHAR RegExpW[] = {'R','e','g','E','x','p',0};
36 static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
37 static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
38 static const WCHAR EnumeratorW[] = {'E','n','u','m','e','r','a','t','o','r',0};
39 static const WCHAR escapeW[] = {'e','s','c','a','p','e',0};
40 static const WCHAR evalW[] = {'e','v','a','l',0};
41 static const WCHAR isNaNW[] = {'i','s','N','a','N',0};
42 static const WCHAR isFiniteW[] = {'i','s','F','i','n','i','t','e',0};
43 static const WCHAR parseIntW[] = {'p','a','r','s','e','I','n','t',0};
44 static const WCHAR parseFloatW[] = {'p','a','r','s','e','F','l','o','a','t',0};
45 static const WCHAR unescapeW[] = {'u','n','e','s','c','a','p','e',0};
46 static const WCHAR GetObjectW[] = {'G','e','t','O','b','j','e','c','t',0};
47 static const WCHAR ScriptEngineW[] = {'S','c','r','i','p','t','E','n','g','i','n','e',0};
48 static const WCHAR ScriptEngineMajorVersionW[] =
49     {'S','c','r','i','p','t','E','n','g','i','n','e','M','a','j','o','r','V','e','r','s','i','o','n',0};
50 static const WCHAR ScriptEngineMinorVersionW[] =
51     {'S','c','r','i','p','t','E','n','g','i','n','e','M','i','n','o','r','V','e','r','s','i','o','n',0};
52 static const WCHAR ScriptEngineBuildVersionW[] =
53     {'S','c','r','i','p','t','E','n','g','i','n','e','B','u','i','l','d','V','e','r','s','i','o','n',0};
54 static const WCHAR CollectGarbageW[] = {'C','o','l','l','e','c','t','G','a','r','b','a','g','e',0};
55 static const WCHAR MathW[] = {'M','a','t','h',0};
56 static const WCHAR encodeURIW[] = {'e','n','c','o','d','e','U','R','I',0};
57
58 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
59
60 static int uri_char_table[] = {
61     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 00-0f */
62     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10-1f */
63     0,2,0,0,1,0,1,2,2,2,2,1,1,2,2,1, /* 20-2f */
64     2,2,2,2,2,2,2,2,2,2,1,1,0,1,0,1, /* 30-3f */
65     1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 40-4f */
66     2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,2, /* 50-5f */
67     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 60-6f */
68     2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,0, /* 70-7f */
69 };
70
71 /* 1 - reserved */
72 /* 2 - unescaped */
73
74 static inline BOOL is_uri_reserved(WCHAR c)
75 {
76     return c < 128 && uri_char_table[c] == 1;
77 }
78
79 static inline BOOL is_uri_unescaped(WCHAR c)
80 {
81     return c < 128 && uri_char_table[c] == 2;
82 }
83
84 static WCHAR int_to_char(int i)
85 {
86     if(i < 10)
87         return '0'+i;
88     return 'A'+i-10;
89 }
90
91 static HRESULT constructor_call(DispatchEx *constr, LCID lcid, WORD flags, DISPPARAMS *dp,
92         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
93 {
94     if(flags != DISPATCH_PROPERTYGET)
95         return jsdisp_call_value(constr, lcid, flags, dp, retv, ei, sp);
96
97     V_VT(retv) = VT_DISPATCH;
98     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(constr);
99     IDispatchEx_AddRef(_IDispatchEx_(constr));
100     return S_OK;
101 }
102
103 static HRESULT JSGlobal_NaN(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
104         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105 {
106     FIXME("\n");
107     return E_NOTIMPL;
108 }
109
110 static HRESULT JSGlobal_Infinity(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
111         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
112 {
113     FIXME("\n");
114     return E_NOTIMPL;
115 }
116
117 static HRESULT JSGlobal_Array(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
118         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
119 {
120     TRACE("\n");
121
122     return constructor_call(dispex->ctx->array_constr, lcid, flags, dp, retv, ei, sp);
123 }
124
125 static HRESULT JSGlobal_Boolean(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
126         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
127 {
128     TRACE("\n");
129
130     return constructor_call(dispex->ctx->bool_constr, lcid, flags, dp, retv, ei, sp);
131 }
132
133 static HRESULT JSGlobal_Date(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
134         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
135 {
136     TRACE("\n");
137
138     return constructor_call(dispex->ctx->date_constr, lcid, flags, dp, retv, ei, sp);
139 }
140
141 static HRESULT JSGlobal_Function(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
142         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
143 {
144     TRACE("\n");
145
146     return constructor_call(dispex->ctx->function_constr, lcid, flags, dp, retv, ei, sp);
147 }
148
149 static HRESULT JSGlobal_Number(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
150         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
151 {
152     TRACE("\n");
153
154     return constructor_call(dispex->ctx->number_constr, lcid, flags, dp, retv, ei, sp);
155 }
156
157 static HRESULT JSGlobal_Object(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
158         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
159 {
160     TRACE("\n");
161
162     return constructor_call(dispex->ctx->object_constr, lcid, flags, dp, retv, ei, sp);
163 }
164
165 static HRESULT JSGlobal_String(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
166         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
167 {
168     TRACE("\n");
169
170     return constructor_call(dispex->ctx->string_constr, lcid, flags, dp, retv, ei, sp);
171 }
172
173 static HRESULT JSGlobal_RegExp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
174         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
175 {
176     TRACE("\n");
177
178     return constructor_call(dispex->ctx->regexp_constr, lcid, flags, dp, retv, ei, sp);
179 }
180
181 static HRESULT JSGlobal_ActiveXObject(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
183 {
184     FIXME("\n");
185     return E_NOTIMPL;
186 }
187
188 static HRESULT JSGlobal_VBArray(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
189         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
190 {
191     FIXME("\n");
192     return E_NOTIMPL;
193 }
194
195 static HRESULT JSGlobal_Enumerator(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
196         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
197 {
198     FIXME("\n");
199     return E_NOTIMPL;
200 }
201
202 static HRESULT JSGlobal_escape(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
203         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
204 {
205     FIXME("\n");
206     return E_NOTIMPL;
207 }
208
209 /* ECMA-262 3rd Edition    15.1.2.1 */
210 static HRESULT JSGlobal_eval(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
211         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
212 {
213     parser_ctx_t *parser_ctx;
214     VARIANT *arg;
215     HRESULT hres;
216
217     TRACE("\n");
218
219     if(!arg_cnt(dp)) {
220         if(retv)
221             V_VT(retv) = VT_EMPTY;
222         return S_OK;
223     }
224
225     arg = get_arg(dp, 0);
226     if(V_VT(arg) != VT_BSTR) {
227         if(retv) {
228             V_VT(retv) = VT_EMPTY;
229             return VariantCopy(retv, arg);
230         }
231         return S_OK;
232     }
233
234     if(!dispex->ctx->exec_ctx) {
235         FIXME("No active exec_ctx\n");
236         return E_UNEXPECTED;
237     }
238
239     TRACE("parsing %s\n", debugstr_w(V_BSTR(arg)));
240     hres = script_parse(dispex->ctx, V_BSTR(arg), &parser_ctx);
241     if(FAILED(hres)) {
242         WARN("parse (%s) failed: %08x\n", debugstr_w(V_BSTR(arg)), hres);
243         return hres;
244     }
245
246     hres = exec_source(dispex->ctx->exec_ctx, parser_ctx, parser_ctx->source, ei, retv);
247     parser_release(parser_ctx);
248
249     return hres;
250 }
251
252 static HRESULT JSGlobal_isNaN(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
253         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
254 {
255     FIXME("\n");
256     return E_NOTIMPL;
257 }
258
259 static HRESULT JSGlobal_isFinite(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
260         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
261 {
262     FIXME("\n");
263     return E_NOTIMPL;
264 }
265
266 static INT char_to_int(WCHAR c)
267 {
268     if('0' <= c && c <= '9')
269         return c - '0';
270     if('a' <= c && c <= 'z')
271         return c - 'a' + 10;
272     if('A' <= c && c <= 'Z')
273         return c - 'A' + 10;
274     return 100;
275 }
276
277 static HRESULT JSGlobal_parseInt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
278         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
279 {
280     DOUBLE ret = 0.0;
281     INT radix=10, i;
282     WCHAR *ptr;
283     BOOL neg = FALSE;
284     BSTR str;
285     HRESULT hres;
286
287     if(!arg_cnt(dp)) {
288         FIXME("NAN\n");
289         return E_NOTIMPL;
290     }
291
292     if(arg_cnt(dp) >= 2) {
293         hres = to_int32(dispex->ctx, get_arg(dp, 1), ei, &radix);
294         if(FAILED(hres))
295             return hres;
296
297         if(!radix) {
298             radix = 10;
299         }else if(radix < 2 || radix > 36) {
300             WARN("radix %d out of range\n", radix);
301             return E_FAIL;
302         }
303     }
304
305     hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
306     if(FAILED(hres))
307         return hres;
308
309     for(ptr = str; isspaceW(*ptr); ptr++);
310
311     switch(*ptr) {
312     case '+':
313         ptr++;
314         break;
315     case '-':
316         neg = TRUE;
317         ptr++;
318         break;
319     case '0':
320         ptr++;
321         if(*ptr == 'x' || *ptr == 'X') {
322             radix = 16;
323             ptr++;
324         }
325     }
326
327     while(*ptr) {
328         i = char_to_int(*ptr++);
329         if(i > radix)
330             break;
331
332         ret = ret*radix + i;
333     }
334
335     SysFreeString(str);
336
337     if(neg)
338         ret = -ret;
339
340     if(retv)
341         num_set_val(retv, ret);
342     return S_OK;
343 }
344
345 static HRESULT JSGlobal_parseFloat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
346         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
347 {
348     FIXME("\n");
349     return E_NOTIMPL;
350 }
351
352 static HRESULT JSGlobal_unescape(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
353         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
354 {
355     FIXME("\n");
356     return E_NOTIMPL;
357 }
358
359 static HRESULT JSGlobal_GetObject(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
360         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
361 {
362     FIXME("\n");
363     return E_NOTIMPL;
364 }
365
366 static HRESULT JSGlobal_ScriptEngine(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
367         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
368 {
369     FIXME("\n");
370     return E_NOTIMPL;
371 }
372
373 static HRESULT JSGlobal_ScriptEngineMajorVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
374         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 {
376     FIXME("\n");
377     return E_NOTIMPL;
378 }
379
380 static HRESULT JSGlobal_ScriptEngineMinorVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
381         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
382 {
383     FIXME("\n");
384     return E_NOTIMPL;
385 }
386
387 static HRESULT JSGlobal_ScriptEngineBuildVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
388         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
389 {
390     FIXME("\n");
391     return E_NOTIMPL;
392 }
393
394 static HRESULT JSGlobal_CollectGarbage(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
395         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
396 {
397     FIXME("\n");
398     return E_NOTIMPL;
399 }
400
401 static HRESULT JSGlobal_encodeURI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
402         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
403 {
404     const WCHAR *ptr;
405     DWORD len = 0, i;
406     char buf[4];
407     BSTR str, ret;
408     WCHAR *rptr;
409     HRESULT hres;
410
411     TRACE("\n");
412
413     if(!arg_cnt(dp)) {
414         if(retv) {
415             ret = SysAllocString(undefinedW);
416             if(!ret)
417                 return E_OUTOFMEMORY;
418
419             V_VT(retv) = VT_BSTR;
420             V_BSTR(retv) = ret;
421         }
422
423         return S_OK;
424     }
425
426     hres = to_string(dispex->ctx, get_arg(dp,0), ei, &str);
427     if(FAILED(hres))
428         return hres;
429
430     for(ptr = str; *ptr; ptr++) {
431         if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
432             len++;
433         }else {
434             i = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, NULL, 0, NULL, NULL)*3;
435             if(!i) {
436                 FIXME("throw URIError\n");
437                 return E_FAIL;
438             }
439
440             len += i;
441         }
442     }
443
444     rptr = ret = SysAllocStringLen(NULL, len);
445     if(!ret)
446         return E_OUTOFMEMORY;
447
448     for(ptr = str; *ptr; ptr++) {
449         if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
450             *rptr++ = *ptr;
451         }else {
452             len = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, buf, sizeof(buf), NULL, NULL);
453             for(i=0; i<len; i++) {
454                 *rptr++ = '%';
455                 *rptr++ = int_to_char((BYTE)buf[i] >> 4);
456                 *rptr++ = int_to_char(buf[i] & 0x0f);
457             }
458         }
459     }
460
461     TRACE("%s -> %s\n", debugstr_w(str), debugstr_w(ret));
462     if(retv) {
463         V_VT(retv) = VT_BSTR;
464         V_BSTR(retv) = ret;
465     }else {
466         SysFreeString(ret);
467     }
468     return S_OK;
469 }
470
471 static const builtin_prop_t JSGlobal_props[] = {
472     {ActiveXObjectW,             JSGlobal_ActiveXObject,             PROPF_METHOD},
473     {ArrayW,                     JSGlobal_Array,                     PROPF_CONSTR},
474     {BooleanW,                   JSGlobal_Boolean,                   PROPF_CONSTR},
475     {CollectGarbageW,            JSGlobal_CollectGarbage,            PROPF_METHOD},
476     {DateW,                      JSGlobal_Date,                      PROPF_CONSTR},
477     {EnumeratorW,                JSGlobal_Enumerator,                PROPF_METHOD},
478     {FunctionW,                  JSGlobal_Function,                  PROPF_CONSTR},
479     {GetObjectW,                 JSGlobal_GetObject,                 PROPF_METHOD},
480     {InfinityW,                  JSGlobal_Infinity,                  0},
481 /*  {MathW,                      JSGlobal_Math,                      0},  */
482     {NaNW,                       JSGlobal_NaN,                       0},
483     {NumberW,                    JSGlobal_Number,                    PROPF_CONSTR},
484     {ObjectW,                    JSGlobal_Object,                    PROPF_CONSTR},
485     {RegExpW,                    JSGlobal_RegExp,                    PROPF_CONSTR},
486     {ScriptEngineW,              JSGlobal_ScriptEngine,              PROPF_METHOD},
487     {ScriptEngineBuildVersionW,  JSGlobal_ScriptEngineBuildVersion,  PROPF_METHOD},
488     {ScriptEngineMajorVersionW,  JSGlobal_ScriptEngineMajorVersion,  PROPF_METHOD},
489     {ScriptEngineMinorVersionW,  JSGlobal_ScriptEngineMinorVersion,  PROPF_METHOD},
490     {StringW,                    JSGlobal_String,                    PROPF_CONSTR},
491     {VBArrayW,                   JSGlobal_VBArray,                   PROPF_METHOD},
492     {encodeURIW,                 JSGlobal_encodeURI,                 PROPF_METHOD},
493     {escapeW,                    JSGlobal_escape,                    PROPF_METHOD},
494     {evalW,                      JSGlobal_eval,                      PROPF_METHOD|1},
495     {isFiniteW,                  JSGlobal_isFinite,                  PROPF_METHOD},
496     {isNaNW,                     JSGlobal_isNaN,                     PROPF_METHOD},
497     {parseFloatW,                JSGlobal_parseFloat,                PROPF_METHOD},
498     {parseIntW,                  JSGlobal_parseInt,                  PROPF_METHOD|2},
499     {unescapeW,                  JSGlobal_unescape,                  PROPF_METHOD}
500 };
501
502 static const builtin_info_t JSGlobal_info = {
503     JSCLASS_GLOBAL,
504     {NULL, NULL, 0},
505     sizeof(JSGlobal_props)/sizeof(*JSGlobal_props),
506     JSGlobal_props,
507     NULL,
508     NULL
509 };
510
511 static HRESULT init_constructors(script_ctx_t *ctx)
512 {
513     HRESULT hres;
514
515     hres = init_function_constr(ctx);
516     if(FAILED(hres))
517         return hres;
518
519     hres = create_array_constr(ctx, &ctx->array_constr);
520     if(FAILED(hres))
521         return hres;
522
523     hres = create_bool_constr(ctx, &ctx->bool_constr);
524     if(FAILED(hres))
525         return hres;
526
527     hres = create_date_constr(ctx, &ctx->date_constr);
528     if(FAILED(hres))
529         return hres;
530
531     hres = create_number_constr(ctx, &ctx->number_constr);
532     if(FAILED(hres))
533         return hres;
534
535     hres = create_object_constr(ctx, &ctx->object_constr);
536     if(FAILED(hres))
537         return hres;
538
539     hres = create_regexp_constr(ctx, &ctx->regexp_constr);
540     if(FAILED(hres))
541         return hres;
542
543     hres = create_string_constr(ctx, &ctx->string_constr);
544     if(FAILED(hres))
545         return hres;
546
547     return S_OK;
548 }
549
550 HRESULT init_global(script_ctx_t *ctx)
551 {
552     DispatchEx *math;
553     VARIANT var;
554     HRESULT hres;
555
556     if(ctx->global)
557         return S_OK;
558
559     hres = init_constructors(ctx);
560     if(FAILED(hres))
561         return hres;
562
563     hres = create_dispex(ctx, &JSGlobal_info, NULL, &ctx->global);
564     if(FAILED(hres))
565         return hres;
566
567     hres = create_math(ctx, &math);
568     if(FAILED(hres))
569         return hres;
570
571     V_VT(&var) = VT_DISPATCH;
572     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(math);
573     hres = jsdisp_propput_name(ctx->global, MathW, ctx->lcid, &var, NULL/*FIXME*/, NULL/*FIXME*/);
574     jsdisp_release(math);
575
576     return hres;
577 }