AsmJS.h (4806B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * 4 * Copyright 2014 Mozilla Foundation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef wasm_AsmJS_h 20 #define wasm_AsmJS_h 21 22 #include "mozilla/Utf8.h" // mozilla::Utf8Unit 23 24 #include <stdint.h> // uint32_t 25 26 #include "jstypes.h" // JS_PUBLIC_API 27 #include "js/CallArgs.h" // JSNative 28 #include "wasm/WasmShareable.h" // ShareableBase 29 30 struct JS_PUBLIC_API JSContext; 31 class JS_PUBLIC_API JSFunction; 32 33 namespace JS { 34 35 class JS_PUBLIC_API Value; 36 37 template <typename T> 38 class Handle; 39 40 } // namespace JS 41 42 namespace js { 43 44 class FrontendContext; 45 class ScriptSource; 46 47 namespace frontend { 48 49 class ParserAtomsTable; 50 class ParseContext; 51 class ParseNode; 52 53 template <class ParseHandler, typename CharT> 54 class Parser; 55 class FullParseHandler; 56 57 } // namespace frontend 58 59 template <typename Unit> 60 using AsmJSParser = frontend::Parser<frontend::FullParseHandler, Unit>; 61 62 // This function takes over parsing of a function starting with "use asm". The 63 // return value indicates whether an error was reported which the caller should 64 // propagate. If no error was reported, the function may still fail to validate 65 // as asm.js. In this case, the parser.tokenStream has been advanced an 66 // indeterminate amount and the entire function should be reparsed from the 67 // beginning. 68 69 [[nodiscard]] extern bool CompileAsmJS(FrontendContext* fc, 70 frontend::ParserAtomsTable& parserAtoms, 71 AsmJSParser<mozilla::Utf8Unit>& parser, 72 frontend::ParseNode* stmtList, 73 bool* validated); 74 75 [[nodiscard]] extern bool CompileAsmJS(FrontendContext* fc, 76 frontend::ParserAtomsTable& parserAtoms, 77 AsmJSParser<char16_t>& parser, 78 frontend::ParseNode* stmtList, 79 bool* validated); 80 81 // asm.js module/export queries: 82 83 extern bool IsAsmJSModuleNative(JSNative native); 84 85 extern bool IsAsmJSModule(JSFunction* fun); 86 87 extern bool IsAsmJSFunction(JSFunction* fun); 88 89 extern bool IsAsmJSStrictModeModuleOrFunction(JSFunction* fun); 90 91 extern bool InstantiateAsmJS(JSContext* cx, unsigned argc, JS::Value* vp); 92 93 // asm.js testing natives: 94 95 extern bool IsAsmJSCompilationAvailable(JSContext* cx, unsigned argc, 96 JS::Value* vp); 97 98 extern bool IsAsmJSCompilationAvailable(JSContext* cx); 99 100 extern bool IsAsmJSModule(JSContext* cx, unsigned argc, JS::Value* vp); 101 102 extern bool IsAsmJSFunction(JSContext* cx, unsigned argc, JS::Value* vp); 103 104 // asm.js toString/toSource support: 105 106 extern JSString* AsmJSFunctionToString(JSContext* cx, 107 JS::Handle<JSFunction*> fun); 108 109 extern JSString* AsmJSModuleToString(JSContext* cx, JS::Handle<JSFunction*> fun, 110 bool isToSource); 111 112 // asm.js heap: 113 114 extern bool IsValidAsmJSHeapLength(size_t length); 115 116 // Minimally expose wasm::Code-lifetime state in AsmJS.cpp to ModuleGenerator 117 // and friends. The only implementation of this interface is in, and private 118 // to, WasmJS.cpp. In every place that stores or uses a CodeMetadataForAsmJS* 119 // (or smart-pointer equivalent), that pointer may be null, which indicates 120 // that the associated module is wasm, not asm.js. 121 122 struct CodeMetadataForAsmJSImpl; 123 124 struct CodeMetadataForAsmJS : public wasm::ShareableBase<CodeMetadataForAsmJS> { 125 CodeMetadataForAsmJS() {}; 126 virtual ~CodeMetadataForAsmJS() = default; 127 128 virtual const CodeMetadataForAsmJSImpl& asAsmJS() const = 0; 129 130 virtual bool mutedErrors() const = 0; 131 virtual const char16_t* displayURL() const = 0; 132 virtual ScriptSource* maybeScriptSource() const = 0; 133 virtual bool getFuncNameForAsmJS(uint32_t funcIndex, 134 wasm::UTF8Bytes* name) const = 0; 135 136 virtual size_t sizeOfExcludingThis( 137 mozilla::MallocSizeOf mallocSizeOf) const = 0; 138 }; 139 140 using MutableCodeMetadataForAsmJS = RefPtr<CodeMetadataForAsmJS>; 141 using SharedCodeMetadataForAsmJS = RefPtr<const CodeMetadataForAsmJS>; 142 143 } // namespace js 144 145 #endif // wasm_AsmJS_h