Commit 6682290d authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

jscript: Support ES5 Array.prototype.sort arguments handling.

parent 8a9bbe49
...@@ -685,23 +685,22 @@ static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigne ...@@ -685,23 +685,22 @@ static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigne
if(FAILED(hres)) if(FAILED(hres))
return hres; return hres;
if(argc > 1) { if(argc >= 1) {
WARN("invalid arg_cnt %d\n", argc); if(is_object_instance(argv[0])) {
return E_FAIL; if(argc > 1 && ctx->version < SCRIPTLANGUAGEVERSION_ES5) {
} WARN("invalid arg_cnt %d\n", argc);
return JS_E_JSCRIPT_EXPECTED;
if(argc == 1) { }
if(!is_object_instance(argv[0])) { cmp_func = iface_to_jsdisp(get_object(argv[0]));
WARN("arg is not dispatch\n"); if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
return E_FAIL; WARN("cmp_func is not a function\n");
} if(cmp_func)
jsdisp_release(cmp_func);
cmp_func = iface_to_jsdisp(get_object(argv[0])); return JS_E_JSCRIPT_EXPECTED;
if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) { }
WARN("cmp_func is not a function\n"); }else if(ctx->version >= SCRIPTLANGUAGEVERSION_ES5 ? !is_undefined(argv[0]) : !is_null(argv[0])) {
if(cmp_func) WARN("invalid arg %s\n", debugstr_jsval(argv[0]));
jsdisp_release(cmp_func); return JS_E_JSCRIPT_EXPECTED;
return E_FAIL;
} }
} }
......
...@@ -1039,6 +1039,19 @@ tmp = [5,3,"2.5",2,true,false,-1]; ...@@ -1039,6 +1039,19 @@ tmp = [5,3,"2.5",2,true,false,-1];
for(var i=0; i < arr.length; i++) for(var i=0; i < arr.length; i++)
ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]); ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
tmp = [3,1,2].sort(function(x,y) { return y-x; }).join();
ok(tmp === "3,2,1", "reverse sorted [3,1,2] = " + tmp);
tmp = [3,1,2].sort(null).join();
ok(tmp === "1,2,3", "null sorted [3,1,2] = " + tmp);
try {
tmp = [3,1,2].sort(function(x,y) { return y-x; }, 1, 2, 3);
ok(false, "expected sort(undefined) exception");
} catch(e) {
ok(e.name === "TypeError", "got exception " + e.name);
}
arr = [5,false,2,0,"abc",3,"a",-1]; arr = [5,false,2,0,"abc",3,"a",-1];
tmp = arr.sort(); tmp = arr.sort();
ok(tmp === arr, "tmp !== arr"); ok(tmp === arr, "tmp !== arr");
......
...@@ -188,6 +188,25 @@ function test_array_map() { ...@@ -188,6 +188,25 @@ function test_array_map() {
next_test(); next_test();
} }
function test_array_sort() {
var r;
r = [3,1,2].sort(function(x,y) { return y-x; }, 1, 2, 3, true, undefined ).join();
ok(r === "3,2,1", "reverse sorted [3,1,2] = " + r);
r = [3,1,2].sort(undefined).join();
ok(r === "1,2,3", "null sorted [3,1,2] = " + r);
try {
r = [3,1,2].sort(null);
ok(false, "expected sort(null) exception");
}catch(e) {
ok(e.name === "TypeError", "got exception " + e.name);
}
next_test();
}
function test_identifier_keywords() { function test_identifier_keywords() {
var o = { var o = {
if: 1, if: 1,
...@@ -883,6 +902,7 @@ var tests = [ ...@@ -883,6 +902,7 @@ var tests = [
test_array_forEach, test_array_forEach,
test_isArray, test_isArray,
test_array_map, test_array_map,
test_array_sort,
test_identifier_keywords, test_identifier_keywords,
test_getOwnPropertyDescriptor, test_getOwnPropertyDescriptor,
test_defineProperty, test_defineProperty,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment