wasm-objects.h 38.8 KB
Newer Older
1 2
// Copyright 2016 the V8 project authors. All rights reserved.  Use of
// this source code is governed by a BSD-style license that can be
3 4
// found in the LICENSE file.

5 6
#ifndef V8_WASM_WASM_OBJECTS_H_
#define V8_WASM_WASM_OBJECTS_H_
7

8 9
#include <memory>

10
#include "src/base/bit-field.h"
11
#include "src/base/bits.h"
12
#include "src/codegen/signature.h"
13
#include "src/debug/debug.h"
Marja Hölttä's avatar
Marja Hölttä committed
14
#include "src/heap/heap.h"
15
#include "src/objects/js-function.h"
16
#include "src/objects/objects.h"
17
#include "src/wasm/struct-types.h"
18
#include "src/wasm/value-type.h"
19
#include "torque-generated/class-definitions-tq.h"
20 21 22 23

// Has to be the last include (doesn't have include guards)
#include "src/objects/object-macros.h"

24 25 26
namespace v8 {
namespace internal {
namespace wasm {
27
struct CompilationEnv;
28
class InterpretedFrame;
Marja Hölttä's avatar
Marja Hölttä committed
29
struct InterpretedFrameDeleter;
30
class NativeModule;
31
class SignatureMap;
32
class WasmCode;
33
struct WasmException;
34
class WasmFeatures;
35
struct WasmGlobal;
36
class WasmInterpreter;
37
struct WasmModule;
38
class WasmValue;
Marja Hölttä's avatar
Marja Hölttä committed
39
class WireBytesRef;
40
}  // namespace wasm
41

Marja Hölttä's avatar
Marja Hölttä committed
42 43 44
class BreakPoint;
class JSArrayBuffer;
class SeqOneByteString;
45
class WasmCapiFunction;
46
class WasmExceptionTag;
47
class WasmExportedFunction;
48
class WasmExternalFunction;
49
class WasmInstanceObject;
50
class WasmJSFunction;
51
class WasmModuleObject;
52
class WasmIndirectFunctionTable;
53

54 55
enum class SharedFlag : uint8_t;

Marja Hölttä's avatar
Marja Hölttä committed
56 57 58
template <class CppType>
class Managed;

59
#define DECL_OPTIONAL_ACCESSORS(name, type) \
60
  DECL_GETTER(has_##name, bool)             \
61
  DECL_ACCESSORS(name, type)
62

63 64 65 66
// A helper for an entry in an indirect function table (IFT).
// The underlying storage in the instance is used by generated code to
// call functions indirectly at runtime.
// Each entry has the following fields:
67
// - object = target instance, if a Wasm function, tuple if imported
68
// - sig_id = signature id of function
69
// - target = entrypoint to Wasm code or import wrapper code
70
class V8_EXPORT_PRIVATE IndirectFunctionTableEntry {
71
 public:
72 73 74 75 76
  inline IndirectFunctionTableEntry(Handle<WasmInstanceObject>, int table_index,
                                    int entry_index);

  inline IndirectFunctionTableEntry(Handle<WasmIndirectFunctionTable> table,
                                    int entry_index);
77

78
  void clear();
79 80
  void Set(int sig_id, Handle<WasmInstanceObject> target_instance,
           int target_func_index);
81
  void Set(int sig_id, Address call_target, Object ref);
82

83 84 85
  Object object_ref() const;
  int sig_id() const;
  Address target() const;
86 87

 private:
88
  Handle<WasmInstanceObject> const instance_;
89
  Handle<WasmIndirectFunctionTable> const table_;
90
  int const index_;
91 92
};

93 94 95 96
// A helper for an entry for an imported function, indexed statically.
// The underlying storage in the instance is used by generated code to
// call imported functions at runtime.
// Each entry is either:
97
//   - Wasm to JS, which has fields
98 99
//      - object = a Tuple2 of the importing instance and the callable
//      - target = entrypoint to import wrapper code
100
//   - Wasm to Wasm, which has fields
101 102
//      - object = target instance
//      - target = entrypoint for the function
103 104
class ImportedFunctionEntry {
 public:
105
  inline ImportedFunctionEntry(Handle<WasmInstanceObject>, int index);
106

107
  // Initialize this entry as a Wasm to JS call. This accepts the isolate as a
108
  // parameter, since it must allocate a tuple.
109 110
  V8_EXPORT_PRIVATE void SetWasmToJs(Isolate*, Handle<JSReceiver> callable,
                                     const wasm::WasmCode* wasm_to_js_wrapper);
111
  // Initialize this entry as a Wasm to Wasm call.
112
  void SetWasmToWasm(WasmInstanceObject target_instance, Address call_target);
113

114 115
  WasmInstanceObject instance();
  JSReceiver callable();
116
  Object object_ref();
117 118 119
  Address target();

 private:
120
  Handle<WasmInstanceObject> const instance_;
121
  int const index_;
122 123
};

124 125
enum InternalizeString : bool { kInternalize = true, kNoInternalize = false };

126 127 128
// Representation of a WebAssembly.Module JavaScript-level object.
class WasmModuleObject : public JSObject {
 public:
129 130 131 132 133
  DECL_CAST(WasmModuleObject)

  DECL_ACCESSORS(managed_native_module, Managed<wasm::NativeModule>)
  DECL_ACCESSORS(export_wrappers, FixedArray)
  DECL_ACCESSORS(script, Script)
134
  inline wasm::NativeModule* native_module() const;
135 136
  inline const std::shared_ptr<wasm::NativeModule>& shared_native_module()
      const;
137
  inline const wasm::WasmModule* module() const;
138 139

  // Dispatched behavior.
140
  DECL_PRINTER(WasmModuleObject)
141
  DECL_VERIFIER(WasmModuleObject)
142

143
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
144
                                TORQUE_GENERATED_WASM_MODULE_OBJECT_FIELDS)
145

146 147
  // Creates a new {WasmModuleObject} for an existing {NativeModule} that is
  // reference counted and might be shared between multiple Isolates.
148
  V8_EXPORT_PRIVATE static Handle<WasmModuleObject> New(
149
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
150 151 152 153
      Handle<Script> script);
  V8_EXPORT_PRIVATE static Handle<WasmModuleObject> New(
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
      Handle<Script> script, Handle<FixedArray> export_wrappers);
154

155
  // Check whether this module was generated from asm.js source.
156
  inline bool is_asm_js();
157 158 159 160 161 162 163 164 165 166 167 168 169

  // Get the module name, if set. Returns an empty handle otherwise.
  static MaybeHandle<String> GetModuleNameOrNull(Isolate*,
                                                 Handle<WasmModuleObject>);

  // Get the function name of the function identified by the given index.
  // Returns a null handle if the function is unnamed or the name is not a valid
  // UTF-8 string.
  static MaybeHandle<String> GetFunctionNameOrNull(Isolate*,
                                                   Handle<WasmModuleObject>,
                                                   uint32_t func_index);

  // Get the function name of the function identified by the given index.
170
  // Returns "func[func_index]" if the function is unnamed or the
171
  // name is not a valid UTF-8 string.
172 173 174 175 176 177 178 179 180
  static Handle<String> GetFunctionName(Isolate*, Handle<WasmModuleObject>,
                                        uint32_t func_index);

  // Get the raw bytes of the function name of the function identified by the
  // given index.
  // Meant to be used for debugging or frame printing.
  // Does not allocate, hence gc-safe.
  Vector<const uint8_t> GetRawFunctionName(uint32_t func_index);

181 182 183
  // Extract a portion of the wire bytes as UTF-8 string, optionally
  // internalized. (Prefer to internalize early if the string will be used for a
  // property lookup anyway.)
184
  static Handle<String> ExtractUtf8StringFromModuleBytes(
185 186
      Isolate*, Handle<WasmModuleObject>, wasm::WireBytesRef,
      InternalizeString);
187
  static Handle<String> ExtractUtf8StringFromModuleBytes(
188 189
      Isolate*, Vector<const uint8_t> wire_byte, wasm::WireBytesRef,
      InternalizeString);
190

191
  OBJECT_CONSTRUCTORS(WasmModuleObject, JSObject);
192 193 194
};

// Representation of a WebAssembly.Table JavaScript-level object.
195
class V8_EXPORT_PRIVATE WasmTableObject : public JSObject {
196
 public:
197
  DECL_CAST(WasmTableObject)
198

199 200
  // The entries array is at least as big as {current_length()}, but might be
  // bigger to make future growth more efficient.
201
  DECL_ACCESSORS(entries, FixedArray)
202
  DECL_INT_ACCESSORS(current_length)
203 204
  // TODO(titzer): introduce DECL_I64_ACCESSORS macro
  DECL_ACCESSORS(maximum_length, Object)
205
  DECL_ACCESSORS(dispatch_tables, FixedArray)
206
  DECL_INT_ACCESSORS(raw_type)
207

208 209 210
  // Dispatched behavior.
  DECL_PRINTER(WasmTableObject)
  DECL_VERIFIER(WasmTableObject)
211

212 213
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
                                TORQUE_GENERATED_WASM_TABLE_OBJECT_FIELDS)
214

215
  inline wasm::ValueType type();
216 217 218

