Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/js/src/jit-test/lib/   (Firefox Browser Version 153.0.1©)  Datei vom 27.6.2026 mit Größe 30 kB image not shown  

Quellcode-Bibliothek wasm-binary.js

  Sprache: JAVA
 

// MagicNumber = 0x6d736100;
const magic0 = 0x00;  // '\0'
const magic1 = 0x61;  // 'a'
const magic2 = 0x73;  // 's'
const magic3 = 0x6d;  // 'm'

// EncodingVersion
const encodingVersion = 0x1;
const ver0 = (encodingVersion >>>  0) & 0xff;
const ver1 = (encodingVersion >>>  8) & 0xff;
const ver2 = (encodingVersion >>> 16) & 0xff;
const ver3 = (encodingVersion >>> 24) & 0xff;

// Section opcodes
const userDefinedId    = 0;
const typeId           = 1;
const importId         = 2;
const functionId       = 3;
const tableId          = 4;
const memoryId         = 5;
const globalId         = 6;
const exportId         = 7;
const startId          = 8;
const elemId           = 9;
const codeId           = 10;
const dataId           = 11;
const dataCountId      = 12;
const tagId            = 13;

// User-defined section names
const nameName         = "name";

// Name section name types
const nameTypeModule    = 0;
const nameTypeFunction  = 1;
const nameTypeLocal     = 2;
const nameTypeTag       = 3;

// Type codes
const I32Code          = 0x7f;
const I64Code          = 0x7e;
const F32Code          = 0x7d;
const F64Code          = 0x7c;
const V128Code         = 0x7b;
const FuncRefCode      = 0x70;
const ExternRefCode    = 0x6f;
const AnyRefCode       = 0x6e;
const EqRefCode        = 0x6d;
const OptRefCode       = 0x63; // (ref null $t), needs heap type immediate
const RefCode          = 0x64; // (ref $t), needs heap type immediate
const FuncCode         = 0x60;
const StructCode       = 0x5f;
const ArrayCode        = 0x5e;
const VoidCode         = 0x40;
const BadType          = 0x79; // reserved for testing
const RecGroupCode     = 0x4e;
const SubFinalTypeCode = 0x4f;
const SubNoFinalTypeCode = 0x50;

// Opcodes
const UnreachableCode  = 0x00
const BlockCode        = 0x02;
const TryCode          = 0x06;
const CatchCode        = 0x07;
const ThrowCode        = 0x08;
const RethrowCode      = 0x09;
const EndCode          = 0x0b;
const ReturnCode       = 0x0f;
const CallCode         = 0x10;
const CallIndirectCode = 0x11;
const ReturnCallCode   = 0x12;
const ReturnCallIndirectCode = 0x13;
const ReturnCallRefCode      = 0x15;
const DelegateCode     = 0x18;
const DropCode         = 0x1a;
const SelectCode       = 0x1b;
const LocalGetCode     = 0x20;
const I32Load          = 0x28;
const I64Load          = 0x29;
const F32Load          = 0x2a;
const F64Load          = 0x2b;
const I32Load8S        = 0x2c;
const I32Load8U        = 0x2d;
const I32Load16S       = 0x2e;
const I32Load16U       = 0x2f;
const I64Load8S        = 0x30;
const I64Load8U        = 0x31;
const I64Load16S       = 0x32;
const I64Load16U       = 0x33;
const I64Load32S       = 0x34;
const I64Load32U       = 0x35;
const I32Store         = 0x36;
const I64Store         = 0x37;
const F32Store         = 0x38;
const F64Store         = 0x39;
const I32Store8        = 0x3a;
const I32Store16       = 0x3b;
const I64Store8        = 0x3c;
const I64Store16       = 0x3d;
const I64Store32       = 0x3e;
const GrowMemoryCode   = 0x40;
const I32ConstCode     = 0x41;
const I64ConstCode     = 0x42;
const F32ConstCode     = 0x43;
const F64ConstCode     = 0x44;
const I32AddCode       = 0x6a;
const I32DivSCode      = 0x6d;
const I32DivUCode      = 0x6e;
const I32RemSCode      = 0x6f;
const I32RemUCode      = 0x70;
const I32TruncSF32Code = 0xa8;
const I32TruncUF32Code = 0xa9;
const I32TruncSF64Code = 0xaa;
const I32TruncUF64Code = 0xab;
const I64TruncSF32Code = 0xae;
const I64TruncUF32Code = 0xaf;
const I64TruncSF64Code = 0xb0;
const I64TruncUF64Code = 0xb1;
const I64DivSCode      = 0x7f;
const I64DivUCode      = 0x80;
const I64RemSCode      = 0x81;
const I64RemUCode      = 0x82;
const RefNullCode      = 0xd0;
const RefIsNullCode    = 0xd1;
const RefFuncCode      = 0xd2;

