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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
32 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
33 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
34 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
35 static const WCHAR anchorW
[] = {'a','n','c','h','o','r',0};
36 static const WCHAR bigW
[] = {'b','i','g',0};
37 static const WCHAR blinkW
[] = {'b','l','i','n','k',0};
38 static const WCHAR boldW
[] = {'b','o','l','d',0};
39 static const WCHAR charAtW
[] = {'c','h','a','r','A','t',0};
40 static const WCHAR charCodeAtW
[] = {'c','h','a','r','C','o','d','e','A','t',0};
41 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
42 static const WCHAR fixedW
[] = {'f','i','x','e','d',0};
43 static const WCHAR fontcolorW
[] = {'f','o','n','t','c','o','l','o','r',0};
44 static const WCHAR fontsizeW
[] = {'f','o','n','t','s','i','z','e',0};
45 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
46 static const WCHAR italicsW
[] = {'i','t','a','l','i','c','s',0};
47 static const WCHAR lastIndexOfW
[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
48 static const WCHAR linkW
[] = {'l','i','n','k',0};
49 static const WCHAR matchW
[] = {'m','a','t','c','h',0};
50 static const WCHAR replaceW
[] = {'r','e','p','l','a','c','e',0};
51 static const WCHAR searchW
[] = {'s','e','a','r','c','h',0};
52 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
53 static const WCHAR smallW
[] = {'s','m','a','l','l',0};
54 static const WCHAR splitW
[] = {'s','p','l','i','t',0};
55 static const WCHAR strikeW
[] = {'s','t','r','i','k','e',0};
56 static const WCHAR subW
[] = {'s','u','b',0};
57 static const WCHAR substringW
[] = {'s','u','b','s','t','r','i','n','g',0};
58 static const WCHAR substrW
[] = {'s','u','b','s','t','r',0};
59 static const WCHAR supW
[] = {'s','u','p',0};
60 static const WCHAR toLowerCaseW
[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
61 static const WCHAR toUpperCaseW
[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
62 static const WCHAR toLocaleLowerCaseW
[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
63 static const WCHAR toLocaleUpperCaseW
[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
64 static const WCHAR localeCompareW
[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
65 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
66 static const WCHAR propertyIsEnumerableW
[] =
67 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
68 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
70 static HRESULT
String_length(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
71 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
73 TRACE("%p\n", dispex
);
76 case DISPATCH_PROPERTYGET
: {
77 StringInstance
*jsthis
= (StringInstance
*)dispex
;
80 V_I4(retv
) = jsthis
->length
;
84 FIXME("unimplemented flags %x\n", flags
);
91 static HRESULT
String_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
92 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
98 static HRESULT
String_valueOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
99 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
105 static HRESULT
String_anchor(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
106 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
112 static HRESULT
String_big(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
113 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
119 static HRESULT
String_blink(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
120 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
126 static HRESULT
String_bold(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
127 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
133 /* ECMA-262 3rd Edition 15.5.4.5 */
134 static HRESULT
String_charAt(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
135 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
137 StringInstance
*strobj
;
144 if(dispex
->builtin_info
->class != JSCLASS_STRING
) {
145 FIXME("not string this not supported\n");
149 strobj
= (StringInstance
*)dispex
;
154 hres
= to_integer(dispex
->ctx
, get_arg(dp
, 0), ei
, &num
);
158 if(V_VT(&num
) == VT_I4
) {
161 WARN("pos = %lf\n", V_R8(&num
));
169 if(0 <= pos
&& pos
< strobj
->length
)
170 str
= SysAllocStringLen(strobj
->str
+pos
, 1);
172 str
= SysAllocStringLen(NULL
, 0);
174 return E_OUTOFMEMORY
;
176 V_VT(retv
) = VT_BSTR
;
181 static HRESULT
String_charCodeAt(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
182 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
188 static HRESULT
String_concat(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
189 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
195 static HRESULT
String_fixed(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
196 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
202 static HRESULT
String_fontcolor(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
203 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
209 static HRESULT
String_fontsize(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
210 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
216 static HRESULT
String_indexOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
217 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
223 static HRESULT
String_italics(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
224 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
230 static HRESULT
String_lastIndexOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
231 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
237 static HRESULT
String_link(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
238 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
244 /* ECMA-262 3rd Edition 15.5.4.10 */
245 static HRESULT
String_match(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
246 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
248 StringInstance
*This
= (StringInstance
*)dispex
;
249 match_result_t
*match_result
;
251 VARIANT var
, *arg_var
;
257 if(dp
->cArgs
- dp
->cNamedArgs
!= 1) {
258 FIXME("unsupported args\n");
262 arg_var
= get_arg(dp
, 0);
263 switch(V_VT(arg_var
)) {
267 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
269 if(regexp
->builtin_info
->class == JSCLASS_REGEXP
) {
270 hres
= regexp_match(regexp
, This
->str
, This
->length
, FALSE
, &match_result
, &match_cnt
);
271 jsdisp_release(regexp
);
276 jsdisp_release(regexp
);
280 FIXME("implemented only for regexp args\n");
288 V_VT(retv
) = VT_NULL
;
292 hres
= create_array(dispex
->ctx
, match_cnt
, &array
);
296 V_VT(&var
) = VT_BSTR
;
298 for(i
=0; i
< match_cnt
; i
++) {
299 V_BSTR(&var
) = SysAllocStringLen(match_result
[i
].str
, match_result
[i
].len
);
301 hres
= E_OUTOFMEMORY
;
305 hres
= jsdisp_propput_idx(array
, i
, lcid
, &var
, ei
, NULL
/*FIXME*/);
306 SysFreeString(V_BSTR(&var
));
312 jsdisp_release(array
);
316 V_VT(retv
) = VT_DISPATCH
;
317 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(array
);
321 static HRESULT
String_replace(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
322 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
328 static HRESULT
String_search(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
329 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
335 static HRESULT
String_slice(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
336 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
342 static HRESULT
String_small(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
343 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
349 static HRESULT
String_split(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
350 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
356 static HRESULT
String_strike(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
357 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
363 static HRESULT
String_sub(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
364 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
370 static HRESULT
String_substring(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
371 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
377 static HRESULT
String_substr(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
378 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
384 static HRESULT
String_sup(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
385 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
391 static HRESULT
String_toLowerCase(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
392 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
398 static HRESULT
String_toUpperCase(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
399 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
405 static HRESULT
String_toLocaleLowerCase(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
406 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
412 static HRESULT
String_toLocaleUpperCase(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
413 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
419 static HRESULT
String_localeCompare(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
420 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
426 static HRESULT
String_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
427 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
433 static HRESULT
String_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
434 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
440 static HRESULT
String_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
441 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
447 static HRESULT
String_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
448 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
454 static void String_destructor(DispatchEx
*dispex
)
456 StringInstance
*This
= (StringInstance
*)dispex
;
458 heap_free(This
->str
);
462 static const builtin_prop_t String_props
[] = {
463 {anchorW
, String_anchor
, PROPF_METHOD
},
464 {bigW
, String_big
, PROPF_METHOD
},
465 {blinkW
, String_blink
, PROPF_METHOD
},
466 {boldW
, String_bold
, PROPF_METHOD
},
467 {charAtW
, String_charAt
, PROPF_METHOD
},
468 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
},
469 {concatW
, String_concat
, PROPF_METHOD
},
470 {fixedW
, String_fixed
, PROPF_METHOD
},
471 {fontcolorW
, String_fontcolor
, PROPF_METHOD
},
472 {fontsizeW
, String_fontsize
, PROPF_METHOD
},
473 {hasOwnPropertyW
, String_hasOwnProperty
, PROPF_METHOD
},
474 {indexOfW
, String_indexOf
, PROPF_METHOD
},
475 {isPrototypeOfW
, String_isPrototypeOf
, PROPF_METHOD
},
476 {italicsW
, String_italics
, PROPF_METHOD
},
477 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
},
478 {lengthW
, String_length
, 0},
479 {linkW
, String_link
, PROPF_METHOD
},
480 {localeCompareW
, String_localeCompare
, PROPF_METHOD
},
481 {matchW
, String_match
, PROPF_METHOD
},
482 {propertyIsEnumerableW
, String_propertyIsEnumerable
, PROPF_METHOD
},
483 {replaceW
, String_replace
, PROPF_METHOD
},
484 {searchW
, String_search
, PROPF_METHOD
},
485 {sliceW
, String_slice
, PROPF_METHOD
},
486 {smallW
, String_small
, PROPF_METHOD
},
487 {splitW
, String_split
, PROPF_METHOD
},
488 {strikeW
, String_strike
, PROPF_METHOD
},
489 {substringW
, String_substring
, PROPF_METHOD
},
490 {substrW
, String_substr
, PROPF_METHOD
},
491 {subW
, String_sub
, PROPF_METHOD
},
492 {supW
, String_sup
, PROPF_METHOD
},
493 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
494 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
495 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
496 {toStringW
, String_toString
, PROPF_METHOD
},
497 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
498 {valueOfW
, String_valueOf
, PROPF_METHOD
}
501 static const builtin_info_t String_info
= {
503 {NULL
, String_value
, 0},
504 sizeof(String_props
)/sizeof(*String_props
),
510 static HRESULT
StringConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
511 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
517 static HRESULT
string_alloc(script_ctx_t
*ctx
, BOOL use_constr
, StringInstance
**ret
)
519 StringInstance
*string
;
522 string
= heap_alloc_zero(sizeof(StringInstance
));
524 return E_OUTOFMEMORY
;
527 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &String_info
, ctx
->string_constr
);
529 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, NULL
);
539 HRESULT
create_string_constr(script_ctx_t
*ctx
, DispatchEx
**ret
)
541 StringInstance
*string
;
544 hres
= string_alloc(ctx
, FALSE
, &string
);
548 hres
= create_builtin_function(ctx
, StringConstr_value
, PROPF_CONSTR
, &string
->dispex
, ret
);
550 jsdisp_release(&string
->dispex
);
554 HRESULT
create_string(script_ctx_t
*ctx
, const WCHAR
*str
, DWORD len
, DispatchEx
**ret
)
556 StringInstance
*string
;
559 hres
= string_alloc(ctx
, TRUE
, &string
);
566 string
->length
= len
;
567 string
->str
= heap_alloc((len
+1)*sizeof(WCHAR
));
569 jsdisp_release(&string
->dispex
);
570 return E_OUTOFMEMORY
;
573 memcpy(string
->str
, str
, len
*sizeof(WCHAR
));
574 string
->str
[len
] = 0;
576 *ret
= &string
->dispex
;