1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WASMModuleParser.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "JSCInlines.h"
32#include "JSWASMModule.h"
33#include "WASMFormat.h"
34
35#define FAIL_WITH_MESSAGE(errorMessage) do { m_errorMessage = errorMessage; return false; } while (0)
36#define READ_UNSIGNED_INT32_OR_FAIL(x, errorMessage) do { if (!m_reader.readUnsignedInt32(x)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
37#define READ_FLOAT_OR_FAIL(x, errorMessage) do { if (!m_reader.readFloat(x)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
38#define READ_DOUBLE_OR_FAIL(x, errorMessage) do { if (!m_reader.readDouble(x)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
39#define FAIL_IF_FALSE(condition, errorMessage) do { if (!(condition)) FAIL_WITH_MESSAGE(errorMessage); } while (0)
40
41namespace JSC {
42
43WASMModuleParser::WASMModuleParser(const SourceCode& source)
44 : m_reader(static_cast<WebAssemblySourceProvider*>(source.provider())->data())
45{
46}
47
48JSWASMModule* WASMModuleParser::parse(VM& vm, JSGlobalObject* globalObject, String& errorMessage)
49{
50 JSWASMModule* module = JSWASMModule::create(vm, globalObject->wasmModuleStructure());
51 parseModule();
52 if (!m_errorMessage.isNull()) {
53 errorMessage = m_errorMessage;
54 return nullptr;
55 }
56 return module;
57}
58
59bool WASMModuleParser::parseModule()
60{
61 uint32_t magicNumber;
62 READ_UNSIGNED_INT32_OR_FAIL(magicNumber, "Cannot read the magic number.");
63 FAIL_IF_FALSE(magicNumber == wasmMagicNumber, "The magic number is incorrect.");
64
65 // TODO: parse the rest
66
67 return true;
68}
69
70JSWASMModule* parseWebAssembly(ExecState* exec, const SourceCode& source, String& errorMessage)
71{
72 WASMModuleParser WASMModuleParser(source);
73 return WASMModuleParser.parse(exec->vm(), exec->lexicalGlobalObject(), errorMessage);
74}
75
76} // namespace JSC
77
78#endif // ENABLE(WEBASSEMBLY)