enumerator.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2019 Andreas Maier
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <assert.h>

#include "jscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

typedef struct {
    jsdisp_t dispex;
29 30 31 32 33
    /* IEnumVARIANT returned by _NewEnum */
    IEnumVARIANT *enumvar;
    /* current item */
    jsval_t item;
    BOOL atend;
34 35
} EnumeratorInstance;

36 37 38 39 40
static inline EnumeratorInstance *enumerator_from_jsdisp(jsdisp_t *jsdisp)
{
    return CONTAINING_RECORD(jsdisp, EnumeratorInstance, dispex);
}

41
static inline EnumeratorInstance *enumerator_this(jsval_t vthis)
42
{
43 44
    jsdisp_t *jsdisp = is_object_instance(vthis) ? to_jsdisp(get_object(vthis)) : NULL;
    return (jsdisp && is_class(jsdisp, JSCLASS_ENUMERATOR)) ? enumerator_from_jsdisp(jsdisp) : NULL;
45 46
}

47
static inline HRESULT enumvar_get_next_item(EnumeratorInstance *This, script_ctx_t *ctx)
48 49 50 51 52 53 54
{
    HRESULT hres;
    VARIANT nextitem;

    if (This->atend)
        return S_OK;

55
    /* don't leak previous value */
56 57 58 59 60 61 62
    jsval_release(This->item);

    /* not at end ... get next item */
    VariantInit(&nextitem);
    hres = IEnumVARIANT_Next(This->enumvar, 1, &nextitem, NULL);
    if (hres == S_OK)
    {
63
        hres = variant_to_jsval(ctx, &nextitem, &This->item);
64 65 66
        VariantClear(&nextitem);
        if (FAILED(hres))
        {
67
            WARN("failed to convert jsval to variant!\n");
68 69 70 71 72 73 74 75 76 77 78 79 80
            This->item = jsval_undefined();
            return hres;
        }
    }
    else
    {
        This->item = jsval_undefined();
        This->atend = TRUE;
    }

    return S_OK;
}

81 82
static void Enumerator_destructor(jsdisp_t *dispex)
{
83
    EnumeratorInstance *This = enumerator_from_jsdisp(dispex);
84

85 86 87
    TRACE("\n");

    jsval_release(This->item);
88
    free(dispex);
89 90
}

91
static HRESULT Enumerator_atEnd(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
92 93
        jsval_t *r)
{
94 95
    EnumeratorInstance *This;

96
    if (!(This = enumerator_this(vthis)))
97
        return JS_E_ENUMERATOR_EXPECTED;
98 99

    TRACE("%d\n", This->atend);
100

101 102 103
    if (r)
        *r = jsval_bool(This->atend);
    return S_OK;
104 105
}

106
static HRESULT Enumerator_item(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
107 108
        jsval_t *r)
{
109 110 111
    EnumeratorInstance *This;

    TRACE("\n");
112

113
    if (!(This = enumerator_this(vthis)))
114
        return JS_E_ENUMERATOR_EXPECTED;
115 116

    return r ? jsval_copy(This->item, r) : S_OK;
117 118
}

119
static HRESULT Enumerator_moveFirst(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
120 121
        jsval_t *r)
{
122 123 124 125 126
    EnumeratorInstance *This;
    HRESULT hres = S_OK;

    TRACE("\n");

127
    if (!(This = enumerator_this(vthis)))
128
        return JS_E_ENUMERATOR_EXPECTED;
129 130 131 132 133 134

    if (This->enumvar)
    {
        hres = IEnumVARIANT_Reset(This->enumvar);
        if (FAILED(hres))
            return hres;
135

136
        This->atend = FALSE;
137
        hres = enumvar_get_next_item(This, ctx);
138 139 140 141 142 143 144
        if(FAILED(hres))
            return hres;
    }

    if (r)
        *r = jsval_undefined();
    return S_OK;
145 146
}

