Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Int/String thaw logic and compatible version checking
  • Loading branch information
sorear committed Aug 16, 2012
1 parent 7832503 commit 4647c66
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/index.html
Expand Up @@ -2,7 +2,7 @@
<head>
<title>Niecza/JS test harness</title>
<script src="niecza.js"></script>
<script>Niecza.Serialization.loadSerFile('CORE.ser', function(){});</script>
<script>Niecza.Serialization.loadSerFile('../obj/Run.CORE.ser', function(){});</script>
</head>
<body>
</body>
Expand Down
58 changes: 58 additions & 0 deletions js/niecza.js
Expand Up @@ -9,10 +9,68 @@ var NSer = Niecza.Serialization = {};
var NSThaw = Niecza.Serialization.Thaw = function (buffer, callback) {
this.buffer = buffer;
this.view = new DataView(buffer);
this.rawView = new Uint8Array(buffer);
this.offset = 0;

try {
var sig = this.string();
if (sig != this.SIGNATURE)
throw "Signature mismatch needed "+SIGNATURE+" got "+sig;
var ver = this.int();
if (ver != this.VERSION)
throw "Version mismatch needed "+VERSION+" got "+ver;
console.log(sig,ver);
} catch(e) {
callback(false, e);
return;
}
callback(true, this);
};

NSThaw.prototype.$className = 'Niecza.Serialization.Thaw';
NSThaw.prototype.SIGNATURE = 'Niecza-Serialized-Module';
NSThaw.prototype.VERSION = 29;

NSThaw.prototype.int = function() {
var shift = 0, accum = 0;
var offset = this.offset, raw = this.rawView;
while (true) {
var b = raw[offset++];
accum |= ((b & 127) << shift);
shift += 7;
if (!(b & 128)) {
if (b & 64) {
accum = accum - (1 << shift);
}
console.log(accum, " at ", offset);
this.offset = offset;
return accum;
}
}
};

NSThaw.prototype.uint = function() {
var shift = 0, accum = 0;
var offset = this.offset, raw = this.rawView;
while (true) {
var b = raw[offset++];
accum |= ((b & 127) << shift);
shift += 7;
if (!(b & 128)) {
console.log(accum, " at ", offset);
this.offset = offset;
return accum;
}
}
};

NSThaw.prototype.string = function() {
var l = this.int();
if (l < 0) return null;
var ary = [];
while (l--) ary[ary.length] = this.uint();
return String.fromCharCode.apply(String, ary); //ick
};


NSer.loadSerFileEnd = function(ev) {
Expand Down

0 comments on commit 4647c66

Please sign in to comment.