  static int Grow(Isolate* isolate, Handle<WasmTableObject> table,
                  uint32_t count, Handle<Object> init_value);
219

220
  static Handle<WasmTableObject> New(Isolate* isolate, wasm::ValueType type,
221 222
                                     uint32_t initial, bool has_maximum,
                                     uint32_t maximum,
223
                                     Handle<FixedArray>* entries);
224

225 226
  static void AddDispatchTable(Isolate* isolate, Handle<WasmTableObject> table,
                               Handle<WasmInstanceObject> instance,
227
                               int table_index);
228

229 230 231
  static bool IsInBounds(Isolate* isolate, Handle<WasmTableObject> table,
                         uint32_t entry_index);

232 233 234
  static bool IsValidElement(Isolate* isolate, Handle<WasmTableObject> table,
                             Handle<Object> entry);

235
  static void Set(Isolate* isolate, Handle<WasmTableObject> table,
236
                  uint32_t index, Handle<Object> entry);
237

238 239
  static Handle<Object> Get(Isolate* isolate, Handle<WasmTableObject> table,
                            uint32_t index);
240

241 242 243
  static void Fill(Isolate* isolate, Handle<WasmTableObject> table,
                   uint32_t start, Handle<Object> entry, uint32_t count);

244
  // TODO(wasm): Unify these three methods into one.
245 246
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
247 248
                                   int entry_index,
                                   const wasm::FunctionSig* sig,
249 250
                                   Handle<WasmInstanceObject> target_instance,
                                   int target_func_index);
251 252 253 254
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
                                   int entry_index,
                                   Handle<WasmJSFunction> function);
255 256 257 258
  static void UpdateDispatchTables(Isolate* isolate,
                                   Handle<WasmTableObject> table,
                                   int entry_index,
                                   Handle<WasmCapiFunction> capi_function);
259

260 261
  static void ClearDispatchTables(Isolate* isolate,
                                  Handle<WasmTableObject> table, int index);
262

263 264 265 266 267 268 269
  static void SetFunctionTablePlaceholder(Isolate* isolate,
                                          Handle<WasmTableObject> table,
                                          int entry_index,
                                          Handle<WasmInstanceObject> instance,
                                          int func_index);

  // This function reads the content of a function table entry and returns it
