Skip to content

Commit

Permalink
Handle wrapper object comparisons with primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndreynolds committed Oct 27, 2012
1 parent 64e788a commit deae954
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 60 deletions.
12 changes: 10 additions & 2 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ fh_assign(JSValue *ctx, Node *node)
}
key = fh_str_from_node(ctx, member->e1)->string.ptr;
}

fh_do_assign(ctx, key, val, node->sval);

if (IS_OBJ(ctx) || IS_FUNC(ctx))
fh_do_assign(ctx, key, val, node->sval);
return val;
}

Expand Down Expand Up @@ -788,6 +789,13 @@ fh_eq(JSValue *a, JSValue *b, bool strict)
if (T_BOTH(a, b, T_OBJECT) || T_BOTH(a, b, T_FUNCTION))
return JSBOOL(a == b);

// At this point, if one of our arguments is a wrapper object, try
// again using the primitive value.
if (IS_OBJ(a) && a->object.wraps)
return fh_eq(a->object.wraps, b, false);
if (IS_OBJ(b) && b->object.wraps)
return fh_eq(a, b->object.wraps, false);

return fh_eq(TO_NUM(a), TO_NUM(b), false);
}

Expand Down
4 changes: 2 additions & 2 deletions test/test_boolean_global.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ assertEquals(true, Boolean(1));
assertEquals(true, Boolean(assertEquals));
assertEquals(true, Boolean(new Object()));
assert(new Boolean(false) !== false);
// FIXME: assert(new Boolean(false) == false);
assert(new Boolean(false) == false);
assert(new Boolean(true) !== true);
// FIXME: assert(new Boolean(true) == true);
assert(new Boolean(true) == true);


// ----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions test/test_number_global.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ assertEquals(99, (new Number(99)).valueOf());
assertEquals('number', typeof Number(1));
assertEquals('number', typeof Number(42));
assertEquals(42, Number('42'));
assert(42 == new Number(42));


// Properties
Expand Down
71 changes: 16 additions & 55 deletions test/test_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,26 @@

var assert = console.assert;


// -----------------------------------------------------------------------------
// Methods
// -----------------------------------------------------------------------------

var obj = { a: 1, b: 2, c:3 };

// TODO: Object.create()

// This one isn't enumerable.
Object.defineProperty(obj, 'd', { value: 4 });
assert(obj.d === 4);

// This one is.
Object.defineProperty(obj, 'e', { value: 5, enumerable: true });
assert(obj.e === 5);

Object.defineProperties(obj, {
f: { value: 6 },
g: { value: 7, enumerable: true }
});
assert(obj.f === 6);
assert(obj.g === 7);

var desc = Object.getOwnPropertyDescriptor(obj, 'a');
assert(desc.value === 1);
assert(desc.configurable);
assert(desc.writable);
assert(desc.enumerable);

var keys = Object.keys(obj);
assert(keys.length === 5);
// Note that we only expect to see enumerable properties.
assert(keys[0] === 'a');
assert(keys[1] === 'b');
assert(keys[2] === 'c');
assert(keys[3] === 'e');
assert(keys[4] === 'g');
var assertEquals = function(a, b) {
if (a !== b)
console.log(a + ' !== ' + b);
assert(a === b);
};


// -----------------------------------------------------------------------------
// Prototype
// Literals
// -----------------------------------------------------------------------------

// Via an object

var x = {a: 42, b: [1,2,3]};
assert(x.toString() === '[object Object]');
assert(x.toLocaleString() === '[object Object]');
assert(x.hasOwnProperty('a'));
assert(!x.hasOwnProperty('toString'));
assert(x.propertyIsEnumerable('b'));
assert(x.valueOf() === x);
var o = { "a": 0, '---;?"': 1, 'c': 2, d: 3, e: 4, '': 5};

// Directly via the prototype
var key = 'a';

assert(Object.prototype.toString() === '[object Object]');
assert(Object.prototype.toLocaleString() === '[object Object]');
assert(Object.prototype.hasOwnProperty('toString'));
assert(!Object.prototype.hasOwnProperty('a'));
assert(!Object.prototype.propertyIsEnumerable('toString'));
assert(Object.prototype.valueOf() === Object.prototype);
assert(o[key] === 0);
assert(o['c'] === 2);
assert(o.c === 2);
assert(o.d === 3);
assert(o['e'] === 4);
assert(o.e === 4);
assert(o['---;?"'] === 1);
assert(o[''] === 5);
15 changes: 14 additions & 1 deletion test/test_object_global.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

var assert = console.assert;

var assertObjectEquals = function(a, b) {
};


// -----------------------------------------------------------------------------
// Object Global
Expand All @@ -13,9 +16,18 @@ assert(typeof Object === 'function');

var obj = { a: 1, b: 2, c:3 };


// Object.create()

// TODO
var newObj = Object.create(Object.prototype, {
foo: {writable: true, configurable: true, value: 'hello'},
bar: {configurable: false, value: 'and'},
baz: {enumerable: true, value: 'goodbye'}
});

assert(newObj.foo === 'hello');
assert(newObj.bar === 'and');
assert(newObj.baz === 'goodbye');


// Object.defineProperty(obj, descriptor)
Expand Down Expand Up @@ -75,6 +87,7 @@ assert(!x.hasOwnProperty('toString'));
assert(x.propertyIsEnumerable('b'));
assert(x.valueOf() === x);


// Directly via the prototype

assert(Object.prototype.toString() === '[object Object]');
Expand Down

0 comments on commit deae954

Please sign in to comment.