swarm-ron
Version:
Swarm Replicated Object Notation (RON)
108 lines (83 loc) • 2.38 kB
JavaScript
"use strict";
const Grammar = require('swarm-ron-grammar');
const UUID = require('swarm-ron-uuid');
class Frame {
constructor (buf) {
this.ints = new Uint32Array(16*(4+2));
this.atoms = "";
this.data = buf;
this.offset = 0;
this.position = 0;
}
next () {
// FIXME: match UUID_ATOM 4 times, then match ATOMS
const re = Grammar.OP;
re.lastIndex = this.offset;
const m = re.exec(this.data);
if (!m) {
} else {
for (let i=1; i<=4; i++) {
if (m[i]) {
UUID.read_uuid(this.ints, (i-1)<<2, m[i]);
}
}
this.atoms = m[5] || "";
}
}
/** append an op;
* this frame must be positioned at the last op.
* @param from {Frame} - another frame (to take an op from) */
append (from) {
if (this.offset !== this.data.length) {
throw new Error("not at the last pos!");
}
let buf = "";
const i1 = this.ints;
const i2 = from.ints;
for (let i=0; i<=3; i++) {
const at = i<<2;
if (i1[at]!==i2[at] || i1[at|1]!==i2[at|1] || i1[at|2]!==i2[at|2] || i1[at|3]!==i2[at|3]) {
buf += Grammar.SPEC_PUNCT[i];
buf += UUID.write_uuid(i2, at, i1, at);
}
}
buf += this.atoms;
this.data += buf;
this.ints.set(from.ints, 0);
this.atoms = from.atoms;
this.offset = this.data.length;
}
/** @returns {Number} -- (integer) number atom (no garantees about js 40-bit int overflows) */
integer (idx) {
}
/** @returns {String} -- clean value string */
string (idx) {
}
/** @returns {String} -- raw string, all escapes included */
esc_string (idx) {
}
/** @returns {Number} -- value (floating point) number atom */
number (idx) {
}
/**
* @returns {UUID} -- value UUID atom */
uuid (idx) {
}
/**
* @returns {UUID} -- op replicated data type UUID */
rdtype () {
}
/**
* @returns {UUID} -- op's object UUID */
object () {
}
/**
* @returns {UUID} -- op's event UUID */
event () {
}
/**
* @returns {UUID} -- op reference/location UUID */
ref () {
}
}
module.exports = Frame;