From 0e5933f70a5f8ab127cce5ac6cbc4b23ebd84af8 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Fri, 30 Nov 2012 13:03:07 +0100 Subject: [PATCH] jscript: Added support for indexed string access. --- dlls/jscript/string.c | 33 ++++++++++++++++++++++++++++++++- dlls/jscript/tests/api.js | 3 +++ dlls/jscript/tests/lang.js | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c index c7aaf6481d8..ce5d7022362 100644 --- a/dlls/jscript/string.c +++ b/dlls/jscript/string.c @@ -1396,6 +1396,35 @@ static void String_destructor(jsdisp_t *dispex) heap_free(This); } +static unsigned String_idx_length(jsdisp_t *jsdisp) +{ + StringInstance *string = (StringInstance*)jsdisp; + + /* + * NOTE: For invoke version < 2, indexed array is not implemented at all. + * Newer jscript.dll versions implement it on string type, not class, + * which is not how it should work according to spec. IE9 implements it + * properly, but it uses its own JavaScript engine inside MSHTML. We + * implement it here, but in the way IE9 and spec work. + */ + return string->dispex.ctx->version < 2 ? 0 : jsstr_length(string->str); +} + +static HRESULT String_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r) +{ + StringInstance *string = (StringInstance*)jsdisp; + jsstr_t *ret; + + TRACE("%p[%u] = %s\n", string, idx, debugstr_wn(string->str->str+idx, 1)); + + ret = jsstr_alloc_len(string->str->str+idx, 1); + if(!ret) + return E_OUTOFMEMORY; + + *r = jsval_string(ret); + return S_OK; +} + static const builtin_prop_t String_props[] = { {anchorW, String_anchor, PROPF_METHOD|1}, {bigW, String_big, PROPF_METHOD}, @@ -1451,7 +1480,9 @@ static const builtin_info_t StringInst_info = { sizeof(StringInst_props)/sizeof(*StringInst_props), StringInst_props, String_destructor, - NULL + NULL, + String_idx_length, + String_idx_get }; /* ECMA-262 3rd Edition 15.5.3.2 */ diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index ce2f15334e0..5f5d009ee8e 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -1785,6 +1785,9 @@ function callTest(argc) { ok(arguments.length === argc+1, "arguments.length = " + arguments.length + " expected " + (argc+1)); for(var i=1; i <= argc; i++) ok(arguments[i] === i, "arguments[i] = " + arguments[i]); + var a = arguments; + for(var i=1; i <= argc; i++) + ok(a[i] === i, "a[i] = " + a[i]); } callTest.call(tmp, 1, 1); diff --git a/dlls/jscript/tests/lang.js b/dlls/jscript/tests/lang.js index 92292a42993..5d1bbfa18ca 100644 --- a/dlls/jscript/tests/lang.js +++ b/dlls/jscript/tests/lang.js @@ -1199,6 +1199,20 @@ try { }catch(e) {} ok(!("prop" in obj), "prop in obj"); +if(invokeVersion >= 2) { + ok("test"[0] === "t", '"test"[0] = ' + test[0]); + ok("test"[5] === undefined, '"test"[0] = ' + test[0]); + + tmp = "test"; + ok(tmp[1] === "e", "tmp[1] = " + tmp[1]); + tmp[1] = "x"; + ok(tmp[1] === "e", "tmp[1] = " + tmp[1]); + ok(tmp["1"] === "e", "tmp['1'] = " + tmp["1"]); + ok(tmp["0x1"] === undefined, "tmp['0x1'] = " + tmp["0x1"]); +}else { + ok("test"[0] === undefined, '"test"[0] = ' + test[0]); +} + ok(isNaN(NaN) === true, "isNaN(NaN) !== true"); ok(isNaN(0.5) === false, "isNaN(0.5) !== false"); ok(isNaN(Infinity) === false, "isNaN(Infinity) !== false");