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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
34 jsval_t last_index_val
;
37 static const WCHAR sourceW
[] = {'s','o','u','r','c','e',0};
38 static const WCHAR globalW
[] = {'g','l','o','b','a','l',0};
39 static const WCHAR ignoreCaseW
[] = {'i','g','n','o','r','e','C','a','s','e',0};
40 static const WCHAR multilineW
[] = {'m','u','l','t','i','l','i','n','e',0};
41 static const WCHAR lastIndexW
[] = {'l','a','s','t','I','n','d','e','x',0};
42 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
43 static const WCHAR execW
[] = {'e','x','e','c',0};
44 static const WCHAR testW
[] = {'t','e','s','t',0};
46 static const WCHAR leftContextW
[] =
47 {'l','e','f','t','C','o','n','t','e','x','t',0};
48 static const WCHAR rightContextW
[] =
49 {'r','i','g','h','t','C','o','n','t','e','x','t',0};
51 static const WCHAR idx1W
[] = {'$','1',0};
52 static const WCHAR idx2W
[] = {'$','2',0};
53 static const WCHAR idx3W
[] = {'$','3',0};
54 static const WCHAR idx4W
[] = {'$','4',0};
55 static const WCHAR idx5W
[] = {'$','5',0};
56 static const WCHAR idx6W
[] = {'$','6',0};
57 static const WCHAR idx7W
[] = {'$','7',0};
58 static const WCHAR idx8W
[] = {'$','8',0};
59 static const WCHAR idx9W
[] = {'$','9',0};
61 static inline RegExpInstance
*regexp_from_jsdisp(jsdisp_t
*jsdisp
)
63 return CONTAINING_RECORD(jsdisp
, RegExpInstance
, dispex
);
66 static inline RegExpInstance
*regexp_from_vdisp(vdisp_t
*vdisp
)
68 return regexp_from_jsdisp(vdisp
->u
.jsdisp
);
71 static void set_last_index(RegExpInstance
*This
, DWORD last_index
)
73 This
->last_index
= last_index
;
74 jsval_release(This
->last_index_val
);
75 This
->last_index_val
= jsval_number(last_index
);
78 static HRESULT
do_regexp_match_next(script_ctx_t
*ctx
, RegExpInstance
*regexp
,
79 DWORD rem_flags
, jsstr_t
*jsstr
, const WCHAR
*str
, match_state_t
*ret
)
83 hres
= regexp_execute(regexp
->jsregexp
, ctx
, &ctx
->tmp_heap
,
84 str
, jsstr_length(jsstr
), ret
);
88 if(rem_flags
& REM_RESET_INDEX
)
89 set_last_index(regexp
, 0);
93 if(!(rem_flags
& REM_NO_CTX_UPDATE
) && ctx
->last_match
!= jsstr
) {
94 jsstr_release(ctx
->last_match
);
95 ctx
->last_match
= jsstr_addref(jsstr
);
98 if(!(rem_flags
& REM_NO_CTX_UPDATE
)) {
99 DWORD i
, n
= min(ARRAY_SIZE(ctx
->match_parens
), ret
->paren_count
);
101 for(i
=0; i
< n
; i
++) {
102 if(ret
->parens
[i
].index
== -1) {
103 ctx
->match_parens
[i
].index
= 0;
104 ctx
->match_parens
[i
].length
= 0;
106 ctx
->match_parens
[i
].index
= ret
->parens
[i
].index
;
107 ctx
->match_parens
[i
].length
= ret
->parens
[i
].length
;
111 if(n
< ARRAY_SIZE(ctx
->match_parens
))
112 memset(ctx
->match_parens
+n
, 0, sizeof(ctx
->match_parens
) - n
*sizeof(ctx
->match_parens
[0]));
115 set_last_index(regexp
, ret
->cp
-str
);
117 if(!(rem_flags
& REM_NO_CTX_UPDATE
)) {
118 ctx
->last_match_index
= ret
->cp
-str
-ret
->match_len
;
119 ctx
->last_match_length
= ret
->match_len
;
125 HRESULT
regexp_match_next(script_ctx_t
*ctx
, jsdisp_t
*dispex
,
126 DWORD rem_flags
, jsstr_t
*jsstr
, match_state_t
**ret
)
128 RegExpInstance
*regexp
= regexp_from_jsdisp(dispex
);
129 match_state_t
*match
;
134 if((rem_flags
& REM_CHECK_GLOBAL
) && !(regexp
->jsregexp
->flags
& REG_GLOB
)) {
135 if(rem_flags
& REM_ALLOC_RESULT
)
140 str
= jsstr_flatten(jsstr
);
142 return E_OUTOFMEMORY
;
144 if(rem_flags
& REM_ALLOC_RESULT
) {
145 match
= alloc_match_state(regexp
->jsregexp
, NULL
, str
);
147 return E_OUTOFMEMORY
;
151 mark
= heap_pool_mark(&ctx
->tmp_heap
);
153 if(rem_flags
& REM_NO_PARENS
) {
154 match
= alloc_match_state(regexp
->jsregexp
, &ctx
->tmp_heap
, NULL
);
156 heap_pool_clear(mark
);
157 return E_OUTOFMEMORY
;
159 match
->cp
= (*ret
)->cp
;
160 match
->match_len
= (*ret
)->match_len
;
165 hres
= do_regexp_match_next(ctx
, regexp
, rem_flags
, jsstr
, str
, match
);
167 if(rem_flags
& REM_NO_PARENS
) {
168 (*ret
)->cp
= match
->cp
;
169 (*ret
)->match_len
= match
->match_len
;
172 heap_pool_clear(mark
);
174 if(hres
!= S_OK
&& (rem_flags
& REM_ALLOC_RESULT
)) {
182 static HRESULT
regexp_match(script_ctx_t
*ctx
, jsdisp_t
*dispex
, jsstr_t
*jsstr
, BOOL gflag
,
183 match_result_t
**match_result
, DWORD
*result_cnt
)
185 RegExpInstance
*This
= regexp_from_jsdisp(dispex
);
186 match_result_t
*ret
= NULL
;
187 match_state_t
*result
;
188 DWORD i
=0, ret_size
= 0;
193 mark
= heap_pool_mark(&ctx
->tmp_heap
);
195 str
= jsstr_flatten(jsstr
);
197 return E_OUTOFMEMORY
;
199 result
= alloc_match_state(This
->jsregexp
, &ctx
->tmp_heap
, str
);
201 heap_pool_clear(mark
);
202 return E_OUTOFMEMORY
;
206 hres
= do_regexp_match_next(ctx
, This
, 0, jsstr
, str
, result
);
207 if(hres
== S_FALSE
) {
217 match_result_t
*old_ret
= ret
;
219 ret
= heap_realloc(old_ret
, (ret_size
<<= 1) * sizeof(match_result_t
));
223 ret
= heap_alloc((ret_size
=4) * sizeof(match_result_t
));
226 hres
= E_OUTOFMEMORY
;
231 ret
[i
].index
= result
->cp
- str
- result
->match_len
;
232 ret
[i
++].length
= result
->match_len
;
234 if(!gflag
&& !(This
->jsregexp
->flags
& REG_GLOB
)) {
240 heap_pool_clear(mark
);
251 static HRESULT
RegExp_get_source(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
255 *r
= jsval_string(jsstr_addref(regexp_from_jsdisp(jsthis
)->str
));
259 static HRESULT
RegExp_get_global(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
263 *r
= jsval_bool(!!(regexp_from_jsdisp(jsthis
)->jsregexp
->flags
& REG_GLOB
));
267 static HRESULT
RegExp_get_ignoreCase(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
271 *r
= jsval_bool(!!(regexp_from_jsdisp(jsthis
)->jsregexp
->flags
& REG_FOLD
));
275 static HRESULT
RegExp_get_multiline(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
279 *r
= jsval_bool(!!(regexp_from_jsdisp(jsthis
)->jsregexp
->flags
& REG_MULTILINE
));
283 static INT
index_from_val(script_ctx_t
*ctx
, jsval_t v
)
288 hres
= to_number(ctx
, v
, &n
);
293 return is_int32(n
) ? n
: 0;
296 static HRESULT
RegExp_get_lastIndex(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
298 RegExpInstance
*regexp
= regexp_from_jsdisp(jsthis
);
302 return jsval_copy(regexp
->last_index_val
, r
);
305 static HRESULT
RegExp_set_lastIndex(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
307 RegExpInstance
*regexp
= regexp_from_jsdisp(jsthis
);
312 jsval_release(regexp
->last_index_val
);
313 hres
= jsval_copy(value
, ®exp
->last_index_val
);
317 regexp
->last_index
= index_from_val(ctx
, value
);
321 static HRESULT
RegExp_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
324 RegExpInstance
*regexp
;
331 if(!is_vclass(jsthis
, JSCLASS_REGEXP
)) {
332 WARN("Not a RegExp\n");
333 return JS_E_REGEXP_EXPECTED
;
336 regexp
= regexp_from_vdisp(jsthis
);
341 len
= jsstr_length(regexp
->str
) + 2;
343 f
= regexp
->jsregexp
->flags
;
348 if(f
& REG_MULTILINE
)
351 ret
= jsstr_alloc_buf(len
, &ptr
);
353 return E_OUTOFMEMORY
;
356 ptr
+= jsstr_flush(regexp
->str
, ptr
);
363 if(f
& REG_MULTILINE
)
366 *r
= jsval_string(ret
);
370 static HRESULT
create_match_array(script_ctx_t
*ctx
, jsstr_t
*input_str
,
371 const match_state_t
*result
, IDispatch
**ret
)
379 static const WCHAR indexW
[] = {'i','n','d','e','x',0};
380 static const WCHAR inputW
[] = {'i','n','p','u','t',0};
381 static const WCHAR lastIndexW
[] = {'l','a','s','t','I','n','d','e','x',0};
382 static const WCHAR zeroW
[] = {'0',0};
384 input
= jsstr_flatten(input_str
);
386 return E_OUTOFMEMORY
;
388 hres
= create_array(ctx
, result
->paren_count
+1, &array
);
392 for(i
=0; i
< result
->paren_count
; i
++) {
393 if(result
->parens
[i
].index
!= -1)
394 str
= jsstr_substr(input_str
, result
->parens
[i
].index
, result
->parens
[i
].length
);
398 hres
= E_OUTOFMEMORY
;
402 hres
= jsdisp_propput_idx(array
, i
+1, jsval_string(str
));
408 while(SUCCEEDED(hres
)) {
409 hres
= jsdisp_propput_name(array
, indexW
, jsval_number(result
->cp
-input
-result
->match_len
));
413 hres
= jsdisp_propput_name(array
, lastIndexW
, jsval_number(result
->cp
-input
));
417 hres
= jsdisp_propput_name(array
, inputW
, jsval_string(jsstr_addref(input_str
)));
421 str
= jsstr_alloc_len(result
->cp
-result
->match_len
, result
->match_len
);
423 hres
= E_OUTOFMEMORY
;
426 hres
= jsdisp_propput_name(array
, zeroW
, jsval_string(str
));
432 jsdisp_release(array
);
436 *ret
= to_disp(array
);
440 static HRESULT
run_exec(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsval_t arg
,
441 jsstr_t
**input
, match_state_t
**result
, BOOL
*ret
)
443 RegExpInstance
*regexp
;
444 match_state_t
*match
;
445 DWORD last_index
= 0;
450 if(!is_vclass(jsthis
, JSCLASS_REGEXP
)) {
451 FIXME("Not a RegExp\n");
455 regexp
= regexp_from_vdisp(jsthis
);
457 hres
= to_flat_string(ctx
, arg
, &jsstr
, &string
);
461 if(regexp
->jsregexp
->flags
& REG_GLOB
) {
462 if(regexp
->last_index
< 0) {
463 jsstr_release(jsstr
);
464 set_last_index(regexp
, 0);
467 *input
= jsstr_empty();
471 last_index
= regexp
->last_index
;
474 match
= alloc_match_state(regexp
->jsregexp
, &ctx
->tmp_heap
, string
+last_index
);
476 jsstr_release(jsstr
);
477 return E_OUTOFMEMORY
;
480 hres
= regexp_match_next(ctx
, ®exp
->dispex
, REM_RESET_INDEX
, jsstr
, &match
);
482 jsstr_release(jsstr
);
491 jsstr_release(jsstr
);
495 static HRESULT
RegExp_exec(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
498 match_state_t
*match
;
506 mark
= heap_pool_mark(&ctx
->tmp_heap
);
508 hres
= run_exec(ctx
, jsthis
, argc
? argv
[0] : jsval_string(jsstr_empty()), &string
, &match
, &b
);
510 heap_pool_clear(mark
);
518 hres
= create_match_array(ctx
, string
, match
, &ret
);
520 *r
= jsval_disp(ret
);
526 heap_pool_clear(mark
);
527 jsstr_release(string
);
531 static HRESULT
RegExp_test(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
534 match_state_t
*match
;
542 mark
= heap_pool_mark(&ctx
->tmp_heap
);
543 hres
= run_exec(ctx
, jsthis
, argc
? argv
[0] : jsval_string(undef_str
= jsstr_undefined()), NULL
, &match
, &b
);
544 heap_pool_clear(mark
);
546 jsstr_release(undef_str
);
555 static HRESULT
RegExp_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
562 return JS_E_FUNCTION_EXPECTED
;
564 FIXME("unimplemented flags %x\n", flags
);
571 static void RegExp_destructor(jsdisp_t
*dispex
)
573 RegExpInstance
*This
= regexp_from_jsdisp(dispex
);
576 regexp_destroy(This
->jsregexp
);
577 jsval_release(This
->last_index_val
);
578 jsstr_release(This
->str
);
582 static const builtin_prop_t RegExp_props
[] = {
583 {execW
, RegExp_exec
, PROPF_METHOD
|1},
584 {globalW
, NULL
,0, RegExp_get_global
},
585 {ignoreCaseW
, NULL
,0, RegExp_get_ignoreCase
},
586 {lastIndexW
, NULL
,0, RegExp_get_lastIndex
, RegExp_set_lastIndex
},
587 {multilineW
, NULL
,0, RegExp_get_multiline
},
588 {sourceW
, NULL
,0, RegExp_get_source
},
589 {testW
, RegExp_test
, PROPF_METHOD
|1},
590 {toStringW
, RegExp_toString
, PROPF_METHOD
}
593 static const builtin_info_t RegExp_info
= {
595 {NULL
, RegExp_value
, 0},
596 ARRAY_SIZE(RegExp_props
),
602 static const builtin_prop_t RegExpInst_props
[] = {
603 {globalW
, NULL
,0, RegExp_get_global
},
604 {ignoreCaseW
, NULL
,0, RegExp_get_ignoreCase
},
605 {lastIndexW
, NULL
,0, RegExp_get_lastIndex
, RegExp_set_lastIndex
},
606 {multilineW
, NULL
,0, RegExp_get_multiline
},
607 {sourceW
, NULL
,0, RegExp_get_source
}
610 static const builtin_info_t RegExpInst_info
= {
612 {NULL
, RegExp_value
, 0},
613 ARRAY_SIZE(RegExpInst_props
),
619 static HRESULT
alloc_regexp(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, RegExpInstance
**ret
)
621 RegExpInstance
*regexp
;
624 regexp
= heap_alloc_zero(sizeof(RegExpInstance
));
626 return E_OUTOFMEMORY
;
629 hres
= init_dispex(®exp
->dispex
, ctx
, &RegExp_info
, object_prototype
);
631 hres
= init_dispex_from_constr(®exp
->dispex
, ctx
, &RegExpInst_info
, ctx
->regexp_constr
);
642 HRESULT
create_regexp(script_ctx_t
*ctx
, jsstr_t
*src
, DWORD flags
, jsdisp_t
**ret
)
644 RegExpInstance
*regexp
;
648 str
= jsstr_flatten(src
);
650 return E_OUTOFMEMORY
;
652 TRACE("%s %x\n", debugstr_wn(str
, jsstr_length(src
)), flags
);
654 hres
= alloc_regexp(ctx
, NULL
, ®exp
);
658 regexp
->str
= jsstr_addref(src
);
659 regexp
->last_index_val
= jsval_number(0);
661 regexp
->jsregexp
= regexp_new(ctx
, &ctx
->tmp_heap
, str
, jsstr_length(regexp
->str
), flags
, FALSE
);
662 if(!regexp
->jsregexp
) {
663 WARN("regexp_new failed\n");
664 jsdisp_release(®exp
->dispex
);
668 *ret
= ®exp
->dispex
;
672 HRESULT
create_regexp_var(script_ctx_t
*ctx
, jsval_t src_arg
, jsval_t
*flags_arg
, jsdisp_t
**ret
)
675 const WCHAR
*opt
= NULL
;
679 if(is_object_instance(src_arg
)) {
682 obj
= iface_to_jsdisp(get_object(src_arg
));
684 if(is_class(obj
, JSCLASS_REGEXP
)) {
685 RegExpInstance
*regexp
= regexp_from_jsdisp(obj
);
687 hres
= create_regexp(ctx
, regexp
->str
, regexp
->jsregexp
->flags
, ret
);
696 if(is_undefined(src_arg
))
699 hres
= to_string(ctx
, src_arg
, &src
);
703 if(flags_arg
&& !is_undefined(*flags_arg
)) {
706 hres
= to_string(ctx
, *flags_arg
, &opt_str
);
707 if(SUCCEEDED(hres
)) {
708 opt
= jsstr_flatten(opt_str
);
710 hres
= parse_regexp_flags(opt
, jsstr_length(opt_str
), &flags
);
712 hres
= E_OUTOFMEMORY
;
713 jsstr_release(opt_str
);
718 hres
= create_regexp(ctx
, src
, flags
, ret
);
723 HRESULT
regexp_string_match(script_ctx_t
*ctx
, jsdisp_t
*re
, jsstr_t
*jsstr
, jsval_t
*r
)
725 static const WCHAR indexW
[] = {'i','n','d','e','x',0};
726 static const WCHAR inputW
[] = {'i','n','p','u','t',0};
727 static const WCHAR lastIndexW
[] = {'l','a','s','t','I','n','d','e','x',0};
729 RegExpInstance
*regexp
= regexp_from_jsdisp(re
);
730 match_result_t
*match_result
;
731 unsigned match_cnt
, i
;
736 str
= jsstr_flatten(jsstr
);
738 return E_OUTOFMEMORY
;
740 if(!(regexp
->jsregexp
->flags
& REG_GLOB
)) {
741 match_state_t
*match
;
744 mark
= heap_pool_mark(&ctx
->tmp_heap
);
745 match
= alloc_match_state(regexp
->jsregexp
, &ctx
->tmp_heap
, str
);
747 heap_pool_clear(mark
);
748 return E_OUTOFMEMORY
;
751 hres
= regexp_match_next(ctx
, ®exp
->dispex
, 0, jsstr
, &match
);
753 heap_pool_clear(mark
);
761 hres
= create_match_array(ctx
, jsstr
, match
, &ret
);
763 *r
= jsval_disp(ret
);
769 heap_pool_clear(mark
);
773 hres
= regexp_match(ctx
, ®exp
->dispex
, jsstr
, FALSE
, &match_result
, &match_cnt
);
785 hres
= create_array(ctx
, match_cnt
, &array
);
789 for(i
=0; i
< match_cnt
; i
++) {
792 tmp_str
= jsstr_substr(jsstr
, match_result
[i
].index
, match_result
[i
].length
);
794 hres
= E_OUTOFMEMORY
;
798 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
799 jsstr_release(tmp_str
);
804 while(SUCCEEDED(hres
)) {
805 hres
= jsdisp_propput_name(array
, indexW
, jsval_number(match_result
[match_cnt
-1].index
));
809 hres
= jsdisp_propput_name(array
, lastIndexW
,
810 jsval_number(match_result
[match_cnt
-1].index
+ match_result
[match_cnt
-1].length
));
814 hres
= jsdisp_propput_name(array
, inputW
, jsval_string(jsstr
));
818 heap_free(match_result
);
820 if(SUCCEEDED(hres
) && r
)
821 *r
= jsval_obj(array
);
823 jsdisp_release(array
);
827 static HRESULT
global_idx(script_ctx_t
*ctx
, DWORD idx
, jsval_t
*r
)
831 ret
= jsstr_substr(ctx
->last_match
, ctx
->match_parens
[idx
].index
, ctx
->match_parens
[idx
].length
);
833 return E_OUTOFMEMORY
;
835 *r
= jsval_string(ret
);
839 static HRESULT
RegExpConstr_get_idx1(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
842 return global_idx(ctx
, 0, r
);
845 static HRESULT
RegExpConstr_get_idx2(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
848 return global_idx(ctx
, 1, r
);
851 static HRESULT
RegExpConstr_get_idx3(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
854 return global_idx(ctx
, 2, r
);
857 static HRESULT
RegExpConstr_get_idx4(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
860 return global_idx(ctx
, 3, r
);
863 static HRESULT
RegExpConstr_get_idx5(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
866 return global_idx(ctx
, 4, r
);
869 static HRESULT
RegExpConstr_get_idx6(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
872 return global_idx(ctx
, 5, r
);
875 static HRESULT
RegExpConstr_get_idx7(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
878 return global_idx(ctx
, 6, r
);
881 static HRESULT
RegExpConstr_get_idx8(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
884 return global_idx(ctx
, 7, r
);
887 static HRESULT
RegExpConstr_get_idx9(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
890 return global_idx(ctx
, 8, r
);
893 static HRESULT
RegExpConstr_get_leftContext(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
899 ret
= jsstr_substr(ctx
->last_match
, 0, ctx
->last_match_index
);
901 return E_OUTOFMEMORY
;
903 *r
= jsval_string(ret
);
907 static HRESULT
RegExpConstr_get_rightContext(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
913 ret
= jsstr_substr(ctx
->last_match
, ctx
->last_match_index
+ctx
->last_match_length
,
914 jsstr_length(ctx
->last_match
) - ctx
->last_match_index
- ctx
->last_match_length
);
916 return E_OUTOFMEMORY
;
918 *r
= jsval_string(ret
);
922 static HRESULT
RegExpConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
928 case DISPATCH_METHOD
:
930 if(is_object_instance(argv
[0])) {
931 jsdisp_t
*jsdisp
= iface_to_jsdisp(get_object(argv
[0]));
933 if(is_class(jsdisp
, JSCLASS_REGEXP
)) {
934 if(argc
> 1 && !is_undefined(argv
[1])) {
935 jsdisp_release(jsdisp
);
936 return JS_E_REGEXP_SYNTAX
;
940 *r
= jsval_obj(jsdisp
);
942 jsdisp_release(jsdisp
);
945 jsdisp_release(jsdisp
);
950 case DISPATCH_CONSTRUCT
: {
954 hres
= create_regexp_var(ctx
, argc
? argv
[0] : jsval_undefined(), argc
> 1 ? argv
+1 : NULL
, &ret
);
965 FIXME("unimplemented flags: %x\n", flags
);
972 static const builtin_prop_t RegExpConstr_props
[] = {
973 {idx1W
, NULL
,0, RegExpConstr_get_idx1
, builtin_set_const
},
974 {idx2W
, NULL
,0, RegExpConstr_get_idx2
, builtin_set_const
},
975 {idx3W
, NULL
,0, RegExpConstr_get_idx3
, builtin_set_const
},
976 {idx4W
, NULL
,0, RegExpConstr_get_idx4
, builtin_set_const
},
977 {idx5W
, NULL
,0, RegExpConstr_get_idx5
, builtin_set_const
},
978 {idx6W
, NULL
,0, RegExpConstr_get_idx6
, builtin_set_const
},
979 {idx7W
, NULL
,0, RegExpConstr_get_idx7
, builtin_set_const
},
980 {idx8W
, NULL
,0, RegExpConstr_get_idx8
, builtin_set_const
},
981 {idx9W
, NULL
,0, RegExpConstr_get_idx9
, builtin_set_const
},
982 {leftContextW
, NULL
,0, RegExpConstr_get_leftContext
, builtin_set_const
},
983 {rightContextW
, NULL
,0, RegExpConstr_get_rightContext
, builtin_set_const
}
986 static const builtin_info_t RegExpConstr_info
= {
988 DEFAULT_FUNCTION_VALUE
,
989 ARRAY_SIZE(RegExpConstr_props
),
995 HRESULT
create_regexp_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
997 RegExpInstance
*regexp
;
1000 static const WCHAR RegExpW
[] = {'R','e','g','E','x','p',0};
1002 hres
= alloc_regexp(ctx
, object_prototype
, ®exp
);
1006 hres
= create_builtin_constructor(ctx
, RegExpConstr_value
, RegExpW
, &RegExpConstr_info
,
1007 PROPF_CONSTR
|2, ®exp
->dispex
, ret
);
1009 jsdisp_release(®exp
->dispex
);
1013 HRESULT
parse_regexp_flags(const WCHAR
*str
, DWORD str_len
, DWORD
*ret
)
1018 for (p
= str
; p
< str
+str_len
; p
++) {
1027 flags
|= REG_MULTILINE
;
1030 flags
|= REG_STICKY
;
1033 WARN("wrong flag %c\n", *p
);