Jacek Caban : jscript: Added String.charAt implementation.

Alexandre Julliard julliard at winehq.org
Fri Sep 19 07:14:50 CDT 2008


Module: wine
Branch: master
Commit: 4731f174352033a2d2c226afdf94b334b5a4ef22
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=4731f174352033a2d2c226afdf94b334b5a4ef22

Author: Jacek Caban <jacek at codeweavers.com>
Date:   Fri Sep 19 00:43:03 2008 +0200

jscript: Added String.charAt implementation.

---

 dlls/jscript/engine.c     |   11 ----------
 dlls/jscript/jscript.h    |   12 +++++++++++
 dlls/jscript/jsutils.c    |   20 +++++++++++++++++++
 dlls/jscript/string.c     |   46 +++++++++++++++++++++++++++++++++++++++++++-
 dlls/jscript/tests/api.js |   19 ++++++++++++++++++
 5 files changed, 95 insertions(+), 13 deletions(-)

diff --git a/dlls/jscript/engine.c b/dlls/jscript/engine.c
index a526431..0175bc7 100644
--- a/dlls/jscript/engine.c
+++ b/dlls/jscript/engine.c
@@ -288,17 +288,6 @@ static inline DOUBLE num_val(const VARIANT *v)
     return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
 }
 
-static inline void num_set_val(VARIANT *v, DOUBLE d)
-{
-    if(d == (DOUBLE)(INT)d) {
-        V_VT(v) = VT_I4;
-        V_I4(v) = d;
-    }else {
-        V_VT(v) = VT_R8;
-        V_R8(v) = d;
-    }
-}
-
 /* ECMA-262 3rd Edition    11.9.6 */
 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
 {
diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h
index 7c0c07b..776a2a3 100644
--- a/dlls/jscript/jscript.h
+++ b/dlls/jscript/jscript.h
@@ -140,6 +140,7 @@ HRESULT create_number(script_ctx_t*,VARIANT*,DispatchEx**);
 HRESULT to_primitive(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
 HRESULT to_boolean(VARIANT*,VARIANT_BOOL*);
 HRESULT to_number(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
+HRESULT to_integer(script_ctx_t*,VARIANT*,jsexcept_t*,VARIANT*);
 HRESULT to_int32(script_ctx_t*,VARIANT*,jsexcept_t*,INT*);
 HRESULT to_string(script_ctx_t*,VARIANT*,jsexcept_t*,BSTR*);
 HRESULT to_object(exec_ctx_t*,VARIANT*,IDispatch**);
@@ -204,6 +205,17 @@ static inline DWORD arg_cnt(const DISPPARAMS *dp)
     return dp->cArgs - dp->cNamedArgs;
 }
 
+static inline void num_set_val(VARIANT *v, DOUBLE d)
+{
+    if(d == (DOUBLE)(INT)d) {
+        V_VT(v) = VT_I4;
+        V_I4(v) = d;
+    }else {
+        V_VT(v) = VT_R8;
+        V_R8(v) = d;
+    }
+}
+
 const char *debugstr_variant(const VARIANT*);
 
 HRESULT WINAPI JScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
diff --git a/dlls/jscript/jsutils.c b/dlls/jscript/jsutils.c
index 7184cb9..027b077 100644
--- a/dlls/jscript/jsutils.c
+++ b/dlls/jscript/jsutils.c
@@ -16,6 +16,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include <math.h>
+
 #include "jscript.h"
 #include "engine.h"
 
@@ -239,6 +241,24 @@ HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
     return S_OK;
 }
 
+/* ECMA-262 3rd Edition    9.4 */
+HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
+{
+    VARIANT num;
+    HRESULT hres;
+
+    hres = to_number(ctx, v, ei, &num);
+    if(FAILED(hres))
+        return hres;
+
+    if(V_VT(&num) == VT_I4)
+        *ret = *v;
+    else
+        num_set_val(ret, V_R8(&num) >= 0.0 ? floor(V_R8(&num)) : -floor(-V_R8(&num)));
+
+    return S_OK;
+}
+
 /* ECMA-262 3rd Edition    9.5 */
 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
 {
diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c
index 0831532..12140d5 100644
--- a/dlls/jscript/string.c
+++ b/dlls/jscript/string.c
@@ -130,11 +130,52 @@ static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS
     return E_NOTIMPL;
 }
 
+/* ECMA-262 3rd Edition    15.5.4.5 */
 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    StringInstance *strobj;
+    BSTR str;
+    INT pos = 0;
+    HRESULT hres;
+
+    TRACE("\n");
+
+    if(dispex->builtin_info->class != JSCLASS_STRING) {
+        FIXME("not string this not supported\n");
+        return E_NOTIMPL;
+    }
+
+    strobj = (StringInstance*)dispex;
+
+    if(arg_cnt(dp)) {
+        VARIANT num;
+
+        hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
+        if(FAILED(hres))
+            return hres;
+
+        if(V_VT(&num) == VT_I4) {
+            pos = V_I4(&num);
+        }else {
+            WARN("pos = %lf\n", V_R8(&num));
+            pos = -1;
+        }
+    }
+
+    if(!retv)
+        return S_OK;
+
+    if(0 <= pos && pos < strobj->length)
+        str = SysAllocStringLen(strobj->str+pos, 1);
+    else
+        str = SysAllocStringLen(NULL, 0);
+    if(!str)
+        return E_OUTOFMEMORY;
+
+    V_VT(retv) = VT_BSTR;
+    V_BSTR(retv) = str;
+    return S_OK;
 }
 
 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
@@ -200,6 +241,7 @@ static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS
     return E_NOTIMPL;
 }
 
+/* ECMA-262 3rd Edition    15.5.4.10 */
 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
 {
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index d6893e0..45765ed 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -16,11 +16,30 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+var tmp;
+
 ok("".length === 0, "\"\".length = " + "".length);
 ok(getVT("".length) == "VT_I4", "\"\".length = " + "".length);
 ok("abc".length === 3, "\"abc\".length = " + "abc".length);
 ok(String.prototype.length === 0, "String.prototype.length = " + String.prototype.length);
 
+tmp = "abc".charAt(0);
+ok(tmp === "a", "'abc',charAt(0) = " + tmp);
+tmp = "abc".charAt(1);
+ok(tmp === "b", "'abc',charAt(1) = " + tmp);
+tmp = "abc".charAt(2);
+ok(tmp === "c", "'abc',charAt(2) = " + tmp);
+tmp = "abc".charAt(3);
+ok(tmp === "", "'abc',charAt(3) = " + tmp);
+tmp = "abc".charAt(4);
+ok(tmp === "", "'abc',charAt(4) = " + tmp);
+tmp = "abc".charAt();
+ok(tmp === "a", "'abc',charAt() = " + tmp);
+tmp = "abc".charAt(-1);
+ok(tmp === "", "'abc',charAt(-1) = " + tmp);
+tmp = "abc".charAt(0,2);
+ok(tmp === "a", "'abc',charAt(0.2) = " + 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