ton-assembly
Version:
TON assembler and disassembler
114 lines • 3.7 kB
JavaScript
;
/**
* Copyright (c) Whales Corp.
* All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dictionary = void 0;
const core_1 = require("@ton/core");
const core_2 = require("@ton/core");
const serializeDict_1 = require("./serializeDict");
const parseDict_1 = require("./parseDict");
const internalKeySerializer_1 = require("./internalKeySerializer");
class Dictionary {
static Keys = {
/**
* Create integer key
* @param bits bits of integer
* @returns DictionaryKey<number>
*/
Int: (bits) => {
return createIntKey(bits);
},
};
/**
* Create an empty map
* @param key key type
* @param value value type
* @returns Dictionary<K, V>
*/
static empty(key, value) {
if (key && value) {
return new Dictionary(new Map(), key, value);
}
else {
return new Dictionary(new Map(), null, null);
}
}
_key;
_value;
_map;
constructor(values, key, value) {
this._key = key;
this._value = value;
this._map = values;
}
set(key, value) {
this._map.set((0, internalKeySerializer_1.serializeInternalKey)(key), value);
return this;
}
*[Symbol.iterator]() {
for (const [k, v] of this._map) {
const key = (0, internalKeySerializer_1.deserializeInternalKey)(k);
yield [key, v];
}
}
/**
* Low level method for rare dictionaries from system contracts.
* Loads dictionary from slice directly without going to the ref.
*
* @param key key description
* @param value value description
* @param sc slice
* @returns Dictionary<K, V>
*/
static loadDirect(key, value, sc) {
if (!sc) {
return Dictionary.empty(key, value);
}
const slice = sc instanceof core_2.Cell ? sc.beginParse() : sc;
const values = (0, parseDict_1.parseDict)(slice, key.bits, value.parse);
const prepare = new Map();
for (let [k, v] of values) {
prepare.set((0, internalKeySerializer_1.serializeInternalKey)(key.parse(k)), v);
}
return new Dictionary(prepare, key, value);
}
storeDirect(builder) {
if (this._map.size === 0) {
throw Error("Cannot store empty dictionary directly");
}
const resolvedKey = this._key;
const resolvedValue = this._value;
if (!resolvedKey) {
throw Error("Key serializer is not defined");
}
if (!resolvedValue) {
throw Error("Value serializer is not defined");
}
let prepared = new Map();
for (const [k, v] of this._map) {
prepared.set(resolvedKey.serialize((0, internalKeySerializer_1.deserializeInternalKey)(k)), v);
}
(0, serializeDict_1.serializeDict)(prepared, resolvedKey.bits, resolvedValue.serialize, builder);
}
}
exports.Dictionary = Dictionary;
function createIntKey(bits) {
return {
bits: bits,
serialize: src => {
if (!Number.isSafeInteger(src)) {
throw Error("Key is not a safe integer: " + src);
}
return (0, core_1.beginCell)().storeInt(src, bits).endCell().beginParse().loadUintBig(bits);
},
parse: src => {
return (0, core_1.beginCell)().storeUint(src, bits).endCell().beginParse().loadInt(bits);
},
};
}
//# sourceMappingURL=Dictionary.js.map