2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
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};
57 static HRESULT
constructor_call(DispatchEx
*constr
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
58 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
60 if(flags
!= DISPATCH_PROPERTYGET
)
61 return jsdisp_call_value(constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
63 V_VT(retv
) = VT_DISPATCH
;
64 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(constr
);
65 IDispatchEx_AddRef(_IDispatchEx_(constr
));
69 static HRESULT
JSGlobal_NaN(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
70 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
76 static HRESULT
JSGlobal_Infinity(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
77 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
83 static HRESULT
JSGlobal_Array(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
84 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
88 return constructor_call(dispex
->ctx
->array_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
91 static HRESULT
JSGlobal_Boolean(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
92 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
96 return constructor_call(dispex
->ctx
->bool_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
99 static HRESULT
JSGlobal_Date(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
100 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
106 static HRESULT
JSGlobal_Function(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
107 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
111 return constructor_call(dispex
->ctx
->function_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
114 static HRESULT
JSGlobal_Number(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
115 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
119 return constructor_call(dispex
->ctx
->number_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
122 static HRESULT
JSGlobal_Object(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
123 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
127 return constructor_call(dispex
->ctx
->object_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
130 static HRESULT
JSGlobal_String(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
131 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
135 return constructor_call(dispex
->ctx
->string_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
138 static HRESULT
JSGlobal_RegExp(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
139 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
143 return constructor_call(dispex
->ctx
->regexp_constr
, lcid
, flags
, dp
, retv
, ei
, sp
);
146 static HRESULT
JSGlobal_ActiveXObject(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
147 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
153 static HRESULT
JSGlobal_VBArray(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
154 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
160 static HRESULT
JSGlobal_Enumerator(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
161 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
167 static HRESULT
JSGlobal_escape(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
168 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
174 /* ECMA-262 3rd Edition 15.1.2.1 */
175 static HRESULT
JSGlobal_eval(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
176 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
178 parser_ctx_t
*parser_ctx
;
186 V_VT(retv
) = VT_EMPTY
;
190 arg
= get_arg(dp
, 0);
191 if(V_VT(arg
) != VT_BSTR
) {
193 V_VT(retv
) = VT_EMPTY
;
194 return VariantCopy(retv
, arg
);
199 if(!dispex
->ctx
->exec_ctx
) {
200 FIXME("No active exec_ctx\n");
204 TRACE("parsing %s\n", debugstr_w(V_BSTR(arg
)));
205 hres
= script_parse(dispex
->ctx
, V_BSTR(arg
), &parser_ctx
);
207 FIXME("parse failed: %08x\n", hres
);
211 hres
= exec_source(dispex
->ctx
->exec_ctx
, parser_ctx
, parser_ctx
->source
, ei
, retv
);
212 parser_release(parser_ctx
);
217 static HRESULT
JSGlobal_isNaN(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
218 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
224 static HRESULT
JSGlobal_isFinite(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
225 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
231 static HRESULT
JSGlobal_parseInt(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
232 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
237 static HRESULT
JSGlobal_parseFloat(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
238 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
244 static HRESULT
JSGlobal_unescape(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
245 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
251 static HRESULT
JSGlobal_GetObject(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
252 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
258 static HRESULT
JSGlobal_ScriptEngine(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
259 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
265 static HRESULT
JSGlobal_ScriptEngineMajorVersion(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
266 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
272 static HRESULT
JSGlobal_ScriptEngineMinorVersion(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
273 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
279 static HRESULT
JSGlobal_ScriptEngineBuildVersion(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
280 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
286 static HRESULT
JSGlobal_CollectGarbage(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
287 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
293 static const builtin_prop_t JSGlobal_props
[] = {
294 {ActiveXObjectW
, JSGlobal_ActiveXObject
, PROPF_METHOD
},
295 {ArrayW
, JSGlobal_Array
, PROPF_CONSTR
},
296 {BooleanW
, JSGlobal_Boolean
, PROPF_CONSTR
},
297 {CollectGarbageW
, JSGlobal_CollectGarbage
, PROPF_METHOD
},
298 {DateW
, JSGlobal_Date
, PROPF_CONSTR
},
299 {EnumeratorW
, JSGlobal_Enumerator
, PROPF_METHOD
},
300 {FunctionW
, JSGlobal_Function
, PROPF_CONSTR
},
301 {GetObjectW
, JSGlobal_GetObject
, PROPF_METHOD
},
302 {InfinityW
, JSGlobal_Infinity
, 0},
303 /* {MathW, JSGlobal_Math, 0}, */
304 {NaNW
, JSGlobal_NaN
, 0},
305 {NumberW
, JSGlobal_Number
, PROPF_CONSTR
},
306 {ObjectW
, JSGlobal_Object
, PROPF_CONSTR
},
307 {RegExpW
, JSGlobal_RegExp
, PROPF_CONSTR
},
308 {ScriptEngineW
, JSGlobal_ScriptEngine
, PROPF_METHOD
},
309 {ScriptEngineBuildVersionW
, JSGlobal_ScriptEngineBuildVersion
, PROPF_METHOD
},
310 {ScriptEngineMajorVersionW
, JSGlobal_ScriptEngineMajorVersion
, PROPF_METHOD
},
311 {ScriptEngineMinorVersionW
, JSGlobal_ScriptEngineMinorVersion
, PROPF_METHOD
},
312 {StringW
, JSGlobal_String
, PROPF_CONSTR
},
313 {VBArrayW
, JSGlobal_VBArray
, PROPF_METHOD
},
314 {escapeW
, JSGlobal_escape
, PROPF_METHOD
},
315 {evalW
, JSGlobal_eval
, PROPF_METHOD
|1},
316 {isFiniteW
, JSGlobal_isFinite
, PROPF_METHOD
},
317 {isNaNW
, JSGlobal_isNaN
, PROPF_METHOD
},
318 {parseFloatW
, JSGlobal_parseFloat
, PROPF_METHOD
},
319 {parseIntW
, JSGlobal_parseInt
, PROPF_METHOD
|2},
320 {unescapeW
, JSGlobal_unescape
, PROPF_METHOD
}
323 static const builtin_info_t JSGlobal_info
= {
326 sizeof(JSGlobal_props
)/sizeof(*JSGlobal_props
),
332 static HRESULT
init_constructors(script_ctx_t
*ctx
)
336 hres
= init_function_constr(ctx
);
340 hres
= create_array_constr(ctx
, &ctx
->array_constr
);
344 hres
= create_bool_constr(ctx
, &ctx
->bool_constr
);
348 hres
= create_number_constr(ctx
, &ctx
->number_constr
);
352 hres
= create_object_constr(ctx
, &ctx
->object_constr
);
356 hres
= create_object_constr(ctx
, &ctx
->regexp_constr
);
360 hres
= create_string_constr(ctx
, &ctx
->string_constr
);
367 HRESULT
init_global(script_ctx_t
*ctx
)
376 hres
= init_constructors(ctx
);
380 hres
= create_dispex(ctx
, &JSGlobal_info
, NULL
, &ctx
->global
);
384 hres
= create_math(ctx
, &math
);
388 V_VT(&var
) = VT_DISPATCH
;
389 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(math
);
390 hres
= jsdisp_propput_name(ctx
->global
, MathW
, ctx
->lcid
, &var
, NULL
/*FIXME*/, NULL
/*FIXME*/);
391 jsdisp_release(math
);