diff --git a/dlls/jscript/enumerator.c b/dlls/jscript/enumerator.c index d0502f6afae..5c06efd1f9b 100644 --- a/dlls/jscript/enumerator.c +++ b/dlls/jscript/enumerator.c @@ -26,6 +26,11 @@ WINE_DEFAULT_DEBUG_CHANNEL(jscript); typedef struct { jsdisp_t dispex; + /* IEnumVARIANT returned by _NewEnum */ + IEnumVARIANT *enumvar; + /* current item */ + jsval_t item; + BOOL atend; } EnumeratorInstance; static const WCHAR atEndW[] = {'a','t','E','n','d',0}; @@ -33,43 +38,142 @@ static const WCHAR itemW[] = {'i','t','e','m',0}; static const WCHAR moveFirstW[] = {'m','o','v','e','F','i','r','s','t',0}; static const WCHAR moveNextW[] = {'m','o','v','e','N','e','x','t',0}; +static inline EnumeratorInstance *enumerator_from_jsdisp(jsdisp_t *jsdisp) +{ + return CONTAINING_RECORD(jsdisp, EnumeratorInstance, dispex); +} + +static inline EnumeratorInstance *enumerator_from_vdisp(vdisp_t *vdisp) +{ + return enumerator_from_jsdisp(vdisp->u.jsdisp); +} + +static inline EnumeratorInstance *enumerator_this(vdisp_t *jsthis) +{ + return is_vclass(jsthis, JSCLASS_ENUMERATOR) ? enumerator_from_vdisp(jsthis) : NULL; +} + +static inline HRESULT enumvar_get_next_item(EnumeratorInstance *This) +{ + HRESULT hres; + VARIANT nextitem; + + if (This->atend) + return S_OK; + + /* dont leak pervious value */ + jsval_release(This->item); + + /* not at end ... get next item */ + VariantInit(&nextitem); + hres = IEnumVARIANT_Next(This->enumvar, 1, &nextitem, NULL); + if (hres == S_OK) + { + hres = variant_to_jsval(&nextitem, &This->item); + VariantClear(&nextitem); + if (FAILED(hres)) + { + WARN("failed to convert jsval to variant!"); + This->item = jsval_undefined(); + return hres; + } + } + else + { + This->item = jsval_undefined(); + This->atend = TRUE; + } + + return S_OK; +} + static void Enumerator_destructor(jsdisp_t *dispex) { - TRACE("Enumerator_destructor\n"); + EnumeratorInstance *This = enumerator_from_jsdisp(dispex); + TRACE("\n"); + + jsval_release(This->item); heap_free(dispex); } static HRESULT Enumerator_atEnd(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { - TRACE("Enumerator_atEnd\n"); + EnumeratorInstance *This; - return E_NOTIMPL; + if (!(This = enumerator_this(jsthis))) + return throw_type_error(ctx, JS_E_ENUMERATOR_EXPECTED, NULL); + + TRACE("%d\n", This->atend); + + if (r) + *r = jsval_bool(This->atend); + return S_OK; } static HRESULT Enumerator_item(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { - TRACE("Enumerator_item\n"); + EnumeratorInstance *This; - return E_NOTIMPL; + TRACE("\n"); + + if (!(This = enumerator_this(jsthis))) + return throw_type_error(ctx, JS_E_ENUMERATOR_EXPECTED, NULL); + + return r ? jsval_copy(This->item, r) : S_OK; } static HRESULT Enumerator_moveFirst(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { - TRACE("Enumerator_moveFirst\n"); + EnumeratorInstance *This; + HRESULT hres = S_OK; - return E_NOTIMPL; + TRACE("\n"); + + if (!(This = enumerator_this(jsthis))) + return throw_type_error(ctx, JS_E_ENUMERATOR_EXPECTED, NULL); + + if (This->enumvar) + { + hres = IEnumVARIANT_Reset(This->enumvar); + if (FAILED(hres)) + return hres; + + This->atend = FALSE; + hres = enumvar_get_next_item(This); + if(FAILED(hres)) + return hres; + } + + if (r) + *r = jsval_undefined(); + return S_OK; } static HRESULT Enumerator_moveNext(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { - TRACE("Enumerator_moveNext\n"); + EnumeratorInstance *This; + HRESULT hres = S_OK; - return E_NOTIMPL; + TRACE("\n"); + + if (!(This = enumerator_this(jsthis))) + return throw_type_error(ctx, JS_E_ENUMERATOR_EXPECTED, NULL); + + if (This->enumvar) + { + hres = enumvar_get_next_item(This); + if (FAILED(hres)) + return hres; + } + + if (r) + *r = jsval_undefined(); + return S_OK; } static const builtin_prop_t Enumerator_props[] = { @@ -80,7 +184,7 @@ static const builtin_prop_t Enumerator_props[] = { }; static const builtin_info_t Enumerator_info = { - JSCLASS_OBJECT, + JSCLASS_ENUMERATOR, {NULL, NULL, 0}, ARRAY_SIZE(Enumerator_props), Enumerator_props, @@ -89,7 +193,7 @@ static const builtin_info_t Enumerator_info = { }; static const builtin_info_t EnumeratorInst_info = { - JSCLASS_OBJECT, + JSCLASS_ENUMERATOR, {NULL, NULL, 0, NULL}, 0, NULL, @@ -97,34 +201,6 @@ static const builtin_info_t EnumeratorInst_info = { NULL }; -static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, - jsval_t *r) -{ - jsdisp_t *obj; - HRESULT hres; - - TRACE("EnumeratorConstr_value\n"); - - switch(flags) { - case DISPATCH_CONSTRUCT: { - if (argc != 1) - return throw_syntax_error(ctx, JS_E_MISSING_ARG, NULL); - - hres = create_enumerator(ctx, &argv[0], &obj); - if(FAILED(hres)) - return hres; - - *r = jsval_obj(obj); - break; - } - default: - FIXME("unimplemented flags: %x\n", flags); - return E_NOTIMPL; - } - - return S_OK; -} - static HRESULT alloc_enumerator(script_ctx_t *ctx, jsdisp_t *object_prototype, EnumeratorInstance **ret) { EnumeratorInstance *enumerator; @@ -140,7 +216,8 @@ static HRESULT alloc_enumerator(script_ctx_t *ctx, jsdisp_t *object_prototype, E hres = init_dispex_from_constr(&enumerator->dispex, ctx, &EnumeratorInst_info, ctx->enumerator_constr); - if(FAILED(hres)) { + if(FAILED(hres)) + { heap_free(enumerator); return hres; } @@ -149,6 +226,100 @@ static HRESULT alloc_enumerator(script_ctx_t *ctx, jsdisp_t *object_prototype, E return S_OK; } +static HRESULT create_enumerator(script_ctx_t *ctx, jsval_t *argv, jsdisp_t **ret) +{ + EnumeratorInstance *enumerator; + HRESULT hres; + IDispatch *obj; + DISPPARAMS dispparams = {NULL, NULL, 0, 0}; + IEnumVARIANT *enumvar = NULL; + + if (argv) + { + VARIANT varresult; + + if (!is_object_instance(*argv)) + { + FIXME("I don't know how to handle this type!\n"); + return E_NOTIMPL; + } + + obj = get_object(*argv); + + /* Try to get a IEnumVARIANT by _NewEnum */ + VariantInit(&varresult); + hres = IDispatch_Invoke(obj, DISPID_NEWENUM, &IID_NULL, LOCALE_NEUTRAL, + DISPATCH_METHOD, &dispparams, &varresult, NULL, NULL); + if (FAILED(hres)) + { + WARN("Enumerator: no DISPID_NEWENUM.\n"); + return E_INVALIDARG; + } + + if ((V_VT(&varresult) == VT_DISPATCH) || (V_VT(&varresult) == VT_UNKNOWN)) + { + hres = IUnknown_QueryInterface(V_UNKNOWN(&varresult), + &IID_IEnumVARIANT, (void**)&enumvar); + } + else + { + FIXME("Enumerator: NewEnum unexpected type of varresult (%d).\n", V_VT(&varresult)); + hres = E_INVALIDARG; + } + VariantClear(&varresult); + if (FAILED(hres)) + return hres; + } + + hres = alloc_enumerator(ctx, NULL, &enumerator); + if (FAILED(hres)) + { + if (enumvar) + IEnumVARIANT_Release(enumvar); + return hres; + } + + enumerator->enumvar = enumvar; + enumerator->atend = !enumvar; + hres = enumvar_get_next_item(enumerator); + if (FAILED(hres)) + { + jsdisp_release(&enumerator->dispex); + return hres; + } + + *ret = &enumerator->dispex; + return S_OK; +} + +static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, + jsval_t *r) +{ + jsdisp_t *obj; + HRESULT hres; + + TRACE("\n"); + + switch(flags) { + case DISPATCH_CONSTRUCT: { + if (argc > 1) + return throw_syntax_error(ctx, JS_E_INVALIDARG, NULL); + + hres = create_enumerator(ctx, (argc == 1) ? &argv[0] : 0, &obj); + if(FAILED(hres)) + return hres; + + *r = jsval_obj(obj); + break; + } + default: + FIXME("unimplemented flags: %x\n", flags); + return E_NOTIMPL; + } + + return S_OK; +} + static const builtin_info_t EnumeratorConstr_info = { JSCLASS_FUNCTION, DEFAULT_FUNCTION_VALUE, @@ -175,16 +346,3 @@ HRESULT create_enumerator_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, return hres; } - -HRESULT create_enumerator(script_ctx_t *ctx, jsval_t *argv, jsdisp_t **ret) -{ - EnumeratorInstance *enumerator; - HRESULT hres; - - hres = alloc_enumerator(ctx, NULL, &enumerator); - if(FAILED(hres)) - return hres; - - *ret = &enumerator->dispex; - return S_OK; -} diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h index baebcc4b6cb..96996e84069 100644 --- a/dlls/jscript/jscript.h +++ b/dlls/jscript/jscript.h @@ -116,6 +116,7 @@ typedef enum { JSCLASS_ARRAY, JSCLASS_BOOLEAN, JSCLASS_DATE, + JSCLASS_ENUMERATOR, JSCLASS_ERROR, JSCLASS_FUNCTION, JSCLASS_GLOBAL, @@ -326,7 +327,6 @@ HRESULT create_bool(script_ctx_t*,BOOL,jsdisp_t**) DECLSPEC_HIDDEN; HRESULT create_number(script_ctx_t*,double,jsdisp_t**) DECLSPEC_HIDDEN; HRESULT create_vbarray(script_ctx_t*,SAFEARRAY*,jsdisp_t**) DECLSPEC_HIDDEN; HRESULT create_json(script_ctx_t*,jsdisp_t**) DECLSPEC_HIDDEN; -HRESULT create_enumerator(script_ctx_t *ctx, jsval_t *argv, jsdisp_t **ret) DECLSPEC_HIDDEN; typedef enum { NO_HINT, @@ -561,6 +561,7 @@ static inline DWORD make_grfdex(script_ctx_t *ctx, DWORD flags) #define JS_E_VBARRAY_EXPECTED MAKE_JSERROR(IDS_NOT_VBARRAY) #define JS_E_INVALID_DELETE MAKE_JSERROR(IDS_INVALID_DELETE) #define JS_E_JSCRIPT_EXPECTED MAKE_JSERROR(IDS_JSCRIPT_EXPECTED) +#define JS_E_ENUMERATOR_EXPECTED MAKE_JSERROR(IDS_NOT_ENUMERATOR) #define JS_E_REGEXP_SYNTAX MAKE_JSERROR(IDS_REGEXP_SYNTAX_ERROR) #define JS_E_INVALID_URI_CODING MAKE_JSERROR(IDS_URI_INVALID_CODING) #define JS_E_INVALID_URI_CHAR MAKE_JSERROR(IDS_URI_INVALID_CHAR) diff --git a/dlls/jscript/object.c b/dlls/jscript/object.c index 19d67b514fe..3fa93c7d66f 100644 --- a/dlls/jscript/object.c +++ b/dlls/jscript/object.c @@ -69,7 +69,7 @@ static HRESULT Object_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, u static const WCHAR regexpW[] = {'R','e','g','E','x','p',0}; static const WCHAR stringW[] = {'S','t','r','i','n','g',0}; /* Keep in sync with jsclass_t enum */ - static const WCHAR *names[] = {NULL, arrayW, booleanW, dateW, errorW, + static const WCHAR *names[] = {NULL, arrayW, booleanW, dateW, objectW, errorW, functionW, NULL, mathW, numberW, objectW, regexpW, stringW, objectW, objectW, objectW}; TRACE("\n"); diff --git a/dlls/jscript/resource.h b/dlls/jscript/resource.h index d15dd7a0339..d171ce20f8a 100644 --- a/dlls/jscript/resource.h +++ b/dlls/jscript/resource.h @@ -56,6 +56,7 @@ #define IDS_INVALID_DELETE 0x1394 #define IDS_NOT_VBARRAY 0x1395 #define IDS_JSCRIPT_EXPECTED 0x1396 +#define IDS_NOT_ENUMERATOR 0x1397 #define IDS_REGEXP_SYNTAX_ERROR 0x1399 #define IDS_URI_INVALID_CHAR 0x13A0 #define IDS_URI_INVALID_CODING 0x13A1 diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index f3d07f89f48..40d64229345 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -58,8 +58,8 @@ testNoEnumerables("Function"); testNoEnumerables("Function.prototype"); testNoEnumerables("testNoEnumerates"); testNoEnumerables("VBArray"); -testNoEnumerables("new Enumerator([])"); -testNoEnumerables("Enumerator([])"); +testNoEnumerables("new Enumerator()"); +testNoEnumerables("Enumerator()"); ok(Object.propertyIsEnumerable("prototype") === false, "Object.prototype is enumerable"); ok(Math.propertyIsEnumerable("E") === false, "Math.E is enumerable"); @@ -343,7 +343,7 @@ ok(tmp === "[object Object]", "toString.call(this) = " + tmp); ok(tmp === "[object Object]", "toString.call(arguments) = " + tmp); tmp = Object.prototype.toString.call(new VBArray(createArray())); ok(tmp === "[object Object]", "toString.call(new VBArray()) = " + tmp); -(tmp = new Enumerator([])).f = Object.prototype.toString; +(tmp = new Enumerator()).f = Object.prototype.toString; ok(tmp.f() === "[object Object]", "tmp.f() = " + tmp.f()); function TSTestConstr() {}