[PATCH 1/3] jscript: implement Enumerator()

Andreas Maier staubim at quantentunnel.de
Wed Apr 24 15:38:00 CDT 2019


Signed-off-by: Andreas Maier <staubim at quantentunnel.de>
---
 dlls/jscript/enumerator.c | 292 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 280 insertions(+), 12 deletions(-)

diff --git a/dlls/jscript/enumerator.c b/dlls/jscript/enumerator.c
index aa7737ac52..9d47b70d42 100644
--- a/dlls/jscript/enumerator.c
+++ b/dlls/jscript/enumerator.c
@@ -17,6 +17,7 @@
  */

 #include <assert.h>
+#include <sal.h>

 #include "jscript.h"

@@ -24,8 +25,23 @@

 WINE_DEFAULT_DEBUG_CHANNEL(jscript);

+#define DATATYPE_NULL            0
+#define DATATYPE_ARRAY           1
+#define DATATYPE_ENUMVARIANT     2
+
 typedef struct {
     jsdisp_t dispex;
+    int datatype;
+    BOOL atend;
+    /* constructor with jsarray e.g. ["A","B"] */
+    jsdisp_t *array;
+    /* current index of jsarray */
+    int arrayidx;
+    int arraylen;
+    /* IEnumVARIANT returned by _NewEnum */
+    IEnumVARIANT *enumvar;
+    /* IEnumVARIANT current item */
+    VARIANT* enumitm;
 } EnumeratorInstance;

 static const WCHAR atEndW[] = {'a','t','E','n','d',0};
@@ -33,43 +49,197 @@ 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 enumary_get_item(
+    _In_ EnumeratorInstance *This,
+    _Out_ jsval_t *r)
+{
+    HRESULT hres;
+
+    if (!r)
+        return S_OK;
+
+    if (This->atend)
+    {
+        *r = jsval_undefined();
+        return S_OK;
+    }
+
+    hres = jsdisp_get_idx(This->array, This->arrayidx, r);
+    if (FAILED(hres))
+        *r = jsval_undefined();
+
+    return S_OK;
+}
+
+static inline HRESULT enumvar_get_next_item(
+    _In_ IEnumVARIANT* enumvar,
+    _Inout_ VARIANT* enumitm,
+    _Inout_ BOOL* atend,
+    _Out_opt_ 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;
+
+    This = enumerator_from_vdisp(jsthis);
+
+    if (r)
+        *r = jsval_bool(This->atend);

-    return E_NOTIMPL;
+    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->datatype == DATATYPE_ARRAY)
+    {
+        hres = enumary_get_item(This, r);
+    }
+    else if (This->datatype == DATATYPE_ENUMVARIANT)
+    {
+        hres = variant_to_jsval(This->enumitm, r);
+    }
+    else if (This->datatype == DATATYPE_NULL)
+    {
+        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->datatype == DATATYPE_ARRAY)
+    {
+        This->arrayidx = 0;
+        This->atend = FALSE;
+        hres = enumary_get_item(This, r);
+    }
+    else if (This->datatype == DATATYPE_ENUMVARIANT)
+    {
+        IEnumVARIANT_Reset(This->enumvar);
+        hres = enumvar_get_next_item(This->enumvar, This->enumitm, &This->atend, r);
+    }
+    else if (This->datatype == DATATYPE_NULL)
+    {
+        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->datatype == DATATYPE_ARRAY)
+    {
+        if (This->arrayidx <= This->arraylen)
+            This->arrayidx++;
+        This->atend = (This->arrayidx > This->arraylen);
+        hres = enumary_get_item(This, r);
+    }
+    else if (This->datatype == DATATYPE_ENUMVARIANT)
+    {
+        hres = enumvar_get_next_item(This->enumvar, This->enumitm, &This->atend, r);
+    }
+    else if (This->datatype == DATATYPE_NULL)
+    {
+        if (r)
+            *r = jsval_undefined();
+        hres = S_OK;
+    }
+
+    return hres;
 }

 static const builtin_prop_t Enumerator_props[] = {
@@ -101,16 +271,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 +290,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 +312,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 +353,106 @@ HRESULT create_enumerator(script_ctx_t *ctx, jsval_t *argv, jsdisp_t **ret)
 {
     EnumeratorInstance *enumerator;
     HRESULT hres;
+    BOOL atend;
+    IDispatch *obj;
+    DISPPARAMS dispparams = {NULL, NULL, 0, 0};
+    VARIANT varresult;
+
+    int datatype = -1;
+    int arrayidx = 0;
+    int arraylen = 0;
+    jsdisp_t *array = NULL;
+    IEnumVARIANT *enumvar = NULL;
+    VARIANT *enumitm = NULL;
+
+    /* new Enumerator() */
+    if (argv == NULL)
+    {
+        datatype = DATATYPE_NULL;
+        atend = TRUE;
+    }
+    else if(is_object_instance(*argv))
+    {
+        obj = get_object(*argv);
+        array = iface_to_jsdisp(obj);
+        if ((array) &&
+            (is_class(array, JSCLASS_ARRAY)))
+        {
+            datatype = DATATYPE_ARRAY;
+            arraylen = array_get_length(array);
+            /* atend is here always FALSE
+             * MS returns a "undefined"-element after
+             * the last element of the given array.
+             * Because of this, there is at least one
+             * element to return. */
+            atend = FALSE;
+        }
+        else
+        {
+            if (array)
+                jsdisp_release(array);
+
+            datatype = DATATYPE_ENUMVARIANT;
+            /* Try to get a IEnumVARIANT by _NewEnum */
+            memset(&varresult, 0, sizeof(VARIANT));
+            VariantInit(&varresult);
+            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");
+                VariantClear(&varresult);
+                return E_INVALIDARG;
+            }
+            if (V_VT(&varresult) != VT_DISPATCH)
+            {
+                ERR("Enumerator: NewEnum unexpected result.\n");
+                VariantClear(&varresult);
+                return E_INVALIDARG;
+            }
+            enumvar = (IEnumVARIANT*)V_DISPATCH(&varresult);
+            VariantClear(&varresult);
+
+            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->datatype = datatype;
+    enumerator->atend = atend;
+    enumerator->array = array;
+    enumerator->arraylen = arraylen;
+    enumerator->arrayidx = arrayidx;
+    enumerator->enumvar = enumvar;
+    enumerator->enumitm = enumitm;

     *ret = &enumerator->dispex;
-    return S_OK;
+
+   return S_OK;
+cleanuperr:
+    if (enumitm)
+    {
+        VariantClear(enumitm);
+        HeapFree(GetProcessHeap(), 0, enumitm);
+    }
+    return hres;
 }
--
2.11.0




More information about the wine-devel mailing list