Skip to content

Commit 7c7ea34

Browse files
committed
[js] Serialize MultiDimArray.
1 parent d2f4787 commit 7c7ea34

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

src/vm/js/nqp-runtime/reprs.js

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,13 @@ MultiDimArray.prototype.compose = function(STable, repr_info_hash) {
751751
var type = repr_info_hash.content.get('array').content.get('type');
752752

753753
if (type) {
754-
STable.type = type._STable.REPR.boxed_primitive;
754+
STable.primType = type._STable.REPR.boxed_primitive;
755755
} else {
756-
STable.type = 0;
756+
STable.primType = 0;
757757
}
758758

759+
STable.type = type || null;
760+
759761
if (dimensions instanceof NQPInt) {
760762
dimensions = dimensions.value;
761763
if (dimensions === 0) {
@@ -846,42 +848,42 @@ MultiDimArray.prototype.create_obj_constructor = function(STable) {
846848
});
847849

848850
STable.addInternalMethod('$$atposnd', function(idx) {
849-
if (STable.type != 0) throw new NQPException("wrong type");
851+
if (STable.primType != 0) throw new NQPException("wrong type");
850852
return this.storage[this.$$calculateIndex(idx)];
851853
});
852854

853855
STable.addInternalMethod('$$bindposnd', function(idx, value) {
854-
if (STable.type != 0) throw new NQPException("wrong type: " + STable.type);
856+
if (STable.primType != 0) throw new NQPException("wrong type: " + STable.primType);
855857
return (this.storage[this.$$calculateIndex(idx)] = value);
856858
});
857859

858860
STable.addInternalMethod('$$atposnd_i', function(idx) {
859-
if (STable.type != 1) throw new NQPException("wrong type: " + STable.type);
861+
if (STable.primType != 1) throw new NQPException("wrong type: " + STable.primType);
860862
return this.storage[this.$$calculateIndex(idx)];
861863
});
862864

863865
STable.addInternalMethod('$$bindposnd_i', function(idx, value) {
864-
if (STable.type != 1) throw new NQPException("wrong type" + STable.type);
866+
if (STable.primType != 1) throw new NQPException("wrong type" + STable.primType);
865867
return (this.storage[this.$$calculateIndex(idx)] = value);
866868
});
867869

868870
STable.addInternalMethod('$$atposnd_n', function(idx) {
869-
if (STable.type != 2) throw new NQPException("wrong type");
871+
if (STable.primType != 2) throw new NQPException("wrong type");
870872
return this.storage[this.$$calculateIndex(idx)];
871873
});
872874

873875
STable.addInternalMethod('$$bindposnd_n', function(idx, value) {
874-
if (STable.type != 2) throw new NQPException("wrong type");
876+
if (STable.primType != 2) throw new NQPException("wrong type");
875877
return (this.storage[this.$$calculateIndex(idx)] = value);
876878
});
877879

878880
STable.addInternalMethod('$$atposnd_s', function(idx) {
879-
if (STable.type != 3) throw new NQPException("wrong type");
881+
if (STable.primType != 3) throw new NQPException("wrong type");
880882
return this.storage[this.$$calculateIndex(idx)];
881883
});
882884

883885
STable.addInternalMethod('$$bindposnd_s', function(idx, value) {
884-
if (STable.type != 3) throw new NQPException("wrong type");
886+
if (STable.primType != 3) throw new NQPException("wrong type");
885887
return (this.storage[this.$$calculateIndex(idx)] = value);
886888
});
887889

@@ -906,4 +908,79 @@ MultiDimArray.prototype.create_obj_constructor = function(STable) {
906908
return c;
907909
};
908910

911+
MultiDimArray.prototype.serialize_repr_data = function(st, cursor) {
912+
if (st.dimensions) {
913+
cursor.varint(st.dimensions);
914+
cursor.ref(st.type);
915+
} else {
916+
cursor.varint(0);
917+
}
918+
919+
};
920+
921+
MultiDimArray.prototype.deserialize_repr_data = function(cursor, STable) {
922+
var dims = cursor.varint();
923+
if (dims > 0) {
924+
STable.dimensions = dims;
925+
STable.type = cursor.variant();
926+
STable.primType = STable.type ? STable.type._STable.REPR.boxed_primitive : 0;
927+
}
928+
};
929+
930+
MultiDimArray.prototype.valuesSize = function(object) {
931+
var size = 1;
932+
for (var i = 0; i < object.dimensions.length; i++) {
933+
size = size * object.dimensions[i];
934+
}
935+
return size;
936+
};
937+
938+
MultiDimArray.prototype.serialize = function(cursor, obj) {
939+
for (var i = 0; i < obj._STable.dimensions; i++) {
940+
cursor.varint(obj.dimensions[i]);
941+
}
942+
var size = this.valuesSize(obj);
943+
for (var i=0;i < size;i++) {
944+
switch (obj._STable.primType) {
945+
case 0:
946+
cursor.ref(obj.storage[i]);
947+
break;
948+
case 1:
949+
cursor.varint(obj.storage[i]);
950+
break;
951+
case 2:
952+
cursor.double(obj.storage[i]);
953+
break;
954+
case 3:
955+
cursor.str(obj.storage[i]);
956+
break;
957+
}
958+
}
959+
}
960+
961+
MultiDimArray.prototype.deserialize_finish = function(object, data) {
962+
object.dimensions = [];
963+
for (var i = 0; i < object._STable.dimensions; i++) {
964+
object.dimensions[i] = data.varint();
965+
}
966+
var size = this.valuesSize(object);
967+
object.storage = [];
968+
for (var i=0;i < size;i++) {
969+
switch (object._STable.primType) {
970+
case 0:
971+
object.storage[i] = data.variant();
972+
break;
973+
case 1:
974+
object.storage[i] = data.varint();
975+
break;
976+
case 2:
977+
object.storage[i] = data.double();
978+
break;
979+
case 3:
980+
object.storage[i] = data.str();
981+
break;
982+
}
983+
}
984+
};
985+
909986
module.exports.MultiDimArray = MultiDimArray;

0 commit comments

Comments
 (0)