Jacek Caban : jscript: Added String.slice implementation.
Alexandre Julliard
julliard at winehq.org
Mon Sep 22 07:04:19 CDT 2008
Module: wine
Branch: master
Commit: 2c6847d000655cc94ecb7bc729d6752748921af5
URL: http://source.winehq.org/git/wine.git/?a=commit;h=2c6847d000655cc94ecb7bc729d6752748921af5
Author: Jacek Caban <jacek at codeweavers.com>
Date: Sun Sep 21 15:42:05 2008 +0200
jscript: Added String.slice implementation.
---
dlls/jscript/string.c | 75 +++++++++++++++++++++++++++++++++++++++++++-
dlls/jscript/tests/api.js | 17 ++++++++++
2 files changed, 90 insertions(+), 2 deletions(-)
diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c
index 60829ab..e690477 100644
--- a/dlls/jscript/string.c
+++ b/dlls/jscript/string.c
@@ -353,11 +353,82 @@ static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARA
return E_NOTIMPL;
}
+/* ECMA-262 3rd Edition 15.5.4.13 */
static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
- FIXME("\n");
- return E_NOTIMPL;
+ const WCHAR *str;
+ DWORD length;
+ INT start=0, end;
+ VARIANT v;
+ HRESULT hres;
+
+ TRACE("\n");
+
+ if(is_class(dispex, JSCLASS_STRING)) {
+ StringInstance *string = (StringInstance*)dispex;
+
+ str = string->str;
+ length = string->length;
+ }else {
+ FIXME("this is not a string class");
+ return E_NOTIMPL;
+ }
+
+ if(arg_cnt(dp)) {
+ hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
+ if(FAILED(hres))
+ return hres;
+
+ if(V_VT(&v) == VT_I4) {
+ start = V_I4(&v);
+ if(start < 0) {
+ start = length + start;
+ if(start < 0)
+ start = 0;
+ }else if(start > length) {
+ start = length;
+ }
+ }else {
+ start = V_R8(&v) < 0.0 ? 0 : length;
+ }
+ }else {
+ start = 0;
+ }
+
+ if(arg_cnt(dp) >= 2) {
+ hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
+ if(FAILED(hres))
+ return hres;
+
+ if(V_VT(&v) == VT_I4) {
+ end = V_I4(&v);
+ if(end < 0) {
+ end = length + end;
+ if(end < 0)
+ end = 0;
+ }else if(end > length) {
+ end = length;
+ }
+ }else {
+ end = V_R8(&v) < 0.0 ? 0 : length;
+ }
+ }else {
+ end = length;
+ }
+
+ if(end < start)
+ end = start;
+
+ if(retv) {
+ BSTR retstr = SysAllocStringLen(str+start, end-start);
+ if(!str)
+ return E_OUTOFMEMORY;
+
+ V_VT(retv) = VT_BSTR;
+ V_BSTR(retv) = retstr;
+ }
+ return S_OK;
}
static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index 9efe856..b220bdb 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -74,6 +74,23 @@ ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
tmp = "abcd".substring();
ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
+tmp = "abcd".slice(1,3);
+ok(tmp === "bc", "'abcd'.slice(1,3) = " + tmp);
+tmp = "abcd".slice(1,-1);
+ok(tmp === "bc", "'abcd'.slice(1,-1) = " + tmp);
+tmp = "abcd".slice(-3,3);
+ok(tmp === "bc", "'abcd'.slice(-3,3) = " + tmp);
+tmp = "abcd".slice(-6,3);
+ok(tmp === "abc", "'abcd'.slice(-6,3) = " + tmp);
+tmp = "abcd".slice(3,1);
+ok(tmp === "", "'abcd'.slice(3,1) = " + tmp);
+tmp = "abcd".slice(true,3);
+ok(tmp === "bc", "'abcd'.slice(true,3) = " + tmp);
+tmp = "abcd".slice();
+ok(tmp === "abcd", "'abcd'.slice() = " + tmp);
+tmp = "abcd".slice(1);
+ok(tmp === "bcd", "'abcd'.slice(1) = " + tmp);
+
var arr = new Array();
ok(typeof(arr) === "object", "arr () is not object");
ok((arr.length === 0), "arr.length is not 0");
More information about the wine-cvs
mailing list