// SIMD opcodes
const V128LoadCode = 0x00;
const V128StoreCode = 0x0b;

// Relaxed SIMD opcodes.
const I8x16RelaxedSwizzleCode = 0x100;
const I32x4RelaxedTruncSSatF32x4Code = 0x101;
const I32x4RelaxedTruncUSatF32x4Code = 0x102;
const I32x4RelaxedTruncSatF64x2SZeroCode = 0x103;
const I32x4RelaxedTruncSatF64x2UZeroCode = 0x104;
const F32x4RelaxedMaddCode = 0x105;
const F32x4RelaxedNmaddCode = 0x106;
const F64x2RelaxedMaddCode = 0x107;
const F64x2RelaxedNmaddCode = 0x108;
const I8x16RelaxedLaneSelectCode = 0x109;
const I16x8RelaxedLaneSelectCode = 0x10a;
const I32x4RelaxedLaneSelectCode = 0x10b;
const I64x2RelaxedLaneSelectCode = 0x10c;
const F32x4RelaxedMinCode = 0x10d;
const F32x4RelaxedMaxCode = 0x10e;
const F64x2RelaxedMinCode = 0x10f;
const F64x2RelaxedMaxCode = 0x110;
const I16x8RelaxedQ15MulrSCode = 0x111;
const I16x8RelaxedDotI8x16I7x16SCode = 0x112;
const I32x4RelaxedDotI8x16I7x16AddSCode = 0x113;

const FirstInvalidOpcode = 0xc5;
const LastInvalidOpcode = 0xfa;
const GcPrefix = 0xfb;
const MiscPrefix = 0xfc;
const SimdPrefix = 0xfd;
const ThreadPrefix = 0xfe;
const MozPrefix = 0xff;

// See WasmConstants.h for documentation.
// Limit this to a group of 8 per line.

const definedOpcodes =
    [0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
     0x06, 0x07, 0x08, 0x09,
     0x0a,
     0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
     0x10, 0x11,
     0x12, 0x13,
     0x14,
     0x15,
     0x18, 0x19,
     0x1a, 0x1b, 0x1c,
     0x1f,
     0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
     0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
     0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
     0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
     0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
     0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
     0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
     0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
     0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
     0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
     0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
     0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
     0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
     0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
     0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
     0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
     0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
     0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
     0xc0, 0xc1, 0xc2, 0xc3, 0xc4,
     0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
     0xf0,
     0xfb, 0xfc, 0xfd, 0xfe, 0xff ];

const undefinedOpcodes = (function () {
    let a = [];
    let j = 0;
    let i = 0;
    while (i < 256) {
        while (definedOpcodes[j] > i)
            a.push(i++);
        assertEq(definedOpcodes[j], i);
        i++;
        j++;
    }
    assertEq(definedOpcodes.length + a.length, 256);
    return a;
})();

// Secondary opcode bytes for misc prefix
const MemoryInitCode = 0x08;    // Pending
const DataDropCode = 0x09;      // Pending
const MemoryCopyCode = 0x0a;    // Pending
const MemoryFillCode = 0x0b;    // Pending
const TableInitCode = 0x0c;     // Pending
const ElemDropCode = 0x0d;      // Pending
const TableCopyCode = 0x0e;     // Pending

const StructNew = 0x00;         // UNOFFICIAL
const StructNewDefault = 0x01;  // UNOFFICIAL
const StructGet = 0x03;         // UNOFFICIAL
const StructSet = 0x06;         // UNOFFICIAL

