2019-04-18 21:08:40 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2019-05-17 16:26:36 +02:00
|
|
|
/* IEnumVARIANT returned by _NewEnum */
|
|
|
|
IEnumVARIANT *enumvar;
|
|
|
|
/* current item */
|
|
|
|
jsval_t item;
|
|
|
|
BOOL atend;
|
2019-04-18 21:08:40 +02:00
|
|
|
} EnumeratorInstance;
|
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
static inline EnumeratorInstance *enumerator_from_jsdisp(jsdisp_t *jsdisp)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(jsdisp, EnumeratorInstance, dispex);
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static inline EnumeratorInstance *enumerator_this(jsval_t vthis)
|
2019-05-17 16:26:36 +02:00
|
|
|
{
|
2022-03-17 17:25:39 +01:00
|
|
|
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;
|
2019-05-17 16:26:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline HRESULT enumvar_get_next_item(EnumeratorInstance *This)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
VARIANT nextitem;
|
|
|
|
|
|
|
|
if (This->atend)
|
|
|
|
return S_OK;
|
|
|
|
|
2019-08-08 15:19:18 +02:00
|
|
|
/* don't leak previous value */
|
2019-05-17 16:26:36 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-08-08 15:26:12 +02:00
|
|
|
WARN("failed to convert jsval to variant!\n");
|
2019-05-17 16:26:36 +02:00
|
|
|
This->item = jsval_undefined();
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
This->item = jsval_undefined();
|
|
|
|
This->atend = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-18 21:08:40 +02:00
|
|
|
static void Enumerator_destructor(jsdisp_t *dispex)
|
|
|
|
{
|
2019-05-17 16:26:36 +02:00
|
|
|
EnumeratorInstance *This = enumerator_from_jsdisp(dispex);
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
jsval_release(This->item);
|
2019-04-18 21:08:40 +02:00
|
|
|
heap_free(dispex);
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Enumerator_atEnd(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2019-04-18 21:08:40 +02:00
|
|
|
jsval_t *r)
|
|
|
|
{
|
2019-05-17 16:26:36 +02:00
|
|
|
EnumeratorInstance *This;
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
if (!(This = enumerator_this(vthis)))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_ENUMERATOR_EXPECTED;
|
2019-05-17 16:26:36 +02:00
|
|
|
|
|
|
|
TRACE("%d\n", This->atend);
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
if (r)
|
|
|
|
*r = jsval_bool(This->atend);
|
|
|
|
return S_OK;
|
2019-04-18 21:08:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Enumerator_item(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2019-04-18 21:08:40 +02:00
|
|
|
jsval_t *r)
|
|
|
|
{
|
2019-05-17 16:26:36 +02:00
|
|
|
EnumeratorInstance *This;
|
|
|
|
|
|
|
|
TRACE("\n");
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
if (!(This = enumerator_this(vthis)))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_ENUMERATOR_EXPECTED;
|
2019-05-17 16:26:36 +02:00
|
|
|
|
|
|
|
return r ? jsval_copy(This->item, r) : S_OK;
|
2019-04-18 21:08:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Enumerator_moveFirst(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2019-04-18 21:08:40 +02:00
|
|
|
jsval_t *r)
|
|
|
|
{
|
2019-05-17 16:26:36 +02:00
|
|
|
EnumeratorInstance *This;
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
if (!(This = enumerator_this(vthis)))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_ENUMERATOR_EXPECTED;
|
2019-05-17 16:26:36 +02:00
|
|
|
|
|
|
|
if (This->enumvar)
|
|
|
|
{
|
|
|
|
hres = IEnumVARIANT_Reset(This->enumvar);
|
|
|
|
if (FAILED(hres))
|
|
|
|
return hres;
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
This->atend = FALSE;
|
|
|
|
hres = enumvar_get_next_item(This);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
*r = jsval_undefined();
|
|
|
|
return S_OK;
|
2019-04-18 21:08:40 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Enumerator_moveNext(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2019-04-18 21:08:40 +02:00
|
|
|
jsval_t *r)
|
|
|
|
{
|
2019-05-17 16:26:36 +02:00
|
|
|
EnumeratorInstance *This;
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
TRACE("\n");
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
if (!(This = enumerator_this(vthis)))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_ENUMERATOR_EXPECTED;
|
2019-05-17 16:26:36 +02:00
|
|
|
|
|
|
|
if (This->enumvar)
|
|
|
|
{
|
|
|
|
hres = enumvar_get_next_item(This);
|
|
|
|
if (FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
*r = jsval_undefined();
|
|
|
|
return S_OK;
|
2019-04-18 21:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Enumerator_props[] = {
|
2020-11-03 23:34:40 +01:00
|
|
|
{L"atEnd", Enumerator_atEnd, PROPF_METHOD},
|
|
|
|
{L"item", Enumerator_item, PROPF_METHOD},
|
|
|
|
{L"moveFirst", Enumerator_moveFirst, PROPF_METHOD},
|
|
|
|
{L"moveNext", Enumerator_moveNext, PROPF_METHOD},
|
2019-04-18 21:08:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Enumerator_info = {
|
2019-05-17 16:26:36 +02:00
|
|
|
JSCLASS_ENUMERATOR,
|
2021-11-25 21:01:37 +01:00
|
|
|
NULL,
|
2019-04-18 21:08:40 +02:00
|
|
|
ARRAY_SIZE(Enumerator_props),
|
|
|
|
Enumerator_props,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t EnumeratorInst_info = {
|
2019-05-17 16:26:36 +02:00
|
|
|
JSCLASS_ENUMERATOR,
|
2021-11-25 21:01:37 +01:00
|
|
|
NULL,
|
2019-04-18 21:08:40 +02:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
Enumerator_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
static HRESULT alloc_enumerator(script_ctx_t *ctx, jsdisp_t *object_prototype, EnumeratorInstance **ret)
|
|
|
|
{
|
|
|
|
EnumeratorInstance *enumerator;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
enumerator = heap_alloc_zero(sizeof(EnumeratorInstance));
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
heap_free(enumerator);
|
|
|
|
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;
|
|
|
|
hres = enumvar_get_next_item(enumerator);
|
|
|
|
if (FAILED(hres))
|
|
|
|
{
|
|
|
|
jsdisp_release(&enumerator->dispex);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = &enumerator->dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT EnumeratorConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2019-04-18 21:08:40 +02:00
|
|
|
jsval_t *r)
|
|
|
|
{
|
|
|
|
jsdisp_t *obj;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
TRACE("\n");
|
2019-04-18 21:08:40 +02:00
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_CONSTRUCT: {
|
2019-05-17 16:26:36 +02:00
|
|
|
if (argc > 1)
|
2020-01-31 17:23:21 +01:00
|
|
|
return JS_E_INVALIDARG;
|
2019-04-18 21:08:40 +02:00
|
|
|
|
2019-05-17 16:26:36 +02:00
|
|
|
hres = create_enumerator(ctx, (argc == 1) ? &argv[0] : 0, &obj);
|
2019-04-18 21:08:40 +02:00
|
|
|
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,
|
2021-11-25 21:01:37 +01:00
|
|
|
Function_value,
|
2019-04-18 21:08:40 +02:00
|
|
|
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;
|
|
|
|
|
2020-11-24 00:49:04 +01:00
|
|
|
hres = create_builtin_constructor(ctx, EnumeratorConstr_value, L"Enumerator",
|
|
|
|
&EnumeratorConstr_info, PROPF_CONSTR|7, &enumerator->dispex, ret);
|
2019-04-18 21:08:40 +02:00
|
|
|
jsdisp_release(&enumerator->dispex);
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|