jscript: Add Object.keys implementation.
Signed-off-by: Jacek Caban <jacek@codeweavers.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
78d6d41c4c
commit
102fe73e66
|
@ -2572,3 +2572,14 @@ HRESULT jsdisp_define_data_property(jsdisp_t *obj, const WCHAR *name, unsigned f
|
|||
prop_desc.value = value;
|
||||
return jsdisp_define_property(obj, name, &prop_desc);
|
||||
}
|
||||
|
||||
HRESULT jsdisp_get_prop_name(jsdisp_t *obj, DISPID id, jsstr_t **r)
|
||||
{
|
||||
dispex_prop_t *prop = get_prop(obj, id);
|
||||
|
||||
if(!prop || !prop->name || prop->type == PROP_DELETED)
|
||||
return DISP_E_MEMBERNOTFOUND;
|
||||
|
||||
*r = jsstr_alloc(prop->name);
|
||||
return *r ? S_OK : E_OUTOFMEMORY;
|
||||
}
|
||||
|
|
|
@ -314,6 +314,7 @@ HRESULT jsdisp_get_own_property(jsdisp_t*,const WCHAR*,BOOL,property_desc_t*) DE
|
|||
HRESULT jsdisp_define_property(jsdisp_t*,const WCHAR*,property_desc_t*) DECLSPEC_HIDDEN;
|
||||
HRESULT jsdisp_define_data_property(jsdisp_t*,const WCHAR*,unsigned,jsval_t) DECLSPEC_HIDDEN;
|
||||
HRESULT jsdisp_next_prop(jsdisp_t*,DISPID,BOOL,DISPID*) DECLSPEC_HIDDEN;
|
||||
HRESULT jsdisp_get_prop_name(jsdisp_t*,DISPID,jsstr_t**);
|
||||
|
||||
HRESULT create_builtin_function(script_ctx_t*,builtin_invoke_t,const WCHAR*,const builtin_info_t*,DWORD,
|
||||
jsdisp_t*,jsdisp_t**) DECLSPEC_HIDDEN;
|
||||
|
|
|
@ -610,7 +610,7 @@ static HRESULT Object_getPrototypeOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD fl
|
|||
return E_NOTIMPL;
|
||||
}
|
||||
|
||||
TRACE("(%s)\n", debugstr_jsval(argv[1]));
|
||||
TRACE("(%s)\n", debugstr_jsval(argv[0]));
|
||||
|
||||
obj = to_jsdisp(get_object(argv[0]));
|
||||
if(!obj) {
|
||||
|
@ -625,12 +625,59 @@ static HRESULT Object_getPrototypeOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD fl
|
|||
return S_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static const builtin_prop_t ObjectConstr_props[] = {
|
||||
{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},
|
||||
{L"getPrototypeOf", Object_getPrototypeOf, PROPF_ES5|PROPF_METHOD|1}
|
||||
{L"getPrototypeOf", Object_getPrototypeOf, PROPF_ES5|PROPF_METHOD|1},
|
||||
{L"keys", Object_keys, PROPF_ES5|PROPF_METHOD|1}
|
||||
};
|
||||
|
||||
static const builtin_info_t ObjectConstr_info = {
|
||||
|
|
|
@ -865,3 +865,19 @@ sync_test("bind", function() {
|
|||
|
||||
ok(Function.prototype.bind.length === 1, "Function.prototype.bind.length = " + Function.prototype.bind.length);
|
||||
});
|
||||
|
||||
sync_test("keys", function() {
|
||||
var o = { a: 1, b: 2, c: 3 };
|
||||
var keys = Object.keys(o).sort().join();
|
||||
ok(keys === "a,b,c", "keys = " + keys);
|
||||
|
||||
o = Object.create(o);
|
||||
keys = Object.keys(o).sort().join();
|
||||
ok(keys === "", "keys = " + keys);
|
||||
|
||||
o.test = 1;
|
||||
keys = Object.keys(o).sort().join();
|
||||
ok(keys === "test", "keys = " + keys);
|
||||
|
||||
ok(Object.keys.length === 1, "Object.keys.length = " + Object.keys.length);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue