bool.c 5.22 KB
Newer Older
1 2
/*
 * Copyright 2008 Jacek Caban for CodeWeavers
3
 * Copyright 2009 Piotr Caban
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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 "jscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

typedef struct {
27
    jsdisp_t dispex;
28 29

    VARIANT_BOOL val;
30 31 32 33 34
} BoolInstance;

static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};

35 36 37 38 39
static inline BoolInstance *bool_this(vdisp_t *jsthis)
{
    return is_vclass(jsthis, JSCLASS_BOOLEAN) ? (BoolInstance*)jsthis->u.jsdisp : NULL;
}

40
/* ECMA-262 3rd Edition    15.6.4.2 */
41
static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
42 43
        VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
44 45
    BoolInstance *bool;

46 47 48 49 50
    static const WCHAR trueW[] = {'t','r','u','e',0};
    static const WCHAR falseW[] = {'f','a','l','s','e',0};

    TRACE("\n");

51
    if(!(bool = bool_this(jsthis)))
52
        return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    if(retv) {
        BSTR val;

        if(bool->val) val = SysAllocString(trueW);
        else val = SysAllocString(falseW);

        if(!val)
            return E_OUTOFMEMORY;

        V_VT(retv) = VT_BSTR;
        V_BSTR(retv) = val;
    }

    return S_OK;
68 69
}

70
/* ECMA-262 3rd Edition    15.6.4.3 */
71
static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
72 73
        VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
74 75
    BoolInstance *bool;

76 77
    TRACE("\n");

78
    if(!(bool = bool_this(jsthis)))
79
        return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
80 81 82 83 84 85 86

    if(retv) {
        V_VT(retv) = VT_BOOL;
        V_BOOL(retv) = bool->val;
    }

    return S_OK;
87 88
}

89
static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
90 91
        VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
92 93 94 95
    TRACE("\n");

    switch(flags) {
    case INVOKE_FUNC:
96
        return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
97 98 99 100 101 102 103
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

static const builtin_prop_t Bool_props[] = {
    {toStringW,              Bool_toString,             PROPF_METHOD},
    {valueOfW,               Bool_valueOf,              PROPF_METHOD}
};

static const builtin_info_t Bool_info = {
    JSCLASS_BOOLEAN,
    {NULL, Bool_value, 0},
    sizeof(Bool_props)/sizeof(*Bool_props),
    Bool_props,
    NULL,
    NULL
};

120
static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
121 122
        VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
123 124 125 126 127 128 129 130 131 132 133
    HRESULT hres;
    VARIANT_BOOL value = VARIANT_FALSE;

    if(arg_cnt(dp)) {
        hres = to_boolean(get_arg(dp,0), &value);
        if(FAILED(hres))
            return hres;
    }

    switch(flags) {
    case DISPATCH_CONSTRUCT: {
134
        jsdisp_t *bool;
135

136
        hres = create_bool(ctx, value, &bool);
137 138 139
        if(FAILED(hres))
            return hres;

140
        var_set_jsdisp(retv, bool);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        return S_OK;
    }

    case INVOKE_FUNC:
        if(retv) {
            V_VT(retv) = VT_BOOL;
            V_BOOL(retv) = value;
        }
        return S_OK;

    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
157 158
}

159
static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
160 161 162 163 164 165 166 167
{
    BoolInstance *bool;
    HRESULT hres;

    bool = heap_alloc_zero(sizeof(BoolInstance));
    if(!bool)
        return E_OUTOFMEMORY;

168 169
    if(object_prototype)
        hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
170
    else
171
        hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
172 173 174 175 176 177 178 179 180 181

    if(FAILED(hres)) {
        heap_free(bool);
        return hres;
    }

    *ret = bool;
    return S_OK;
}

182
HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
183 184 185 186
{
    BoolInstance *bool;
    HRESULT hres;

187 188
    static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};

189
    hres = alloc_bool(ctx, object_prototype, &bool);
190 191 192
    if(FAILED(hres))
        return hres;

193 194
    hres = create_builtin_function(ctx, BoolConstr_value, BooleanW, NULL,
            PROPF_CONSTR|1, &bool->dispex, ret);
195 196 197 198

    jsdisp_release(&bool->dispex);
    return hres;
}
199

200
HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, jsdisp_t **ret)
201 202 203 204
{
    BoolInstance *bool;
    HRESULT hres;

205
    hres = alloc_bool(ctx, NULL, &bool);
206 207 208 209 210 211 212 213
    if(FAILED(hres))
        return hres;

    bool->val = b;

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