// External type codes (used for imports/exports)
const ExternFuncCode   = 0x00;
const ExternTableCode  = 0x01;
const ExternMemCode    = 0x02;
const ExternGlobalCode = 0x03;
const ExternTagCode    = 0x04;

// ResizableFlags
const HasMaximumFlag   = 0x1;

function toU8(array) {
    for (const [i, b] of array.entries()) {
        assertEq(b < 256true, `expected byte at index ${i} but got ${b}`);
    }
    return Uint8Array.from(array);
}

function toResizableU8(array) {
    let sab = new ArrayBuffer(array.length, { maxByteLength: array.length });
    let view = new Uint8Array(sab);
    for (const [i, b] of array.entries()) {
        assertEq(b < 256true, `expected byte at index ${i} but got ${b}`);
        view[i] = b;
    }
    return view;
}

function toSharedU8(array) {
    let sab = new SharedArrayBuffer(array.length);
    let view = new Uint8Array(sab);
    for (const [i, b] of array.entries()) {
        assertEq(b < 256true, `expected byte at index ${i} but got ${b}`);
        view[i] = b;
    }
    return view;
}

function toGrowableSharedU8(array) {
    let sab = new SharedArrayBuffer(array.length, { maxByteLength: array.length });
    let view = new Uint8Array(sab);
    for (const [i, b] of array.entries()) {
        assertEq(b < 256true, `expected byte at index ${i} but got ${b}`);
        view[i] = b;
    }
    return view;
}

function varU32(u32) {
    assertEq(u32 >= 0true, `varU32 input must be number between 0 and 2^32-1, got ${u32}`);
    assertEq(u32 < Math.pow(2,32), true, `varU32 input must be number between 0 and 2^32-1, got ${u32}`);
    var bytes = [];
    do {
        var byte = u32 & 0x7f;
        u32 >>>= 7;
        if (u32 != 0)
            byte |= 0x80;
        bytes.push(byte);
    } while (u32 != 0);
    return bytes;
}

function varS32(s32) {
    assertEq(s32 >= -Math.pow(2,31), true, `varS32 input must be number between -2^31 and 2^31-1got ${s32}`);
    assertEq(s32 < Math.pow(2,31), true, `varS32 input must be number between -2^31 and 2^31-1, got ${s32}`);
    var bytes = [];
    do {
        var byte = s32 & 0x7f;
        s32 >>= 7;
        if (s32 != 0 && s32 != -1)
            byte |= 0x80;
        bytes.push(byte);
    } while (s32 != 0 && s32 != -1);
    return bytes;
}

function varU64(u64) {
    u64 = BigInt(u64);
    assertEq(u64 >= 0n, true, `varU64 input must be number between 0 and 2^64-1, got ${u64}`);
    assertEq(u64 < 2n**64n, true, `varU64 input must be number between 0 and 2^64-1, got ${u64}`);
    var bytes = [];
    do {
        var byte = Number(u64 & 0x7fn);
        u64 >>= 7n;
        if (u64 !== 0n)
            byte |= 0x80;
        bytes.push(byte);
    } while (u64 !== 0n);
    return bytes;
}

function moduleHeaderThen(...rest) {
    return [magic0, magic1, magic2, magic3, ver0, ver1, ver2, ver3, ...rest];
}

function string(name) {
    var nameBytes = name.split('').map(c => {
        var code = c.charCodeAt(0);
        assertEq(code < 128true); // TODO
        return code
    });
    return varU32(nameBytes.length).concat(nameBytes);
}

function encodedString(name, len) {
    var name = unescape(encodeURIComponent(name)); // break into string of utf8 code points
    var nameBytes = name.split('').map(c => c.charCodeAt(0)); // map to array of numbers
    return varU32(len === undefined ? nameBytes.length : len).concat(nameBytes);
}

function moduleWithSections(sections, toBuffer=toU8) {
    const bytes = moduleHeaderThen();
    for (const section of sections) {
        bytes.push(section.name);
        bytes.push(...varU32(section.length ?? section.body.length));
        for (let byte of section.body) {
            bytes.push(byte);
        }
    }
    return toBuffer(bytes);
}