147
static HRESULT Enumerator_moveNext(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
148 149
        jsval_t *r)
{
150 151 152 153
    EnumeratorInstance *This;
    HRESULT hres = S_OK;

    TRACE("\n");
154

155
    if (!(This = enumerator_this(vthis)))
156
        return JS_E_ENUMERATOR_EXPECTED;
157 158 159

    if (This->enumvar)
    {
160
        hres = enumvar_get_next_item(This, ctx);
161 162 163 164 165 166 167
        if (FAILED(hres))
            return hres;
    }

    if (r)
        *r = jsval_undefined();
    return S_OK;
168 169 170
}

static const builtin_prop_t Enumerator_props[] = {
171 172 173 174
    {L"atEnd",     Enumerator_atEnd,     PROPF_METHOD},
    {L"item",      Enumerator_item,      PROPF_METHOD},
    {L"moveFirst", Enumerator_moveFirst, PROPF_METHOD},
    {L"moveNext",  Enumerator_moveNext,  PROPF_METHOD},
175 176 177
};

static const builtin_info_t Enumerator_info = {
178
    JSCLASS_ENUMERATOR,
179
    NULL,
180 181 182 183 184 185 186
    ARRAY_SIZE(Enumerator_props),
    Enumerator_props,
    NULL,
    NULL
};

static const builtin_info_t EnumeratorInst_info = {
187
    JSCLASS_ENUMERATOR,
188
    NULL,
189 190 191 192 193 194
    0,
    NULL,
    Enumerator_destructor,
    NULL
};

195 196 197 198 199
static HRESULT alloc_enumerator(script_ctx_t *ctx, jsdisp_t *object_prototype, EnumeratorInstance **ret)
{
    EnumeratorInstance *enumerator;
    HRESULT hres;

200
    enumerator = calloc(1, sizeof(EnumeratorInstance));
201 202 203 204 205 206 207 208 209 210 211
    if(!enumerator)
        return E_OUTOFMEMORY;

    if(object_prototype)
        hres = init_dispex(&enumerator->dispex, ctx, &Enumerator_info, object_prototype);
    else
        hres = init_dispex_from_constr(&enumerator->dispex, ctx, &EnumeratorInst_info,
                                       ctx->enumerator_constr);

    if(FAILED(hres))
    {
212
        free(enumerator);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        return hres;
    }

    *ret = enumerator;
    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;
275
    hres = enumvar_get_next_item(enumerator, ctx);
276 277 278 279 280 281 282 283 284 285
    if (FAILED(hres))
    {
        jsdisp_release(&enumerator->dispex);
        return hres;
    }

    *ret = &enumerator->dispex;
    return S_OK;
}

286
static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
287 288 289 290 291
        jsval_t *r)
{
    jsdisp_t *obj;
    HRESULT hres;

292
    TRACE("\n");
293 294 295

    switch(flags) {
    case DISPATCH_CONSTRUCT: {
296
        if (argc > 1)
297
            return JS_E_INVALIDARG;
298

299
        hres = create_enumerator(ctx, (argc == 1) ? &argv[0] : 0, &obj);
300 301 302
        if(FAILED(hres))
            return hres;

303 304
        if(r) *r = jsval_obj(obj);
        else  jsdisp_release(obj);
305 306 307 308 309 310 311 312 313 314 315 316
        break;
    }
    default:
        FIXME("unimplemented flags: %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
}

static const builtin_info_t EnumeratorConstr_info = {
    JSCLASS_FUNCTION,
317
    Function_value,
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    0,
    NULL,
    NULL,
    NULL
};

HRESULT create_enumerator_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
{
    EnumeratorInstance *enumerator;
    HRESULT hres;

    hres = alloc_enumerator(ctx, object_prototype, &enumerator);
    if(FAILED(hres))
        return hres;

333 334
    hres = create_builtin_constructor(ctx, EnumeratorConstr_value, L"Enumerator",
            &EnumeratorConstr_info, PROPF_CONSTR|7, &enumerator->dispex, ret);
335 336 337 338
    jsdisp_release(&enumerator->dispex);

    return hres;
}