Signed-off-by: Andreas Maier staubim@quantentunnel.de --- dlls/jscript/enumerator.c | 234 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 222 insertions(+), 12 deletions(-)
diff --git a/dlls/jscript/enumerator.c b/dlls/jscript/enumerator.c index aa7737ac52..aa80da20af 100644 --- a/dlls/jscript/enumerator.c +++ b/dlls/jscript/enumerator.c @@ -26,6 +26,12 @@ WINE_DEFAULT_DEBUG_CHANNEL(jscript);
typedef struct { jsdisp_t dispex; + BOOL isnull; + BOOL atend; + /* IEnumVARIANT returned by _NewEnum */ + IEnumVARIANT *enumvar; + /* IEnumVARIANT current item */ + VARIANT* enumitm; } EnumeratorInstance;
static const WCHAR atEndW[] = {'a','t','E','n','d',0}; @@ -33,43 +39,156 @@ 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 HRESULT enumvar_get_next_item( + IEnumVARIANT* enumvar, VARIANT* enumitm, + BOOL* atend, jsval_t *r) +{ + HRESULT hres; + + /* not at end ... get next item */ + if (!(*atend)) + { + /* Assume valid variant */ + VariantClear(enumitm); + hres = IEnumVARIANT_Next(enumvar, 1, enumitm, NULL); + if (hres != S_OK) + { + VariantClear(enumitm); + *atend = TRUE; + } + } + + if (*atend) + { + if (r) + *r = jsval_undefined(); + return S_OK; + } + + if (r) + return variant_to_jsval(enumitm, r); + + return S_OK; +} + static void Enumerator_destructor(jsdisp_t *dispex) { + EnumeratorInstance *This; + TRACE("Enumerator_destructor\n");
+ This = enumerator_from_jsdisp(dispex); + + if (This->enumitm) + { + VariantClear(This->enumitm); + heap_free(This->enumitm); + } + heap_free(dispex); }
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; + This = enumerator_from_vdisp(jsthis); + + if (r) + *r = jsval_bool(This->atend); + + TRACE("Enumerator_atEnd %d\n", This->atend); + + return S_OK; }
HRESULT Enumerator_item(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { + EnumeratorInstance *This; + HRESULT hres = E_FAIL; + TRACE("Enumerator_item\n");
- return E_NOTIMPL; + This = enumerator_from_vdisp(jsthis); + + if (!This->isnull) + { + hres = variant_to_jsval(This->enumitm, r); + } + else + { + if (r) + *r = jsval_undefined(); + hres = S_OK; + } + + return hres; }
HRESULT Enumerator_moveFirst(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { + EnumeratorInstance *This; + HRESULT hres = E_FAIL; + TRACE("Enumerator_moveFirst\n");
- return E_NOTIMPL; + This = enumerator_from_vdisp(jsthis); + if (!This->isnull) + { + IEnumVARIANT_Reset(This->enumvar); + hres = enumvar_get_next_item(This->enumvar, This->enumitm, &This->atend, r); + } + else + { + if (r) + *r = jsval_undefined(); + hres = S_OK; + } + + return hres; }
HRESULT Enumerator_moveNext(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { + EnumeratorInstance *This; + HRESULT hres = E_FAIL; + TRACE("Enumerator_moveNext\n");
- return E_NOTIMPL; + This = enumerator_from_vdisp(jsthis); + + if (This->atend) + { + if (r) + *r = jsval_undefined(); + return S_OK; + } + if (!This->isnull) + { + hres = enumvar_get_next_item(This->enumvar, This->enumitm, &This->atend, r); + } + else + { + if (r) + *r = jsval_undefined(); + hres = S_OK; + } + + return hres; }
static const builtin_prop_t Enumerator_props[] = { @@ -101,16 +220,18 @@ static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD fl jsval_t *r) { jsdisp_t *obj; + jsval_t *arg0; HRESULT hres;
TRACE("EnumeratorConstr_value\n");
switch(flags) { case DISPATCH_CONSTRUCT: { - if (argc != 1) - return throw_syntax_error(ctx, JS_E_MISSING_ARG, NULL); + if (argc > 1) + return throw_syntax_error(ctx, JS_E_INVALIDARG, NULL);
- hres = create_enumerator(ctx, &argv[0], &obj); + arg0 = (argc == 1) ? &argv[0] : 0; + hres = create_enumerator(ctx, arg0, &obj); if(FAILED(hres)) return hres;
@@ -118,7 +239,7 @@ static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD fl break; } default: - FIXME("unimplemented flags: %x\n", flags); + ERR("unimplemented flags: %x\n", flags); return E_NOTIMPL; }
@@ -140,7 +261,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; } @@ -180,11 +302,99 @@ 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}; + VARIANT varresult; + + BOOL atend = TRUE; + BOOL isnull = TRUE; + IEnumVARIANT *enumvar = NULL; + VARIANT *enumitm = NULL; + + memset(&varresult, 0, sizeof(VARIANT)); + VariantInit(&varresult); + + /* new Enumerator() */ + if (argv == NULL) + { + isnull = TRUE; + atend = TRUE; + } + else if(is_object_instance(*argv)) + { + obj = get_object(*argv); + + isnull = FALSE; + /* Try to get a IEnumVARIANT by _NewEnum */ + hres = IDispatch_Invoke(obj, + DISPID_NEWENUM, &IID_NULL, LOCALE_NEUTRAL, + DISPATCH_METHOD, &dispparams, &varresult, + NULL, NULL); + if (FAILED(hres)) + { + ERR("Enumerator: no DISPID_NEWENUM.\n"); + hres = E_INVALIDARG; + goto cleanuperr; + } + if (V_VT(&varresult) == VT_DISPATCH) + { + enumvar = (IEnumVARIANT*)V_DISPATCH(&varresult); + VariantClear(&varresult); + } + else if (V_VT(&varresult) == VT_UNKNOWN) + { + hres = IUnknown_QueryInterface(V_UNKNOWN(&varresult), + &IID_IEnumVARIANT, (void**)&enumvar); + if (FAILED(hres)) + { + hres = E_INVALIDARG; + goto cleanuperr; + } + enumvar = (IEnumVARIANT*)V_DISPATCH(&varresult); + VariantClear(&varresult); + } + else + { + ERR("Enumerator: NewEnum unexpected type of varresult (%d).\n", V_VT(&varresult)); + hres = E_INVALIDARG; + goto cleanuperr; + } + + enumitm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VARIANT)); + if (!enumitm) + { + hres = E_OUTOFMEMORY; + goto cleanuperr; + } + VariantInit(enumitm); + + atend = FALSE; + enumvar_get_next_item(enumvar, enumitm, &atend, NULL); + } + else + { + ERR("I don't know how to handle this type!\n"); + hres = E_NOTIMPL; + goto cleanuperr; + }
hres = alloc_enumerator(ctx, NULL, &enumerator); if(FAILED(hres)) - return hres; + goto cleanuperr; + enumerator->isnull = isnull; + enumerator->atend = atend; + enumerator->enumvar = enumvar; + enumerator->enumitm = enumitm;
*ret = &enumerator->dispex; - return S_OK; + + return S_OK; +cleanuperr: + VariantClear(&varresult); + if (enumitm) + { + VariantClear(enumitm); + HeapFree(GetProcessHeap(), 0, enumitm); + } + return hres; } -- 2.11.0