270 271 272 273 274 275
  // through the out parameters {is_valid}, {is_null}, {instance},
  // {function_index}, and {maybe_js_function}.
  static void GetFunctionTableEntry(
      Isolate* isolate, Handle<WasmTableObject> table, int entry_index,
      bool* is_valid, bool* is_null, MaybeHandle<WasmInstanceObject>* instance,
      int* function_index, MaybeHandle<WasmJSFunction>* maybe_js_function);
276

277
  OBJECT_CONSTRUCTORS(WasmTableObject, JSObject);
278 279 280 281 282
};

// Representation of a WebAssembly.Memory JavaScript-level object.
class WasmMemoryObject : public JSObject {
 public:
283
  DECL_CAST(WasmMemoryObject)
284

285
  DECL_ACCESSORS(array_buffer, JSArrayBuffer)
286
  DECL_INT_ACCESSORS(maximum_pages)
287
  DECL_OPTIONAL_ACCESSORS(instances, WeakArrayList)
288

289 290 291
  // Dispatched behavior.
  DECL_PRINTER(WasmMemoryObject)
  DECL_VERIFIER(WasmMemoryObject)
292

293
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
294
                                TORQUE_GENERATED_WASM_MEMORY_OBJECT_FIELDS)
295

296
  // Add an instance to the internal (weak) list.
297 298 299
  V8_EXPORT_PRIVATE static void AddInstance(Isolate* isolate,
                                            Handle<WasmMemoryObject> memory,
                                            Handle<WasmInstanceObject> object);
300
  inline bool has_maximum_pages();
301

302
  V8_EXPORT_PRIVATE static Handle<WasmMemoryObject> New(
303
      Isolate* isolate, MaybeHandle<JSArrayBuffer> buffer, uint32_t maximum);
304

305 306 307 308
  V8_EXPORT_PRIVATE static MaybeHandle<WasmMemoryObject> New(Isolate* isolate,
                                                             uint32_t initial,
                                                             uint32_t maximum,
                                                             SharedFlag shared);
309

310 311
  void update_instances(Isolate* isolate, Handle<JSArrayBuffer> buffer);

312 313
  V8_EXPORT_PRIVATE static int32_t Grow(Isolate*, Handle<WasmMemoryObject>,
                                        uint32_t pages);
314

315
  OBJECT_CONSTRUCTORS(WasmMemoryObject, JSObject);
316 317
};

318 319 320
// Representation of a WebAssembly.Global JavaScript-level object.
class WasmGlobalObject : public JSObject {
 public:
321
  DECL_CAST(WasmGlobalObject)
322

323 324
  DECL_ACCESSORS(untagged_buffer, JSArrayBuffer)
  DECL_ACCESSORS(tagged_buffer, FixedArray)
325
  DECL_INT32_ACCESSORS(offset)
326
  DECL_INT_ACCESSORS(raw_type)
327
  DECL_PRIMITIVE_ACCESSORS(type, wasm::ValueType)
328 329 330
  // TODO(7748): Once we improve the encoding of mutability/type, turn this back
  // into a boolean accessor.
  DECL_INT_ACCESSORS(is_mutable)
331

332 333 334 335
  // Dispatched behavior.
  DECL_PRINTER(WasmGlobalObject)
  DECL_VERIFIER(WasmGlobalObject)

336
  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
337
                                TORQUE_GENERATED_WASM_GLOBAL_OBJECT_FIELDS)
338

339
  V8_EXPORT_PRIVATE static MaybeHandle<WasmGlobalObject> New(
340 341
      Isolate* isolate, MaybeHandle<JSArrayBuffer> maybe_untagged_buffer,
      MaybeHandle<FixedArray> maybe_tagged_buffer, wasm::ValueType type,
342
      int32_t offset, bool is_mutable);
343

344
  inline int type_size() const;
345 346

  inline int32_t GetI32();
347
  inline int64_t GetI64();
348 349
  inline float GetF32();
  inline double GetF64();
350
  inline Handle<Object> GetRef();
351 352

  inline void SetI32(int32_t value);
353
  inline void SetI64(int64_t value);
354 355
  inline void SetF32(float value);
  inline void SetF64(double value);
356
  inline void SetExternRef(Handle<Object> value);
357
  inline bool SetFuncRef(Isolate* isolate, Handle<Object> value);
358 359 360 361 362 363

 private:
  // This function returns the address of the global's data in the
  // JSArrayBuffer. This buffer may be allocated on-heap, in which case it may
  // not have a fixed address.
  inline Address address() const;
364

365
  OBJECT_CONSTRUCTORS(WasmGlobalObject, JSObject);
366 367
};

368
// Representation of a WebAssembly.Instance JavaScript-level object.
369
class V8_EXPORT_PRIVATE WasmInstanceObject : public JSObject {
370
 public:
371 372 373 374 375 376 377 378 379
  DECL_CAST(WasmInstanceObject)

