initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

View file

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 2.8.12)
configure_file(string.js string.js)
configure_file(bytearray.js bytearray.js)
configure_file(object.js object.js)
configure_file(uint64.js uint64.js)
configure_file(vocabulary.js vocabulary.js)
configure_file(vector.js vector.js)
configure_file(address.js address.js)
configure_file(event.js event.js)
configure_file(boolean.js boolean.js)
configure_file(blob.js blob.js)

228
libjs/wType/address.js Normal file
View file

@ -0,0 +1,228 @@
"use strict";
var Object = require("./object");
var String = require("./string");
var Address = Object.inherit({
"className": "Address",
"constructor": function(data) {
Object.fn.constructor.call(this);
this._data = [];
this._parseSource(data || []);
},
"destructor": function() {
this.clear();
Object.fn.destructor.call(this);
},
"<": function(other) {
if (!(other instanceof Address)) {
throw new Error("Can compare Address only with Address");
}
var hopMe;
var hopOt;
for (var i = 0; i < this._data.length; ++i) {
hopMe = this._data[i];
hopOt = other._data[i];
if (hopOt === undefined) {
return false;
}
if (hopMe["<"](hopOt)) {
return true;
}
if (hopMe[">"](hopOt)) {
return false;
}
}
return this._data.length < other._data.length;
},
">": function(other) {
if (!(other instanceof Address)) {
throw new Error("Can compare Address only with Address");
}
var hopMe;
var hopOt;
for (var i = 0; i < this._data.length; ++i) {
hopMe = this._data[i];
hopOt = other._data[i];
if (hopOt === undefined) {
return true;
}
if (hopMe[">"](hopOt)) {
return true;
}
if (hopMe["<"](hopOt)) {
return false;
}
}
return this._data.length > other._data.length;
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
if (this._data.length !== other._data.length) {
return false;
}
var hopMe;
var hopOt;
for (var i = 0; i < this._data.length; ++i) {
hopMe = this._data[i];
hopOt = other._data[i];
if ( !(hopMe["=="](hopOt)) ) {
return false;
}
}
return true;
},
"+": function(other) {
var res = this.clone();
res["+="](other);
return res;
},
"+=": function(other) {
if (other instanceof Address) {
for (var i = 0; i < other._data.length; ++i) {
this._data.push(other._data[i].clone());
}
} else {
throw new Error("Can add to Address only Address");
}
return this;
},
"<<": function(n) {
var res = new Address();
for (var i = n; i < this._data.length; ++i) {
res._data.push(this._data[i].clone());
}
return res;
},
">>": function(n) {
var res = new Address();
for (var i = 0; i < this._data.length - n; ++i) {
res._data.push(this._data[i].clone());
}
return res;
},
"clear": function() {
for (var i = 0; i < this._data.length; ++i) {
this._data[i].destructor();
}
this._data = [];
},
"clone": function() {
var clone = new Address();
for (var i = 0; i < this._data.length; ++i) {
clone._data.push(this._data[i].clone());
}
return clone;
},
"deserialize": function(ba) {
this.clear()
var length = ba.pop32();
for (var i = 0; i < length; ++i) {
var hop = new String();
hop.deserialize(ba);
this._data.push(hop);
}
},
"serialize": function(ba) {
ba.push32(this._data.length)
for (var i = 0; i < this._data.length; ++i) {
this._data[i].serialize(ba);
}
},
"length": function() {
return this._data.length;
},
"size": function() {
var size = 4;
for (var i = 0; i < this._data.length; ++i) {
size += this._data[i].size();
}
return size;
},
"toString": function() {
var str = "";
str += "["
for (var i = 0; i < this._data.length; ++i) {
if (i !== 0) {
str +=", ";
}
str += this._data[i].toString();
}
str += "]";
return str;
},
"begins": function(other) {
var size = other._data.length;
if (size > this._data.length) {
return false;
}
for (var i = 0; i < size; ++i) {
var myHop = this._data[i];
var othHop = other._data[i];
if (!myHop["=="](othHop)) {
return false;
}
}
return true;
},
"ends": function(other) {
var size = other._data.length;
if (size > this._data.length) {
return false;
}
for (var i = 1; i <= size; ++i) {
var myHop = this._data[this._data.length - i];
var othHop = other._data[other._data.length - i];
if (!myHop["=="](othHop)) {
return false;
}
}
return true;
},
"back": function() {
return this._data[this._data.length - 1].clone();
},
"front": function() {
return this._data[0].clone();
},
"toArray": function() {
var arr = [];
for (var i = 0; i < this._data.length; ++i) {
arr.push(this._data[i].toString());
}
return arr;
},
"_parseSource": function(data) {
for (var i = 0; i < data.length; ++i) {
this._data.push(new String(data[i]));
}
}
});
module.exports = Address;

