Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Int/String thaw logic and compatible version checking

  • Loading branch information...
commit 4647c66c57aa5dd4d6c8d366335705baa2608966 1 parent 7832503
@sorear authored
Showing with 59 additions and 1 deletion.
  1. +1 −1  js/index.html
  2. +58 −0 js/niecza.js
View
2  js/index.html
@@ -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>
View
58 js/niecza.js
@@ -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) {
Please sign in to comment.
Something went wrong with that request. Please try again.