11cb0ef41Sopenharmony_ci// Copyright 2021 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#ifndef INCLUDE_V8_WASM_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_V8_WASM_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <functional>
91cb0ef41Sopenharmony_ci#include <memory>
101cb0ef41Sopenharmony_ci#include <string>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "v8-local-handle.h"  // NOLINT(build/include_directory)
131cb0ef41Sopenharmony_ci#include "v8-memory-span.h"   // NOLINT(build/include_directory)
141cb0ef41Sopenharmony_ci#include "v8-object.h"        // NOLINT(build/include_directory)
151cb0ef41Sopenharmony_ci#include "v8config.h"         // NOLINT(build/include_directory)
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciclass ArrayBuffer;
201cb0ef41Sopenharmony_ciclass Promise;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cinamespace internal {
231cb0ef41Sopenharmony_cinamespace wasm {
241cb0ef41Sopenharmony_ciclass NativeModule;
251cb0ef41Sopenharmony_ciclass StreamingDecoder;
261cb0ef41Sopenharmony_ci}  // namespace wasm
271cb0ef41Sopenharmony_ci}  // namespace internal
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci/**
301cb0ef41Sopenharmony_ci * An owned byte buffer with associated size.
311cb0ef41Sopenharmony_ci */
321cb0ef41Sopenharmony_cistruct OwnedBuffer {
331cb0ef41Sopenharmony_ci  std::unique_ptr<const uint8_t[]> buffer;
341cb0ef41Sopenharmony_ci  size_t size = 0;
351cb0ef41Sopenharmony_ci  OwnedBuffer(std::unique_ptr<const uint8_t[]> buffer, size_t size)
361cb0ef41Sopenharmony_ci      : buffer(std::move(buffer)), size(size) {}
371cb0ef41Sopenharmony_ci  OwnedBuffer() = default;
381cb0ef41Sopenharmony_ci};
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// Wrapper around a compiled WebAssembly module, which is potentially shared by
411cb0ef41Sopenharmony_ci// different WasmModuleObjects.
421cb0ef41Sopenharmony_ciclass V8_EXPORT CompiledWasmModule {
431cb0ef41Sopenharmony_ci public:
441cb0ef41Sopenharmony_ci  /**
451cb0ef41Sopenharmony_ci   * Serialize the compiled module. The serialized data does not include the
461cb0ef41Sopenharmony_ci   * wire bytes.
471cb0ef41Sopenharmony_ci   */
481cb0ef41Sopenharmony_ci  OwnedBuffer Serialize();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  /**
511cb0ef41Sopenharmony_ci   * Get the (wasm-encoded) wire bytes that were used to compile this module.
521cb0ef41Sopenharmony_ci   */
531cb0ef41Sopenharmony_ci  MemorySpan<const uint8_t> GetWireBytesRef();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const std::string& source_url() const { return source_url_; }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci private:
581cb0ef41Sopenharmony_ci  friend class WasmModuleObject;
591cb0ef41Sopenharmony_ci  friend class WasmStreaming;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  explicit CompiledWasmModule(std::shared_ptr<internal::wasm::NativeModule>,
621cb0ef41Sopenharmony_ci                              const char* source_url, size_t url_length);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  const std::shared_ptr<internal::wasm::NativeModule> native_module_;
651cb0ef41Sopenharmony_ci  const std::string source_url_;
661cb0ef41Sopenharmony_ci};
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci// An instance of WebAssembly.Memory.
691cb0ef41Sopenharmony_ciclass V8_EXPORT WasmMemoryObject : public Object {
701cb0ef41Sopenharmony_ci public:
711cb0ef41Sopenharmony_ci  WasmMemoryObject() = delete;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  /**
741cb0ef41Sopenharmony_ci   * Returns underlying ArrayBuffer.
751cb0ef41Sopenharmony_ci   */
761cb0ef41Sopenharmony_ci  Local<ArrayBuffer> Buffer();
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  V8_INLINE static WasmMemoryObject* Cast(Value* value) {
791cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
801cb0ef41Sopenharmony_ci    CheckCast(value);
811cb0ef41Sopenharmony_ci#endif
821cb0ef41Sopenharmony_ci    return static_cast<WasmMemoryObject*>(value);
831cb0ef41Sopenharmony_ci  }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci private:
861cb0ef41Sopenharmony_ci  static void CheckCast(Value* object);
871cb0ef41Sopenharmony_ci};
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci// All the tiers of Wasm execution.
901cb0ef41Sopenharmony_cienum class WasmExecutionTier : int8_t {
911cb0ef41Sopenharmony_ci  kNone,
921cb0ef41Sopenharmony_ci  kLiftoff,
931cb0ef41Sopenharmony_ci  kTurbofan,
941cb0ef41Sopenharmony_ci};
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci// An instance of WebAssembly.Module.
971cb0ef41Sopenharmony_ciclass V8_EXPORT WasmModuleObject : public Object {
981cb0ef41Sopenharmony_ci public:
991cb0ef41Sopenharmony_ci  WasmModuleObject() = delete;
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  /**
1021cb0ef41Sopenharmony_ci   * Efficiently re-create a WasmModuleObject, without recompiling, from
1031cb0ef41Sopenharmony_ci   * a CompiledWasmModule.
1041cb0ef41Sopenharmony_ci   */
1051cb0ef41Sopenharmony_ci  static MaybeLocal<WasmModuleObject> FromCompiledModule(
1061cb0ef41Sopenharmony_ci      Isolate* isolate, const CompiledWasmModule&);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  /**
1091cb0ef41Sopenharmony_ci   * Get the compiled module for this module object. The compiled module can be
1101cb0ef41Sopenharmony_ci   * shared by several module objects.
1111cb0ef41Sopenharmony_ci   */
1121cb0ef41Sopenharmony_ci  CompiledWasmModule GetCompiledModule();
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  /**
1151cb0ef41Sopenharmony_ci   * Compile a Wasm function of the specified index with the specified tier.
1161cb0ef41Sopenharmony_ci   */
1171cb0ef41Sopenharmony_ci  bool CompileFunction(Isolate* isolate, uint32_t function_index,
1181cb0ef41Sopenharmony_ci                       WasmExecutionTier tier);
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  /**
1211cb0ef41Sopenharmony_ci   * Deserialize or compile Wasm module.
1221cb0ef41Sopenharmony_ci   */
1231cb0ef41Sopenharmony_ci  static MaybeLocal<WasmModuleObject> DeserializeOrCompile(
1241cb0ef41Sopenharmony_ci      Isolate* isolate, MemorySpan<const uint8_t> wire_bytes,
1251cb0ef41Sopenharmony_ci      MemorySpan<const uint8_t> wasm_cache, bool& cacheRejected);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  /**
1281cb0ef41Sopenharmony_ci   * Compile a Wasm module from the provided uncompiled bytes.
1291cb0ef41Sopenharmony_ci   */
1301cb0ef41Sopenharmony_ci  static MaybeLocal<WasmModuleObject> Compile(
1311cb0ef41Sopenharmony_ci      Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  V8_INLINE static WasmModuleObject* Cast(Value* value) {
1341cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
1351cb0ef41Sopenharmony_ci    CheckCast(value);
1361cb0ef41Sopenharmony_ci#endif
1371cb0ef41Sopenharmony_ci    return static_cast<WasmModuleObject*>(value);
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci private:
1411cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
1421cb0ef41Sopenharmony_ci};
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci/**
1451cb0ef41Sopenharmony_ci * The V8 interface for WebAssembly streaming compilation. When streaming
1461cb0ef41Sopenharmony_ci * compilation is initiated, V8 passes a {WasmStreaming} object to the embedder
1471cb0ef41Sopenharmony_ci * such that the embedder can pass the input bytes for streaming compilation to
1481cb0ef41Sopenharmony_ci * V8.
1491cb0ef41Sopenharmony_ci */
1501cb0ef41Sopenharmony_ciclass V8_EXPORT WasmStreaming final {
1511cb0ef41Sopenharmony_ci public:
1521cb0ef41Sopenharmony_ci  class WasmStreamingImpl;
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  ~WasmStreaming();
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  /**
1591cb0ef41Sopenharmony_ci   * Pass a new chunk of bytes to WebAssembly streaming compilation.
1601cb0ef41Sopenharmony_ci   * The buffer passed into {OnBytesReceived} is owned by the caller.
1611cb0ef41Sopenharmony_ci   */
1621cb0ef41Sopenharmony_ci  void OnBytesReceived(const uint8_t* bytes, size_t size);
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  /**
1651cb0ef41Sopenharmony_ci   * {Finish} should be called after all received bytes where passed to
1661cb0ef41Sopenharmony_ci   * {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
1671cb0ef41Sopenharmony_ci   * must not be called after {Abort} has been called already.
1681cb0ef41Sopenharmony_ci   * If {can_use_compiled_module} is true and {SetCompiledModuleBytes} was
1691cb0ef41Sopenharmony_ci   * previously called, the compiled module bytes can be used.
1701cb0ef41Sopenharmony_ci   * If {can_use_compiled_module} is false, the compiled module bytes previously
1711cb0ef41Sopenharmony_ci   * set by {SetCompiledModuleBytes} should not be used.
1721cb0ef41Sopenharmony_ci   */
1731cb0ef41Sopenharmony_ci  void Finish(bool can_use_compiled_module = true);
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  /**
1761cb0ef41Sopenharmony_ci   * Abort streaming compilation. If {exception} has a value, then the promise
1771cb0ef41Sopenharmony_ci   * associated with streaming compilation is rejected with that value. If
1781cb0ef41Sopenharmony_ci   * {exception} does not have value, the promise does not get rejected.
1791cb0ef41Sopenharmony_ci   * {Abort} must not be called repeatedly, or after {Finish}.
1801cb0ef41Sopenharmony_ci   */
1811cb0ef41Sopenharmony_ci  void Abort(MaybeLocal<Value> exception);
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  /**
1841cb0ef41Sopenharmony_ci   * Passes previously compiled module bytes. This must be called before
1851cb0ef41Sopenharmony_ci   * {OnBytesReceived}, {Finish}, or {Abort}. Returns true if the module bytes
1861cb0ef41Sopenharmony_ci   * can be used, false otherwise. The buffer passed via {bytes} and {size}
1871cb0ef41Sopenharmony_ci   * is owned by the caller. If {SetCompiledModuleBytes} returns true, the
1881cb0ef41Sopenharmony_ci   * buffer must remain valid until either {Finish} or {Abort} completes.
1891cb0ef41Sopenharmony_ci   * The compiled module bytes should not be used until {Finish(true)} is
1901cb0ef41Sopenharmony_ci   * called, because they can be invalidated later by {Finish(false)}.
1911cb0ef41Sopenharmony_ci   */
1921cb0ef41Sopenharmony_ci  bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size);
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  /**
1951cb0ef41Sopenharmony_ci   * Sets a callback which is called whenever a significant number of new
1961cb0ef41Sopenharmony_ci   * functions are ready for serialization.
1971cb0ef41Sopenharmony_ci   */
1981cb0ef41Sopenharmony_ci  void SetMoreFunctionsCanBeSerializedCallback(
1991cb0ef41Sopenharmony_ci      std::function<void(CompiledWasmModule)>);
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  /*
2021cb0ef41Sopenharmony_ci   * Sets the UTF-8 encoded source URL for the {Script} object. This must be
2031cb0ef41Sopenharmony_ci   * called before {Finish}.
2041cb0ef41Sopenharmony_ci   */
2051cb0ef41Sopenharmony_ci  void SetUrl(const char* url, size_t length);
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  /**
2081cb0ef41Sopenharmony_ci   * Unpacks a {WasmStreaming} object wrapped in a  {Managed} for the embedder.
2091cb0ef41Sopenharmony_ci   * Since the embedder is on the other side of the API, it cannot unpack the
2101cb0ef41Sopenharmony_ci   * {Managed} itself.
2111cb0ef41Sopenharmony_ci   */
2121cb0ef41Sopenharmony_ci  static std::shared_ptr<WasmStreaming> Unpack(Isolate* isolate,
2131cb0ef41Sopenharmony_ci                                               Local<Value> value);
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci private:
2161cb0ef41Sopenharmony_ci  std::unique_ptr<WasmStreamingImpl> impl_;
2171cb0ef41Sopenharmony_ci};
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci}  // namespace v8
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci#endif  // INCLUDE_V8_WASM_H_
222