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 
29 struct JS_PUBLIC_API JSContext;
30 class JS_PUBLIC_API JSFunction;
31 
32 namespace JS {
33 
34 class JS_PUBLIC_API Value;
35 
36 template <typename T>
37 class Handle;
38 
39 }  // namespace JS
40 
41 namespace js {
42 
43 namespace frontend {
44 
45 class ParserAtomsTable;
46 class ParseContext;
47 class ParseNode;
48 
49 template <class ParseHandler, typename CharT>
50 class Parser;
51 class FullParseHandler;
52 
53 }  // namespace frontend
54 
55 template <typename Unit>
56 using AsmJSParser = frontend::Parser<frontend::FullParseHandler, Unit>;
57 
58 // This function takes over parsing of a function starting with "use asm". The
59 // return value indicates whether an error was reported which the caller should
60 // propagate. If no error was reported, the function may still fail to validate
61 // as asm.js. In this case, the parser.tokenStream has been advanced an
62 // indeterminate amount and the entire function should be reparsed from the
63 // beginning.
64 
65 [[nodiscard]] extern bool CompileAsmJS(JSContext* cx,
66                                        frontend::ParserAtomsTable& parserAtoms,
67                                        AsmJSParser<mozilla::Utf8Unit>& parser,
68                                        frontend::ParseNode* stmtList,
69                                        bool* validated);
70 
71 [[nodiscard]] extern bool CompileAsmJS(JSContext* cx,
72                                        frontend::ParserAtomsTable& parserAtoms,
73                                        AsmJSParser<char16_t>& parser,
74                                        frontend::ParseNode* stmtList,
75                                        bool* validated);
76 
77 // asm.js module/export queries:
78 
79 extern bool IsAsmJSModuleNative(JSNative native);
80 
81 extern bool IsAsmJSModule(JSFunction* fun);
82 
83 extern bool IsAsmJSFunction(JSFunction* fun);
84 
85 extern bool IsAsmJSStrictModeModuleOrFunction(JSFunction* fun);
86 
87 extern bool InstantiateAsmJS(JSContext* cx, unsigned argc, JS::Value* vp);
88 
89 // asm.js testing natives:
90 
91 extern bool IsAsmJSCompilationAvailable(JSContext* cx, unsigned argc,
92                                         JS::Value* vp);
93 
94 extern bool IsAsmJSCompilationAvailable(JSContext* cx);
95 
96 extern bool IsAsmJSModule(JSContext* cx, unsigned argc, JS::Value* vp);
97 
98 extern bool IsAsmJSFunction(JSContext* cx, unsigned argc, JS::Value* vp);
99 
100 // asm.js toString/toSource support:
101 
102 extern JSString* AsmJSFunctionToString(JSContext* cx,
103                                        JS::Handle<JSFunction*> fun);
104 
105 extern JSString* AsmJSModuleToString(JSContext* cx, JS::Handle<JSFunction*> fun,
106                                      bool isToSource);
107 
108 // asm.js heap:
109 
110 extern bool IsValidAsmJSHeapLength(size_t length);
111 
112 }  // namespace js
113 
114 #endif  // wasm_AsmJS_h
115