Make Realm object properties enumerable

This commit is contained in:
Scott Kyle 2015-11-25 10:48:51 -08:00
parent 7a54b0a256
commit 4109c86343
4 changed files with 30 additions and 8 deletions

View File

@ -19,7 +19,6 @@ function create(realmId, info) {
let schema = info.schema;
let constructor = (registeredConstructors[realmId] || {})[schema.name];
let object = constructor ? Object.create(constructor.prototype) : {};
let props = {};
object[keys.realm] = realmId;
object[keys.id] = info.id;
@ -28,14 +27,13 @@ function create(realmId, info) {
schema.properties.forEach((prop) => {
let name = prop.name;
props[name] = {
Object.defineProperty(object, name, {
enumerable: true,
get: util.getterForProperty(name),
set: util.setterForProperty(name),
};
});
});
Object.defineProperties(object, props);
return object;
}

View File

@ -42,8 +42,14 @@ bool ObjectSetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef jsPro
return true;
}
void ObjectPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
return;
void ObjectPropertyNames(JSContextRef ctx, JSObjectRef jsObject, JSPropertyNameAccumulatorRef propertyNames) {
Object *obj = RJSGetInternal<Object *>(jsObject);
for (auto &prop : obj->object_schema.properties) {
JSStringRef propertyName = RJSStringForString(prop.name);
JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
JSStringRelease(propertyName);
}
}
JSClassRef RJSObjectClass() {

View File

@ -253,6 +253,24 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
},
testEnumerablePropertyNames: function() {
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), new ArrayBuffer()];
var realm = new Realm({schema: [schemas.BasicTypes]});
var object;
realm.write(function() {
object = realm.create('BasicTypesObject', basicTypesValues);
});
var propNames = schemas.BasicTypes.properties.map(function(prop) { return prop.name; });
TestCase.assertArraysEqual(Object.keys(object), propNames, 'Object.keys');
for (var key in object) {
TestCase.assertEqual(key, propNames.shift());
}
TestCase.assertEqual(propNames.length, 0);
},
testDataProperties: function() {
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
var object;

View File

@ -35,7 +35,7 @@ function runTests() {
}
catch (e) {
console.log('- ' + testName);
console.warn(e);
console.warn(e.message);
}
finally {
if (testSuite.afterEach) {