2008-09-10 02:33:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-11-14 15:49:46 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-09-10 02:33:15 +02:00
|
|
|
#include "jscript.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *jsdisp;
|
2009-09-23 16:19:58 +02:00
|
|
|
const WCHAR *str;
|
2009-09-23 16:18:39 +02:00
|
|
|
|
2009-07-16 01:16:15 +02:00
|
|
|
/* Keep in sync with jsclass_t enum */
|
2020-03-20 14:35:27 +01:00
|
|
|
static const WCHAR *names[] = {
|
|
|
|
NULL,
|
|
|
|
L"[object Array]",
|
|
|
|
L"[object Boolean]",
|
|
|
|
L"[object Date]",
|
|
|
|
L"[object Object]",
|
|
|
|
L"[object Error]",
|
|
|
|
L"[object Function]",
|
|
|
|
NULL,
|
|
|
|
L"[object Math]",
|
|
|
|
L"[object Number]",
|
|
|
|
L"[object Object]",
|
|
|
|
L"[object RegExp]",
|
|
|
|
L"[object String]",
|
|
|
|
L"[object Object]",
|
|
|
|
L"[object Object]",
|
|
|
|
L"[object Object]"
|
|
|
|
};
|
2009-07-16 01:16:15 +02:00
|
|
|
|
2009-07-07 13:45:38 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
jsdisp = get_jsdisp(jsthis);
|
2009-09-23 16:19:58 +02:00
|
|
|
if(!jsdisp) {
|
2020-03-20 14:35:27 +01:00
|
|
|
str = L"[object Object]";
|
2009-09-23 16:19:58 +02:00
|
|
|
}else if(names[jsdisp->builtin_info->class]) {
|
|
|
|
str = names[jsdisp->builtin_info->class];
|
|
|
|
}else {
|
2012-11-14 15:49:46 +01:00
|
|
|
assert(jsdisp->builtin_info->class != JSCLASS_NONE);
|
2020-03-17 05:36:38 +01:00
|
|
|
FIXME("jsdisp->builtin_info->class = %d\n", jsdisp->builtin_info->class);
|
2009-07-16 01:16:15 +02:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r) {
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *ret;
|
2020-03-20 14:35:27 +01:00
|
|
|
ret = jsstr_alloc(str);
|
2016-09-07 10:50:11 +02:00
|
|
|
if(!ret)
|
2009-07-07 13:45:38 +02:00
|
|
|
return E_OUTOFMEMORY;
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_string(ret);
|
2009-07-07 13:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2009-07-07 13:45:38 +02:00
|
|
|
TRACE("\n");
|
2009-08-12 12:48:58 +02:00
|
|
|
|
2009-09-23 16:18:39 +02:00
|
|
|
if(!is_jsdisp(jsthis)) {
|
|
|
|
FIXME("Host object this\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
return jsdisp_call_name(jsthis->u.jsdisp, L"toString", DISPATCH_METHOD, 0, NULL, r);
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2009-07-07 14:07:47 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r) {
|
2009-09-23 16:18:39 +02:00
|
|
|
IDispatch_AddRef(jsthis->u.disp);
|
2012-09-17 15:16:20 +02:00
|
|
|
*r = jsval_disp(jsthis->u.disp);
|
2009-07-07 14:07:47 +02:00
|
|
|
}
|
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_hasOwnProperty(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_t *name;
|
2011-09-02 12:24:12 +02:00
|
|
|
DISPID id;
|
2013-03-11 16:05:27 +01:00
|
|
|
BSTR bstr;
|
2011-09-02 12:24:12 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(!argc) {
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_bool(FALSE);
|
2011-09-02 12:24:12 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 19:01:49 +02:00
|
|
|
hres = to_string(ctx, argv[0], &name);
|
2011-09-02 12:24:12 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(is_jsdisp(jsthis)) {
|
2018-05-10 19:20:06 +02:00
|
|
|
property_desc_t prop_desc;
|
2013-03-27 11:04:09 +01:00
|
|
|
const WCHAR *name_str;
|
2012-07-23 15:53:52 +02:00
|
|
|
|
2013-03-27 11:04:09 +01:00
|
|
|
name_str = jsstr_flatten(name);
|
2018-05-10 19:20:06 +02:00
|
|
|
if(!name_str) {
|
|
|
|
jsstr_release(name);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = jsdisp_get_own_property(jsthis->u.jsdisp, name_str, TRUE, &prop_desc);
|
2013-03-11 16:05:27 +01:00
|
|
|
jsstr_release(name);
|
2018-05-10 19:20:06 +02:00
|
|
|
if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME)
|
2012-07-23 15:53:52 +02:00
|
|
|
return hres;
|
|
|
|
|
2018-05-10 19:20:06 +02:00
|
|
|
if(r) *r = jsval_bool(hres == S_OK);
|
2011-09-02 12:24:12 +02:00
|
|
|
return S_OK;
|
2012-07-23 15:53:52 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:16:01 +02:00
|
|
|
|
2013-03-11 16:05:27 +01:00
|
|
|
bstr = SysAllocStringLen(NULL, jsstr_length(name));
|
|
|
|
if(bstr)
|
|
|
|
jsstr_flush(name, bstr);
|
|
|
|
jsstr_release(name);
|
|
|
|
if(!bstr)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-10-11 12:16:01 +02:00
|
|
|
|
2013-03-11 16:05:27 +01:00
|
|
|
if(is_dispex(jsthis))
|
|
|
|
hres = IDispatchEx_GetDispID(jsthis->u.dispex, bstr, make_grfdex(ctx, fdexNameCaseSensitive), &id);
|
|
|
|
else
|
|
|
|
hres = IDispatch_GetIDsOfNames(jsthis->u.disp, &IID_NULL, &bstr, 1, ctx->lcid, &id);
|
2011-09-02 12:24:12 +02:00
|
|
|
|
2013-03-11 16:05:27 +01:00
|
|
|
SysFreeString(bstr);
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_bool(SUCCEEDED(hres));
|
2011-09-02 12:24:12 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_propertyIsEnumerable(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2018-05-10 19:20:16 +02:00
|
|
|
property_desc_t prop_desc;
|
2013-03-27 11:04:09 +01:00
|
|
|
const WCHAR *name;
|
|
|
|
jsstr_t *name_str;
|
2012-11-05 15:04:19 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(argc != 1) {
|
|
|
|
FIXME("argc %d not supported\n", argc);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_jsdisp(jsthis)) {
|
|
|
|
FIXME("Host object this\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2013-03-27 11:04:09 +01:00
|
|
|
hres = to_flat_string(ctx, argv[0], &name_str, &name);
|
2012-11-05 15:04:19 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2018-05-10 19:20:16 +02:00
|
|
|
hres = jsdisp_get_own_property(jsthis->u.jsdisp, name, TRUE, &prop_desc);
|
2013-03-27 11:04:09 +01:00
|
|
|
jsstr_release(name_str);
|
2018-05-10 19:20:16 +02:00
|
|
|
if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME)
|
2012-11-05 15:04:19 +01:00
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(r)
|
2018-05-10 19:20:16 +02:00
|
|
|
*r = jsval_bool(hres == S_OK && (prop_desc.flags & PROPF_ENUMERABLE) != 0);
|
2012-11-05 15:04:19 +01:00
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT Object_isPrototypeOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2015-02-06 12:09:40 +01:00
|
|
|
static HRESULT Object_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2015-02-05 14:49:01 +01:00
|
|
|
jsstr_t *ret;
|
|
|
|
|
2008-09-21 15:39:38 +02:00
|
|
|
TRACE("\n");
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
ret = jsstr_alloc(L"[object Object]");
|
2015-02-05 14:49:01 +01:00
|
|
|
if(!ret)
|
|
|
|
return E_OUTOFMEMORY;
|
2008-09-21 15:39:38 +02:00
|
|
|
|
2015-02-05 14:49:01 +01:00
|
|
|
*r = jsval_string(ret);
|
2008-09-21 15:39:38 +02:00
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
static void Object_destructor(jsdisp_t *dispex)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
|
|
|
heap_free(dispex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const builtin_prop_t Object_props[] = {
|
2020-03-23 21:30:09 +01:00
|
|
|
{L"hasOwnProperty", Object_hasOwnProperty, PROPF_METHOD|1},
|
|
|
|
{L"isPrototypeOf", Object_isPrototypeOf, PROPF_METHOD|1},
|
|
|
|
{L"propertyIsEnumerable", Object_propertyIsEnumerable, PROPF_METHOD|1},
|
|
|
|
{L"toLocaleString", Object_toLocaleString, PROPF_METHOD},
|
|
|
|
{L"toString", Object_toString, PROPF_METHOD},
|
|
|
|
{L"valueOf", Object_valueOf, PROPF_METHOD}
|
2008-09-10 02:33:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t Object_info = {
|
|
|
|
JSCLASS_OBJECT,
|
2015-02-05 14:49:01 +01:00
|
|
|
{NULL, NULL,0, Object_get_value},
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(Object_props),
|
2008-09-10 02:33:15 +02:00
|
|
|
Object_props,
|
|
|
|
Object_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-07-23 15:53:22 +02:00
|
|
|
static const builtin_info_t ObjectInst_info = {
|
|
|
|
JSCLASS_OBJECT,
|
2015-02-05 14:49:01 +01:00
|
|
|
{NULL, NULL,0, Object_get_value},
|
2012-07-23 15:53:22 +02:00
|
|
|
0, NULL,
|
|
|
|
Object_destructor,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-05-10 19:19:56 +02:00
|
|
|
static void release_property_descriptor(property_desc_t *desc)
|
|
|
|
{
|
2018-05-11 14:43:20 +02:00
|
|
|
if(desc->explicit_value)
|
|
|
|
jsval_release(desc->value);
|
|
|
|
if(desc->getter)
|
|
|
|
jsdisp_release(desc->getter);
|
|
|
|
if(desc->setter)
|
|
|
|
jsdisp_release(desc->setter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT to_property_descriptor(script_ctx_t *ctx, jsdisp_t *attr_obj, property_desc_t *desc)
|
|
|
|
{
|
|
|
|
DISPID id;
|
|
|
|
jsval_t v;
|
|
|
|
BOOL b;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
memset(desc, 0, sizeof(*desc));
|
|
|
|
desc->value = jsval_undefined();
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"enumerable", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
desc->mask |= PROPF_ENUMERABLE;
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &v);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
hres = to_boolean(v, &b);
|
|
|
|
jsval_release(v);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
if(b)
|
|
|
|
desc->flags |= PROPF_ENUMERABLE;
|
|
|
|
}else if(hres != DISP_E_UNKNOWNNAME) {
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"configurable", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
desc->mask |= PROPF_CONFIGURABLE;
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &v);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
hres = to_boolean(v, &b);
|
|
|
|
jsval_release(v);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
if(b)
|
|
|
|
desc->flags |= PROPF_CONFIGURABLE;
|
|
|
|
}else if(hres != DISP_E_UNKNOWNNAME) {
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"value", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &desc->value);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
desc->explicit_value = TRUE;
|
|
|
|
}else if(hres != DISP_E_UNKNOWNNAME) {
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"writable", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
desc->mask |= PROPF_WRITABLE;
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &v);
|
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
hres = to_boolean(v, &b);
|
|
|
|
jsval_release(v);
|
|
|
|
if(SUCCEEDED(hres) && b)
|
|
|
|
desc->flags |= PROPF_WRITABLE;
|
|
|
|
}
|
|
|
|
}else if(hres == DISP_E_UNKNOWNNAME) {
|
|
|
|
hres = S_OK;
|
|
|
|
}
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
release_property_descriptor(desc);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"get", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
desc->explicit_getter = TRUE;
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &v);
|
|
|
|
if(SUCCEEDED(hres) && !is_undefined(v)) {
|
|
|
|
if(!is_object_instance(v)) {
|
|
|
|
FIXME("getter is not an object\n");
|
|
|
|
jsval_release(v);
|
|
|
|
hres = E_FAIL;
|
|
|
|
}else {
|
|
|
|
/* FIXME: Check IsCallable */
|
|
|
|
desc->getter = to_jsdisp(get_object(v));
|
|
|
|
if(!desc->getter)
|
|
|
|
FIXME("getter is not JS object\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if(hres == DISP_E_UNKNOWNNAME) {
|
|
|
|
hres = S_OK;
|
|
|
|
}
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
release_property_descriptor(desc);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_get_id(attr_obj, L"set", 0, &id);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres)) {
|
|
|
|
desc->explicit_setter = TRUE;
|
|
|
|
hres = jsdisp_propget(attr_obj, id, &v);
|
|
|
|
if(SUCCEEDED(hres) && !is_undefined(v)) {
|
|
|
|
if(!is_object_instance(v)) {
|
|
|
|
FIXME("setter is not an object\n");
|
|
|
|
jsval_release(v);
|
|
|
|
hres = E_FAIL;
|
|
|
|
}else {
|
|
|
|
/* FIXME: Check IsCallable */
|
|
|
|
desc->setter = to_jsdisp(get_object(v));
|
|
|
|
if(!desc->setter)
|
|
|
|
FIXME("setter is not JS object\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if(hres == DISP_E_UNKNOWNNAME) {
|
|
|
|
hres = S_OK;
|
|
|
|
}
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
release_property_descriptor(desc);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(desc->explicit_getter || desc->explicit_setter) {
|
|
|
|
if(desc->explicit_value)
|
2020-01-31 17:23:33 +01:00
|
|
|
hres = JS_E_PROP_DESC_MISMATCH;
|
2018-05-11 14:43:20 +02:00
|
|
|
else if(desc->mask & PROPF_WRITABLE)
|
2020-01-31 17:23:33 +01:00
|
|
|
hres = JS_E_INVALID_WRITABLE_PROP_DESC;
|
2018-05-11 14:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hres))
|
|
|
|
release_property_descriptor(desc);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:31:18 +02:00
|
|
|
static HRESULT jsdisp_define_properties(script_ctx_t *ctx, jsdisp_t *obj, jsval_t list_val)
|
|
|
|
{
|
|
|
|
DISPID id = DISPID_STARTENUM;
|
|
|
|
property_desc_t prop_desc;
|
|
|
|
IDispatch *list_disp;
|
|
|
|
jsdisp_t *list_obj, *desc_obj;
|
|
|
|
jsval_t desc_val;
|
|
|
|
BSTR name;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = to_object(ctx, list_val, &list_disp);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(!(list_obj = to_jsdisp(list_disp))) {
|
|
|
|
FIXME("non-JS list obj\n");
|
|
|
|
IDispatch_Release(list_disp);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
hres = jsdisp_next_prop(list_obj, id, TRUE, &id);
|
|
|
|
if(hres != S_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = jsdisp_propget(list_obj, id, &desc_val);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!is_object_instance(desc_val) || !get_object(desc_val) || !(desc_obj = to_jsdisp(get_object(desc_val)))) {
|
|
|
|
jsval_release(desc_val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = to_property_descriptor(ctx, desc_obj, &prop_desc);
|
|
|
|
jsdisp_release(desc_obj);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = IDispatchEx_GetMemberName(&list_obj->IDispatchEx_iface, id, &name);
|
|
|
|
if(SUCCEEDED(hres))
|
|
|
|
hres = jsdisp_define_property(obj, name, &prop_desc);
|
|
|
|
release_property_descriptor(&prop_desc);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsdisp_release(list_obj);
|
|
|
|
return FAILED(hres) ? hres : S_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:43:20 +02:00
|
|
|
static HRESULT Object_defineProperty(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
property_desc_t prop_desc;
|
|
|
|
jsdisp_t *obj, *attr_obj;
|
|
|
|
const WCHAR *name;
|
|
|
|
jsstr_t *name_str;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(argc < 1 || !is_object_instance(argv[0]))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_OBJECT_EXPECTED;
|
2018-05-11 14:43:20 +02:00
|
|
|
obj = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!obj) {
|
|
|
|
FIXME("not implemented non-JS object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = to_flat_string(ctx, argc >= 2 ? argv[1] : jsval_undefined(), &name_str, &name);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(argc >= 3 && is_object_instance(argv[2])) {
|
|
|
|
attr_obj = to_jsdisp(get_object(argv[2]));
|
|
|
|
if(attr_obj) {
|
|
|
|
hres = to_property_descriptor(ctx, attr_obj, &prop_desc);
|
|
|
|
}else {
|
|
|
|
FIXME("not implemented non-JS object\n");
|
|
|
|
hres = E_NOTIMPL;
|
|
|
|
}
|
|
|
|
}else {
|
2020-01-31 17:23:33 +01:00
|
|
|
hres = JS_E_OBJECT_EXPECTED;
|
2018-05-11 14:43:20 +02:00
|
|
|
}
|
|
|
|
jsstr_release(name_str);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = jsdisp_define_property(obj, name, &prop_desc);
|
|
|
|
release_property_descriptor(&prop_desc);
|
2019-08-21 12:32:01 +02:00
|
|
|
if(SUCCEEDED(hres) && r)
|
|
|
|
*r = jsval_obj(jsdisp_addref(obj));
|
2018-05-11 14:43:20 +02:00
|
|
|
return hres;
|
2018-05-10 19:19:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 13:34:13 +02:00
|
|
|
static HRESULT Object_defineProperties(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
2019-08-21 12:31:18 +02:00
|
|
|
jsdisp_t *obj;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(argc < 1 || !is_object_instance(argv[0]) || !get_object(argv[0]) || !(obj = to_jsdisp(get_object(argv[0])))) {
|
|
|
|
FIXME("not an object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("%p\n", obj);
|
|
|
|
|
|
|
|
hres = jsdisp_define_properties(ctx, obj, argc >= 2 ? argv[1] : jsval_undefined());
|
|
|
|
if(SUCCEEDED(hres) && r)
|
|
|
|
*r = jsval_obj(jsdisp_addref(obj));
|
|
|
|
return hres;
|
2018-05-14 13:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:19:56 +02:00
|
|
|
static HRESULT Object_getOwnPropertyDescriptor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
property_desc_t prop_desc;
|
|
|
|
jsdisp_t *obj, *desc_obj;
|
|
|
|
const WCHAR *name;
|
|
|
|
jsstr_t *name_str;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if(argc < 1 || !is_object_instance(argv[0]))
|
2020-01-31 17:23:33 +01:00
|
|
|
return JS_E_OBJECT_EXPECTED;
|
2018-05-10 19:19:56 +02:00
|
|
|
obj = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!obj) {
|
|
|
|
FIXME("not implemented non-JS object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = to_flat_string(ctx, argc >= 2 ? argv[1] : jsval_undefined(), &name_str, &name);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = jsdisp_get_own_property(obj, name, FALSE, &prop_desc);
|
|
|
|
jsstr_release(name_str);
|
|
|
|
if(hres == DISP_E_UNKNOWNNAME) {
|
|
|
|
if(r) *r = jsval_undefined();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = create_object(ctx, NULL, &desc_obj);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2018-05-11 14:43:20 +02:00
|
|
|
if(prop_desc.explicit_getter || prop_desc.explicit_setter) {
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_define_data_property(desc_obj, L"get", PROPF_ALL,
|
2018-06-11 15:54:26 +02:00
|
|
|
prop_desc.getter ? jsval_obj(prop_desc.getter) : jsval_undefined());
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_define_data_property(desc_obj, L"set", PROPF_ALL,
|
2018-06-11 15:54:26 +02:00
|
|
|
prop_desc.setter ? jsval_obj(prop_desc.setter) : jsval_undefined());
|
2018-05-11 14:43:20 +02:00
|
|
|
}else {
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_propput_name(desc_obj, L"value", prop_desc.value);
|
2018-05-11 14:43:20 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_define_data_property(desc_obj, L"writable", PROPF_ALL,
|
2018-06-11 15:54:26 +02:00
|
|
|
jsval_bool(!!(prop_desc.flags & PROPF_WRITABLE)));
|
2018-05-11 14:43:20 +02:00
|
|
|
}
|
2018-05-10 19:19:56 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_define_data_property(desc_obj, L"enumerable", PROPF_ALL,
|
2018-06-11 15:54:26 +02:00
|
|
|
jsval_bool(!!(prop_desc.flags & PROPF_ENUMERABLE)));
|
2018-05-10 19:19:56 +02:00
|
|
|
if(SUCCEEDED(hres))
|
2020-03-23 21:30:09 +01:00
|
|
|
hres = jsdisp_define_data_property(desc_obj, L"configurable", PROPF_ALL,
|
2018-06-11 15:54:26 +02:00
|
|
|
jsval_bool(!!(prop_desc.flags & PROPF_CONFIGURABLE)));
|
2018-05-10 19:19:56 +02:00
|
|
|
|
|
|
|
release_property_descriptor(&prop_desc);
|
|
|
|
if(SUCCEEDED(hres) && r)
|
|
|
|
*r = jsval_obj(desc_obj);
|
|
|
|
else
|
|
|
|
jsdisp_release(desc_obj);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:14:44 +01:00
|
|
|
static HRESULT Object_create(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
jsdisp_t *proto = NULL, *obj;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(!argc || (!is_object_instance(argv[0]) && !is_null(argv[0]))) {
|
|
|
|
FIXME("Invalid arg\n");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("(%s)\n", debugstr_jsval(argv[0]));
|
|
|
|
|
|
|
|
if(argc && is_object_instance(argv[0])) {
|
|
|
|
if(get_object(argv[0]))
|
|
|
|
proto = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!proto) {
|
|
|
|
FIXME("Non-JS prototype\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
}else if(!is_null(argv[0])) {
|
|
|
|
FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2020-03-17 05:36:37 +01:00
|
|
|
hres = create_dispex(ctx, &ObjectInst_info, proto, &obj);
|
2019-08-21 12:31:33 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(argc >= 2 && !is_undefined(argv[1]))
|
|
|
|
hres = jsdisp_define_properties(ctx, obj, argv[1]);
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres) && r)
|
2019-03-15 16:14:44 +01:00
|
|
|
*r = jsval_obj(obj);
|
2019-08-21 12:31:33 +02:00
|
|
|
else
|
|
|
|
jsdisp_release(obj);
|
|
|
|
return hres;
|
2019-03-15 16:14:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 14:39:27 +01:00
|
|
|
static HRESULT Object_getPrototypeOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
jsdisp_t *obj;
|
|
|
|
|
|
|
|
if(!argc || !is_object_instance(argv[0])) {
|
|
|
|
FIXME("invalid arguments\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:29:51 +02:00
|
|
|
TRACE("(%s)\n", debugstr_jsval(argv[0]));
|
2019-03-11 14:39:27 +01:00
|
|
|
|
|
|
|
obj = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!obj) {
|
|
|
|
FIXME("Non-JS object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(r)
|
|
|
|
*r = obj->prototype
|
|
|
|
? jsval_obj(jsdisp_addref(obj->prototype))
|
|
|
|
: jsval_null();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:29:51 +02:00
|
|
|
static HRESULT Object_keys(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
|
|
|
|
unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
DISPID id = DISPID_STARTENUM;
|
|
|
|
jsdisp_t *obj, *array;
|
|
|
|
unsigned i = 0;
|
|
|
|
jsstr_t *key;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(!argc || !is_object_instance(argv[0])) {
|
|
|
|
FIXME("invalid arguments %s\n", debugstr_jsval(argv[0]));
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("(%s)\n", debugstr_jsval(argv[0]));
|
|
|
|
|
|
|
|
obj = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!obj) {
|
|
|
|
FIXME("Non-JS object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = create_array(ctx, 0, &array);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
do {
|
|
|
|
hres = jsdisp_next_prop(obj, id, TRUE, &id);
|
|
|
|
if(hres != S_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = jsdisp_get_prop_name(obj, id, &key);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = jsdisp_propput_idx(array, i++, jsval_string(key));
|
|
|
|
jsstr_release(key);
|
|
|
|
} while(hres == S_OK);
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres) && r)
|
|
|
|
*r = jsval_obj(array);
|
|
|
|
else
|
|
|
|
jsdisp_release(array);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:26:39 +02:00
|
|
|
static HRESULT Object_preventExtensions(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
|
|
|
|
{
|
|
|
|
jsdisp_t *obj;
|
|
|
|
|
|
|
|
if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
|
|
|
|
FIXME("invalid arguments\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXME("(%s) semi-stub\n", debugstr_jsval(argv[0]));
|
|
|
|
|
|
|
|
obj = to_jsdisp(get_object(argv[0]));
|
|
|
|
if(!obj) {
|
|
|
|
FIXME("Non-JS object\n");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(r) *r = jsval_obj(jsdisp_addref(obj));
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:19:56 +02:00
|
|
|
static const builtin_prop_t ObjectConstr_props[] = {
|
2020-03-23 21:30:09 +01:00
|
|
|
{L"create", Object_create, PROPF_ES5|PROPF_METHOD|2},
|
|
|
|
{L"defineProperties", Object_defineProperties, PROPF_ES5|PROPF_METHOD|2},
|
|
|
|
{L"defineProperty", Object_defineProperty, PROPF_ES5|PROPF_METHOD|2},
|
|
|
|
{L"getOwnPropertyDescriptor", Object_getOwnPropertyDescriptor, PROPF_ES5|PROPF_METHOD|2},
|
2020-06-04 17:29:51 +02:00
|
|
|
{L"getPrototypeOf", Object_getPrototypeOf, PROPF_ES5|PROPF_METHOD|1},
|
2020-06-05 18:26:39 +02:00
|
|
|
{L"keys", Object_keys, PROPF_ES5|PROPF_METHOD|1},
|
|
|
|
{L"preventExtensions", Object_preventExtensions, PROPF_ES5|PROPF_METHOD|1},
|
2018-05-10 19:19:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const builtin_info_t ObjectConstr_info = {
|
|
|
|
JSCLASS_FUNCTION,
|
|
|
|
DEFAULT_FUNCTION_VALUE,
|
2018-07-09 20:59:12 +02:00
|
|
|
ARRAY_SIZE(ObjectConstr_props),
|
2018-05-10 19:19:56 +02:00
|
|
|
ObjectConstr_props,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-09-17 15:16:42 +02:00
|
|
|
static HRESULT ObjectConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
|
2012-09-18 19:01:49 +02:00
|
|
|
jsval_t *r)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2008-09-10 21:08:09 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
switch(flags) {
|
2009-09-17 01:05:55 +02:00
|
|
|
case DISPATCH_METHOD:
|
2016-06-20 20:54:48 +02:00
|
|
|
case DISPATCH_CONSTRUCT: {
|
|
|
|
jsdisp_t *obj;
|
|
|
|
|
2012-06-25 14:08:38 +02:00
|
|
|
if(argc) {
|
2012-09-17 15:16:42 +02:00
|
|
|
if(!is_undefined(argv[0]) && !is_null(argv[0]) && (!is_object_instance(argv[0]) || get_object(argv[0]))) {
|
2009-09-17 01:05:55 +02:00
|
|
|
IDispatch *disp;
|
|
|
|
|
2012-09-17 15:19:19 +02:00
|
|
|
hres = to_object(ctx, argv[0], &disp);
|
2009-09-17 01:05:55 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_disp(disp);
|
|
|
|
else
|
2009-09-17 01:05:55 +02:00
|
|
|
IDispatch_Release(disp);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
2008-09-10 21:08:09 +02:00
|
|
|
|
2009-09-23 16:17:17 +02:00
|
|
|
hres = create_object(ctx, NULL, &obj);
|
2008-09-10 21:08:09 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-17 15:16:20 +02:00
|
|
|
if(r)
|
|
|
|
*r = jsval_obj(obj);
|
2010-10-19 23:14:35 +02:00
|
|
|
else
|
|
|
|
jsdisp_release(obj);
|
2008-09-10 21:08:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("unimplemented flags: %x\n", flags);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_object_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
|
2008-09-10 02:33:15 +02:00
|
|
|
{
|
2020-03-23 21:30:09 +01:00
|
|
|
return create_builtin_constructor(ctx, ObjectConstr_value, L"Object", &ObjectConstr_info, PROPF_CONSTR,
|
2009-07-12 19:52:31 +02:00
|
|
|
object_prototype, ret);
|
|
|
|
}
|
2008-09-10 02:33:15 +02:00
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_object_prototype(script_ctx_t *ctx, jsdisp_t **ret)
|
2009-07-12 19:52:31 +02:00
|
|
|
{
|
|
|
|
return create_dispex(ctx, &Object_info, NULL, ret);
|
2008-09-10 02:33:15 +02:00
|
|
|
}
|
2008-09-10 21:08:09 +02:00
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
HRESULT create_object(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t **ret)
|
2008-09-10 21:08:09 +02:00
|
|
|
{
|
2010-09-06 16:11:49 +02:00
|
|
|
jsdisp_t *object;
|
2008-09-10 21:08:09 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2010-09-06 16:11:49 +02:00
|
|
|
object = heap_alloc_zero(sizeof(jsdisp_t));
|
2008-09-10 21:08:09 +02:00
|
|
|
if(!object)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-23 15:53:22 +02:00
|
|
|
hres = init_dispex_from_constr(object, ctx, &ObjectInst_info, constr ? constr : ctx->object_constr);
|
2008-09-10 21:08:09 +02:00
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(object);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = object;
|
|
|
|
return S_OK;
|
|
|
|
}
|