11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#if !V8_ENABLE_WEBASSEMBLY
61cb0ef41Sopenharmony_ci#error This header should only be included if WebAssembly is enabled.
71cb0ef41Sopenharmony_ci#endif  // !V8_ENABLE_WEBASSEMBLY
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef V8_WASM_WASM_FEATURES_H_
101cb0ef41Sopenharmony_ci#define V8_WASM_WASM_FEATURES_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// The feature flags are declared in their own header.
131cb0ef41Sopenharmony_ci#include "src/base/enum-set.h"
141cb0ef41Sopenharmony_ci#include "src/base/macros.h"
151cb0ef41Sopenharmony_ci#include "src/wasm/wasm-feature-flags.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// Features that are always enabled and do not have a flag.
181cb0ef41Sopenharmony_ci#define FOREACH_WASM_NON_FLAG_FEATURE(V) \
191cb0ef41Sopenharmony_ci  V(reftypes, "reference type opcodes", true)
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// All features, including features that do not have flags.
221cb0ef41Sopenharmony_ci#define FOREACH_WASM_FEATURE(V) \
231cb0ef41Sopenharmony_ci  FOREACH_WASM_FEATURE_FLAG(V)  \
241cb0ef41Sopenharmony_ci  FOREACH_WASM_NON_FLAG_FEATURE(V)
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cinamespace v8 {
271cb0ef41Sopenharmony_cinamespace internal {
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciclass Context;
301cb0ef41Sopenharmony_citemplate <typename T>
311cb0ef41Sopenharmony_ciclass Handle;
321cb0ef41Sopenharmony_ciclass Isolate;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cinamespace wasm {
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cienum WasmFeature {
371cb0ef41Sopenharmony_ci#define DECL_FEATURE_ENUM(feat, ...) kFeature_##feat,
381cb0ef41Sopenharmony_ci  FOREACH_WASM_FEATURE(DECL_FEATURE_ENUM)
391cb0ef41Sopenharmony_ci#undef DECL_FEATURE_ENUM
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Enabled or detected features.
431cb0ef41Sopenharmony_ciclass WasmFeatures : public base::EnumSet<WasmFeature> {
441cb0ef41Sopenharmony_ci public:
451cb0ef41Sopenharmony_ci  constexpr WasmFeatures() = default;
461cb0ef41Sopenharmony_ci  explicit constexpr WasmFeatures(std::initializer_list<WasmFeature> features)
471cb0ef41Sopenharmony_ci      : EnumSet(features) {}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Simplified getters. Use {has_foo()} instead of {contains(kFeature_foo)}.
501cb0ef41Sopenharmony_ci#define DECL_FEATURE_GETTER(feat, ...) \
511cb0ef41Sopenharmony_ci  constexpr bool has_##feat() const { return contains(kFeature_##feat); }
521cb0ef41Sopenharmony_ci  FOREACH_WASM_FEATURE(DECL_FEATURE_GETTER)
531cb0ef41Sopenharmony_ci#undef DECL_FEATURE_GETTER
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  static constexpr const char* name_for_feature(WasmFeature feature) {
561cb0ef41Sopenharmony_ci    switch (feature) {
571cb0ef41Sopenharmony_ci#define NAME(feat, ...)              \
581cb0ef41Sopenharmony_ci  case WasmFeature::kFeature_##feat: \
591cb0ef41Sopenharmony_ci    return #feat;
601cb0ef41Sopenharmony_ci      FOREACH_WASM_FEATURE(NAME)
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci#undef NAME
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  static inline constexpr WasmFeatures All();
651cb0ef41Sopenharmony_ci  static inline constexpr WasmFeatures None();
661cb0ef41Sopenharmony_ci  static inline constexpr WasmFeatures ForAsmjs();
671cb0ef41Sopenharmony_ci  // Retuns optional features that are enabled by flags, plus features that are
681cb0ef41Sopenharmony_ci  // not enabled by a flag and are always on.
691cb0ef41Sopenharmony_ci  static WasmFeatures FromFlags();
701cb0ef41Sopenharmony_ci  static V8_EXPORT_PRIVATE WasmFeatures FromIsolate(Isolate*);
711cb0ef41Sopenharmony_ci  static V8_EXPORT_PRIVATE WasmFeatures FromContext(Isolate*,
721cb0ef41Sopenharmony_ci                                                    Handle<Context> context);
731cb0ef41Sopenharmony_ci};
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci// static
761cb0ef41Sopenharmony_ciconstexpr WasmFeatures WasmFeatures::All() {
771cb0ef41Sopenharmony_ci#define LIST_FEATURE(feat, ...) kFeature_##feat,
781cb0ef41Sopenharmony_ci  return WasmFeatures({FOREACH_WASM_FEATURE(LIST_FEATURE)});
791cb0ef41Sopenharmony_ci#undef LIST_FEATURE
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci// static
831cb0ef41Sopenharmony_ciconstexpr WasmFeatures WasmFeatures::None() { return {}; }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci// static
861cb0ef41Sopenharmony_ciconstexpr WasmFeatures WasmFeatures::ForAsmjs() { return {}; }
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci}  // namespace wasm
891cb0ef41Sopenharmony_ci}  // namespace internal
901cb0ef41Sopenharmony_ci}  // namespace v8
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#endif  // V8_WASM_WASM_FEATURES_H_
93