  DECL_ACCESSORS(module_object, WasmModuleObject)
  DECL_ACCESSORS(exports_object, JSObject)
  DECL_ACCESSORS(native_context, Context)
  DECL_OPTIONAL_ACCESSORS(memory_object, WasmMemoryObject)
  DECL_OPTIONAL_ACCESSORS(untagged_globals_buffer, JSArrayBuffer)
  DECL_OPTIONAL_ACCESSORS(tagged_globals_buffer, FixedArray)
  DECL_OPTIONAL_ACCESSORS(imported_mutable_globals_buffers, FixedArray)
380
  DECL_OPTIONAL_ACCESSORS(tables, FixedArray)
381
  DECL_OPTIONAL_ACCESSORS(indirect_function_tables, FixedArray)
382 383 384 385
  DECL_ACCESSORS(imported_function_refs, FixedArray)
  DECL_OPTIONAL_ACCESSORS(indirect_function_table_refs, FixedArray)
  DECL_OPTIONAL_ACCESSORS(managed_native_allocations, Foreign)
  DECL_OPTIONAL_ACCESSORS(exceptions_table, FixedArray)
386
  DECL_OPTIONAL_ACCESSORS(wasm_external_functions, FixedArray)
387
  DECL_ACCESSORS(managed_object_maps, FixedArray)
388
  DECL_PRIMITIVE_ACCESSORS(memory_start, byte*)
389 390
  DECL_PRIMITIVE_ACCESSORS(memory_size, size_t)
  DECL_PRIMITIVE_ACCESSORS(memory_mask, size_t)
391
  DECL_PRIMITIVE_ACCESSORS(isolate_root, Address)
392
  DECL_PRIMITIVE_ACCESSORS(stack_limit_address, Address)
393
  DECL_PRIMITIVE_ACCESSORS(real_stack_limit_address, Address)
394
  DECL_PRIMITIVE_ACCESSORS(imported_function_targets, Address*)
395
  DECL_PRIMITIVE_ACCESSORS(globals_start, byte*)
396
  DECL_PRIMITIVE_ACCESSORS(imported_mutable_globals, Address*)
397
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_size, uint32_t)
398 399
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_sig_ids, uint32_t*)
  DECL_PRIMITIVE_ACCESSORS(indirect_function_table_targets, Address*)
400
  DECL_PRIMITIVE_ACCESSORS(jump_table_start, Address)
401 402
  DECL_PRIMITIVE_ACCESSORS(data_segment_starts, Address*)
  DECL_PRIMITIVE_ACCESSORS(data_segment_sizes, uint32_t*)
403
  DECL_PRIMITIVE_ACCESSORS(dropped_elem_segments, byte*)
404
  DECL_PRIMITIVE_ACCESSORS(hook_on_function_call_address, Address)
405
  DECL_PRIMITIVE_ACCESSORS(num_liftoff_function_calls_array, uint32_t*)
406

407 408
  // Clear uninitialized padding space. This ensures that the snapshot content
  // is deterministic. Depending on the V8 build mode there could be no padding.
409 410
  V8_INLINE void clear_padding();

411 412 413 414
  // Dispatched behavior.
  DECL_PRINTER(WasmInstanceObject)
  DECL_VERIFIER(WasmInstanceObject)

415
// Layout description.
416
#define WASM_INSTANCE_OBJECT_FIELDS(V)                                    \
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  /* Often-accessed fields go first to minimize generated code size. */   \
  V(kMemoryStartOffset, kSystemPointerSize)                               \
  V(kMemorySizeOffset, kSizetSize)                                        \
  V(kMemoryMaskOffset, kSizetSize)                                        \
  V(kStackLimitAddressOffset, kSystemPointerSize)                         \
  V(kImportedFunctionRefsOffset, kTaggedSize)                             \
  V(kImportedFunctionTargetsOffset, kSystemPointerSize)                   \
  V(kIndirectFunctionTableRefsOffset, kTaggedSize)                        \
  V(kIndirectFunctionTableTargetsOffset, kSystemPointerSize)              \
  V(kIndirectFunctionTableSigIdsOffset, kSystemPointerSize)               \
  V(kIndirectFunctionTableSizeOffset, kUInt32Size)                        \
  /* Optional padding to align system pointer size fields */              \
  V(kOptionalPaddingOffset, POINTER_SIZE_PADDING(kOptionalPaddingOffset)) \
  V(kGlobalsStartOffset, kSystemPointerSize)                              \
  V(kImportedMutableGlobalsOffset, kSystemPointerSize)                    \
  V(kIsolateRootOffset, kSystemPointerSize)                               \
  V(kJumpTableStartOffset, kSystemPointerSize)                            \
  /* End of often-accessed fields. */                                     \
435 436 437 438 439 440 441
  V(kModuleObjectOffset, kTaggedSize)                                     \
  V(kExportsObjectOffset, kTaggedSize)                                    \
  V(kNativeContextOffset, kTaggedSize)                                    \
  V(kMemoryObjectOffset, kTaggedSize)                                     \
  V(kUntaggedGlobalsBufferOffset, kTaggedSize)                            \
  V(kTaggedGlobalsBufferOffset, kTaggedSize)                              \
  V(kImportedMutableGlobalsBuffersOffset, kTaggedSize)                    \
442
  V(kTablesOffset, kTaggedSize)                                           \
443
  V(kIndirectFunctionTablesOffset, kTaggedSize)                           \
444 445
  V(kManagedNativeAllocationsOffset, kTaggedSize)                         \
  V(kExceptionsTableOffset, kTaggedSize)                                  \
446
  V(kWasmExternalFunctionsOffset, kTaggedSize)                            \
447
  V(kManagedObjectMapsOffset, kTaggedSize)                                \
448 449 450
  V(kRealStackLimitAddressOffset, kSystemPointerSize)                     \
  V(kDataSegmentStartsOffset, kSystemPointerSize)                         \
  V(kDataSegmentSizesOffset, kSystemPointerSize)                          \
451
  V(kDroppedElemSegmentsOffset, kSystemPointerSize)                       \
452
  V(kHookOnFunctionCallAddressOffset, kSystemPointerSize)                 \
453
  V(kNumLiftoffFunctionCallsArrayOffset, kSystemPointerSize)              \
454
  V(kHeaderSize, 0)
455 456 457

  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
                                WASM_INSTANCE_OBJECT_FIELDS)
458
  STATIC_ASSERT(IsAligned(kHeaderSize, kTaggedSize));
459 460 461 462 463 464 465 466
  // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
  // fields (external pointers, doubles and BigInt data) are only kTaggedSize
  // aligned so checking for alignments of fields bigger than kTaggedSize
  // doesn't make sense until v8:8875 is fixed.
#define ASSERT_FIELD_ALIGNED(offset, size)                                 \
  STATIC_ASSERT(size == 0 || IsAligned(offset, size) ||                    \
                (COMPRESS_POINTERS_BOOL && (size == kSystemPointerSize) && \
                 IsAligned(offset, kTaggedSize)));
