Source/JavaScriptCore/ChangeLog

 12015-07-24 Sukolsak Sakshuwong <sukolsak@gmail.com>
 2
 3 Implement WebAssembly module parser
 4 https://bugs.webkit.org/show_bug.cgi?id=147293
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Implement WebAssembly module parser for WebAssembly files produced by pack-asmjs
 9 <https://github.com/WebAssembly/polyfill-prototype-1>. This patch only checks
 10 the magic number at the beginning of the files. Parsing of the rest will be
 11 implemented in a subsequent patch.
 12
 13 * API/JSScriptRef.cpp:
 14 * CMakeLists.txt:
 15 * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
 16 * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
 17 * JavaScriptCore.xcodeproj/project.pbxproj:
 18 * jsc.cpp:
 19 (GlobalObject::finishCreation):
 20 (functionLoadWebAssembly):
 21 * parser/SourceCode.cpp:
 22 (JSC::SourceCode::toUTF8):
 23 * parser/SourceProvider.h:
 24 (JSC::WebAssemblySourceProvider::create):
 25 (JSC::WebAssemblySourceProvider::data):
 26 (JSC::WebAssemblySourceProvider::WebAssemblySourceProvider):
 27 * runtime/JSGlobalObject.cpp:
 28 (JSC::JSGlobalObject::init):
 29 (JSC::JSGlobalObject::visitChildren):
 30 * runtime/JSGlobalObject.h:
 31 (JSC::JSGlobalObject::wasmModuleStructure):
 32 * wasm/WASMFormat.h: Copied from Source/JavaScriptCore/parser/SourceCode.cpp.
 33 * wasm/WASMModuleParser.cpp: Added.
 34 (JSC::WASMModuleParser::WASMModuleParser):
 35 (JSC::WASMModuleParser::parse):
 36 (JSC::WASMModuleParser::parseModule):
 37 (JSC::parseWebAssembly):
 38 * wasm/WASMModuleParser.h: Copied from Source/JavaScriptCore/parser/SourceCode.cpp.
 39 * wasm/WASMReader.cpp: Copied from Source/JavaScriptCore/parser/SourceCode.cpp.
 40 (JSC::WASMReader::readUnsignedInt32):
 41 (JSC::WASMReader::readFloat):
 42 (JSC::WASMReader::readDouble):
 43 * wasm/WASMReader.h: Copied from Source/JavaScriptCore/parser/SourceCode.cpp.
 44 (JSC::WASMReader::WASMReader):
 45
1462015-07-24 Yusuke Suzuki <utatane.tea@gmail.com>
247
348 Avoid 2 times name iteration in Object.assign

Source/JavaScriptCore/API/JSScriptRef.cpp

@@public:
4646 return WTF::adoptRef(*new OpaqueJSScript(vm, url, startingLineNumber, source));
4747 }
4848
 49#if ENABLE(WEBASSEMBLY)
 50 virtual bool isStringSource() const override
 51 {
 52 return true;
 53 }
 54#endif
 55
