jscript: Added String.charAt implementation.

This commit is contained in:
Jacek Caban 2008-09-19 00:43:03 +02:00 committed by Alexandre Julliard
parent 536573934a
commit 4731f17435
5 changed files with 95 additions and 13 deletions

View File

@ -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)
{

View File

@ -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**);

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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");