467 468
  WASM_INSTANCE_OBJECT_FIELDS(ASSERT_FIELD_ALIGNED)
#undef ASSERT_FIELD_ALIGNED
469
#undef WASM_INSTANCE_OBJECT_FIELDS
470

471
  static constexpr uint16_t kTaggedFieldOffsets[] = {
472 473
      kImportedFunctionRefsOffset,
      kIndirectFunctionTableRefsOffset,
474 475 476 477 478 479 480 481
      kModuleObjectOffset,
      kExportsObjectOffset,
      kNativeContextOffset,
      kMemoryObjectOffset,
      kUntaggedGlobalsBufferOffset,
      kTaggedGlobalsBufferOffset,
      kImportedMutableGlobalsBuffersOffset,
      kTablesOffset,
482
      kIndirectFunctionTablesOffset,
483 484
      kManagedNativeAllocationsOffset,
      kExceptionsTableOffset,
485 486
      kWasmExternalFunctionsOffset,
      kManagedObjectMapsOffset};
487

488
  const wasm::WasmModule* module();
489

490
  static bool EnsureIndirectFunctionTableWithMinimumSize(
491 492
      Handle<WasmInstanceObject> instance, int table_index,
      uint32_t minimum_size);
493

494
  void SetRawMemory(byte* mem_start, size_t mem_size);
495

496
  static Handle<WasmInstanceObject> New(Isolate*, Handle<WasmModuleObject>);
497

498 499
  Address GetCallTarget(uint32_t func_index);

500 501 502 503
  static int IndirectFunctionTableSize(Isolate* isolate,
                                       Handle<WasmInstanceObject> instance,
                                       uint32_t table_index);

504 505 506
  // Copies table entries. Returns {false} if the ranges are out-of-bounds.
  static bool CopyTableEntries(Isolate* isolate,
                               Handle<WasmInstanceObject> instance,
507 508
                               uint32_t table_dst_index,
                               uint32_t table_src_index, uint32_t dst,
509
                               uint32_t src,
510 511
                               uint32_t count) V8_WARN_UNUSED_RESULT;

512 513 514 515 516 517 518 519
  // Copy table entries from an element segment. Returns {false} if the ranges
  // are out-of-bounds.
  static bool InitTableEntries(Isolate* isolate,
                               Handle<WasmInstanceObject> instance,
                               uint32_t table_index, uint32_t segment_index,
                               uint32_t dst, uint32_t src,
                               uint32_t count) V8_WARN_UNUSED_RESULT;

520 521
  // Iterates all fields in the object except the untagged fields.
  class BodyDescriptor;
522

523
  static MaybeHandle<WasmExternalFunction> GetWasmExternalFunction(
524
      Isolate* isolate, Handle<WasmInstanceObject> instance, int index);
525

526
  // Acquires the {WasmExternalFunction} for a given {function_index} from the
527 528 529
  // cache of the given {instance}, or creates a new {WasmExportedFunction} if
  // it does not exist yet. The new {WasmExportedFunction} is added to the
  // cache of the {instance} immediately.
530 531 532
  static Handle<WasmExternalFunction> GetOrCreateWasmExternalFunction(
      Isolate* isolate, Handle<WasmInstanceObject> instance,
      int function_index);
533

534
  static void SetWasmExternalFunction(Isolate* isolate,
535 536
                                      Handle<WasmInstanceObject> instance,
                                      int index,
537
                                      Handle<WasmExternalFunction> val);
538

539 540 541 542 543
  // Imports a constructed {WasmJSFunction} into the indirect function table of
  // this instance. Note that this might trigger wrapper compilation, since a
  // {WasmJSFunction} is instance-independent and just wraps a JS callable.
  static void ImportWasmJSFunctionIntoTable(Isolate* isolate,
                                            Handle<WasmInstanceObject> instance,
544
                                            int table_index, int entry_index,
545 546
                                            Handle<WasmJSFunction> js_function);

547 548 549 550 551 552 553 554 555 556
  // Get a raw pointer to the location where the given global is stored.
  // {global} must not be a reference type.
  static uint8_t* GetGlobalStorage(Handle<WasmInstanceObject>,
                                   const wasm::WasmGlobal&);

  // Get the FixedArray and the index in that FixedArray for the given global,
  // which must be a reference type.
  static std::pair<Handle<FixedArray>, uint32_t> GetGlobalBufferAndIndex(
      Handle<WasmInstanceObject>, const wasm::WasmGlobal&);

557 558 559 560
  // Get the value of a global in the given instance.
  static wasm::WasmValue GetGlobalValue(Handle<WasmInstanceObject>,
                                        const wasm::WasmGlobal&);

561 562 563 564 565
  // Get the name of a global in the given instance by index.
  static MaybeHandle<String> GetGlobalNameOrNull(Isolate*,
                                                 Handle<WasmInstanceObject>,
                                                 uint32_t global_index);

566 567 568 569 570
  // Get the name of a memory in the given instance by index.
  static MaybeHandle<String> GetMemoryNameOrNull(Isolate*,
                                                 Handle<WasmInstanceObject>,
                                                 uint32_t memory_index);

571 572 573 574 575
  // Get the name of a table in the given instance by index.
  static MaybeHandle<String> GetTableNameOrNull(Isolate*,
                                                Handle<WasmInstanceObject>,
                                                uint32_t table_index);

576
  OBJECT_CONSTRUCTORS(WasmInstanceObject, JSObject);
577 578

 private:
579 580 581 582 583
  // Get the name in the given instance by index and kind.
  static MaybeHandle<String> GetNameFromImportsAndExportsOrNull(
      Isolate*, Handle<WasmInstanceObject>, wasm::ImportExportKindCode kind,
      uint32_t index);