4956 virtual const String& source() const override
5057 {
5158 return m_source;

Source/JavaScriptCore/CMakeLists.txt

@@set(JavaScriptCore_SOURCES
411411 tools/JSDollarVMPrototype.cpp
412412
413413 wasm/JSWASMModule.cpp
 414 wasm/WASMModuleParser.cpp
 415 wasm/WASMReader.cpp
414416
415417 yarr/RegularExpression.cpp
416418 yarr/YarrCanonicalizeUCS2.cpp

Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj

867867 <ClCompile Include="..\tools\JSDollarVM.cpp" />
868868 <ClCompile Include="..\tools\JSDollarVMPrototype.cpp" />
869869 <ClCompile Include="..\wasm\JSWASMModule.cpp" />
 870 <ClCompile Include="..\wasm\WASMModuleParser.cpp" />
 871 <ClCompile Include="..\wasm\WASMReader.cpp" />
870872 <ClCompile Include="..\yarr\RegularExpression.cpp" />
871873 <ClCompile Include="..\yarr\YarrCanonicalizeUCS2.cpp" />
872874 <ClCompile Include="..\yarr\YarrInterpreter.cpp" />

17311733 <ClInclude Include="..\tools\ProfileTreeNode.h" />
17321734 <ClInclude Include="..\tools\TieredMMapArray.h" />
17331735 <ClInclude Include="..\wasm\JSWASMModule.h" />
 1736 <ClInclude Include="..\wasm\WASMFormat.h" />
 1737 <ClInclude Include="..\wasm\WASMModuleParser.h" />
 1738 <ClInclude Include="..\wasm\WASMReader.h" />
17341739 <ClInclude Include="..\yarr\RegularExpression.h" />
17351740 <ClInclude Include="..\yarr\Yarr.h" />
17361741 <ClInclude Include="..\yarr\YarrCanonicalizeUCS2.h" />

Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters

17921792 <ClCompile Include="..\wasm\JSWASMModule.cpp">
17931793 <Filter>wasm</Filter>
17941794 </ClCompile>
 1795 <ClCompile Include="..\wasm\WASMModuleParser.cpp">
 1796 <Filter>wasm</Filter>
 1797 </ClCompile>
 1798 <ClCompile Include="..\wasm\WASMReader.cpp">
 1799 <Filter>wasm</Filter>
 1800 </ClCompile>
17951801 </ItemGroup>
17961802 <ItemGroup>
17971803 <ClInclude Include="..\API\APICallbackFunction.h">

42884294 <ClInclude Include="..\wasm\JSWASMModule.h">
42894295 <Filter>wasm</Filter>
42904296 </ClInclude>
 4297 <ClInclude Include="..\wasm\WASMFormat.h">
 4298 <Filter>wasm</Filter>
 4299 </ClInclude>
 4300 <ClInclude Include="..\wasm\WASMModuleParser.h">
 4301 <Filter>wasm</Filter>
 4302 </ClInclude>
 4303 <ClInclude Include="..\wasm\WASMReader.h">
 4304 <Filter>wasm</Filter>
 4305 </ClInclude>
42914306 </ItemGroup>
42924307 <ItemGroup>
42934308 <None Include="JavaScriptCorePreLink.cmd" />

Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

10231023 70ECA6091AFDBEA200449739 /* TemplateRegistryKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 70ECA6041AFDBEA200449739 /* TemplateRegistryKey.h */; settings = {ATTRIBUTES = (Private, ); }; };
10241024 79EE0BFF1B4AFB85000385C9 /* VariableEnvironment.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */; };
10251025 79EE0C001B4AFB85000385C9 /* VariableEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = 79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */; settings = {ATTRIBUTES = (Private, ); }; };
 1026 7B39F76D1B62DE2E00360FB4 /* WASMModuleParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */; };
 1027 7B39F76E1B62DE3200360FB4 /* WASMModuleParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
 1028 7B39F76F1B62DE3200360FB4 /* WASMFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76B1B62DE2200360FB4 /* WASMFormat.h */; settings = {ATTRIBUTES = (Private, ); }; };
 1029 7B39F7701B62DE3200360FB4 /* WASMReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B39F76C1B62DE2200360FB4 /* WASMReader.h */; settings = {ATTRIBUTES = (Private, ); }; };
 1030 7B39F7721B63574D00360FB4 /* WASMReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B39F7711B63574B00360FB4 /* WASMReader.cpp */; };
10261031 7B98D1361B60CD5F0023B1A4 /* JSWASMModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */; };
10271032 7B98D1371B60CD620023B1A4 /* JSWASMModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */; settings = {ATTRIBUTES = (Private, ); }; };
10281033 7C008CDA187124BB00955C24 /* JSPromiseDeferred.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C008CD8187124BB00955C24 /* JSPromiseDeferred.cpp */; };

27692774 70ECA6041AFDBEA200449739 /* TemplateRegistryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TemplateRegistryKey.h; sourceTree = "<group>"; };
27702775 79EE0BFD1B4AFB85000385C9 /* VariableEnvironment.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VariableEnvironment.cpp; sourceTree = "<group>"; };
27712776 79EE0BFE1B4AFB85000385C9 /* VariableEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VariableEnvironment.h; sourceTree = "<group>"; };
 2777 7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMModuleParser.cpp; sourceTree = "<group>"; };
 2778 7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMModuleParser.h; sourceTree = "<group>"; };
 2779 7B39F76B1B62DE2200360FB4 /* WASMFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMFormat.h; sourceTree = "<group>"; };
 2780 7B39F76C1B62DE2200360FB4 /* WASMReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WASMReader.h; sourceTree = "<group>"; };
 2781 7B39F7711B63574B00360FB4 /* WASMReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WASMReader.cpp; sourceTree = "<group>"; };
27722782 7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWASMModule.cpp; sourceTree = "<group>"; };
27732783 7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSWASMModule.h; sourceTree = "<group>"; };
27742784 7C008CD8187124BB00955C24 /* JSPromiseDeferred.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSPromiseDeferred.cpp; sourceTree = "<group>"; };

42934303 children = (
42944304 7B98D1341B60CD5A0023B1A4 /* JSWASMModule.cpp */,
42954305 7B98D1351B60CD5A0023B1A4 /* JSWASMModule.h */,
 4306 7B39F76B1B62DE2200360FB4 /* WASMFormat.h */,
 4307 7B39F7691B62DE2200360FB4 /* WASMModuleParser.cpp */,
 4308 7B39F76A1B62DE2200360FB4 /* WASMModuleParser.h */,
 4309 7B39F7711B63574B00360FB4 /* WASMReader.cpp */,
 4310 7B39F76C1B62DE2200360FB4 /* WASMReader.h */,