/**
 * Creates a type section for a module. Example:
 *
 *     typeSection([
 *         // (type (func (param i32 i64)))
 *         { kind: FuncCode, args: [I32Code, I64Code], ret: [] },
 *         // (type (func (result (ref 123))))
 *         { kind: FuncCode, args: [], ret: [[RefCode, ...varS32(123)]] },
 *
 *         // GC types are supported:
 *         { kind: StructCode, fields: [I32Code, { mut: true, type: [RefCode, ...varS32(123)] }] },
 *         { kind: ArrayCode, elem: { mut: true, type: I32Code } }] },
 *         { kind: ArrayCode, elem: { mut: true, type: [RefCode, ...varS32(123)] } }] },
 *
 *         // Recursion groups can be created with the recGroup function
 *         recGroup([
 *             { kind: StructCode, fields: [I32Code, I64Code] },
 *             { kind: StructCode, sub: 5, fields: [I32Code, I64Code, I32Code] },
 *         ]),
 *     ])
 *
 * ## Full documentation
 *
 * This function takes an array of type objects in one of the following formats:
 *
 *     { kind: FuncCode, args: <ResultType>, ret: <ResultType> }
 *     { kind: StructCode, fields: [<FieldType>] }
 *     { kind: ArrayCode, elem: <FieldType> }
 *
 * Each type object can also have the following optional fields:
 *
 *   - `sub: <number>`: Makes the type a subtype of the given type index.
 *     By default it will not have any parent types.
 *   - `final: <boolean>`: Controls whether the type is final. Default `true`.
 *
 * And finally, types can be placed in a recursion group by wrapping them
 * with the `recGroup` function.
 *
 * ### ResultType
 *
 * A result type is a vector of value types. You provide this as an array
 * where each entry is the bytes for the type. For example, for a function
 * with `(return i32 (ref 123))`, you might provide:
 *
 *     [[I32Code], [RefCode, ...varS32(123)]]
 *
 * If a value type is only a single byte, you can pass it directly instead of
 * passing an array:
 *
 *     [I32Code, [RefCode, ...varS32(123)]]
 *
 * If there is only a single value type, you can omit the outer array too:
 *
 *     I32Code // same as [I32Code], same as [[I32Code]]
 *
 * And finally, `VoidCode` is a special case that results in an empty vector.
 *
 *     VoidCode // same as []
 *
 * Note that if you want to encode a single type, but that type has multiple
 * bytes, you will need to keep the outermost array.
 *
 *     [I32Code, I64Code]        // sugar for [[I32Code], [I64Code]], so two types
 *     [RefCode, ...varS32(123)] // will be interpreted as [[RefCode], [123]],
 *                               // i.e. two types - not what you want
 *
 * ### FieldType
 *
 * A field type is used for struct and array values, and is a value type plus
 * mutability info. The general form looks like:
 *
 *     { mut: <boolean>, type: <bytes> }
 *
 * For example, `(mut i32)` would look like:
 *
 *     { mut: true, type: [I32Code] }
 *
 * If the type is a single byte, you can omit the array:
 *
 *     { mut: true, type: I32Code }
 *
 * And if you wish for the field to be immutable, you can provide the type only:
 *
 *     I32Code // same as { mut: false, type: I32Code }
 *
 */

function typeSection(types) {
    var body = [];
    body.push(...varU32(types.length)); // technically a count of recursion groups
    for (const type of types) {
        if (type.isRecursionGroup) {
            body.push(RecGroupCode);
            body.push(...varU32(type.types.length));
            for (const t of type.types) {
                for (const byte of _encodeType(t)) {
                    body.push(byte);
                }
            }
        } else {
            for (const byte of _encodeType(type)) {
                body.push(byte);
            }
        }
    }
    return { name: typeId, body };
}

function recGroup(types) {
    return { isRecursionGroup: true, types };
}

/**
 * Returns a "normalized" version of all the ResultType stuff from `typeSection`,
 * i.e. an array of array of bytes for each value type.
 */