584 585
  static void InitDataSegmentArrays(Handle<WasmInstanceObject>,
                                    Handle<WasmModuleObject>);
586 587
  static void InitElemSegmentArrays(Handle<WasmInstanceObject>,
                                    Handle<WasmModuleObject>);
588 589
};

590 591 592
// Representation of WebAssembly.Exception JavaScript-level object.
class WasmExceptionObject : public JSObject {
 public:
593
  DECL_CAST(WasmExceptionObject)
594

595 596
  DECL_ACCESSORS(serialized_signature, PodArray<wasm::ValueType>)
  DECL_ACCESSORS(exception_tag, HeapObject)
597

598 599 600
  // Dispatched behavior.
  DECL_PRINTER(WasmExceptionObject)
  DECL_VERIFIER(WasmExceptionObject)
601 602

  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
603
                                TORQUE_GENERATED_WASM_EXCEPTION_OBJECT_FIELDS)
604

605 606 607 608 609
  // Checks whether the given {sig} has the same parameter types as the
  // serialized signature stored within this exception object.
  bool IsSignatureEqual(const wasm::FunctionSig* sig);

  static Handle<WasmExceptionObject> New(Isolate* isolate,
610 611
                                         const wasm::FunctionSig* sig,
                                         Handle<HeapObject> exception_tag);
612

613
  OBJECT_CONSTRUCTORS(WasmExceptionObject, JSObject);
614 615
};

616
// A Wasm exception that has been thrown out of Wasm code.
617
class V8_EXPORT_PRIVATE WasmExceptionPackage : public JSReceiver {
618
 public:
619 620 621
  static Handle<WasmExceptionPackage> New(
      Isolate* isolate, Handle<WasmExceptionTag> exception_tag,
      int encoded_size);
622 623 624

  // The below getters return {undefined} in case the given exception package
  // does not carry the requested values (i.e. is of a different type).
625 626 627 628
  static Handle<Object> GetExceptionTag(
      Isolate* isolate, Handle<WasmExceptionPackage> exception_package);
  static Handle<Object> GetExceptionValues(
      Isolate* isolate, Handle<WasmExceptionPackage> exception_package);
629 630 631

  // Determines the size of the array holding all encoded exception values.
  static uint32_t GetEncodedSize(const wasm::WasmException* exception);
632 633 634

  DECL_CAST(WasmExceptionPackage)
  OBJECT_CONSTRUCTORS(WasmExceptionPackage, JSReceiver);
635 636 637
};

// A Wasm function that is wrapped and exported to JavaScript.
638
// Representation of WebAssembly.Function JavaScript-level object.
639 640
class WasmExportedFunction : public JSFunction {
 public:
641
  WasmInstanceObject instance();
642
  V8_EXPORT_PRIVATE int function_index();
643

644
  V8_EXPORT_PRIVATE static bool IsWasmExportedFunction(Object object);
645

646
  V8_EXPORT_PRIVATE static Handle<WasmExportedFunction> New(
647 648
      Isolate* isolate, Handle<WasmInstanceObject> instance, int func_index,
      int arity, Handle<Code> export_wrapper);
649

650
  Address GetWasmCallTarget();
651

652
  V8_EXPORT_PRIVATE const wasm::FunctionSig* sig();
653

654
  DECL_CAST(WasmExportedFunction)
655
  OBJECT_CONSTRUCTORS(WasmExportedFunction, JSFunction);
656 657
};

658 659 660 661 662 663
// A Wasm function that was created by wrapping a JavaScript callable.
// Representation of WebAssembly.Function JavaScript-level object.
class WasmJSFunction : public JSFunction {
 public:
  static bool IsWasmJSFunction(Object object);

664 665
  static Handle<WasmJSFunction> New(Isolate* isolate,
                                    const wasm::FunctionSig* sig,
666 667
                                    Handle<JSReceiver> callable);

668
  JSReceiver GetCallable() const;
669 670
  // Deserializes the signature of this function using the provided zone. Note
  // that lifetime of the signature is hence directly coupled to the zone.
671 672
  const wasm::FunctionSig* GetSignature(Zone* zone);
  bool MatchesSignature(const wasm::FunctionSig* sig);
673

674 675 676 677
  DECL_CAST(WasmJSFunction)
  OBJECT_CONSTRUCTORS(WasmJSFunction, JSFunction);
};

678 679 680 681 682 683
// An external function exposed to Wasm via the C/C++ API.
class WasmCapiFunction : public JSFunction {
 public:
  static bool IsWasmCapiFunction(Object object);

  static Handle<WasmCapiFunction> New(
684
      Isolate* isolate, Address call_target, Handle<Foreign> embedder_data,
685 686 687 688 689 690 691 692 693 694 695 696
      Handle<PodArray<wasm::ValueType>> serialized_signature);

  Address GetHostCallTarget() const;
  PodArray<wasm::ValueType> GetSerializedSignature() const;
  // Checks whether the given {sig} has the same parameter types as the
  // serialized signature stored within this C-API function object.
  bool IsSignatureEqual(const wasm::FunctionSig* sig) const;

  DECL_CAST(WasmCapiFunction)
  OBJECT_CONSTRUCTORS(WasmCapiFunction, JSFunction);
};

697 698 699 700 701 702 703 704 705 706 707 708 709
// Any external function that can be imported/exported in modules. This abstract
// class just dispatches to the following concrete classes:
//  - {WasmExportedFunction}: A proper Wasm function exported from a module.
//  - {WasmJSFunction}: A function constructed via WebAssembly.Function in JS.
// // TODO(wasm): Potentially {WasmCapiFunction} will be added here as well.
class WasmExternalFunction : public JSFunction {
 public:
  static bool IsWasmExternalFunction(Object object);

  DECL_CAST(WasmExternalFunction)
  OBJECT_CONSTRUCTORS(WasmExternalFunction, JSFunction);
};