42964311 );
42974312 path = wasm;
42984313 sourceTree = "<group>";

61876202 0F5A1274192D9FDF008764A3 /* DFGDoesGC.h in Headers */,
61886203 0F2B66F517B6B5AB00A7AE3F /* JSGenericTypedArrayViewPrototype.h in Headers */,
61896204 0F2B66F617B6B5AB00A7AE3F /* JSGenericTypedArrayViewPrototypeInlines.h in Headers */,
 6205 7B39F76E1B62DE3200360FB4 /* WASMModuleParser.h in Headers */,
61906206 BC18C4210E16F5CD00B34460 /* JSGlobalObject.h in Headers */,
61916207 A5FD0086189B1B7E00633231 /* JSGlobalObjectConsoleAgent.h in Headers */,
61926208 A59455931824744700CC3843 /* JSGlobalObjectDebuggable.h in Headers */,

62756291 BC18C52E0E16FCE100B34460 /* Lexer.lut.h in Headers */,
62766292 86D3B3C310159D7F002865E7 /* LinkBuffer.h in Headers */,
62776293 0F431738146BAC69007E3890 /* ListableHandler.h in Headers */,
 6294 7B39F76F1B62DE3200360FB4 /* WASMFormat.h in Headers */,
62786295 A7E2EA6B0FB460CF00601F06 /* LiteralParser.h in Headers */,
62796296 0F0FC45A14BD15F500B81154 /* LLIntCallLinkInfo.h in Headers */,
62806297 0FC3CD0019ADA410006AC72A /* DFGBlockWorklist.h in Headers */,

65316548 0F5F08CF146C7633000472A9 /* UnconditionalFinalizer.h in Headers */,
65326549 A7B601821639FD2A00372BA3 /* UnlinkedCodeBlock.h in Headers */,
65336550 0F2E892C16D028AD009E4FD2 /* UnusedPointer.h in Headers */,
 6551 7B39F7701B62DE3200360FB4 /* WASMReader.h in Headers */,
65346552 0F963B3813FC6FE90002D9B2 /* ValueProfile.h in Headers */,
65356553 0F426A481460CBB300131F8F /* ValueRecovery.h in Headers */,
65366554 0F9C5E5F18E35F5E00D431C3 /* FTLDWARFRegister.h in Headers */,

71657183 0F9D339617FFC4E60073C2BC /* DFGFlushedAt.cpp in Sources */,
71667184 0F9D36941AE9CC33000D4DFB /* DFGCleanUpPhase.cpp in Sources */,
71677185 A7D89CF717A0B8CC00773AD8 /* DFGFlushFormat.cpp in Sources */,
 7186 7B39F76D1B62DE2E00360FB4 /* WASMModuleParser.cpp in Sources */,
71687187 0F3A1BF91A9ECB7D000DE01A /* DFGPutStackSinkingPhase.cpp in Sources */,
71697188 86EC9DC71328DF82002B2AD7 /* DFGGraph.cpp in Sources */,
71707189 0F2FCCF918A60070001A27F8 /* DFGGraphSafepoint.cpp in Sources */,

75797598 0F24E55017EE274900ABB217 /* Repatch.cpp in Sources */,
75807599 62D2D38F1ADF103F000206C1 /* FunctionRareData.cpp in Sources */,
75817600 0F7700921402FF3C0078EB39 /* SamplingCounter.cpp in Sources */,
 7601 7B39F7721B63574D00360FB4 /* WASMReader.cpp in Sources */,
75827602 1429D8850ED21C3D00B89619 /* SamplingTool.cpp in Sources */,
75837603 70EC0EC61AA0D7DA00B6AAFA /* StringIteratorPrototype.cpp in Sources */,
75847604 A5FD0067189AFE9C00633231 /* ScriptArguments.cpp in Sources */,

