2015-10-08 00:08:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let keys = require('./keys');
|
|
|
|
let rpc = require('./rpc');
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
module.exports = {
|
|
|
|
createList,
|
|
|
|
createMethods,
|
2015-10-19 22:26:42 +00:00
|
|
|
getterForProperty,
|
|
|
|
setterForProperty,
|
2015-10-19 19:06:47 +00:00
|
|
|
};
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-19 22:26:42 +00:00
|
|
|
function createList(prototype, realmId, info, mutable) {
|
2015-10-19 19:06:47 +00:00
|
|
|
let list = Object.create(prototype);
|
2015-10-14 23:05:49 +00:00
|
|
|
let size = 0;
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-19 22:26:42 +00:00
|
|
|
Object.defineProperty(list, 'length', {get: getterForProperty('length')});
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
list[keys.resize] = function(length) {
|
2015-10-08 04:20:14 +00:00
|
|
|
if (length == null) {
|
|
|
|
length = this.length;
|
|
|
|
}
|
|
|
|
if (length == size) {
|
2015-10-08 00:08:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
if (length > size) {
|
|
|
|
let props = {};
|
|
|
|
|
|
|
|
for (let i = size; i < length; i++) {
|
|
|
|
props[i] = {
|
2015-10-19 22:26:42 +00:00
|
|
|
get: getterForProperty(i),
|
|
|
|
set: mutable ? setterForProperty(i) : undefined,
|
2015-10-08 04:20:14 +00:00
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
Object.defineProperties(this, props);
|
|
|
|
}
|
|
|
|
else if (length < size) {
|
|
|
|
for (let i = size - 1; i >= length; i--) {
|
|
|
|
delete this[i];
|
|
|
|
}
|
|
|
|
}
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
size = length;
|
|
|
|
};
|
2015-10-08 00:08:19 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
list[keys.realm] = realmId;
|
|
|
|
list[keys.id] = info.id;
|
|
|
|
list[keys.type] = info.type;
|
|
|
|
list[keys.resize](info.size);
|
|
|
|
|
2015-10-08 04:20:14 +00:00
|
|
|
return list;
|
2015-10-08 00:08:19 +00:00
|
|
|
}
|
2015-10-19 19:06:47 +00:00
|
|
|
|
|
|
|
function createMethods(prototype, type, methodNames, resize) {
|
|
|
|
let props = {};
|
|
|
|
|
|
|
|
for (let name of methodNames) {
|
|
|
|
props[name] = {
|
|
|
|
value: function() {
|
|
|
|
let realmId = this[keys.realm];
|
|
|
|
let id = this[keys.id];
|
|
|
|
|
|
|
|
if (!realmId || !id) {
|
|
|
|
throw new TypeError(name + ' method was not called a Realm object!');
|
|
|
|
}
|
|
|
|
if (this[keys.type] !== type) {
|
|
|
|
throw new TypeError(name + ' method was called on an object of the wrong type!');
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = rpc.callMethod(realmId, id, type, name, Array.from(arguments));
|
|
|
|
|
|
|
|
if (resize) {
|
|
|
|
this[keys.resize]();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperties(prototype, props);
|
|
|
|
}
|
2015-10-19 22:26:42 +00:00
|
|
|
|
|
|
|
function getterForProperty(name) {
|
|
|
|
return function() {
|
|
|
|
return rpc.getProperty(this[keys.realm], this[keys.id], name);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setterForProperty(name) {
|
|
|
|
return function(value) {
|
|
|
|
rpc.setProperty(this[keys.realm], this[keys.id], name, value);
|
|
|
|
};
|
|
|
|
}
|