65
libjs/wType/blob.js Normal file
View file

@ -0,0 +1,65 @@
"use strict";
var Object = require("./object");
var Blob = Object.inherit({
"className": "Blob",
"constructor": function(/*ArrayBuffer*/data) {
Object.fn.constructor.call(this);
data = data || new ArrayBuffer(0);
this._data = data;
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return this.size() == other.size(); //TODO let's pretend one shall never wish to compare blobs)
},
"base64": function() {
var arr = new Uint8Array(this._data);
var bin = "";
for (var i = 0; i < arr.length; ++i) {
bin += String.fromCharCode(arr[i]);
}
return btoa(bin);
},
"clone": function() {
var clone = new Blob(this._data.slice(0));
return clone;
},
"deserialize": function(ba) {
var length = ba.pop32();
this._data = new ArrayBuffer(length);
var view = new Uint8Array(this._data);
for (var i = 0; i < length; ++i) {
view[i] = ba.pop8();
}
},
"length": function() {
return this._data.byteLength;
},
"serialize": function(ba) {
ba.push32(this._data.byteLength);
var view = new Uint8Array(this._data);
for (var i = 0; i < view.length; ++i) {
ba.push8(view[i]);
}
},
"size": function() {
return this._data.byteLength + 4;
},
"toString": function() {
return "File <" + this._data.byteLength + ">";
},
"valueOf": function() {
return this._data;
}
});
module.exports = Blob;

62
libjs/wType/boolean.js Normal file
View file

@ -0,0 +1,62 @@
"use strict";
var Object = require("./object");
var Boolean = Object.inherit({
"className": "Boolean",
"constructor": function(bool) {
Object.fn.constructor.call(this);
this._data = bool === true;
},
"<": function(other) {
if (!(other instanceof Boolean)) {
throw new Error("Can compare Boolean only with Boolean");
}
return this._data < other._data;
},
">": function(other) {
if (!(other instanceof Boolean)) {
throw new Error("Can compare Boolean only with Boolean");
}
return this._data > other._data;
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return this._data === other._data;
},
"clone": function() {
return new Boolean(this._data);
},
"deserialize": function(ba) {
var int = ba.pop8();
if (int === 253) {
this._data = true;
} else {
this._data = false;
}
},
"length": function() {
return 1;
},
"size": function() {
return 1;
},
"serialize": function(ba) {
if (this._data) {
ba.push8(253);
} else {
ba.push8(0);
}
},
"toString": function() {
return this._data.toString();
},
"valueOf": function() {
return this._data;
}
});
module.exports = Boolean;

106
libjs/wType/bytearray.js Normal file
View file