function _resultType(input) {
    if (input === VoidCode) {
        return [];
    }
    if (typeof input === "number") {
        input = [input];
    }
    input = input.map(valType => Array.isArray(valType) ? valType : [valType]);
    return input;
}

/**
 * Returns a "normalized" version of FieldType from `typeSection`, i.e. an object
 * of the form `{ mut: <boolean>, type: <bytes> }`.
 */

function _fieldType(input) {
    if (typeof input !== "object" || Array.isArray(input)) {
        input = { mut: false, type: input };
    }
    if (!Array.isArray(input.type)) {
        input.type = [input.type];
    }
    return input;
}

/**
 * Encodes a type object from `typeSection`. This basically corresponds to `subtypeDef`
 * in the GC spec doc.
 */

function _encodeType(typeObj) {
    const typeBytes = [];
    // Types are now final by default.
    const final = typeObj.final ?? true;
    if (typeObj.sub !== undefined) {
        typeBytes.push(final ? SubFinalTypeCode : SubNoFinalTypeCode);
        typeBytes.push(...varU32(1), ...varU32(typeObj.sub));
    }
    else if (final == false) {
        // This type is extensible even if no supertype is defined.
        typeBytes.push(SubNoFinalTypeCode);
        typeBytes.push(0x00);
    }
    typeBytes.push(typeObj.kind);
    switch (typeObj.kind) {
    case FuncCode: {
        const args = _resultType(typeObj.args);
        const ret = _resultType(typeObj.ret);
        typeBytes.push(...varU32(args.length));
        for (const t of args) {
            typeBytes.push(...t);
        }
        typeBytes.push(...varU32(ret.length));
        for (const t of ret) {
            typeBytes.push(...t);
        }
    } break;
    case StructCode: {
        // fields
        typeBytes.push(...varU32(typeObj.fields.length));
        for (const f of typeObj.fields) {
            typeBytes.push(..._encodeFieldType(f));
        }
    } break;
    case ArrayCode: {
        // elem
        typeBytes.push(..._encodeFieldType(typeObj.elem));
    } break;
    default:
        throw new Error(`unknown type kind ${typeObj.kind} in type section`);
    }
    return typeBytes;
}

function _encodeFieldType(fieldTypeObj) {
    fieldTypeObj = _fieldType(fieldTypeObj);
    return [...fieldTypeObj.type, fieldTypeObj.mut ? 0x01 : 0x00];
}

/**
 * A convenience function to create a type section containing only function
 * types. This is basically sugar for `typeSection`, although you do not have
 * to provide `kind: FuncCode` on each definition as you would there.
 *
 * Example:
 *
 *     sigSection([
 *         // (type (func (param i32 i64)))
 *         { args: [I32Code, I64Code], ret: [] },
 *         // (type (func (result (ref 123))))
 *         { args: [], ret: [[RefCode, ...varS32(123)]] },
 *     ])
 *
 */

function sigSection(sigs) {
    return typeSection(sigs.map(sig => ({ kind: FuncCode, ...sig })));
}

function declSection(decls) {
    var body = [];
    body.push(...varU32(decls.length));
    for (let decl of decls)
        body.push(...varU32(decl));
    return { name: functionId, body };
}

function funcBody(func, withEndCode=true) {
    var body = varU32(func.locals.length);
    for (let local of func.locals)
        body.push(...varU32(local));
    for (let byte of func.body) {
        body.push(byte);
    }
    if (withEndCode)
        body.push(EndCode);
    body.splice(00, ...varU32(body.length));
    return body;
}

function bodySection(bodies) {
    var body = varU32(bodies.length).concat(...bodies);
    return { name: codeId, body };
}

/**
 * Encode an import section:
 * https://webassembly.github.io/spec/core/binary/modules.html#binary-importsec
 *
 *     importSection([
 *         // Normal encoding
 *         { module: "a", item: "b", type: externtype({ funcTypeIndex: 123 }) },
 *
 *         // Compact encoding 1
 *         { module: "a", items: [
 *             { item: "c", type: externtype({ globalType: globalType({ valType: I32Code, mut: true }) }) },
 *             { item: "d", type: externtype({ memType: limits({ min: 0 }) }) },
 *         ]},
 *
 *         // Compact encoding 2
 *         {
 *             module: "a",
 *             type: externtype({ globalType: globalType({ valType: [RefCode, ExternRefCode] }) }),
 *             items: ["e", "f", "g"],
 *         },
 *     ]);
 */

