DataStructure added

This commit is contained in:
Abdussamed 2024-10-26 19:13:22 +03:00
parent 385d40fe87
commit 3a5924d46c
1 changed files with 86 additions and 16 deletions

100
memory.js
View File

@ -1,7 +1,24 @@
class ProcessableData { class ProcessableData {
/**
* @param {Uint8Array} data
* @param {number} from
* @param {number} to
*/
constructor(data, from = 0, to){ constructor(data, from = 0, to){
if(data){ if(data)
this.data = data; {
if(data instanceof ArrayBuffer)
{
this.data = new Uint8Array(data);
}else if(data instanceof Uint8Array)
{
this.data = data;
}else if(typeof data == "number")
{
this.data = new Uint8Array(data);
}else{
throw new Error("data is not type ArrayBuffer or Uint8Array");
}
this.init(from, to); this.init(from, to);
} }
} }
@ -256,7 +273,7 @@ class ConstructedData extends ProcessableData {
is4Byte, is4Byte,
is8Byte, is8Byte,
issigned, issigned,
255 0
); );
if(isInt == false) if(isInt == false)
@ -483,6 +500,12 @@ class DataAnchor extends ConstructedData {
} }
class DataStructure { class DataStructure {
constructor(structure){
if(typeof structure == "string")
{
this.setHeadSigner(structure);
}
}
/** @type {boolean} */ /** @type {boolean} */
sign = true; sign = true;
/** @type {string} */ /** @type {string} */
@ -494,6 +517,13 @@ class DataStructure {
/** @type {any} */ /** @type {any} */
value = null; value = null;
size = 1; size = 1;
reset(){
this.size = 1;
this.fixedLength = true;
this.list = true;
this.type = "uint8";
this.sign = true;
}
/** @param {DataAnchor} da */ /** @param {DataAnchor} da */
setData(da){ setData(da){
let dataCost = da.getProcessor(this.type); let dataCost = da.getProcessor(this.type);
@ -534,11 +564,11 @@ class DataStructure {
} }
/** @param {DataAnchor} da */ /** @param {DataAnchor} da */
getData(da){ getData(da){
let result = null, sign; let result = null;
if(this.sign == true) if(this.sign == true)
{ {
// Verinin içine veri türüni işliyor // Verinin içine veri türüni işliyor
sign = da.readUInt8(); this.type = da.readUInt8();
} }
let dataCost = da.getProcessor(this.type); let dataCost = da.getProcessor(this.type);
if(this.fixedLength == false && this.list == true) if(this.fixedLength == false && this.list == true)
@ -551,7 +581,7 @@ class DataStructure {
if(this.list == true) if(this.list == true)
{ {
result = []; result = [];
for (const data of this.value) for (let t = 0;t < this.size; t++)
{ {
result.push(dataCost.read()); result.push(dataCost.read());
} }
@ -562,13 +592,13 @@ class DataStructure {
if(this.list == true) if(this.list == true)
{ {
result = []; result = [];
for (const data of this.value) for (let t = 0;t < this.size; t++)
{ {
let length = da.readVarNumber(data.length); let length = da.readVarNumber();
result.push(dataCost.read(length)); result.push(dataCost.read(length));
} }
}else{ }else{
let length = da.readVarNumber(data.length); let length = da.readVarNumber();
result = dataCost.read(length); result = dataCost.read(length);
} }
} }
@ -576,23 +606,63 @@ class DataStructure {
} }
/** @param {string} dataSigner */ /** @param {string} dataSigner */
setHeadSigner(dataSigner){ setHeadSigner(dataSigner){
let regex = /^(\w+)(\[\d*\])?$/.exec(dataSigner); let regex = /^(\w+)(\?)(\[\d*\])?$/.exec(dataSigner);
if(regex[1] == 'any') if(regex[1] != 'any')
{
this.type = regex[1];
}
if(regex[2] == '?')
{ {
this.sign = true; this.sign = true;
}else{ }else{
this.sign = false; this.sign = false;
this.type = regex[1];
} }
if(regex[2]) if(regex[3])
{ {
this.list = true; this.list = true;
this.fixedLength = regex[2] != '[]' this.fixedLength = regex[3] != '[]'
this.size = regex[2].slice(1,-1) | 0; this.size = regex[3].slice(1,-1) | 0;
}else{ }else{
this.list = false; this.list = false;
} }
} }
getHeadSigner(){
let t = [];
if(this.sign)
{
t.push('any')
}else{
t.push(this.type)
}
if(this.list)
{
if(this.fixedLength == false)
{
t.push('[]')
}else{
t.push('[', this.size,']')
}
}
return t.join('')
}
static ds = new DataStructure();
/**
* @param {string} sign
*/
static settle(da, sign, value, size)
{
this.ds.setHeadSigner(sign);
this.ds.value = value;
typeof size == "number" && (this.ds.size = size);
this.ds.setData(da);
this.ds.reset();
}
static gettle(da, sign)
{
this.ds.setHeadSigner(sign);
return this.ds.getData(da);
}
} }