Пример #1
0
static bool
DecodeModule(JSContext* cx, UniqueChars filename, const uint8_t* bytes, uint32_t length,
             ImportNameVector* importNames, ExportMap* exportMap,
             MutableHandle<WasmModuleObject*> moduleObj)
{
    Decoder d(bytes, bytes + length);

    uint32_t u32;
    if (!d.readU32(&u32) || u32 != MagicNumber)
        return Fail(cx, d, "failed to match magic number");

    if (!d.readU32(&u32) || u32 != EncodingVersion)
        return Fail(cx, d, "failed to match binary version");

    UniqueModuleGeneratorData init = MakeUnique<ModuleGeneratorData>();
    if (!init)
        return false;

    if (!DecodeSignatureSection(cx, d, init.get()))
        return false;

    if (!DecodeDeclarationSection(cx, d, init.get()))
        return false;

    if (!DecodeImportSection(cx, d, init.get(), importNames))
        return false;

    ModuleGenerator mg(cx);
    if (!mg.init(Move(init)))
        return false;

    if (!DecodeExportsSection(cx, d, mg, exportMap))
        return false;

    HeapUsage heapUsage = HeapUsage::None;

    if (!DecodeCodeSection(cx, d, mg))
        return false;

    CacheableCharsVector funcNames;

    while (!d.readCStringIf(EndSection)) {
        if (!DecodeUnknownSection(cx, d))
            return false;
    }

    if (!d.done())
        return Fail(cx, d, "failed to consume all bytes of module");

    UniqueModuleData module;
    UniqueStaticLinkData link;
    SlowFunctionVector slowFuncs(cx);
    if (!mg.finish(heapUsage, Move(filename), Move(funcNames), &module, &link, &slowFuncs))
        return false;

    moduleObj.set(WasmModuleObject::create(cx));
    if (!moduleObj)
        return false;

    if (!moduleObj->init(cx->new_<Module>(Move(module))))
        return false;

    return moduleObj->module().staticallyLink(cx, *link);
}
Пример #2
0
static bool
DecodeModule(JSContext* cx, UniqueChars file, const uint8_t* bytes, uint32_t length,
             ImportNameVector* importNames, UniqueExportMap* exportMap,
             MutableHandle<ArrayBufferObject*> heap, MutableHandle<WasmModuleObject*> moduleObj)
{
    Decoder d(bytes, bytes + length);

    uint32_t u32;
    if (!d.readFixedU32(&u32) || u32 != MagicNumber)
        return Fail(cx, d, "failed to match magic number");

    if (!d.readFixedU32(&u32) || u32 != EncodingVersion)
        return Fail(cx, d, "failed to match binary version");

    UniqueModuleGeneratorData init = js::MakeUnique<ModuleGeneratorData>(cx);
    if (!init)
        return false;

    if (!DecodeSignatures(cx, d, init.get()))
        return false;

    if (!DecodeImportTable(cx, d, init.get(), importNames))
        return false;

    if (!DecodeFunctionSignatures(cx, d, init.get()))
        return false;

    if (!DecodeFunctionTable(cx, d, init.get()))
        return false;

    ModuleGenerator mg(cx);
    if (!mg.init(Move(init), Move(file)))
        return false;

    if (!DecodeMemory(cx, d, mg, heap))
        return false;

    if (!DecodeExportTable(cx, d, mg))
        return false;

    if (!DecodeFunctionBodies(cx, d, mg))
        return false;

    if (!DecodeDataSegments(cx, d, heap))
        return false;

    CacheableCharsVector funcNames;

    while (!d.done()) {
        if (!d.skipSection())
            return Fail(cx, d, "failed to skip unknown section at end");
    }

    UniqueModuleData module;
    UniqueStaticLinkData staticLink;
    SlowFunctionVector slowFuncs(cx);
    if (!mg.finish(Move(funcNames), &module, &staticLink, exportMap, &slowFuncs))
        return false;

    moduleObj.set(WasmModuleObject::create(cx));
    if (!moduleObj)
        return false;

    if (!moduleObj->init(cx->new_<Module>(Move(module))))
        return false;

    return moduleObj->module().staticallyLink(cx, *staticLink);
}