function importSection(importGroups) {
    var body = [];
    body.push(...varU32(importGroups.length));
    for (let group of importGroups) {
        body.push(...string(group.module));
        if (group.items) {
            if (group.type) {
                // Compact encoding (one module/type, many item names)
                body.push(...string(""));
                body.push(0x7E);
                body.push(...group.type);
                body.push(...varU32(group.numItems ?? group.items.length));
                for (const item of group.items) {
                    body.push(...string(item));
                }
            } else {
                // Compact encoding (one module, many name/type pairs)
                body.push(...string(""));
                body.push(0x7F);
                body.push(...varU32(group.numItems ?? group.items.length));
                for (const item of group.items) {
                    body.push(...string(item.item));
                    body.push(...item.type);
                }
            }
        } else {
            // Single-item encoding
            body.push(...string(group.item));
            body.push(...group.type);
        }
    }
    return { name: importId, body };
}

function exportSection(exports) {
    var body = [];
    body.push(...varU32(exports.length));
    for (let exp of exports) {
        body.push(...string(exp.name));
        if (exp.hasOwnProperty("funcIndex")) {
            body.push(...varU32(ExternFuncCode));
            body.push(...varU32(exp.funcIndex));
        } else if (exp.hasOwnProperty("memIndex")) {
            body.push(...varU32(ExternMemCode));
            body.push(...varU32(exp.memIndex));
        } else if (exp.hasOwnProperty("tagIndex")) {
            body.push(...varU32(ExternTagCode));
            body.push(...varU32(exp.tagIndex));
        } else {
            throw "Bad export " + exp;
        }
    }
    return { name: exportId, body };
}

/**
 * Encode a table section:
 * https://wasm-dsl.github.io/spectec/core/binary/modules.html#table-section
 *
 *     tableSection([
 *         // (table 10 100 funcref), two ways
 *         { type: tableType(FuncRefCode, { min: 10, max: 100 }) },
 *         { elemType: FuncRefCode, min: 10, max: 100 },
 *
 *         // (table i64 10 (ref func) ref.func 123), two ways
 *         {
 *             type: tableType([RefCode, FuncRefCode], { addrType: "i64", min: 10n }),
 *             init: [RefFuncCode, ...varU32(123), EndCode],
 *         },
 *         {
 *             elemType: [RefCode, FuncRefCode],
 *             addrType: "i64", min: 10n,
 *             init: [RefFuncCode, ...varU32(123), EndCode],
 *         },
 *     ]);
 */

function tableSection(tables) {
    var body = [];
    body.push(...varU32(tables.length));
    for (const table of tables) {
        if (table.init) {
            body.push(0x40, 0x00);
        }
        if (table.type) {
            body.push(...table.type);
        } else {
            body.push(...tableType(table.elemType, limits({
                addrType: table.addrType ?? "i32",
                min: table.min, max: table.max,
            })));
        }
        if (table.init) {
            body.push(...table.init);
        }
    }
    return { name: tableId, body };
}

/**
 * Encode a table section of funcs with the given initial size (and no max).
 * Useful for the typical wasm 1.0 use of tables.
 */

function defaultTableSection(initialSize) {
    return tableSection([{ elemType: FuncRefCode, min: initialSize }]);
}

/**
 * Create limits:
 * https://wasm-dsl.github.io/spectec/core/binary/types.html#binary-limits
 *
 *     limits({ min: 0 })
 *     limits({ addrType: "i64", min: 0n, max: 10n })
 */

function limits({ addrType = "i32", min, max }) {
    var body = [];
    body.push((addrType === "i64" ? 0x04 : 0x00) & (max === undefined ? 0x00 : 0x01));
    body.push(...varU64(min));
    if (max !== undefined) {
        body.push(...varU64(max));
    }
    return body;
}

