WebAssembly JS API: implement more sections
https://bugs.webkit.org/show_bug.cgi?id=164023

Reviewed by Keith Miller.

On the JSC side:

 - Put in parser stubs for all WebAssembly sections.
 - Parse Import, Export sections.
 - Use tryReserveCapacity instead of reserve, and bail out of the parser if it fails. This prevents the parser from bringing everything down when faced with a malicious input.
 - Encapsulate all parsed module information into its own structure, making it easier to pass around (from parser to Plan to Module to Instance).
 - Create WasmFormat.cpp to hold parsed module information's dtor to avoid including WasmMemory.h needlessly.
JSTests:

 - parseCode: avoid overflow through function size.
 - Remove all remainders of polyfill-prototype-1, and update license.
 - Add missing WasmOps.h and WasmValidateInlines.h auto-generation for cmake build.

On the Builder.js testing side:

 - Implement Type, Import (function only), Export (function only) sections.
 - Check section order and uniqueness.
 - Optionally auto-generate the Type section from subsequent Export / Import / Code entries.
 - Allow re-exporting an import.

* wasm/Builder.js: build type, import, and export sections
(const._normalizeFunctionSignature):
* wasm/Builder_WebAssemblyBinary.js: Added. Forked from Builder.js
(const.emitters.Type):
(const.emitters.Import):
(const.emitters.Function):
(const.emitters.Table):
(const.emitters.Memory):
(const.emitters.Global):
(const.emitters.Export):
(const.emitters.Start):
(const.emitters.Element):
(const.emitters.Code):
(const.emitters.Data):
(export.const.Binary):
* wasm/LowLevelBinary.js: Add a few useful outputs
(export.default.LowLevelBinary.prototype.varuint1):
(export.default.LowLevelBinary.prototype.varint7):
* wasm/WASM.js: value type and external kind helpers
* wasm/assert.js: array element-wise equality comparison
(const._eq):
* wasm/js-api/test_Module.js:
(ModuleWithImports):
* wasm/self-test/test_BuilderJSON.js: many more tests for all the new Builder APIs, and update to some older tests which now require a Type section or rejiggered Function signature
(const.assertOpThrows):
(SectionsWithSameCustomName):
(TwoTypeSections):
(EmptyImportSection):
(ImportBeforeTypeSections):
* wasm/self-test/test_BuilderWebAssembly.js: remove a test which wasn't helpful and is now obsolete
(CustomSection):

Source/JavaScriptCore:

 - Remove all remainders of polyfill-prototype-1, and update license.
 - Add missing WasmOps.h and WasmValidateInlines.h auto-generation for cmake build.

On the Builder.js testing side:

 - Implement Type, Import (function only), Export (function only) sections.
 - Check section order and uniqueness.
 - Optionally auto-generate the Type section from subsequent Export / Import / Code entries.
 - Allow re-exporting an import.