710 711 712 713 714 715 716 717
class WasmIndirectFunctionTable : public Struct {
 public:
  DECL_PRIMITIVE_ACCESSORS(size, uint32_t)
  DECL_PRIMITIVE_ACCESSORS(sig_ids, uint32_t*)
  DECL_PRIMITIVE_ACCESSORS(targets, Address*)
  DECL_OPTIONAL_ACCESSORS(managed_native_allocations, Foreign)
  DECL_ACCESSORS(refs, FixedArray)

718 719
  V8_EXPORT_PRIVATE static Handle<WasmIndirectFunctionTable> New(
      Isolate* isolate, uint32_t size);
720 721
  static void Resize(Isolate* isolate, Handle<WasmIndirectFunctionTable> table,
                     uint32_t new_size);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

  DECL_CAST(WasmIndirectFunctionTable)

  DECL_PRINTER(WasmIndirectFunctionTable)
  DECL_VERIFIER(WasmIndirectFunctionTable)

  DEFINE_FIELD_OFFSET_CONSTANTS(
      HeapObject::kHeaderSize,
      TORQUE_GENERATED_WASM_INDIRECT_FUNCTION_TABLE_FIELDS)

  STATIC_ASSERT(kStartOfStrongFieldsOffset == kManagedNativeAllocationsOffset);
  using BodyDescriptor = FlexibleBodyDescriptor<kStartOfStrongFieldsOffset>;

  OBJECT_CONSTRUCTORS(WasmIndirectFunctionTable, Struct);
};

738 739 740
class WasmCapiFunctionData : public Struct {
 public:
  DECL_PRIMITIVE_ACCESSORS(call_target, Address)
741
  DECL_ACCESSORS(embedder_data, Foreign)
742 743 744 745 746 747 748 749 750 751 752
  DECL_ACCESSORS(wrapper_code, Code)
  DECL_ACCESSORS(serialized_signature, PodArray<wasm::ValueType>)

  DECL_CAST(WasmCapiFunctionData)

  DECL_PRINTER(WasmCapiFunctionData)
  DECL_VERIFIER(WasmCapiFunctionData)

  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
                                TORQUE_GENERATED_WASM_CAPI_FUNCTION_DATA_FIELDS)

753
  STATIC_ASSERT(kStartOfStrongFieldsOffset == kEmbedderDataOffset);
754
  using BodyDescriptor = FlexibleBodyDescriptor<kStartOfStrongFieldsOffset>;
755 756 757 758

  OBJECT_CONSTRUCTORS(WasmCapiFunctionData, Struct);
};

759 760 761
// Information for a WasmExportedFunction which is referenced as the function
// data of the SharedFunctionInfo underlying the function. For details please
// see the {SharedFunctionInfo::HasWasmExportedFunctionData} predicate.
762
class WasmExportedFunctionData : public Struct {
763
 public:
764
  DECL_ACCESSORS(wrapper_code, Code)
765
  DECL_ACCESSORS(instance, WasmInstanceObject)
766 767
  DECL_INT_ACCESSORS(jump_table_offset)
  DECL_INT_ACCESSORS(function_index)
768
  DECL_ACCESSORS(c_wrapper_code, Object)
769
  DECL_ACCESSORS(wasm_call_target, Object)
770
  DECL_INT_ACCESSORS(packed_args_size)
771
  DECL_ACCESSORS(signature, Foreign)
772

773
  DECL_CAST(WasmExportedFunctionData)
774 775 776 777 778

  // Dispatched behavior.
  DECL_PRINTER(WasmExportedFunctionData)
  DECL_VERIFIER(WasmExportedFunctionData)

779
  // Layout description.
780 781 782
  DEFINE_FIELD_OFFSET_CONSTANTS(
      HeapObject::kHeaderSize,
      TORQUE_GENERATED_WASM_EXPORTED_FUNCTION_DATA_FIELDS)
783

784
  OBJECT_CONSTRUCTORS(WasmExportedFunctionData, Struct);
785 786
};

787 788 789 790 791
// Information for a WasmJSFunction which is referenced as the function data of
// the SharedFunctionInfo underlying the function. For details please see the
// {SharedFunctionInfo::HasWasmJSFunctionData} predicate.
class WasmJSFunctionData : public Struct {
 public:
792 793 794
  DECL_INT_ACCESSORS(serialized_return_count)
  DECL_INT_ACCESSORS(serialized_parameter_count)
  DECL_ACCESSORS(serialized_signature, PodArray<wasm::ValueType>)
795
  DECL_ACCESSORS(callable, JSReceiver)
796 797 798 799 800 801 802 803 804 805
  DECL_ACCESSORS(wrapper_code, Code)

  DECL_CAST(WasmJSFunctionData)

  // Dispatched behavior.
  DECL_PRINTER(WasmJSFunctionData)
  DECL_VERIFIER(WasmJSFunctionData)

  // Layout description.
  DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
806
                                TORQUE_GENERATED_WASM_JS_FUNCTION_DATA_FIELDS)
807 808 809 810

  OBJECT_CONSTRUCTORS(WasmJSFunctionData, Struct);
};

811 812 813 814 815 816 817 818 819 820 821
class WasmScript : public AllStatic {
 public:
  // Set a breakpoint on the given byte position inside the given module.
  // This will affect all live and future instances of the module.
  // The passed position might be modified to point to the next breakable
  // location inside the same function.
  // If it points outside a function, or behind the last breakable location,
  // this function returns false and does not set any breakpoint.
  V8_EXPORT_PRIVATE static bool SetBreakPoint(Handle<Script>, int* position,
                                              Handle<BreakPoint> break_point);

822 823 824 825 826 827 828 829 830 831 832 833 834
  // Set a breakpoint on first breakable position of the given function index
  // inside the given module. This will affect all live and future instances of
  // the module.
  V8_EXPORT_PRIVATE static bool SetBreakPointOnFirstBreakableForFunction(
      Handle<Script>, int function_index, Handle<BreakPoint> break_point);

