diff --git a/js/src/builtin/Object.cpp b/js/src/builtin/Object.cpp index 27063c6e5127..6fd5ff784478 100644 --- a/js/src/builtin/Object.cpp +++ b/js/src/builtin/Object.cpp @@ -791,6 +791,20 @@ obj_keys(JSContext *cx, unsigned argc, Value *vp) return true; } +/* ES6 draft 15.2.3.16 */ +static JSBool +obj_is(JSContext *cx, unsigned argc, Value *vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + bool same; + if (!SameValue(cx, args.get(0), args.get(1), &same)) + return false; + + args.rval().setBoolean(same); + return true; +} + static JSBool obj_getOwnPropertyNames(JSContext *cx, unsigned argc, Value *vp) { @@ -982,6 +996,7 @@ JSFunctionSpec js::object_static_methods[] = { JS_FN("getPrototypeOf", obj_getPrototypeOf, 1,0), JS_FN("getOwnPropertyDescriptor", obj_getOwnPropertyDescriptor,2,0), JS_FN("keys", obj_keys, 1,0), + JS_FN("is", obj_is, 2,0), JS_FN("defineProperty", obj_defineProperty, 3,0), JS_FN("defineProperties", obj_defineProperties, 2,0), JS_FN("create", obj_create, 2,0), diff --git a/js/src/jit-test/tests/basic/object-is.js b/js/src/jit-test/tests/basic/object-is.js new file mode 100644 index 000000000000..a62b5dfe8068 --- /dev/null +++ b/js/src/jit-test/tests/basic/object-is.js @@ -0,0 +1,41 @@ +/* + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ + */ + +var list = [ + [1, 1, true], + [0, 1, false], + [3.5, 3.5, true], + [0, 0, true], + [0, -0, false], + [-0, 0, false], + [-0, -0, true], + + [true, true, true], + [true, false, false], + [false, false, true], + + [NaN, NaN, true], + [NaN, undefined, false], + [Infinity, -Infinity, false], + [Infinity, Infinity, true], +] + +for (var test of list) { + assertEq(Object.is(test[0], test[1]), test[2]) +} + +var obj = {} +assertEq(Object.is(obj, obj), true); +assertEq(Object.is(obj, {}), false); +assertEq(Object.is([], []), false); + +assertEq(Object.is(null, null, null), true); + +/* Not defined parameters are undefined ... */ +assertEq(Object.is(null), false); +assertEq(Object.is(undefined), true); +assertEq(Object.is(), true); + +assertEq(Object.is.length, 2);