Jacek Caban : jscript: Add Object.keys implementation.
Alexandre Julliard
julliard at winehq.org
Thu Jun 4 16:08:03 CDT 2020
Module: wine
Branch: master
Commit: 102fe73e66859127794cca9bd884a71a78175220
URL: https://source.winehq.org/git/wine.git/?a=commit;h=102fe73e66859127794cca9bd884a71a78175220
Author: Jacek Caban <jacek at codeweavers.com>
Date: Thu Jun 4 17:29:51 2020 +0200
jscript: Add Object.keys implementation.
Signed-off-by: Jacek Caban <jacek at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>
---
dlls/jscript/dispex.c | 11 +++++++++++
dlls/jscript/jscript.h | 1 +
dlls/jscript/object.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
dlls/mshtml/tests/es5.js | 16 +++++++++++++++
4 files changed, 77 insertions(+), 2 deletions(-)
diff --git a/dlls/jscript/dispex.c b/dlls/jscript/dispex.c
index e986de6ef2..d9097c47dd 100644
--- a/dlls/jscript/dispex.c
+++ b/dlls/jscript/dispex.c
@@ -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;
+}
diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h
index b84f62ff33..e1ab5e5824 100644
--- a/dlls/jscript/jscript.h
+++ b/dlls/jscript/jscript.h
@@ -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;
diff --git a/dlls/jscript/object.c b/dlls/jscript/object.c
index ef249e91a9..bac79ebde2 100644
--- a/dlls/jscript/object.c
+++ b/dlls/jscript/object.c
@@ -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 = {
diff --git a/dlls/mshtml/tests/es5.js b/dlls/mshtml/tests/es5.js
index dca5821c0c..c179ca143f 100644
--- a/dlls/mshtml/tests/es5.js
+++ b/dlls/mshtml/tests/es5.js
@@ -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);
+});
More information about the wine-cvs
mailing list