@ -0,0 +1,106 @@
"use strict";
var Class = require("../utils/class");
var ByteArray = Class.inherit({
"className": "ByteArray",
"constructor": function(size) {
Class.fn.constructor.call(this);
this._referenceMode = false;
this._data = new Uint8Array(size);
this._shiftBegin = 0;
this._shiftEnd = 0;
},
"_checkReference": function() {
if (this._referenceMode) {
var buffer = new ArrayBuffer(this._data.length - this._shiftBegin);
var newData = new Uint8Array(buffer);
newData.set(this._data, this._shiftBegin);
this._data = newData;
this._shiftBegin = 0;
this._referenceMode = false;
}
},
"fill": function(/*Uint8Array*/arr, /*Number*/size, /*[Number]*/shift) {
this._checkReference();
shift = shift || 0;
if (this._shiftEnd === 0 && (this._data.length <= size - shift)) {
this._referenceMode = true;
this._data = arr.subarray(shift, this._data.length + shift);
this._shiftEnd = this._data.length;
shift += this._shiftEnd;
} else {
while (!this.filled() && shift < size) {
this._data[this._shiftEnd] = arr[shift];
++shift;
++this._shiftEnd;
}
}
return shift;
},
"filled": function() {
return this._data.length === this._shiftEnd;
},
"size": function() {
return this._shiftEnd - this._shiftBegin;
},
"maxSize": function() {
return this._data.length;
},
"push8": function(int) {
this._checkReference();
this._data[this._shiftEnd] = int;
++this._shiftEnd;
},
"push16": function(int) {
var h = (int >> 8) & 0xff;
var l = int & 0xff;
this.push8(h);
this.push8(l);
},
"push32": function(int) {
var hh = (int >> 24) & 0xff;
var hl = (int >> 16) & 0xff;
var lh = (int >> 8) & 0xff;
var ll = int & 0xff;
this.push8(hh);
this.push8(hl);
this.push8(lh);
this.push8(ll);
},
"push64": function(int) {
},
"pop8": function(int) {
var ret = this._data[this._shiftBegin];
++this._shiftBegin;
return ret;
},
"pop16": function(int) {
var ret = (this.pop8() << 8);
ret = ret | this.pop8();
return ret;
},
"pop32": function(int) {
var ret = this.pop8() << 24;
ret = ret | (this.pop8() << 16);
ret = ret | (this.pop8() << 8);
ret = ret | this.pop8();
return ret;
},
"pop64": function(int) {
},
"data": function() {
return this._data.subarray(this._shiftBegin, this._shiftEnd);
}
});
module.exports = ByteArray;

121
libjs/wType/event.js Normal file
View file

@ -0,0 +1,121 @@
"use strict";
var Object = require("./object");
var Uint64 = require("./uint64");
var Address = require("./address");
var Boolean = require("./boolean");
var Event = Object.inherit({
"className": "Event",
"constructor": function(addr, object, isSystem) {
Object.fn.constructor.call(this);
if (!(object instanceof Object) && object !== undefined) {
throw new Error("Wrong arguments to construct Event");
}
if (!(addr instanceof Address) && addr !== undefined) {
throw new Error("Wrong arguments to construct Event");
}
this._destination = addr !== undefined ? addr : new Address();
this._data = object !== undefined ? object : new Object();
this._senderId = new Uint64();
this._system = new Boolean(isSystem);
},
"destructor": function() {
this.clear();
Object.fn.destructor.call(this);
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return this._destination["=="](other._destination) &&
this._system["=="](other._system) &&
this._senderId["=="](other._senderId) &&
this._data["=="](other._data)
},
"clear": function() {
this._system.destructor();
this._destination.destructor();
this._senderId.destructor();
this._data.destructor();
},
"clone": function() {
var clone = new Event();
clone._destination = this._destination.clone();
clone._data = this._data.clone();
clone._senderId = this._senderId.clone();
clone._system = this._system.clone();
return clone;
},
"deserialize": function(ba) {
this._system = new Boolean();
this._system.deserialize(ba);
if (!this.isSystem()) {
this._destination = new Address();
this._destination.deserialize(ba);
this._senderId = new Uint64();
this._senderId.deserialize(ba);
}
this._data = Object.fromByteArray(ba);
},
"getData": function() {
return this._data;
},
"getDestination": function() {
return this._destination;
},
"getSenderId": function() {
return this._senderId;
},
"isSystem": function() {
return this._system.valueOf();
},
"length": function() {
return 2 + this._destination.length() + this._data.length();
},
"serialize": function(ba) {
this._system.serialize(ba);
if (!this.isSystem()) {
this._destination.serialize(ba);
this._senderId.serialize(ba);
}
ba.push8(this._data.getType());
this._data.serialize(ba);
},
"setSenderId": function(id) {
if (!(id instanceof Uint64)) {
throw new Error("Can't set id, which is not Uint64");
}
this._senderId = id;
},
"size": function() {
var size = this._system.size() + this._data.size() + 1
if (!this.isSystem()) {
size += this._senderId.size() + this._destination.size();
}
return size;
},
"toString": function() {
var str = "{";
str += "system: " + this._system.toString();
str += " destination: " + this._destination.toString();
str += " sender: " + this._senderId.toString();
str += " data: " + this._data.toString();
str += "}";
return str;
}
});
module.exports = Event;