/**
 * Create a table type:
 * https://wasm-dsl.github.io/spectec/core/binary/types.html#table-types
 *
 *     tableType(FuncRefCode, limits({ min: 0 }))
 *     tableType([RefCode, ...varS32(123)], limits({ addrType: "i64", min: 0n, max: 10n }))
 */

function tableType(elemType, limits) {
    var body = [];
    if (typeof elemType === "number") {
        elemType = [elemType];
    }
    body.push(...elemType);
    body.push(...limits);
    return body;
}

function memorySection(initialSize) {
    var body = [];
    body.push(...varU32(1));           // number of memories
    body.push(...varU32(0x0));         // for now, no maximum
    body.push(...varU32(initialSize));
    return { name: memoryId, body };
}

function tagSection(tags) {
    var body = [];
    body.push(...varU32(tags.length));
    for (let tag of tags) {
        body.push(...varU32(0)); // exception attribute
        body.push(...varU32(tag.type));
    }
    return { name: tagId, body };
}

function dataSection(segmentArrays) {
    var body = [];
    body.push(...varU32(segmentArrays.length));
    for (let array of segmentArrays) {
        body.push(...varU32(0)); // memory index
        body.push(...varU32(I32ConstCode));
        body.push(...varS32(array.offset));
        body.push(...varU32(EndCode));
        body.push(...varU32(array.elems.length));
        for (let elem of array.elems)
            body.push(...varU32(elem));
    }
    return { name: dataId, body };
}

function dataCountSection(count) {
    var body = [];
    body.push(...varU32(count));
    return { name: dataCountId, body };
}

function globalSection(globalArray) {
    var body = [];
    body.push(...varU32(globalArray.length));
    for (let globalObj of globalArray) {
        // Value type
        body.push(...varU32(globalObj.valType));
        // Flags
        body.push(globalObj.flags & 255);
        // Initializer expression
        body.push(...globalObj.initExpr);
    }
    return { name: globalId, body };
}

/**
 * Encode a global type:
 *
 *     globalType({ valType: I32Code, mut: true });
 *     globalType({ valType: FuncRefCode, mut: false });
 *     globalType({ valType: [RefCode, FuncRefCode], mut: false });
 *     globalType({ valType: [RefCode, ...varS32(123)], mut: true });
 */

function globalType(global) {
    var body = [];
    body.push(...(Array.isArray(global.valType) ? global.valType : varU32(global.valType)));
    body.push(global.mut ? 0x01 : 0x00);
    return body;
}

/**
 * Encode an externtype:
 * https://webassembly.github.io/spec/core/binary/types.html#external-types
 *
 *     externtype({ funcTypeIndex: 1 });
 *     externtype({ tableType: tableType(FuncRefCode, limits({ min: 0 })) });
 *     externtype({ memType: limits({ min: 0 }) });
 *     externtype({ globalType: globalType({ valType: I32Code, mut: true }) });
 */

function externtype(t) {
    if (t.hasOwnProperty("funcTypeIndex")) {
        return [ExternFuncCode, ...varU32(t.funcTypeIndex)];
    } else if (t.hasOwnProperty("tableType")) {
        return [ExternTableCode, ...t.tableType];
    } else if (t.hasOwnProperty("memType")) {
        return [ExternMemCode, ...t.memType];
    } else if (t.hasOwnProperty("globalType")) {
        return [ExternGlobalCode, ...t.globalType];
    } else if (t.hasOwnProperty("tagType")) {
        return [ExternTagCode, ...t.tagType];
    } else {
        throw new Error("unknown externtype");
    }
}