* CMakeLists.txt: missing auto-genration
* JavaScriptCore.xcodeproj/project.pbxproj: merge conflict
* testWasm.cpp: update for API changes, no functional change
(checkPlan):
(runWasmTests):
* wasm/WasmFormat.cpp: add a dtor which requires extra headers which I'd rather not include in WasmFormat.h
(JSC::Wasm::ModuleInformation::~ModuleInformation):
* wasm/WasmFormat.h: Add External, Import, Functioninformation, Export, ModuleInformation, CompiledFunction, and remove obsolete stuff which was a holdover from the first implementation (all that code is now gone, so remove its license)
(JSC::Wasm::External::isValid):
* wasm/WasmModuleParser.cpp: simplify some, make names consistent with the WebAssembly section names, check memory allocations so they can fail early
(JSC::Wasm::ModuleParser::parse):
(JSC::Wasm::ModuleParser::parseType):
(JSC::Wasm::ModuleParser::parseImport):
(JSC::Wasm::ModuleParser::parseFunction):
(JSC::Wasm::ModuleParser::parseTable):
(JSC::Wasm::ModuleParser::parseMemory):
(JSC::Wasm::ModuleParser::parseGlobal):
(JSC::Wasm::ModuleParser::parseExport):
(JSC::Wasm::ModuleParser::parseStart):
(JSC::Wasm::ModuleParser::parseElement):
(JSC::Wasm::ModuleParser::parseCode): avoid overflow through function size.
(JSC::Wasm::ModuleParser::parseData):
* wasm/WasmModuleParser.h:
(JSC::Wasm::ModuleParser::moduleInformation):
* wasm/WasmParser.h:
(JSC::Wasm::Parser::consumeUTF8String): add as required by spec
(JSC::Wasm::Parser::parseExternalKind): add as per spec
* wasm/WasmPlan.cpp:
(JSC::Wasm::Plan::Plan): fix some ownership, improve some error messages
* wasm/WasmPlan.h: fix some ownership
(JSC::Wasm::Plan::getModuleInformation):
(JSC::Wasm::Plan::getMemory):
(JSC::Wasm::Plan::compiledFunctionCount):
(JSC::Wasm::Plan::compiledFunction):
(JSC::Wasm::Plan::getCompiledFunctions):
* wasm/WasmSections.h: macroize with description, so that error messages are super pretty. This could be auto-generated.
* wasm/js/JSWebAssemblyModule.cpp:
(JSC::JSWebAssemblyModule::create): take module information
(JSC::JSWebAssemblyModule::JSWebAssemblyModule): ditto
* wasm/js/JSWebAssemblyModule.h:
(JSC::JSWebAssemblyModule::moduleInformation):
* wasm/js/WebAssemblyInstanceConstructor.cpp:
(JSC::constructJSWebAssemblyInstance): check that modules with imports are instantiated with an import object, as per spec. This needs to be tested.
* wasm/js/WebAssemblyMemoryConstructor.cpp:
(JSC::constructJSWebAssemblyMemory):
* wasm/js/WebAssemblyModuleConstructor.cpp:
(JSC::constructJSWebAssemblyModule):
* wasm/js/WebAssemblyTableConstructor.cpp:
(JSC::constructJSWebAssemblyTable):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@208401 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/JSTests/wasm/js-api/test_basic_api.js b/JSTests/wasm/js-api/test_basic_api.js
index 054eaef..80ece29 100644
--- a/JSTests/wasm/js-api/test_basic_api.js
+++ b/JSTests/wasm/js-api/test_basic_api.js
@@ -28,7 +28,7 @@
 };
 
 
-assert.notUndef(WebAssembly);
+assert.isNotUndef(WebAssembly);
 checkOwnPropertyDescriptor(utilities.global, "WebAssembly", { typeofvalue: "object", writable: true, configurable: true, enumerable: false });
 assert.eq(String(WebAssembly), "[object WebAssembly]");
 assert.isUndef(WebAssembly.length);
@@ -37,13 +37,13 @@
 assert.throws(() => new WebAssembly(), TypeError, `WebAssembly is not a constructor (evaluating 'new WebAssembly()')`);
 
 for (const f in functionProperties) {
-    assert.notUndef(WebAssembly[f]);
+    assert.isNotUndef(WebAssembly[f]);
     assert.eq(WebAssembly[f].name, f);
     assert.eq(WebAssembly[f].length, functionProperties[f].length);
 }
 
 for (const c in constructorProperties) {
-    assert.notUndef(WebAssembly[c]);
+    assert.isNotUndef(WebAssembly[c]);
     assert.eq(WebAssembly[c].name, c);
     assert.eq(WebAssembly[c].length, constructorProperties[c].length);
     checkOwnPropertyDescriptor(WebAssembly, c, constructorProperties[c]);
@@ -66,7 +66,7 @@
         assert.instanceof(instance, WebAssembly.Instance);
         for (const invalid of invalidInstanceImports)
             assert.throws(() => new WebAssembly[c](new WebAssembly.Module(emptyModuleArray), invalid), TypeError, `second argument to WebAssembly.Instance must be undefined or an Object (evaluating 'new WebAssembly[c](new WebAssembly.Module(emptyModuleArray), invalid)')`);
-        assert.notUndef(instance.exports);
+        assert.isNotUndef(instance.exports);
         checkOwnPropertyDescriptor(instance, "exports", { typeofvalue: "object", writable: true, configurable: true, enumerable: true });
         assert.isUndef(instance.exports.__proto__);
         assert.eq(Reflect.isExtensible(instance.exports), false);