Source/JavaScriptCore/jsc.cpp

4343#include "JSONObject.h"
4444#include "JSProxy.h"
4545#include "JSString.h"
 46#include "JSWASMModule.h"
4647#include "ProfilerDatabase.h"
4748#include "SamplingTool.h"
4849#include "StackVisitor.h"

5051#include "StructureRareDataInlines.h"
5152#include "TestRunnerUtils.h"
5253#include "TypeProfilerLog.h"
 54#include "WASMModuleParser.h"
5355#include <math.h>
5456#include <stdio.h>
5557#include <stdlib.h>

@@static EncodedJSValue JSC_HOST_CALL functionReturnTypeFor(ExecState*);
492494static EncodedJSValue JSC_HOST_CALL functionDumpBasicBlockExecutionRanges(ExecState*);
493495static EncodedJSValue JSC_HOST_CALL functionHasBasicBlockExecuted(ExecState*);
494496static EncodedJSValue JSC_HOST_CALL functionEnableExceptionFuzz(ExecState*);
 497#if ENABLE(WEBASSEMBLY)
 498static EncodedJSValue JSC_HOST_CALL functionLoadWebAssembly(ExecState*);
 499#endif
495500
496501#if ENABLE(SAMPLING_FLAGS)
497502static EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*);

@@protected:
655660
656661 addFunction(vm, "enableExceptionFuzz", functionEnableExceptionFuzz, 0);
657662
 663#if ENABLE(WEBASSEMBLY)
 664 addFunction(vm, "loadWebAssembly", functionLoadWebAssembly, 1);
 665#endif
 666
658667 JSArray* array = constructEmptyArray(globalExec(), 0);
659668 for (size_t i = 0; i < arguments.size(); ++i)
660669 array->putDirectIndex(globalExec(), i, jsString(globalExec(), arguments[i]));

@@EncodedJSValue JSC_HOST_CALL functionEnableExceptionFuzz(ExecState*)
11831192 return JSValue::encode(jsUndefined());
11841193}
11851194
 1195#if ENABLE(WEBASSEMBLY)
 1196EncodedJSValue JSC_HOST_CALL functionLoadWebAssembly(ExecState* exec)
 1197{
 1198 String fileName = exec->argument(0).toString(exec)->value(exec);
 1199 Vector<char> buffer;
 1200 if (!fillBufferWithContentsOfFile(fileName, buffer))
 1201 return JSValue::encode(exec->vm().throwException(exec, createError(exec, ASCIILiteral("Could not open file."))));
 1202 RefPtr<WebAssemblySourceProvider> sourceProvider = WebAssemblySourceProvider::create(reinterpret_cast<Vector<uint8_t>&>(buffer), fileName);
 1203 SourceCode source(sourceProvider);
 1204 String errorMessage;
 1205 JSWASMModule* module = parseWebAssembly(exec, source, errorMessage);
 1206 if (!module)
 1207 return JSValue::encode(exec->vm().throwException(exec, createSyntaxError(exec, errorMessage)));
 1208 return JSValue::encode(module);
 1209}
 1210#endif
 1211
11861212// Use SEH for Release builds only to get rid of the crash report dialog
11871213// (luckily the same tests fail in Release and Debug builds so far). Need to
11881214// be in a separate main function because the jscmain function requires object

Source/JavaScriptCore/parser/SourceCode.cpp

@@CString SourceCode::toUTF8() const
3636 if (!m_provider)
3737 return CString("", 0);
3838
 39#if ENABLE(WEBASSEMBLY)
 40 if (!m_provider->isStringSource())
 41 return CString("", 0);
 42#endif
 43
3944 return m_provider->source().impl()->utf8ForRange(m_startChar, m_endChar - m_startChar);
4045}
4146

Source/JavaScriptCore/parser/SourceProvider.h