42
libjs/wType/factory.js Normal file
View file

@ -0,0 +1,42 @@
"use strict";
var Object = require("./object");
var types = {
"String" : require("./string"),
"Vocabulary": require("./vocabulary"),
"Uint64" : require("./uint64"),
"Address" : require("./address"),
"Boolean" : require("./boolean"),
"Event" : require("./event"),
"Vector" : require("./vector"),
"Blob" : require("./blob")
}
var storage = global.Object.create(null);
for (var name in types) {
if (types.hasOwnProperty(name)) {
var typeId = Object.objectType[name];
if (typeId === undefined) {
throw new Error("wType initialization error - can't find type id for type " + name);
}
storage[typeId] = types[name];
}
}
function create(/*ByteArray*/ba) {
var type = ba.pop8();
var Type = storage[type];
if (Type === undefined) {
throw new Error("Unsupported data type found during deserialization: " + type);
}
var obj = new Type();
obj.deserialize(ba);
return obj;
}
Object.fromByteArray = create;
module.exports = create;

70
libjs/wType/object.js Normal file
View file

@ -0,0 +1,70 @@
"use strict";
var Class = require("../utils/class");
var Object = Class.inherit({
"className": "Object",
"constructor": function() {
Class.fn.constructor.call(this);
},
"<": function(other) {
throw new Error(this.className + " has no reimplemented method \"<\"");
},
">": function(other) {
throw new Error(this.className + " has no reimplemented method \">\"");
},
"==": function(other) {
throw new Error(this.className + " has no reimplemented method \"==\"");
},
"clone": function() {
throw new Error(this.className + " has no reimplemented method \"clone\"");
},
"getType": function() {
var type = Object.objectType[this.className];
if (type === undefined) {
throw new Error("Undefined type of " + this.className);
}
return type;
},
"length": function() {
throw new Error(this.className + " has no reimplemented method \"length\"");
},
"size": function() {
throw new Error(this.className + " has no reimplemented method \"size\"");
},
"toString": function() {
throw new Error(this.className + " has no reimplemented method \"toString\"");
},
"valueOf": function() {
throw new Error(this.className + " has no reimplemented method \"valueOf\"");
}
});
Object.objectType = {
"String" : 0,
"Vocabulary": 1,
"Uint64" : 2,
"Address" : 3,
"Boolean" : 4,
"Event" : 5,
"Vector" : 6,
"Blob" : 7
};
Object.reverseObjectType = {
0 : "String",
1 : "Vocabulary",
2 : "Uint64",
3 : "Address",
4 : "Boolean",
5 : "Event",
6 : "Vector",
7 : "Blob"
}
Object.fromByteArray = function() {
throw new Error("Initialization error. Object.fromByteArray is not implemented, it implements in factory.js");
}
module.exports = Object;

84
libjs/wType/string.js Normal file
View file