  // Set a breakpoint at the breakable offset of the given function index
  // inside the given module. This will affect all live and future instances of
  // the module.
  V8_EXPORT_PRIVATE static bool SetBreakPointForFunction(
      Handle<Script>, int function_index, int breakable_offset,
      Handle<BreakPoint> break_point);

835 836 837 838 839
  // Remove a previously set breakpoint at the given byte position inside the
  // given module. If this breakpoint is not found this function returns false.
  V8_EXPORT_PRIVATE static bool ClearBreakPoint(Handle<Script>, int position,
                                                Handle<BreakPoint> break_point);

840 841 842 843 844
  // Remove a previously set breakpoint by id. If this breakpoint is not found,
  // returns false.
  V8_EXPORT_PRIVATE static bool ClearBreakPointById(Handle<Script>,
                                                    int breakpoint_id);

845 846 847
  // Remove all set breakpoints.
  static void ClearAllBreakpoints(Script);

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
  // Get a list of all possible breakpoints within a given range of this module.
  V8_EXPORT_PRIVATE static bool GetPossibleBreakpoints(
      wasm::NativeModule* native_module, const debug::Location& start,
      const debug::Location& end, std::vector<debug::BreakLocation>* locations);

  // Return an empty handle if no breakpoint is hit at that location, or a
  // FixedArray with all hit breakpoint objects.
  static MaybeHandle<FixedArray> CheckBreakPoints(Isolate*, Handle<Script>,
                                                  int position);

 private:
  // Helper functions that update the breakpoint info list.
  static void AddBreakpointToInfo(Handle<Script>, int position,
                                  Handle<BreakPoint> break_point);
};

864 865 866 867
// Tags provide an object identity for each exception defined in a wasm module
// header. They are referenced by the following fields:
//  - {WasmExceptionObject::exception_tag}  : The tag of the exception object.
//  - {WasmInstanceObject::exceptions_table}: List of tags used by an instance.
868 869
class WasmExceptionTag
    : public TorqueGeneratedWasmExceptionTag<WasmExceptionTag, Struct> {
870
 public:
871 872
  V8_EXPORT_PRIVATE static Handle<WasmExceptionTag> New(Isolate* isolate,
                                                        int index);
873 874 875

  DECL_PRINTER(WasmExceptionTag)

876
  TQ_OBJECT_CONSTRUCTORS(WasmExceptionTag)
877 878
};

879 880
// Data annotated to the asm.js Module function. Used for later instantiation of
// that function.
881
class AsmWasmData : public Struct {
882 883 884
 public:
  static Handle<AsmWasmData> New(
      Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
885
      Handle<FixedArray> export_wrappers, Handle<HeapNumber> uses_bitset);
886

887 888 889
  DECL_ACCESSORS(managed_native_module, Managed<wasm::NativeModule>)
  DECL_ACCESSORS(export_wrappers, FixedArray)
  DECL_ACCESSORS(uses_bitset, HeapNumber)
890

891
  DECL_CAST(AsmWasmData)
892 893 894
  DECL_PRINTER(AsmWasmData)
  DECL_VERIFIER(AsmWasmData)

895 896
  DEFINE_FIELD_OFFSET_CONSTANTS(Struct::kHeaderSize,
                                TORQUE_GENERATED_ASM_WASM_DATA_FIELDS)
897

898
  OBJECT_CONSTRUCTORS(AsmWasmData, Struct);
899 900
};

901 902
class WasmTypeInfo : public TorqueGeneratedWasmTypeInfo<WasmTypeInfo, Foreign> {
 public:
903 904
  inline void clear_foreign_address(Isolate* isolate);

905 906 907 908 909 910 911 912
  DECL_CAST(WasmTypeInfo)
  DECL_PRINTER(WasmTypeInfo)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmTypeInfo)
};

913 914 915 916
class WasmStruct : public TorqueGeneratedWasmStruct<WasmStruct, HeapObject> {
 public:
  static inline wasm::StructType* type(Map map);
  inline wasm::StructType* type() const;
917
  static inline wasm::StructType* GcSafeType(Map map);
918 919 920 921 922 923 924 925 926 927 928

  inline ObjectSlot RawField(int raw_offset);

  DECL_CAST(WasmStruct)
  DECL_PRINTER(WasmStruct)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmStruct)
};

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
class WasmArray : public TorqueGeneratedWasmArray<WasmArray, HeapObject> {
 public:
  static inline wasm::ArrayType* type(Map map);
  inline wasm::ArrayType* type() const;
  static inline wasm::ArrayType* GcSafeType(Map map);

  static inline int SizeFor(Map map, int length);

  DECL_CAST(WasmArray)
  DECL_PRINTER(WasmArray)

  class BodyDescriptor;

  TQ_OBJECT_CONSTRUCTORS(WasmArray)
};

945 946
#undef DECL_OPTIONAL_ACCESSORS

947 948 949 950 951 952
namespace wasm {

Handle<Map> CreateStructMap(Isolate* isolate, const WasmModule* module,
                            int struct_index, Handle<Map> rtt_parent);
Handle<Map> CreateArrayMap(Isolate* isolate, const WasmModule* module,
                           int array_index, Handle<Map> rtt_parent);
953 954
Handle<Map> CreateGenericRtt(Isolate* isolate, const WasmModule* module,
                             Handle<Map> rtt_parent);
955 956 957
Handle<Map> AllocateSubRtt(Isolate* isolate,
                           Handle<WasmInstanceObject> instance, uint32_t type,
                           Handle<Map> parent);
958 959 960

}  // namespace wasm

961 962 963
}  // namespace internal
}  // namespace v8

964 965
#include "src/objects/object-macros-undef.h"

966
#endif  // V8_WASM_WASM_OBJECTS_H_