123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- "use strict";
- if (typeof Uint16Array == "undefined"){
- global.Uint16Array = function(length){
- Array.call(this, length);
- for(var i = 0; i < length; ++i)
- this[i] = 0;
- };
- }
- function applyMap(from, to){
- for(var p in from)
- to[p] = from[p];
- }
- function Class(){}
- Class.extend = function extend(methods){
- function Type(){
- applyMap(methods, this);
- }
- Type.prototype = this.prototype;
- var result = methods.init;
- result.prototype = new Type(); // inherit this.prototype
- result.prototype.constructor = result; // to see constructor name in diagnostic
-
- result.extend = extend;
- return result;
- };
- var methods = {
- extend: function(cons, base, scope){
- function Type(){}
- Type.prototype = base.prototype;
- cons.prototype = new Type();
- cons.prototype.constructor = cons;
- cons.prototype.$scope = scope;
- },
- typeGuard: function(from, to){
- if (!from)
- return from;
- if (!(from instanceof to)){
- var fromStr;
- var toStr;
- var scope;
-
- if (from && from.constructor && from.constructor.name){
- var name = from.constructor.name;
- scope = from.$scope;
- fromStr = scope ? scope + "." + name : name;
- }
- else
- fromStr = "" + from;
-
- if (to.name){
- toStr = "" + to.name;
- scope = to.prototype.$scope;
- toStr = scope ? scope + "." + toStr : toStr;
- }
- else
- toStr = "" + to;
-
- var msg = "cannot cast";
- if (fromStr || toStr)
- msg += " to '" + toStr + "' from '" + fromStr + "'";
- throw new Error(msg);
- }
- return from;
- },
- charAt: function(s, index){
- if (index >= 0 && index < s.length)
- return s.charCodeAt(index);
- throw new Error("index out of bounds: " + index);
- },
- getAt: function(where, index){
- if (index >= 0 && index < where.length)
- return where[index];
- throw new Error("index out of bounds: " + index);
- },
- putAt: function(where, index, what){
- if (index >= 0 && index < where.length)
- where[index] = what;
- else
- throw new Error("index out of bounds: " + index);
- },
- makeArray: function(/*dimensions, initializer*/){
- var forward = Array.prototype.slice.call(arguments);
- var result = new Array(forward.shift());
- var i;
- if (forward.length == 1){
- var init = forward[0];
- if (typeof init == "function")
- for(i = 0; i < result.length; ++i)
- result[i] = init();
- else
- for(i = 0; i < result.length; ++i)
- result[i] = init;
- }
- else
- for(i = 0; i < result.length; ++i)
- result[i] = this.makeArray.apply(this, forward);
- return result;
- },
- __setupCharArrayMethods: function(a){
- var rtl = this;
- a.charCodeAt = function(i){return this[i];};
- a.slice = function(){
- var result = Array.prototype.slice.apply(this, arguments);
- rtl.__setupCharArrayMethods(result);
- return result;
- };
- a.toString = function(){
- return String.fromCharCode.apply(this, this);
- };
- },
- __makeCharArray: function(length){
- var result = new Uint16Array(length);
- this.__setupCharArrayMethods(result);
- return result;
- },
- makeCharArray: function(/*dimensions*/){
- var forward = Array.prototype.slice.call(arguments);
- var length = forward.pop();
- if (!forward.length)
- return this.__makeCharArray(length);
- function makeArray(){
- var forward = Array.prototype.slice.call(arguments);
- var result = new Array(forward.shift());
- var i;
- if (forward.length == 1){
- var init = forward[0];
- for(i = 0; i < result.length; ++i)
- result[i] = init();
- }
- else
- for(i = 0; i < result.length; ++i)
- result[i] = makeArray.apply(undefined, forward);
- return result;
- }
- forward.push(this.__makeCharArray.bind(this, length));
- return makeArray.apply(undefined, forward);
- },
- makeSet: function(/*...*/){
- var result = 0;
-
- function checkBit(b){
- if (b < 0 || b > 31)
- throw new Error("integers between 0 and 31 expected, got " + b);
- }
- function setBit(b){
- checkBit(b);
- result |= 1 << b;
- }
-
- for(var i = 0; i < arguments.length; ++i){
- var b = arguments[i];
- if (b instanceof Array){
- var from = b[0];
- var to = b[1];
- if (to < from)
- throw new Error("invalid SET diapason: " + from + ".." + to);
- for(var bi = from; bi <= to; ++bi)
- setBit(bi);
- }
- else
- setBit(b);
- }
- return result;
- },
- makeRef: function(obj, prop){
- return {set: function(v){ obj[prop] = v; },
- get: function(){ return obj[prop]; }};
- },
- setInclL: function(l, r){return (l & r) == l;},
- setInclR: function(l, r){return (l & r) == r;},
- assignArrayFromString: function(a, s){
- var i;
- for(i = 0; i < s.length; ++i)
- a[i] = s.charCodeAt(i);
- for(i = s.length; i < a.length; ++i)
- a[i] = 0;
- },
- strCmp: function(s1, s2){
- var cmp = 0;
- var i = 0;
- while (!cmp && i < s1.length && i < s2.length){
- cmp = s1.charCodeAt(i) - s2.charCodeAt(i);
- ++i;
- }
- return cmp ? cmp : s1.length - s2.length;
- },
- copy: function(from, to, type){
- var r = type.record;
- if (r){
- for(var f in r){
- var fieldType = r[f];
- if (fieldType)
- this.copy(from[f], to[f], fieldType);
- else
- to[f] = from[f];
- }
- return;
- }
- var a = type.array;
- if (a !== undefined ){
- if (a === null)
- // shallow copy
- Array.prototype.splice.apply(to, [0, to.length].concat(from));
- else {
- // deep copy
- to.splice(0, to.length);
- for(var i = 0; i < from.length; ++i)
- to.push(this.clone(from[i], a));
- }
- }
- },
- clone: function(from, type, recordCons){
- var result;
- var r = type.record;
- if (r){
- var Ctr = recordCons || from.constructor;
- result = new Ctr();
- this.copy(from, result, type);
- return result;
- }
- var a = type.array;
- if (a !== undefined ){
- if (a === null)
- // shallow clone
- return from.slice();
- // deep clone
- var length = from.length;
- result = new Array(length);
- for(var i = 0; i < length; ++i)
- result[i] = this.clone(from[i], a);
- return result;
- }
- },
- assert: function(condition){
- if (!condition)
- throw new Error("assertion failed");
- }
- };
- exports.Class = Class;
- exports.rtl = {
- dependencies: {
- "copy": ["clone"],
- "clone": ["copy"],
- "makeCharArray": ["__makeCharArray"],
- "__makeCharArray": ["__setupCharArrayMethods"]
- },
- methods: methods,
- nodejsModule: "rtl.js"
- };
- exports.applyMap = applyMap;
- applyMap(methods, exports);
|