static HRESULT RegExpConstr_rightContext(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { TRACE("\n"); switch(flags) { case DISPATCH_PROPERTYGET: { jsstr_t *ret; ret = jsstr_substr(ctx->last_match, ctx->last_match_index+ctx->last_match_length, jsstr_length(ctx->last_match) - ctx->last_match_index - ctx->last_match_length); if(!ret) return E_OUTOFMEMORY; *r = jsval_string(ret); break; } case DISPATCH_PROPERTYPUT: break; default: FIXME("unsupported flags\n"); return E_NOTIMPL; } return S_OK; }
/* ECMA-262 3rd Edition 15.5.4.15 */ static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { INT start=0, end, length; jsstr_t *str; double d; HRESULT hres; TRACE("\n"); hres = get_string_val(ctx, jsthis, &str); if(FAILED(hres)) return hres; length = jsstr_length(str); if(argc >= 1) { hres = to_integer(ctx, argv[0], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(d >= 0) start = is_int32(d) ? min(length, d) : length; } if(argc >= 2) { hres = to_integer(ctx, argv[1], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(d >= 0) end = is_int32(d) ? min(length, d) : length; else end = 0; } else { end = length; } if(start > end) { INT tmp = start; start = end; end = tmp; } if(r) { jsstr_t *ret = jsstr_substr(str, start, end-start); if(ret) *r = jsval_string(ret); else hres = E_OUTOFMEMORY; } jsstr_release(str); return hres; }
static HRESULT rep_call(script_ctx_t *ctx, jsdisp_t *func, jsstr_t *jsstr, const WCHAR *str, match_state_t *match, jsstr_t **ret) { jsval_t *argv; unsigned argc; jsval_t val; jsstr_t *tmp_str; DWORD i; HRESULT hres = S_OK; argc = match->paren_count+3; argv = heap_alloc_zero(sizeof(*argv)*argc); if(!argv) return E_OUTOFMEMORY; tmp_str = jsstr_alloc_len(match->cp-match->match_len, match->match_len); if(!tmp_str) hres = E_OUTOFMEMORY; argv[0] = jsval_string(tmp_str); if(SUCCEEDED(hres)) { for(i=0; i < match->paren_count; i++) { if(match->parens[i].index != -1) tmp_str = jsstr_substr(jsstr, match->parens[i].index, match->parens[i].length); else tmp_str = jsstr_empty(); if(!tmp_str) { hres = E_OUTOFMEMORY; break; } argv[i+1] = jsval_string(tmp_str); } } if(SUCCEEDED(hres)) { argv[match->paren_count+1] = jsval_number(match->cp-str - match->match_len); argv[match->paren_count+2] = jsval_string(jsstr); } if(SUCCEEDED(hres)) hres = jsdisp_call_value(func, NULL, DISPATCH_METHOD, argc, argv, &val); for(i=0; i <= match->paren_count; i++) jsstr_release(get_string(argv[i])); heap_free(argv); if(FAILED(hres)) return hres; hres = to_string(ctx, val, ret); jsval_release(val); return hres; }
/* ECMA-262 3rd Edition B.2.3 */ static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { int start=0, len, length; jsstr_t *str; double d; HRESULT hres; TRACE("\n"); hres = get_string_val(ctx, jsthis, &str); if(FAILED(hres)) return hres; length = jsstr_length(str); if(argc >= 1) { hres = to_integer(ctx, argv[0], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(d >= 0) start = is_int32(d) ? min(length, d) : length; } if(argc >= 2) { hres = to_integer(ctx, argv[1], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(d >= 0.0) len = is_int32(d) ? min(length-start, d) : length-start; else len = 0; } else { len = length-start; } hres = S_OK; if(r) { jsstr_t *ret = jsstr_substr(str, start, len); if(ret) *r = jsval_string(ret); else hres = E_OUTOFMEMORY; } jsstr_release(str); return hres; }
static HRESULT String_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r) { StringInstance *string = (StringInstance*)jsdisp; jsstr_t *ret; ret = jsstr_substr(string->str, idx, 1); if(!ret) return E_OUTOFMEMORY; TRACE("%p[%u] = %s\n", string, idx, debugstr_jsstr(ret)); *r = jsval_string(ret); return S_OK; }
/* ECMA-262 3rd Edition 15.5.4.5 */ static HRESULT String_charAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { jsstr_t *str, *ret; INT pos = 0; HRESULT hres; TRACE("\n"); hres = get_string_val(ctx, jsthis, &str); if(FAILED(hres)) return hres; if(argc) { double d; hres = to_integer(ctx, argv[0], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } pos = is_int32(d) ? d : -1; } if(!r) { jsstr_release(str); return S_OK; } if(0 <= pos && pos < jsstr_length(str)) { ret = jsstr_substr(str, pos, 1); if(!ret) return E_OUTOFMEMORY; } else { ret = jsstr_empty(); } *r = jsval_string(ret); return S_OK; }
static HRESULT global_idx(script_ctx_t *ctx, DWORD flags, DWORD idx, jsval_t *r) { switch(flags) { case DISPATCH_PROPERTYGET: { jsstr_t *ret; ret = jsstr_substr(ctx->last_match, ctx->match_parens[idx].index, ctx->match_parens[idx].length); if(!ret) return E_OUTOFMEMORY; *r = jsval_string(ret); break; } case DISPATCH_PROPERTYPUT: break; default: FIXME("unsupported flags\n"); return E_NOTIMPL; } return S_OK; }
HRESULT regexp_string_match(script_ctx_t *ctx, jsdisp_t *re, jsstr_t *str, jsval_t *r) { static const WCHAR indexW[] = {'i','n','d','e','x',0}; static const WCHAR inputW[] = {'i','n','p','u','t',0}; static const WCHAR lastIndexW[] = {'l','a','s','t','I','n','d','e','x',0}; RegExpInstance *regexp = (RegExpInstance*)re; match_result_t *match_result; unsigned match_cnt, i; jsdisp_t *array; HRESULT hres; if(!(regexp->jsregexp->flags & REG_GLOB)) { match_state_t *match; heap_pool_t *mark; mark = heap_pool_mark(&ctx->tmp_heap); match = alloc_match_state(regexp->jsregexp, &ctx->tmp_heap, str->str); if(!match) { heap_pool_clear(mark); return E_OUTOFMEMORY; } hres = regexp_match_next(ctx, ®exp->dispex, 0, str, &match); if(FAILED(hres)) { heap_pool_clear(mark); return hres; } if(r) { if(hres == S_OK) { IDispatch *ret; hres = create_match_array(ctx, str, match, &ret); if(SUCCEEDED(hres)) *r = jsval_disp(ret); }else { *r = jsval_null(); } } heap_pool_clear(mark); return S_OK; } hres = regexp_match(ctx, ®exp->dispex, str, FALSE, &match_result, &match_cnt); if(FAILED(hres)) return hres; if(!match_cnt) { TRACE("no match\n"); if(r) *r = jsval_null(); return S_OK; } hres = create_array(ctx, match_cnt, &array); if(FAILED(hres)) return hres; for(i=0; i < match_cnt; i++) { jsstr_t *tmp_str; tmp_str = jsstr_substr(str, match_result[i].index, match_result[i].length); if(!tmp_str) { hres = E_OUTOFMEMORY; break; } hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str)); jsstr_release(tmp_str); if(FAILED(hres)) break; } while(SUCCEEDED(hres)) { hres = jsdisp_propput_name(array, indexW, jsval_number(match_result[match_cnt-1].index)); if(FAILED(hres)) break; hres = jsdisp_propput_name(array, lastIndexW, jsval_number(match_result[match_cnt-1].index + match_result[match_cnt-1].length)); if(FAILED(hres)) break; hres = jsdisp_propput_name(array, inputW, jsval_string(str)); break; } heap_free(match_result); if(SUCCEEDED(hres) && r) *r = jsval_obj(array); else jsdisp_release(array); return hres; }
static HRESULT create_match_array(script_ctx_t *ctx, jsstr_t *input, const match_state_t *result, IDispatch **ret) { jsdisp_t *array; jsstr_t *str; DWORD i; HRESULT hres = S_OK; static const WCHAR indexW[] = {'i','n','d','e','x',0}; static const WCHAR inputW[] = {'i','n','p','u','t',0}; static const WCHAR lastIndexW[] = {'l','a','s','t','I','n','d','e','x',0}; static const WCHAR zeroW[] = {'0',0}; hres = create_array(ctx, result->paren_count+1, &array); if(FAILED(hres)) return hres; for(i=0; i < result->paren_count; i++) { if(result->parens[i].index != -1) str = jsstr_substr(input, result->parens[i].index, result->parens[i].length); else str = jsstr_empty(); if(!str) { hres = E_OUTOFMEMORY; break; } hres = jsdisp_propput_idx(array, i+1, jsval_string(str)); jsstr_release(str); if(FAILED(hres)) break; } while(SUCCEEDED(hres)) { hres = jsdisp_propput_name(array, indexW, jsval_number(result->cp-input->str-result->match_len)); if(FAILED(hres)) break; hres = jsdisp_propput_name(array, lastIndexW, jsval_number(result->cp-input->str)); if(FAILED(hres)) break; hres = jsdisp_propput_name(array, inputW, jsval_string(jsstr_addref(input))); if(FAILED(hres)) break; str = jsstr_alloc_len(result->cp-result->match_len, result->match_len); if(!str) { hres = E_OUTOFMEMORY; break; } hres = jsdisp_propput_name(array, zeroW, jsval_string(str)); jsstr_release(str); break; } if(FAILED(hres)) { jsdisp_release(array); return hres; } *ret = to_disp(array); return S_OK; }
/* ECMA-262 3rd Edition 15.5.4.13 */ static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { int start=0, end, length; jsstr_t *str; double d; HRESULT hres; TRACE("\n"); hres = get_string_val(ctx, jsthis, &str); if(FAILED(hres)) return hres; length = jsstr_length(str); if(argc) { hres = to_integer(ctx, argv[0], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(is_int32(d)) { start = d; if(start < 0) { start = length + start; if(start < 0) start = 0; } else if(start > length) { start = length; } } else if(d > 0) { start = length; } } if(argc >= 2) { hres = to_integer(ctx, argv[1], &d); if(FAILED(hres)) { jsstr_release(str); return hres; } if(is_int32(d)) { end = d; if(end < 0) { end = length + end; if(end < 0) end = 0; } else if(end > length) { end = length; } } else { end = d < 0.0 ? 0 : length; } } else { end = length; } if(end < start) end = start; if(r) { jsstr_t *retstr = jsstr_substr(str, start, end-start); if(!retstr) { jsstr_release(str); return E_OUTOFMEMORY; } *r = jsval_string(retstr); } jsstr_release(str); return S_OK; }