• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if !V8_ENABLE_WEBASSEMBLY
6 #error This header should only be included if WebAssembly is enabled.
7 #endif  // !V8_ENABLE_WEBASSEMBLY
8 
9 #ifndef V8_WASM_WASM_FEATURES_H_
10 #define V8_WASM_WASM_FEATURES_H_
11 
12 // The feature flags are declared in their own header.
13 #include "src/base/enum-set.h"
14 #include "src/base/macros.h"
15 #include "src/wasm/wasm-feature-flags.h"
16 
17 // Features that are always enabled and do not have a flag.
18 #define FOREACH_WASM_NON_FLAG_FEATURE(V) \
19   V(reftypes, "reference type opcodes", true)
20 
21 // All features, including features that do not have flags.
22 #define FOREACH_WASM_FEATURE(V) \
23   FOREACH_WASM_FEATURE_FLAG(V)  \
24   FOREACH_WASM_NON_FLAG_FEATURE(V)
25 
26 namespace v8 {
27 namespace internal {
28 
29 class Context;
30 template <typename T>
31 class Handle;
32 class Isolate;
33 
34 namespace wasm {
35 
36 enum WasmFeature {
37 #define DECL_FEATURE_ENUM(feat, ...) kFeature_##feat,
38   FOREACH_WASM_FEATURE(DECL_FEATURE_ENUM)
39 #undef DECL_FEATURE_ENUM
40 };
41 
42 // Enabled or detected features.
43 class WasmFeatures : public base::EnumSet<WasmFeature> {
44  public:
45   constexpr WasmFeatures() = default;
WasmFeatures(std::initializer_list<WasmFeature> features)46   explicit constexpr WasmFeatures(std::initializer_list<WasmFeature> features)
47       : EnumSet(features) {}
48 
49   // Simplified getters. Use {has_foo()} instead of {contains(kFeature_foo)}.
50 #define DECL_FEATURE_GETTER(feat, ...) \
51   constexpr bool has_##feat() const { return contains(kFeature_##feat); }
FOREACH_WASM_FEATURE(DECL_FEATURE_GETTER)52   FOREACH_WASM_FEATURE(DECL_FEATURE_GETTER)
53 #undef DECL_FEATURE_GETTER
54 
55   static constexpr const char* name_for_feature(WasmFeature feature) {
56     switch (feature) {
57 #define NAME(feat, ...)              \
58   case WasmFeature::kFeature_##feat: \
59     return #feat;
60       FOREACH_WASM_FEATURE(NAME)
61     }
62 #undef NAME
63   }
64   static inline constexpr WasmFeatures All();
65   static inline constexpr WasmFeatures None();
66   static inline constexpr WasmFeatures ForAsmjs();
67   // Retuns optional features that are enabled by flags, plus features that are
68   // not enabled by a flag and are always on.
69   static WasmFeatures FromFlags();
70   static V8_EXPORT_PRIVATE WasmFeatures FromIsolate(Isolate*);
71   static V8_EXPORT_PRIVATE WasmFeatures FromContext(Isolate*,
72                                                     Handle<Context> context);
73 };
74 
75 // static
All()76 constexpr WasmFeatures WasmFeatures::All() {
77 #define LIST_FEATURE(feat, ...) kFeature_##feat,
78   return WasmFeatures({FOREACH_WASM_FEATURE(LIST_FEATURE)});
79 #undef LIST_FEATURE
80 }
81 
82 // static
None()83 constexpr WasmFeatures WasmFeatures::None() { return {}; }
84 
85 // static
ForAsmjs()86 constexpr WasmFeatures WasmFeatures::ForAsmjs() { return {}; }
87 
88 }  // namespace wasm
89 }  // namespace internal
90 }  // namespace v8
91 
92 #endif  // V8_WASM_WASM_FEATURES_H_
93