/**
 * Encode an element section:
 * https://webassembly.github.io/spec/core/binary/modules.html#element-section
 *
 * This is complicated because the encoding is
 * complicated: active/passive/declarative, explicit table index or no, indices
 * vs. expressions.
 *
 * The eight variants defined by the spec can be expressed like so:
 *
 *     elemSection([{ mode: "active", offset: 0, indices: [1, 2, 3] }]);
 *     elemSection([{ mode: "passive", indices: [1, 2, 3] }]);
 *     elemSection([{ mode: "active", table: 1, offset: 0, indices: [1, 2, 3] }]);
 *     elemSection([{ mode: "declarative", indices: [1, 2, 3] }]);
 *     elemSection([{ mode: "active", offset: 0, exprs: [[RefFuncCode, ...varU32(123), EndCode]] }]);
 *     elemSection([{ mode: "passive", elemType: [FuncRefCode], exprs: [[RefFuncCode, ...varU32(123), EndCode]] }]);
 *     elemSection([{ mode: "active", table: 1, offset: 0, exprs: [[RefFuncCode, ...varU32(123), EndCode]] }]);
 *     elemSection([{ mode: "declarative", elemType: [FuncRefCode], exprs: [[RefFuncCode, ...varU32(123), EndCode]] }]);
 */

function elemSection(elemSegments) {
    const body = [];
    body.push(...varU32(elemSegments.length));
    for (const segment of elemSegments) {
        if (!["active""passive""declarative"].includes(segment.mode)) {
            throw new Error(`segment mode must be "active""passive", or "declarative", but got ${segment.mode}`);
        }
        if (!segment.indices && !segment.exprs) {
            throw new Error("segment must have either .indices or .exprs");
        }
        if (segment.mode === "active" && segment.offset === undefined) {
            throw new Error("active element segment must have .offset");
        }
        if (segment.mode !== "active" && segment.exprs && segment.elemType === undefined) {
            throw new Error("non-active element segment with expression encoding must have .elemType");
        }

        const b0 = segment.mode === "active" ? 0 : 1;
        const b1 = (
            segment.mode === "active"
            ? (segment.table === undefined ? 0 : 1)
            : (segment.mode === "passive" ? 0 : 1)
        );
        const b2 = segment.exprs === undefined ? 0 : 1;
        const flag = (b0 << 0) | (b1 << 1) | (b2 << 2);

        body.push(flag);
        if (segment.table !== undefined) {
            body.push(...varU32(segment.table));
        }
        if (segment.mode === "active") {
            body.push(...varU32(I32ConstCode));
            body.push(...varS32(segment.offset));
            body.push(...varU32(EndCode));
        }
        if (segment.exprs === undefined) {
            // Function index encoding
            if (segment.mode !== "active" || segment.table !== undefined) {
                body.push(0x00); // elemkind
            }
            body.push(...varU32(segment.indices.length));
            for (const idx of segment.indices) {
                body.push(...varU32(idx));
            }
        } else {
            // Elem expression encoding
            if (segment.mode !== "active") {
                body.push(...segment.elemType);
            }
            body.push(...varU32(segment.exprs.length));
            for (const elemExpr of segment.exprs) {
                body.push(...elemExpr);
            }
        }
    }
    return { name: elemId, body };
}

function moduleNameSubsection(moduleName) {
    var body = [];
    body.push(...varU32(nameTypeModule));

    var subsection = encodedString(moduleName);
    body.push(...varU32(subsection.length));
    body.push(...subsection);

    return body;
}

function funcNameSubsection(funcNames, subsectionLen = null) {
    var body = [];
    body.push(...varU32(nameTypeFunction));

    var subsection = varU32(funcNames.length);

    var funcIndex = 0;
    for (let f of funcNames) {
        subsection.push(...varU32(f.index ? f.index : funcIndex));
        subsection.push(...encodedString(f.name, f.nameLen));
        funcIndex++;
    }

    body.push(...varU32(subsectionLen ?? subsection.length));
    body.push(...subsection);
    return body;
}

function nameSection(subsections, sectionLength = null) {
    var body = [];
    body.push(...string(nameName));

    for (let ss of subsections)
        body.push(...ss);

    return { name: userDefinedId, length: sectionLength, body };
}

function customSection(name, ...body) {
    return { name: userDefinedId, body: [...string(name), ...body] };
}

function tableSection0() {
    var body = [];
    body.push(...varU32(0));           // number of tables
    return { name: tableId, body };
}

function memorySection0() {
    var body = [];
    body.push(...varU32(0));           // number of memories
    return { name: memoryId, body };
}

Messung V0.5 in Prozent
C=92 H=84 G=87

¤ Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.0.21Bemerkung:  ¤

*Bot Zugriff






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.