1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2018 Yusuke Suzuki <[email protected]>.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "WasmFormat.h"
32#include "WasmOps.h"
33#include "WasmParser.h"
34
35namespace JSC { namespace Wasm {
36
37class SectionParser final : public Parser<void> {
38public:
39 SectionParser(const uint8_t* data, size_t size, size_t offsetInSource, ModuleInformation& info)
40 : Parser(data, size)
41 , m_offsetInSource(offsetInSource)
42 , m_info(info)
43 {
44 }
45
46#define WASM_SECTION_DECLARE_PARSER(NAME, ID, DESCRIPTION) PartialResult WARN_UNUSED_RETURN parse ## NAME();
47 FOR_EACH_KNOWN_WASM_SECTION(WASM_SECTION_DECLARE_PARSER)
48#undef WASM_SECTION_DECLARE_PARSER
49
50 PartialResult WARN_UNUSED_RETURN parseCustom();
51
52private:
53 template <typename ...Args>
54 NEVER_INLINE UnexpectedResult WARN_UNUSED_RETURN fail(Args... args) const
55 {
56 using namespace FailureHelper; // See ADL comment in namespace above.
57 return UnexpectedResult(makeString("WebAssembly.Module doesn't parse at byte "_s, String::number(m_offset + m_offsetInSource), ": "_s, makeString(args)...));
58 }
59
60 PartialResult WARN_UNUSED_RETURN parseGlobalType(Global&);
61 PartialResult WARN_UNUSED_RETURN parseMemoryHelper(bool isImport);
62 PartialResult WARN_UNUSED_RETURN parseTableHelper(bool isImport);
63 PartialResult WARN_UNUSED_RETURN parseResizableLimits(uint32_t& initial, Optional<uint32_t>& maximum);
64 PartialResult WARN_UNUSED_RETURN parseInitExpr(uint8_t&, uint64_t&, Type& initExprType);
65
66 size_t m_offsetInSource;
67 Ref<ModuleInformation> m_info;
68};
69
70} } // namespace JSC::Wasm
71
72#endif // ENABLE(WEBASSEMBLY)
73