@@namespace JSC {
4343
4444 JS_EXPORT_PRIVATE virtual ~SourceProvider();
4545
 46#if ENABLE(WEBASSEMBLY)
 47 virtual bool isStringSource() const = 0;
 48#endif
4649 virtual const String& source() const = 0;
4750 String getRange(int start, int end) const
4851 {

@@namespace JSC {
7982 return adoptRef(*new StringSourceProvider(source, url, startPosition));
8083 }
8184
 85#if ENABLE(WEBASSEMBLY)
 86 virtual bool isStringSource() const override
 87 {
 88 return true;
 89 }
 90#endif
 91
8292 virtual const String& source() const override
8393 {
8494 return m_source;

@@namespace JSC {
94104 String m_source;
95105 };
96106
 107#if ENABLE(WEBASSEMBLY)
 108 class WebAssemblySourceProvider : public SourceProvider {
 109 public:
 110 static Ref<WebAssemblySourceProvider> create(const Vector<uint8_t>& data, const String& url)
 111 {
 112 return adoptRef(*new WebAssemblySourceProvider(data, url));
 113 }
 114
 115 virtual bool isStringSource() const override
 116 {
 117 return false;
 118 }
 119
 120 virtual const String& source() const override
 121 {
 122 return m_source;
 123 }
 124
 125 const Vector<uint8_t>& data() const
 126 {
 127 return m_data;
 128 }
 129
 130 private:
 131 WebAssemblySourceProvider(const Vector<uint8_t>& data, const String& url)
 132 : SourceProvider(url, TextPosition::minimumPosition())
 133 , m_source("[WebAssembly source]")
 134 , m_data(data)
 135 {
 136 }
 137
 138 String m_source;
 139 Vector<uint8_t> m_data;
 140 };
 141#endif
 142
97143} // namespace JSC
98144
99145#endif // SourceProvider_h

Source/JavaScriptCore/runtime/JSGlobalObject.cpp

9292#include "JSTypedArrayConstructors.h"
9393#include "JSTypedArrayPrototypes.h"
9494#include "JSTypedArrays.h"
 95#include "JSWASMModule.h"
9596#include "JSWeakMap.h"
9697#include "JSWeakSet.h"
9798#include "JSWithScope.h"

@@void JSGlobalObject::init(VM& vm)
340341 m_promisePrototype.set(vm, this, JSPromisePrototype::create(exec, this, JSPromisePrototype::createStructure(vm, this, m_objectPrototype.get())));
341342 m_promiseStructure.set(vm, this, JSPromise::createStructure(vm, this, m_promisePrototype.get()));
342343
 344#if ENABLE(WEBASSEMBLY)
 345 m_wasmModuleStructure.set(vm, this, JSWASMModule::createStructure(vm, this));
 346#endif
 347
343348 m_parseIntFunction.set(vm, this, JSFunction::create(vm, this, 2, vm.propertyNames->parseInt.string(), globalFuncParseInt, NoIntrinsic));
344349 putDirectWithoutTransition(vm, vm.propertyNames->parseInt, m_parseIntFunction.get(), DontEnum | Function);
345350

@@void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
799804 visitor.append(&thisObject->m_dollarVMStructure);
800805 visitor.append(&thisObject->m_internalFunctionStructure);
801806 visitor.append(&thisObject->m_promiseStructure);
 807#if ENABLE(WEBASSEMBLY)
 808 visitor.append(&thisObject->m_wasmModuleStructure);
 809#endif
802810
803811#define VISIT_SIMPLE_TYPE(CapitalName, lowerName, properName, instanceType, jsName) \
804812 visitor.append(&thisObject->m_ ## lowerName ## Prototype); \

Source/JavaScriptCore/runtime/JSGlobalObject.h

@@protected:
243243 WriteBarrier<Structure> m_iteratorResultStructure;
244244 WriteBarrier<Structure> m_regExpMatchesArrayStructure;
245245 WriteBarrier<Structure> m_promiseStructure;
 246#if ENABLE(WEBASSEMBLY)
 247 WriteBarrier<Structure> m_wasmModuleStructure;
 248#endif
246249
247250#define DEFINE_STORAGE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
248251 WriteBarrier<capitalName ## Prototype> m_ ## lowerName ## Prototype; \

@@public:
475478 static ptrdiff_t iteratorResultStructureOffset() { return OBJECT_OFFSETOF(JSGlobalObject, m_iteratorResultStructure); }
476479 Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
477480 Structure* promiseStructure() const { return m_promiseStructure.get(); }
 481#if ENABLE(WEBASSEMBLY)
 482 Structure* wasmModuleStructure() const { return m_wasmModuleStructure.get(); }
 483#endif
478484
479485 JS_EXPORT_PRIVATE void setRemoteDebuggingEnabled(bool);
480486 JS_EXPORT_PRIVATE bool remoteDebuggingEnabled() const;

Source/JavaScriptCore/wasm/WASMFormat.h

 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#ifndef WASMFormat_h
 27#define WASMFormat_h
 28
 29#if ENABLE(WEBASSEMBLY)
 30
 31namespace JSC {
 32
 33static const uint32_t wasmMagicNumber = 0x6d736177;
 34
 35} // namespace JSC
 36
 37#endif // ENABLE(WEBASSEMBLY)
 38
 39#endif // WASMFormat_h

Source/JavaScriptCore/wasm/WASMModuleParser.cpp

 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)

Source/JavaScriptCore/wasm/WASMModuleParser.h

 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#ifndef WASMModuleParser_h
 27#define WASMModuleParser_h
 28
 29#if ENABLE(WEBASSEMBLY)
 30
 31#include "WASMReader.h"
 32#include <wtf/text/WTFString.h>
 33
 34namespace JSC {
 35
 36class ExecState;
 37class JSGlobalObject;
 38class JSWASMModule;
 39class SourceCode;
 40class VM;
 41
 42class WASMModuleParser {
 43public:
 44 WASMModuleParser(const SourceCode&);
 45 JSWASMModule* parse(VM&, JSGlobalObject*, String& errorMessage);
 46
 47private:
 48 bool parseModule();
 49
 50 WASMReader m_reader;
 51 String m_errorMessage;
 52};
 53
 54JS_EXPORT_PRIVATE JSWASMModule* parseWebAssembly(ExecState*, const SourceCode&, String& errorMessage);
 55
 56} // namespace JSC
 57
 58#endif // ENABLE(WEBASSEMBLY)
 59
 60#endif // WASMModuleParser_h

Source/JavaScriptCore/wasm/WASMReader.cpp

 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 "WASMReader.h"
 28
 29#if ENABLE(WEBASSEMBLY)
 30
 31#define CHECK_READ(length) do { if (m_cursor + length > m_buffer.end()) return false; } while (0)
 32
 33namespace JSC {
 34
 35bool WASMReader::readUnsignedInt32(uint32_t& result)
 36{
 37 CHECK_READ(4);
 38 result = m_cursor[0] | m_cursor[1] << 8 | m_cursor[2] << 16 | m_cursor[3] << 24;
 39 m_cursor += 4;
 40 return true;
 41}
 42
 43bool WASMReader::readFloat(float& result)
 44{
 45 CHECK_READ(4);
 46 union {
 47 uint8_t bytes[4];
 48 float floatValue;
 49 } u = {
 50#if CPU(BIG_ENDIAN)
 51 { m_cursor[3], m_cursor[2], m_cursor[1], m_cursor[0] }
 52#else
 53 { m_cursor[0], m_cursor[1], m_cursor[2], m_cursor[3] }
 54#endif
 55 };
 56 result = u.floatValue;
 57 m_cursor += 4;
 58 return true;
 59}
 60
 61bool WASMReader::readDouble(double& result)
 62{
 63 CHECK_READ(8);
 64 union {
 65 uint8_t bytes[8];
 66 double doubleValue;
 67 } u = {
 68#if CPU(BIG_ENDIAN)
 69 { m_cursor[0], m_cursor[1], m_cursor[2], m_cursor[3], m_cursor[4], m_cursor[5], m_cursor[6], m_cursor[7] }
 70#else
 71 { m_cursor[7], m_cursor[6], m_cursor[5], m_cursor[4], m_cursor[3], m_cursor[2], m_cursor[1], m_cursor[0] }
 72#endif
 73 };
 74 result = u.doubleValue;
 75 m_cursor += 8;
 76 return true;
 77}
 78
 79} // namespace JSC
 80
 81#endif // ENABLE(WEBASSEMBLY)

Source/JavaScriptCore/wasm/WASMReader.h

 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#ifndef WASMReader_h
 27#define WASMReader_h
 28
 29#if ENABLE(WEBASSEMBLY)
 30
 31#include <wtf/Vector.h>
 32
 33namespace JSC {
 34
 35class WASMReader {
 36public:
 37 WASMReader(const Vector<uint8_t>& buffer, unsigned initialIndex = 0)
 38 : m_buffer(buffer)
 39 , m_cursor(buffer.data() + initialIndex)
 40 {
 41 }
 42
 43 bool readUnsignedInt32(uint32_t& result);
 44 bool readFloat(float& result);
 45 bool readDouble(double& result);
 46
 47private:
 48 const Vector<uint8_t>& m_buffer;
 49 const uint8_t* m_cursor;
 50};
 51
 52} // namespace JSC
 53
 54#endif // ENABLE(WEBASSEMBLY)
 55
 56#endif // WASMReader_h