tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bug1440512.js (7029B)


      1 // |jit-test| skip-if: !wasmDebuggingEnabled()
      2 var g = newGlobal({newCompartment: true});
      3 var dbg = new g.Debugger(this);
      4 var dbg = new Debugger;
      5 var kWasmH0 = 0;
      6 var kWasmH1 = 0x61;
      7 var kWasmH2 = 0x73;
      8 var kWasmH3 = 0x6d;
      9 var kWasmV0 = 0x1;
     10 var kWasmV1 = 0;
     11 var kWasmV2 = 0;
     12 var kWasmV3 = 0;
     13 let kTypeSectionCode = 1; // Function signature declarations
     14 let kImportSectionCode = 2; // Import declarations
     15 let kFunctionSectionCode = 3; // Function declarations
     16 let kExportSectionCode = 7; // Exports
     17 let kCodeSectionCode = 10; // Function code
     18 let kWasmFunctionTypeForm = 0x60;
     19 let kWasmI32 = 0x7f;
     20 let kExternalFunction = 0;
     21 let kSig_i_i = makeSig([kWasmI32], [kWasmI32]);
     22 function makeSig(params, results) {
     23    return {
     24        params: params,
     25        results: results
     26    };
     27 }
     28 let kExprEnd = 0x0b;
     29 let kExprGetLocal = 0x20;
     30 class Binary extends Array {
     31    emit_u8(val) {
     32        this.push(val);
     33    }
     34    emit_u32v(val) {
     35        while (true) {
     36            let v = val & 0xff;
     37            val = val >>> 7;
     38                this.push(v);
     39                break;
     40        }
     41    }
     42    emit_bytes(data) {
     43        for (let i = 0; i < data.length; i++) {
     44            this.push(data[i] & 0xff);
     45        }
     46    }
     47    emit_string(string) {
     48        let string_utf8 = unescape(encodeURIComponent(string));
     49        this.emit_u32v(string_utf8.length);
     50        for (let i = 0; i < string_utf8.length; i++) {
     51            this.emit_u8(string_utf8.charCodeAt(i));
     52        }
     53    }
     54    emit_header() {
     55        this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3);
     56    }
     57    emit_section(section_code, content_generator) {
     58        this.emit_u8(section_code);
     59        let section = new Binary;
     60        content_generator(section);
     61        this.emit_u32v(section.length);
     62        for (var i = 0; i < section.length; i++) this.push(section[i]);
     63    }
     64 }
     65 class WasmFunctionBuilder {
     66    constructor(module, name, type_index) {
     67        this.module = module;
     68    }
     69    exportAs(name) {
     70        this.module.addExport(name, this.index);
     71    }
     72    addBody(body) {
     73        this.body = body.slice();
     74        this.body.push(kExprEnd);
     75        return this;
     76    }
     77 }
     78 class WasmModuleBuilder {
     79    constructor() {
     80        this.types = [];
     81        this.imports = [];
     82        this.exports = [];
     83        this.functions = [];
     84    }
     85    addType(type) {
     86        this.types.push(type);
     87    }
     88    addFunction(name, type) {
     89        let type_index = (typeof type) == "number" ? type : this.addType(type);
     90        let func = new WasmFunctionBuilder(this, name, type_index);
     91        this.functions.push(func);
     92        return func;
     93    }
     94    addImport(module = "", name, type) {
     95        this.imports.push({
     96            module: module,
     97            name: name,
     98            kind: kExternalFunction,
     99        });
    100    }
    101    addExport(name, index) {
    102        this.exports.push({
    103            name: name,
    104        });
    105    }
    106    toArray(debug = false) {
    107        let binary = new Binary;
    108        let wasm = this;
    109        binary.emit_header();
    110        if (wasm.types.length > 0) {
    111            binary.emit_section(kTypeSectionCode, section => {
    112                section.emit_u32v(wasm.types.length);
    113                for (let type of wasm.types) {
    114                    section.emit_u8(kWasmFunctionTypeForm);
    115                    section.emit_u32v(type.params.length);
    116                    for (let param of type.params) {
    117                        section.emit_u8(param);
    118                    }
    119                    section.emit_u32v(type.results.length);
    120                    for (let result of type.results) {
    121                        section.emit_u8(result);
    122                    }
    123                }
    124            });
    125            binary.emit_section(kImportSectionCode, section => {
    126                section.emit_u32v(wasm.imports.length);
    127                for (let imp of wasm.imports) {
    128                    section.emit_string(imp.module);
    129                    section.emit_string(imp.name || '');
    130                    section.emit_u8(imp.kind);
    131                    if (imp.kind == kExternalFunction) {
    132                        section.emit_u32v(imp.type);
    133                    }
    134                }
    135            });
    136            binary.emit_section(kFunctionSectionCode, section => {
    137                section.emit_u32v(wasm.functions.length);
    138                for (let func of wasm.functions) {
    139                    section.emit_u32v(func.type_index);
    140                }
    141            });
    142        }
    143        var mem_export = (wasm.memory !== undefined && wasm.memory.exp);
    144        var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
    145        if (exports_count > 0) {
    146            binary.emit_section(kExportSectionCode, section => {
    147                section.emit_u32v(exports_count);
    148                for (let exp of wasm.exports) {
    149                    section.emit_string(exp.name);
    150                    section.emit_u8(exp.kind);
    151                    section.emit_u8(0);
    152                }
    153            });
    154        }
    155        if (wasm.start_index !== undefined) {
    156        }
    157        if (wasm.functions.length > 0) {
    158            binary.emit_section(kCodeSectionCode, section => {
    159                section.emit_u32v(wasm.functions.length);
    160                for (let func of wasm.functions) {
    161                    let local_decls = [];
    162                    let header = new Binary;
    163                    header.emit_u32v(local_decls.length);
    164                    section.emit_u32v(header.length + func.body.length);
    165                    section.emit_bytes(header);
    166                    section.emit_bytes(func.body);
    167                }
    168            });
    169        }
    170        let num_function_names = 0;
    171        let num_functions_with_local_names = 0;
    172        if (num_function_names > 0 || num_functions_with_local_names > 0 || wasm.name !== undefined) {
    173            binary.emit_section(kUnknownSectionCode, section => {
    174                if (num_function_names > 0) {
    175                }
    176            });
    177        }
    178        return binary;
    179    }
    180    toBuffer(debug = false) {
    181        let bytes = this.toArray(debug);
    182        let buffer = new ArrayBuffer(bytes.length);
    183        let view = new Uint8Array(buffer);
    184        for (let i = 0; i < bytes.length; i++) {
    185            let val = bytes[i];
    186            view[i] = val | 0;
    187        }
    188        return buffer;
    189    }
    190    toModule(debug = false) {
    191        return new WebAssembly.Module(this.toBuffer(debug));
    192    }
    193 }
    194 const verifyHeap = gc;
    195 function testProperties(obj) {
    196    for (let i = 0; i < 3; i++) {
    197        verifyHeap();
    198    }
    199 }
    200 (function ExportedFunctionTest() {
    201    var builder = new WasmModuleBuilder();
    202    builder.addFunction("exp", kSig_i_i).addBody([
    203        kExprGetLocal, 0,
    204    ]).exportAs("exp");
    205    let module1 = builder.toModule();
    206    let instance1 = new WebAssembly.Instance(module1);
    207    let g = instance1.exports.exp;
    208    testProperties(g);
    209    builder.addImport("imp", "func", kSig_i_i);
    210    let module2 = builder.toModule();
    211    let instance2 = new WebAssembly.Instance(module2, {
    212        imp: {
    213            func: g
    214        }
    215    });
    216 })();