ホームページ > ウェブフロントエンド > jsチュートリアル > protobuf.jsとLong.jsの使い方を詳しく解説

protobuf.jsとLong.jsの使い方を詳しく解説

php中世界最好的语言
リリース: 2018-03-16 10:58:41
オリジナル
5087 人が閲覧しました

今回は、protobuf.js と Long.js の使用方法について詳しく説明します。protobuf.js と Long.js を使用する際の 注意事項 は何ですか?実際の事例を見てみましょう。

protobuf.js の構造は、ロード後の webpack の構造と非常によく似ています。この種の

モジュラーの組み合わせは、優れた構造方法です。 1 つは異なる読み込み方法に適応しており、2 つのモジュールは直接独立しています。 webpack の方が高機能です。ただし、js ライブラリを自分でカプセル化する場合は、これで十分です。さらに、モジュールには統合された外部インターフェイス module.exports があります。これはノードと非常によく似ています。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<a href="https://www.php.cn/code/11829.html" target="_blank">(function(global, undefined) {    "use strict";

    (function prelude(modules, cache, entries) {        function $require(name) {            var $module = cache[name];            //没有就去加载

            if (!$module)

                modules[name][0].call($module = cache[name] = { exports: {} }, $require$module$module.exports);            return $module.exports;

        }        //曝光成全局

        var proto = global.proto = $require(entries[0]);        // AMD

        if (typeof define === "function" && define.amd) {

            define(["long"], function(Long) {                if (Long && Long.isLong) {

                    proto.util.Long = Long;

                    proto.configure();

                }

            });            return proto;

        }        //CommonJS

        if (typeof module === "object" && module && module.exports)

            module.exports = proto;

    })    //传参    ({        1: [function (require, module, exports) {            function first() {

                console.log("first");

            }

            module.exports = first;

        }, {}],        2: [function(require, module, exports) {            function second() {

                console.log("second");

            }

            module.exports = second;

        }],        3: [function (require, module, exports) {            var proto = {};

            proto.first = require(1);

            proto.second = require(2);

            proto.build = "full";

            module.exports = proto;

        }]

      }, {}, [3]);

})(typeof window==="object"&&window||typeof self==="object"&&self||this)</a>

16 ビットを超える形状を扱う場合は Long.js を使用する必要があります。 主に fromString と toString です。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

  function fromString(str, unsigned, radix) {        if (str.length === 0)            throw Error('empty string');        if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")            return ZERO;        if (typeof unsigned === 'number') {            // For goog.math.long compatibility

            radix = unsigned,

            unsigned = false;

        else {

            unsigned = !!unsigned;

        }

        radix = radix || 10;        if (radix < 2 || 36 < radix)            throw RangeError(&#39;radix&#39;);        var p;        if ((p = str.indexOf(&#39;-&#39;)) > 0)            throw Error('interior hyphen');        else if (p === 0) {            return fromString(str.substring(1), unsigned, radix).neg();

        }        // Do several (8) digits each time through the loop, so as to

        // minimize the calls to the very expensive emulated p.

        var radixToPower = fromNumber(pow_dbl(radix, 8));        var result = ZERO;        for (var i = 0; i < str.length; i += 8) {            var size = Math.min(8, str.length - i),                value = parseInt(str.substring(i, i + size), radix);            if (size < 8) {                var power = fromNumber(pow_dbl(radix, size));

                result = result.mul(power).add(fromNumber(value));

            } else {                result = result.mul(radixToPower);

                result = result.add(fromNumber(value));

            }

        }

        result.unsigned = unsigned;        return result;

    }

ログイン後にコピー

fromstring のアイデアは、

string

8 桁を 1 つずつインターセプトすることです。次にLong型(上位ビット、位置、符号ビット)に変換して加算します。最後はドラゴンです。 4294967296 は 2 の 32 乗です。各演算の前に基数演算 mul(radixToPower) または mul(power) があり、どちらも結果の桁数が正しいことを確認します。 たとえば、{low:123} と {low:1} を加算する前に、まず {low:123} に 10 を乗算して {low:1230} を取得し、次に {low:1} でビット単位の演算を実行します。最初のビットは上位ビットであるため、直接追加することはできません。

1

2

function fromBits(lowBits, highBits, unsigned) {        return new Long(lowBits, highBits, unsigned);

    }

ログイン後にコピー
fromBitsはLong

object

に変換されます。 value%4294967296 は下位ビットを取得します。 /ハイになりましょう。結果は変位ごとに結合されます。 mul はビットの乗算、add はビットの加算です。 原則として、64 ビット ファイルを 4 つのセグメントに分割します。それぞれ16ビット。 this.low を 16 ビット左にシフトして、Low の 32 ~ 17 ビットを取得します。 次に、add オブジェクトを使用して同じビットを追加します 最終的なマージは | 操作によって行われます。移動後に元に戻すのは本当に賢いです。しばらく理解できなかったようです。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

LongPrototype.add = function add(addend) {        if (!isLong(addend))

            addend = fromValue(addend);        // pide each number into 4 chunks of 16 bits, and then sum the chunks.

        var a48 = this.high >>> 16;        var a32 = this.high & 0xFFFF;        var a16 = this.low >>> 16;        var a00 = this.low & 0xFFFF;        var b48 = addend.high >>> 16;        var b32 = addend.high & 0xFFFF;        var b16 = addend.low >>> 16;        var b00 = addend.low & 0xFFFF;        var c48 = 0, c32 = 0, c16 = 0, c00 = 0;

        c00 += a00 + b00;

        c16 += c00 >>> 16;

        c00 &= 0xFFFF;

        c16 += a16 + b16;

        c32 += c16 >>> 16;

        c16 &= 0xFFFF;

        c32 += a32 + b32;

        c48 += c32 >>> 16;

        c32 &= 0xFFFF;

        c48 += a48 + b48;

        c48 &= 0xFFFF;        return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);

    };

ログイン後にコピー

>>> はどう違いますか? ? 。

toString

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

LongPrototype.toString = function toString(radix) {

        radix = radix || 10;        if (radix < 2 || 36 < radix)            throw RangeError(&#39;radix&#39;);        if (this.isZero())            return &#39;0&#39;;        if (this.isNegative()) { // Unsigned Longs are never negative

            if (this.eq(MIN_VALUE)) {                // We need to change the Long value before it can be negated, so we remove

                // the bottom-most digit in this base and then recurse to do the rest.

                var radixLong = fromNumber(radix),

                    p = this.p(radixLong),

                    rem1 = p.mul(radixLong).sub(this);                return p.toString(radix) + rem1.toInt().toString(radix);

            } else

                return &#39;-&#39; + this.neg().toString(radix);

        }        // Do several (6) digits each time through the loop, so as to

        // minimize the calls to the very expensive emulated p.

        var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),

            rem = this;        var result = &#39;&#39;;        while (true) {            var remp = rem.p(radixToPower),

                intval = rem.sub(remp.mul(radixToPower)).toInt() >>> 0,

                digits = intval.toString(radix);

            rem = remp;            if (rem.isZero())                return digits + result;            else {                while (digits.length < 6)

                    digits = '0' + digits;

                result = '' + digits + result;

            }

        }

    };

ログイン後にコピー

もsubの後に書きます。つまり、fromstring の逆の操作です。

この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、php 中国語 Web サイトの他の関連記事に注目してください。

推奨読書:

興味深いUglifyJS


JSを自動的にproto Jsと一致させる方法

以上がprotobuf.jsとLong.jsの使い方を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート