2008-09-10 02:34:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
2009-07-06 10:38:51 +02:00
|
|
|
* Copyright 2009 Piotr Caban
|
2008-09-10 02:34:16 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-01-27 20:43:55 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-09-10 02:34:16 +02:00
|
|
|
#include "jscript.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
|
|
|
|
|
|
|
typedef struct {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t dispex;
|
2008-09-16 20:45:29 +02:00
|
|
|
|
2012-09-17 15:20:01 +02:00
|
|
|
BOOL val;
|
2008-09-10 02:34:16 +02:00
|
|
|
} BoolInstance;
|
|
|
|
|
2016-09-22 10:00:36 +02:00
|
|
|
static inline BoolInstance *bool_from_jsdisp(jsdisp_t *jsdisp)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(jsdisp, BoolInstance, dispex);
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static inline HRESULT boolval_this(jsval_t vthis, BOOL *ret)
|
2016-09-22 10:00:36 +02:00
|
|
|
{
|
2022-03-17 17:25:39 +01:00
|
|
|
jsdisp_t *jsdisp;
|
|
|
|
if(is_bool(vthis))
|
|
|
|
*ret = get_bool(vthis);
|
|
|
|
else if(is_object_instance(vthis) && (jsdisp = to_jsdisp(get_object(vthis))) && is_class(jsdisp, JSCLASS_BOOLEAN))
|
|
|
|
*ret = bool_from_jsdisp(jsdisp)->val;
|
|
|
|
else
|
|
|
|
return JS_E_BOOLEAN_EXPECTED;
|
|
|
|
return S_OK;
|
2009-09-23 16:18:39 +02:00
|
|
|
}
|
|
|
|
|
2016-01-27 20:43:55 +01:00
|
|
|
BOOL bool_obj_value(jsdisp_t *obj)
|
|
|
|
{
|
|
|
|
assert(is_class(obj, JSCLASS_BOOLEAN));
|
2016-09-22 10:00:36 +02:00
|
|
|
return bool_from_jsdisp(obj)->val;
|
2016-01-27 20:43:55 +01:00
|
|
|
}
|
|
|
|
|
2009-07-06 10:38:41 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.6.4.2 */
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Bool_toString(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
2022-03-17 17:25:39 +01:00
|
|
|
BOOL boolval;
|
|
|
|
HRESULT hres;
|
2009-09-23 16:18:39 +02:00
|
|
|
|
2009-07-06 10:38:41 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
hres = boolval_this(vthis, &boolval);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2009-07-06 10:38:41 +02:00
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r) {
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *val;
|
2009-07-06 10:38:41 +02:00
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
val = jsstr_alloc(boolval ? L"true" : L"false");
|
2009-07-06 10:38:41 +02:00
|
|
|
if(!val)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_string(val);
|
2009-07-06 10:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:34:16 +02:00
|
|
|
}
|
|
|
|
|
2009-07-06 10:38:45 +02:00
|
|
|
/* ECMA-262 3rd Edition 15.6.4.3 */
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Bool_valueOf(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
2022-03-17 17:25:39 +01:00
|
|
|
BOOL boolval;
|
|
|
|
HRESULT hres;
|
2009-09-23 16:18:39 +02:00
|
|
|
|
2009-07-06 10:38:45 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
hres = boolval_this(vthis, &boolval);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2009-07-06 10:38:45 +02:00
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
2022-03-17 17:25:39 +01:00
|
|
|
*r = jsval_bool(boolval);
|
2009-07-06 10:38:45 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:34:16 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT Bool_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
2009-07-22 13:02:30 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case INVOKE_FUNC:
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_FUNCTION_EXPECTED;
|
2009-07-22 13:02:30 +02:00
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
|
2008-09-10 02:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Bool_props[] = {
|
2020-11-03 23:34:40 +01:00
|
|
|
{L"toString", Bool_toString, PROPF_METHOD},
|
|
|
|
{L"valueOf", Bool_valueOf, PROPF_METHOD}
|
2008-09-10 02:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Bool_info = {
|
|
|
|
JSCLASS_BOOLEAN,
|
2021-11-25 21:01:37 +01:00
|
|
|
Bool_value,
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(Bool_props),
|
2008-09-10 02:34:16 +02:00
|
|
|
Bool_props,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-07-24 13:16:46 +02:00
|
|
|
static const builtin_info_t BoolInst_info = {
|
|
|
|
JSCLASS_BOOLEAN,
|
2021-11-25 21:01:37 +01:00
|
|
|
Bool_value,
|
2012-07-24 13:16:46 +02:00
|
|
|
0, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-03-17 17:25:39 +01:00
|
|
|
static HRESULT BoolConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
2012-09-17 15:16:42 +02:00
|
|
|
BOOL value = FALSE;
|
2009-07-06 10:38:36 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-17 15:18:03 +02:00
|
|
|
hres = to_boolean(argv[0], &value);
|
2009-07-06 10:38:36 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(flags) {
|
|
|
|
case DISPATCH_CONSTRUCT: {
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *bool;
|
2009-07-06 10:38:36 +02:00
|
|
|
|
2022-04-14 18:24:38 +02:00
|
|
|
if(!r)
|
|
|
|
return S_OK;
|
|
|
|
|
2009-09-23 16:17:17 +02:00
|
|
|
hres = create_bool(ctx, value, &bool);
|
2009-07-06 10:38:36 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_obj(bool);
|
2009-07-06 10:38:36 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
case INVOKE_FUNC:
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_bool(value);
|
2009-07-06 10:38:36 +02:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:34:16 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
|
|
|
BoolInstance *bool;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
bool = heap_alloc_zero(sizeof(BoolInstance));
|
|
|
|
if(!bool)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2009-08-14 11:54:51 +02:00
|
|
|
if(object_prototype)
|
|
|
|
hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
|
2008-09-10 02:34:16 +02:00
|
|
|
else
|
2012-07-24 13:16:46 +02:00
|
|
|
hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
|
2008-09-10 02:34:16 +02:00
|
|
|
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(bool);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = bool;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
|
2008-09-10 02:34:16 +02:00
|
|
|
{
|
|
|
|
BoolInstance *bool;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-08-14 11:54:51 +02:00
|
|
|
hres = alloc_bool(ctx, object_prototype, &bool);
|
2008-09-10 02:34:16 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2020-11-24 00:49:04 +01:00
|
|
|
hres = create_builtin_constructor(ctx, BoolConstr_value, L"Boolean", NULL,
|
2009-10-13 22:08:43 +02:00
|
|
|
PROPF_CONSTR|1, &bool->dispex, ret);
|
2008-09-10 02:34:16 +02:00
|
|
|
|
|
|
|
jsdisp_release(&bool->dispex);
|
|
|
|
return hres;
|
|
|
|
}
|
2008-09-16 20:45:29 +02:00
|
|
|
|
2012-09-17 15:20:01 +02:00
|
|
|
HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
|
2008-09-16 20:45:29 +02:00
|
|
|
{
|
|
|
|
BoolInstance *bool;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2009-08-14 11:54:51 +02:00
|
|
|
hres = alloc_bool(ctx, NULL, &bool);
|
2008-09-16 20:45:29 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
bool->val = b;
|
|
|
|
|
|
|
|
*ret = &bool->dispex;
|
|
|
|
return S_OK;
|
|
|
|
}
|