@ -0,0 +1,84 @@
"use strict";
var Object = require("./object");
var String = Object.inherit({
"className": "String",
"constructor": function(source) {
Object.fn.constructor.call(this);
this._data = "";
this._parseSource(source || "");
},
"destructor": function () {
this.clear();
Object.fn.destructor.call(this);
},
"<": function(other) {
if (!(other instanceof String)) {
throw new Error("Can compare String only with String");
}
return this._data < other._data;
},
">": function(other) {
if (!(other instanceof String)) {
throw new Error("Can compare String only with String");
}
return this._data > other._data;
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return this._data === other._data;
},
"+=": function(str) {
this._data += str.toString();
},
"clear": function() {
this._data = "";
},
"clone": function() {
var clone = new String(this._data);
return clone;
},
"deserialize": function(ba) {
this.clear();
var size = ba.pop32();
for (var i = 0; i < size; ++i) {
var cc = ba.pop16();
this._data += global.String.fromCharCode(cc);
}
},
"length": function() {
return this._data.length;
},
"serialize": function(ba) {
ba.push32(this._data.length);
for (var i = 0; i < this._data.length; ++i) {
var code = this._data.charCodeAt(i);
ba.push16(code);
}
},
"size": function() {
return this._data.length * 2 + 4;
},
"toString": function() {
return this._data;
},
"valueOf": function() {
return this.toString();
},
"_parseSource": function(source) {
if (typeof source !== "string") {
throw new Error("Wrong argument to construct String");
}
this._data = source;
}
});
module.exports = String;

95
libjs/wType/uint64.js Normal file
View file

@ -0,0 +1,95 @@
"use strict";
var Object = require("./object");
var Uint64 = Object.inherit({
"className": "Uint64",
"constructor": function(int) {
Object.fn.constructor.call(this);
this._h = 0;
this._l = 0;
this._parseSource(int || 0);
},
"<": function(other) {
if (!(other instanceof Uint64)) {
throw new Error("Can compare Uint64 only with Uint64");
}
if (this._h < other._h) {
return true;
} else if(this._h === other._h) {
return this._l < other._l;
} else {
return false;
}
},
">": function(other) {
if (!(other instanceof Uint64)) {
throw new Error("Can compare Uint64 only with Uint64");
}
if (this._h > other._h) {
return true;
} else if(this._h === other._h) {
return this._l > other._l;
} else {
return false;
}
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
return (this._h == other._h) && (this._l == other._l);
},
"++": function() {
++this._l;
if (this._l === 4294967296) {
this._l = 0;
++this._h;
}
},
"clone": function() {
var clone = new Uint64();
clone._l = this._l;
clone._h = this._h;
return clone;
},
"deserialize": function(ba) {
this._h = ba.pop32();
this._l = ba.pop32();
},
"length": function() {
return 1;
},
"serialize": function(ba) {
ba.push32(this._h);
ba.push32(this._l);
},
"size": function() {
return 8;
},
"toString": function() {
if (this._h !== 0) {
console.log(this._h);
console.log(this._l);
throw new Error("Don't know yet how to show uint64 in javascript");
}
return this._l.toString();
},
"valueOf": function() {
if (this._h !== 0) {
throw new Error("Don't know yet how to show uint64 in javascript");
}
return this._l;
},
"_parseSource": function(int) {
if (parseInt(int) !== int) {
throw new Error("Wrong argument to construct Uint64");
}
this._l = int & 0xffffffff;
}
});
module.exports = Uint64;

112
libjs/wType/vector.js Normal file
View file

@ -0,0 +1,112 @@
"use strict";
var Object = require("./object");
var Vector = Object.inherit({
"className": "Vector",
"constructor": function() {
Object.fn.constructor.call(this);
this._data = [];
},
"destructor": function() {
this.clear();
Object.fn.destructor.call(this);
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
if (this._data.length !== other._data.length) {
return false;
}
var hopMe;
var hopOt;
for (var i = 0; i < this._data.length; ++i) {
hopMe = this._data[i];
hopOt = other._data[i];
if ( !(hopMe["=="](hopOt)) ) {
return false;
}
}
return true;
},
"at": function(index) {
return this._data[index];
},
"clear": function() {
for (var i = 0; i < this._data.length; ++i) {
this._data[i].destructor();
}
this._data = [];
},
"clone": function() {
var clone = new Vector();
for (var i = 0; i < this._data.length; ++i) {
clone.push(this._data[i].clone());
}
return clone;
},
"deserialize": function(ba) {
this.clear();
var length = ba.pop32();
for (var i = 0; i < length; ++i) {
var value = Object.fromByteArray(ba);
this.push(value);
}
},
"length": function() {
return this._data.length;
},
"push": function(value) {
if (!(value instanceof Object)) {
throw new Error("An attempt to insert not a W::Object into a vector");
}
this._data.push(value);
},
"serialize": function(ba) {
ba.push32(this._data.length);
for (var i = 0; i < this._data.length; ++i) {
var el = this._data[i];
ba.push8(el.getType());
el.serialize(ba);
}
},
"size": function() {
var size = 4;
for (var i = 0; i < this._data.length; ++i) {
size += this._data[i].size() + 1;
}
return size;
},
"toString": function() {
var str = "[";
var ft = true;
for (var i = 0; i < this._data.length; ++i) {
if (ft) {
ft = false;
} else {
str += ", ";
}
str += this._data[i].toString();
}
str += "]";
return str;
}
});
module.exports = Vector;

155
libjs/wType/vocabulary.js Normal file
View file

@ -0,0 +1,155 @@
"use strict";
var Object = require("./object");
var String = require("./string");
var Vocabulary = Object.inherit({
"className": "Vocabulary",
"constructor": function() {
Object.fn.constructor.call(this);
this._data = global.Object.create(null);
this._length = 0;
},
"destructor": function() {
this.clear();
Object.fn.destructor.call(this);
},
"==": function(other) {
if (this.getType() !== other.getType()) {
return false;
}
if (this._length !== other._length) {
return false;
}
var keysMe = this.getKeys();
var key;
var mValue;
var oValue;
for (var i = 0; i < this._length; ++i) {
key = keysMe[i];
oValue = other._data[key];
if (oValue === undefined) {
return false;
}
mValue = this._data[key];
if (!oValue["=="](mValue)) {
return false;
}
}
return true;
},
"at": function(str) {
return this._data[str];
},
"clear": function() {
for (var key in this._data) {
this._data[key].destructor();
}
this._data = global.Object.create(null);
this._length = 0;
},
"clone": function() {
var clone = new Vocabulary();
for (var key in this._data) {
clone._data[key] = this._data[key].clone();
}
clone._length = this._length;
return clone;
},
"deserialize": function(ba) {
this.clear();
this._length = ba.pop32()
for (var i = 0; i < this._length; ++i) {
var key = new String();
key.deserialize(ba);
var value = Object.fromByteArray(ba);
this._data[key.toString()] = value;
}
},
"erase": function(key) {
var value = this._data[key];
if (value === undefined) {
throw new Error("An attempt to erase not existing object from vocabulary");
}
value.destructor();
delete this._data[key];
--this._length;
},
"getKeys": function() {
return global.Object.keys(this._data);
},
"has": function(key) {
return this._data[key] instanceof Object;
},
"insert": function(key, value) {
if (!(value instanceof Object)) {
throw new Error("An attempt to insert not a W::Object into vocabulary");
}
var oldValue = this._data[key];
if (oldValue !== undefined) {
oldValue.destructor();
--this._length;
}
this._data[key] = value
++this._length;
},
"length": function() {
return this._length;
},
"serialize": function(ba) {
ba.push32(this._length);
for (var key in this._data) {
var sKey = new String(key);
var value = this._data[key];
sKey.serialize(ba);
ba.push8(value.getType());
value.serialize(ba);
}
},
"size": function() {
var size = 4;
for (var key in this._data) {
var sKey = new String(key);
var value = this._data[key];
size += sKey.size();
size += value.size() + 1;
}
return size;
},
"toString": function() {
var str = "{";
var ft = true;
for (var key in this._data) {
if (ft) {
ft = false;
} else {
str += ", ";
}
str += key + ": ";
str += this._data[key].toString();
}
str += "}";
return str;
}
});
module.exports = Vocabulary;