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

#ifndef V8_WASM_OBJECTS_H_
#define V8_WASM_OBJECTS_H_

8
#include "src/debug/debug.h"
9
#include "src/debug/interface-types.h"
10
#include "src/objects.h"
11
#include "src/trap-handler/trap-handler.h"
12
#include "src/wasm/wasm-limits.h"
13 14 15 16

namespace v8 {
namespace internal {
namespace wasm {
17
class InterpretedFrame;
18
struct WasmModule;
19 20
struct WasmInstance;
class WasmInterpreter;
21 22 23 24 25
}

class WasmCompiledModule;
class WasmDebugInfo;
class WasmInstanceObject;
26
class WasmInstanceWrapper;
27 28 29 30 31

#define DECLARE_CASTS(name)             \
  static bool Is##name(Object* object); \
  static name* cast(Object* object)

32 33
#define DECLARE_GETTER(name, type) type* name()

34
#define DECLARE_ACCESSORS(name, type) \
35 36
  void set_##name(type* value);       \
  DECLARE_GETTER(name, type)
37 38 39

#define DECLARE_OPTIONAL_ACCESSORS(name, type) \
  bool has_##name();                           \
40
  DECLARE_ACCESSORS(name, type)
41

42 43 44 45
#define DECLARE_OPTIONAL_GETTER(name, type) \
  bool has_##name();                        \
  DECLARE_GETTER(name, type)

46 47 48
// Representation of a WebAssembly.Module JavaScript-level object.
class WasmModuleObject : public JSObject {
 public:
49
  // If a second field is added, we need a kWrapperTracerHeader field as well.
50
  // TODO(titzer): add the brand as an embedder field instead of a property.
51 52 53 54
  enum Fields { kCompiledModule, kFieldCount };

  DECLARE_CASTS(WasmModuleObject);

55
  WasmCompiledModule* compiled_module();
56 57 58 59 60 61 62 63

  static Handle<WasmModuleObject> New(
      Isolate* isolate, Handle<WasmCompiledModule> compiled_module);
};

// Representation of a WebAssembly.Table JavaScript-level object.
class WasmTableObject : public JSObject {
 public:
64
  // The 0-th field is used by the Blink Wrapper Tracer.
65
  // TODO(titzer): add the brand as an embedder field instead of a property.
66 67 68 69 70 71 72
  enum Fields {
    kWrapperTracerHeader,
    kFunctions,
    kMaximum,
    kDispatchTables,
    kFieldCount
  };
73 74 75

  DECLARE_CASTS(WasmTableObject);
  DECLARE_ACCESSORS(functions, FixedArray);
76
  DECLARE_GETTER(dispatch_tables, FixedArray);
77 78

  uint32_t current_length();
79 80
  bool has_maximum_length();
  int64_t maximum_length();  // Returns < 0 if no maximum.
81
  void grow(Isolate* isolate, uint32_t count);
82 83

  static Handle<WasmTableObject> New(Isolate* isolate, uint32_t initial,
84
                                     int64_t maximum,
85 86 87 88
                                     Handle<FixedArray>* js_functions);
  static Handle<FixedArray> AddDispatchTable(
      Isolate* isolate, Handle<WasmTableObject> table,
      Handle<WasmInstanceObject> instance, int table_index,
89
      Handle<FixedArray> function_table, Handle<FixedArray> signature_table);
90 91 92 93 94
};

// Representation of a WebAssembly.Memory JavaScript-level object.
class WasmMemoryObject : public JSObject {
 public:
95
  // The 0-th field is used by the Blink Wrapper Tracer.
96
  // TODO(titzer): add the brand as an embedder field instead of a property.
97 98 99 100 101 102 103
  enum Fields : uint8_t {
    kWrapperTracerHeader,
    kArrayBuffer,
    kMaximum,
    kInstancesLink,
    kFieldCount
  };
104 105

  DECLARE_CASTS(WasmMemoryObject);
106
  DECLARE_OPTIONAL_ACCESSORS(buffer, JSArrayBuffer);
107
  DECLARE_OPTIONAL_ACCESSORS(instances_link, WasmInstanceWrapper);
108

109 110
  void AddInstance(Isolate* isolate, Handle<WasmInstanceObject> object);
  void ResetInstancesLink(Isolate* isolate);
111
  uint32_t current_pages();
112 113
  bool has_maximum_pages();
  int32_t maximum_pages();  // Returns < 0 if there is no maximum.
114 115 116

  static Handle<WasmMemoryObject> New(Isolate* isolate,
                                      Handle<JSArrayBuffer> buffer,
117
                                      int32_t maximum);
118

119
  static int32_t Grow(Isolate*, Handle<WasmMemoryObject>, uint32_t pages);
120 121 122 123 124
};

// Representation of a WebAssembly.Instance JavaScript-level object.
class WasmInstanceObject : public JSObject {
 public:
125
  // The 0-th field is used by the Blink Wrapper Tracer.
126
  // TODO(titzer): add the brand as an embedder field instead of a property.
127
  enum Fields {
128
    kWrapperTracerHeader,
129 130 131 132 133
    kCompiledModule,
    kMemoryObject,
    kMemoryArrayBuffer,
    kGlobalsArrayBuffer,
    kDebugInfo,
134
    kWasmMemInstanceWrapper,
135 136 137 138 139 140 141 142 143 144
    kFieldCount
  };

  DECLARE_CASTS(WasmInstanceObject);

  DECLARE_ACCESSORS(compiled_module, WasmCompiledModule);
  DECLARE_OPTIONAL_ACCESSORS(globals_buffer, JSArrayBuffer);
  DECLARE_OPTIONAL_ACCESSORS(memory_buffer, JSArrayBuffer);
  DECLARE_OPTIONAL_ACCESSORS(memory_object, WasmMemoryObject);
  DECLARE_OPTIONAL_ACCESSORS(debug_info, WasmDebugInfo);
145
  DECLARE_OPTIONAL_ACCESSORS(instance_wrapper, WasmInstanceWrapper);
146 147 148 149

  WasmModuleObject* module_object();
  wasm::WasmModule* module();

150 151
  // Get the debug info associated with the given wasm object.
  // If no debug info exists yet, it is created automatically.
152
  static Handle<WasmDebugInfo> GetOrCreateDebugInfo(Handle<WasmInstanceObject>);
153

154 155 156 157 158 159 160 161
  static Handle<WasmInstanceObject> New(Isolate*, Handle<WasmCompiledModule>);

  int32_t GetMemorySize();

  static int32_t GrowMemory(Isolate*, Handle<WasmInstanceObject>,
                            uint32_t pages);

  uint32_t GetMaxMemoryPages();
162 163 164 165 166
};

// Representation of an exported WASM function.
class WasmExportedFunction : public JSFunction {
 public:
167 168
  // The 0-th field is used by the Blink Wrapper Tracer.
  enum Fields { kWrapperTracerHeader, kInstance, kIndex, kFieldCount };
169 170 171 172 173 174 175 176

  DECLARE_CASTS(WasmExportedFunction);

  WasmInstanceObject* instance();
  int function_index();

  static Handle<WasmExportedFunction> New(Isolate* isolate,
                                          Handle<WasmInstanceObject> instance,
177 178 179
                                          MaybeHandle<String> maybe_name,
                                          int func_index, int arity,
                                          Handle<Code> export_wrapper);
180 181
};

182 183
// Information shared by all WasmCompiledModule objects for the same module.
class WasmSharedModuleData : public FixedArray {
184
  // The 0-th field is used by the Blink Wrapper Tracer.
185
  enum Fields {
186
    kWrapperTracerHeader,
187 188 189 190
    kModuleWrapper,
    kModuleBytes,
    kScript,
    kAsmJsOffsetTable,
191
    kBreakPointInfos,
192
    kLazyCompilationOrchestrator,
193 194 195 196 197 198 199 200 201 202
    kFieldCount
  };

 public:
  DECLARE_CASTS(WasmSharedModuleData);

  DECLARE_GETTER(module, wasm::WasmModule);
  DECLARE_OPTIONAL_ACCESSORS(module_bytes, SeqOneByteString);
  DECLARE_GETTER(script, Script);
  DECLARE_OPTIONAL_ACCESSORS(asm_js_offset_table, ByteArray);
203
  DECLARE_OPTIONAL_GETTER(breakpoint_infos, FixedArray);
204 205 206 207 208 209 210 211 212

  static Handle<WasmSharedModuleData> New(
      Isolate* isolate, Handle<Foreign> module_wrapper,
      Handle<SeqOneByteString> module_bytes, Handle<Script> script,
      Handle<ByteArray> asm_js_offset_table);

  // Check whether this module was generated from asm.js source.
  bool is_asm_js();

213 214
  static void ReinitializeAfterDeserialization(Isolate*,
                                               Handle<WasmSharedModuleData>);
215 216 217 218 219 220

  static void AddBreakpoint(Handle<WasmSharedModuleData>, int position,
                            Handle<Object> break_point_object);

  static void SetBreakpointsOnNewInstance(Handle<WasmSharedModuleData>,
                                          Handle<WasmInstanceObject>);
221 222 223 224 225 226

  static void PrepareForLazyCompilation(Handle<WasmSharedModuleData>);

 private:
  DECLARE_OPTIONAL_GETTER(lazy_compilation_orchestrator, Foreign);
  friend class WasmCompiledModule;
227 228
};

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
// This represents the set of wasm compiled functions, together
// with all the information necessary for re-specializing them.
//
// We specialize wasm functions to their instance by embedding:
//   - raw interior pointers into the backing store of the array buffer
//     used as memory of a particular WebAssembly.Instance object.
//   - bounds check limits, computed at compile time, relative to the
//     size of the memory.
//   - the objects representing the function tables and signature tables
//   - raw pointer to the globals buffer.
//
// Even without instantiating, we need values for all of these parameters.
// We need to track these values to be able to create new instances and
// to be able to serialize/deserialize.
// The design decisions for how we track these values is not too immediate,
// and it deserves a summary. The "tricky" ones are: memory, globals, and
// the tables (signature and functions).
// The first 2 (memory & globals) are embedded as raw pointers to native
// buffers. All we need to track them is the start addresses and, in the
// case of memory, the size. We model all of them as HeapNumbers, because
// we need to store size_t values (for addresses), and potentially full
// 32 bit unsigned values for the size. Smis are 31 bits.
// For tables, we need to hold a reference to the JS Heap object, because
// we embed them as objects, and they may move.
253 254 255 256 257 258 259 260 261
class WasmCompiledModule : public FixedArray {
 public:
  enum Fields { kFieldCount };

  static WasmCompiledModule* cast(Object* fixed_array) {
    SLOW_DCHECK(IsWasmCompiledModule(fixed_array));
    return reinterpret_cast<WasmCompiledModule*>(fixed_array);
  }

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
#define WCM_OBJECT_OR_WEAK(TYPE, NAME, ID, TYPE_CHECK, SETTER_MODIFIER) \
 public:                                                                \
  Handle<TYPE> NAME() const { return handle(ptr_to_##NAME()); }         \
                                                                        \
  MaybeHandle<TYPE> maybe_##NAME() const {                              \
    if (has_##NAME()) return NAME();                                    \
    return MaybeHandle<TYPE>();                                         \
  }                                                                     \
                                                                        \
  TYPE* maybe_ptr_to_##NAME() const {                                   \
    Object* obj = get(ID);                                              \
    if (!(TYPE_CHECK)) return nullptr;                                  \
    return TYPE::cast(obj);                                             \
  }                                                                     \
                                                                        \
  TYPE* ptr_to_##NAME() const {                                         \
    Object* obj = get(ID);                                              \
    DCHECK(TYPE_CHECK);                                                 \
    return TYPE::cast(obj);                                             \
  }                                                                     \
                                                                        \
  bool has_##NAME() const {                                             \
    Object* obj = get(ID);                                              \
    return TYPE_CHECK;                                                  \
  }                                                                     \
                                                                        \
  void reset_##NAME() { set_undefined(ID); }                            \
                                                                        \
  SETTER_MODIFIER:                                                      \
  void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); }    \
  void set_ptr_to_##NAME(TYPE* value) { set(ID, value); }
293

294
#define WCM_OBJECT(TYPE, NAME) \
295 296 297 298
  WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME, obj->Is##TYPE(), public)

#define WCM_CONST_OBJECT(TYPE, NAME) \
  WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME, obj->Is##TYPE(), private)
299 300

#define WCM_WASM_OBJECT(TYPE, NAME) \
301
  WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME, TYPE::Is##TYPE(obj), private)
302

303 304
#define WCM_SMALL_CONST_NUMBER(TYPE, NAME)                         \
 public:                                                           \
305 306 307
  TYPE NAME() const {                                              \
    return static_cast<TYPE>(Smi::cast(get(kID_##NAME))->value()); \
  }                                                                \
308 309
                                                                   \
 private:                                                          \
310 311
  void set_##NAME(TYPE value) { set(kID_##NAME, Smi::FromInt(value)); }

312 313 314 315 316 317 318
#define WCM_WEAK_LINK(TYPE, NAME)                                          \
  WCM_OBJECT_OR_WEAK(WeakCell, weak_##NAME, kID_##NAME, obj->IsWeakCell(), \
                     public)                                               \
                                                                           \
 public:                                                                   \
  Handle<TYPE> NAME() const {                                              \
    return handle(TYPE::cast(weak_##NAME()->value()));                     \
319 320
  }

321
#define WCM_LARGE_NUMBER(TYPE, NAME)                                   \
322
 public:                                                               \
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  TYPE NAME() const {                                                  \
    Object* value = get(kID_##NAME);                                   \
    DCHECK(value->IsMutableHeapNumber());                              \
    return static_cast<TYPE>(HeapNumber::cast(value)->value());        \
  }                                                                    \
                                                                       \
  void set_##NAME(TYPE value) {                                        \
    Object* number = get(kID_##NAME);                                  \
    DCHECK(number->IsMutableHeapNumber());                             \
    HeapNumber::cast(number)->set_value(static_cast<double>(value));   \
  }                                                                    \
                                                                       \
  static void recreate_##NAME(Handle<WasmCompiledModule> obj,          \
                              Factory* factory, TYPE init_val) {       \
    Handle<HeapNumber> number = factory->NewHeapNumber(                \
        static_cast<double>(init_val), MutableMode::MUTABLE, TENURED); \
    obj->set(kID_##NAME, *number);                                     \
  }                                                                    \
  bool has_##NAME() const { return get(kID_##NAME)->IsMutableHeapNumber(); }

343 344 345 346
// Add values here if they are required for creating new instances or
// for deserialization, and if they are serializable.
// By default, instance values go to WasmInstanceObject, however, if
// we embed the generated code with a value, then we track that value here.
347 348 349
#define CORE_WCM_PROPERTY_TABLE(MACRO)                        \
  MACRO(WASM_OBJECT, WasmSharedModuleData, shared)            \
  MACRO(OBJECT, Context, native_context)                      \
350 351
  MACRO(SMALL_CONST_NUMBER, uint32_t, num_imported_functions) \
  MACRO(CONST_OBJECT, FixedArray, code_table)                 \
352 353 354
  MACRO(OBJECT, FixedArray, weak_exported_functions)          \
  MACRO(OBJECT, FixedArray, function_tables)                  \
  MACRO(OBJECT, FixedArray, signature_tables)                 \
355
  MACRO(CONST_OBJECT, FixedArray, empty_function_tables)      \
356 357 358
  MACRO(LARGE_NUMBER, size_t, embedded_mem_start)             \
  MACRO(LARGE_NUMBER, size_t, globals_start)                  \
  MACRO(LARGE_NUMBER, uint32_t, embedded_mem_size)            \
359
  MACRO(SMALL_CONST_NUMBER, uint32_t, min_mem_pages)          \
360 361 362
  MACRO(WEAK_LINK, WasmCompiledModule, next_instance)         \
  MACRO(WEAK_LINK, WasmCompiledModule, prev_instance)         \
  MACRO(WEAK_LINK, JSObject, owning_instance)                 \
363
  MACRO(WEAK_LINK, WasmModuleObject, wasm_module)
364 365

#if DEBUG
366
#define DEBUG_ONLY_TABLE(MACRO) MACRO(SMALL_CONST_NUMBER, uint32_t, instance_id)
367 368
#else
#define DEBUG_ONLY_TABLE(IGNORE)
369 370

 public:
371
  uint32_t instance_id() const { return static_cast<uint32_t>(-1); }
372 373 374 375 376 377 378 379 380 381 382 383 384 385
#endif

#define WCM_PROPERTY_TABLE(MACRO) \
  CORE_WCM_PROPERTY_TABLE(MACRO)  \
  DEBUG_ONLY_TABLE(MACRO)

 private:
  enum PropertyIndices {
#define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME,
    WCM_PROPERTY_TABLE(INDICES) Count
#undef INDICES
  };

 public:
386 387 388 389 390
  static Handle<WasmCompiledModule> New(
      Isolate* isolate, Handle<WasmSharedModuleData> shared,
      Handle<FixedArray> code_table,
      MaybeHandle<FixedArray> maybe_empty_function_tables,
      MaybeHandle<FixedArray> maybe_signature_tables);
391 392

  static Handle<WasmCompiledModule> Clone(Isolate* isolate,
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
                                          Handle<WasmCompiledModule> module);
  static void Reset(Isolate* isolate, WasmCompiledModule* module);

  Address GetEmbeddedMemStartOrNull() const {
    DisallowHeapAllocation no_gc;
    if (has_embedded_mem_start()) {
      return reinterpret_cast<Address>(embedded_mem_start());
    }
    return nullptr;
  }

  Address GetGlobalsStartOrNull() const {
    DisallowHeapAllocation no_gc;
    if (has_globals_start()) {
      return reinterpret_cast<Address>(globals_start());
    }
    return nullptr;
410 411 412 413 414
  }

  uint32_t mem_size() const;
  uint32_t default_mem_size() const;

415 416 417 418 419 420 421 422
  void ResetSpecializationMemInfoIfNeeded();
  static void SetSpecializationMemInfoFrom(
      Factory* factory, Handle<WasmCompiledModule> compiled_module,
      Handle<JSArrayBuffer> buffer);
  static void SetGlobalsStartAddressFrom(
      Factory* factory, Handle<WasmCompiledModule> compiled_module,
      Handle<JSArrayBuffer> buffer);

423 424 425 426
#define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME)
  WCM_PROPERTY_TABLE(DECLARATION)
#undef DECLARATION

427
 public:
428 429 430 431 432 433 434 435
// Allow to call method on WasmSharedModuleData also on this object.
#define FORWARD_SHARED(type, name) \
  type name() { return shared()->name(); }
  FORWARD_SHARED(SeqOneByteString*, module_bytes)
  FORWARD_SHARED(wasm::WasmModule*, module)
  FORWARD_SHARED(Script*, script)
  FORWARD_SHARED(bool, is_asm_js)
#undef FORWARD_SHARED
436

437
  static bool IsWasmCompiledModule(Object* obj);
438

439 440
  void PrintInstancesChain();

441 442
  static void ReinitializeAfterDeserialization(Isolate*,
                                               Handle<WasmCompiledModule>);
443

444
  // Get the function name of the function identified by the given index.
445 446
  // Returns a null handle if the function is unnamed or the name is not a valid
  // UTF-8 string.
447 448 449 450 451 452 453 454 455 456
  static MaybeHandle<String> GetFunctionNameOrNull(
      Isolate* isolate, Handle<WasmCompiledModule> compiled_module,
      uint32_t func_index);

  // Get the function name of the function identified by the given index.
  // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a
  // valid UTF-8 string.
  static Handle<String> GetFunctionName(
      Isolate* isolate, Handle<WasmCompiledModule> compiled_module,
      uint32_t func_index);
457

458 459 460 461 462 463
  // 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);

464 465 466
  // Return the byte offset of the function identified by the given index.
  // The offset will be relative to the start of the module bytes.
  // Returns -1 if the function index is invalid.
467
  int GetFunctionOffset(uint32_t func_index);
468

469 470 471
  // Returns the function containing the given byte offset.
  // Returns -1 if the byte offset is not contained in any function of this
  // module.
472
  int GetContainingFunction(uint32_t byte_offset);
473 474 475 476 477 478

  // Translate from byte offset in the module to function number and byte offset
  // within that function, encoded as line and column in the position info.
  // Returns true if the position is valid inside this module, false otherwise.
  bool GetPositionInfo(uint32_t position, Script::PositionInfo* info);

479 480
  // Get the asm.js source position from a byte offset.
  // Must only be called if the associated wasm object was created from asm.js.
481
  static int GetAsmJsSourcePosition(Handle<WasmCompiledModule> compiled_module,
482 483
                                    uint32_t func_index, uint32_t byte_offset,
                                    bool is_at_number_conversion);
484

485 486 487 488 489
  // Compute the disassembly of a wasm function.
  // Returns the disassembly string and a list of <byte_offset, line, column>
  // entries, mapping wasm byte offsets to line and column in the disassembly.
  // The list is guaranteed to be ordered by the byte_offset.
  // Returns an empty string and empty vector if the function index is invalid.
490
  debug::WasmDisassembly DisassembleFunction(int func_index);
491

492 493 494 495 496 497 498
  // Extract a portion of the wire bytes as UTF-8 string.
  // Returns a null handle if the respective bytes do not form a valid UTF-8
  // string.
  static MaybeHandle<String> ExtractUtf8StringFromModuleBytes(
      Isolate* isolate, Handle<WasmCompiledModule> compiled_module,
      uint32_t offset, uint32_t size);

499 500 501
  // Get a list of all possible breakpoints within a given range of this module.
  bool GetPossibleBreakpoints(const debug::Location& start,
                              const debug::Location& end,
502
                              std::vector<debug::BreakLocation>* locations);
503

504 505 506 507 508 509 510 511 512
  // 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.
  static bool SetBreakPoint(Handle<WasmCompiledModule>, int* position,
                            Handle<Object> break_point_object);

513 514 515 516
  // Return an empty handle if no breakpoint is hit at that location, or a
  // FixedArray with all hit breakpoint objects.
  MaybeHandle<FixedArray> CheckBreakPoints(int position);

517 518 519 520 521 522 523 524 525 526 527 528 529
  // Compile lazily the function called in the given caller code object at the
  // given offset.
  // If the called function cannot be determined from the caller (indirect
  // call / exported function), func_index must be set. Otherwise it can be -1.
  // If patch_caller is set, then all direct calls to functions which were
  // already lazily compiled are patched (at least the given call site).
  // Returns the Code to be called at the given call site, or an empty Handle if
  // an error occured during lazy compilation. In this case, an exception has
  // been set on the isolate.
  static MaybeHandle<Code> CompileLazy(Isolate*, Handle<WasmInstanceObject>,
                                       Handle<Code> caller, int offset,
                                       int func_index, bool patch_caller);

530 531 532 533
  void ReplaceCodeTableForTesting(Handle<FixedArray> testing_table) {
    set_code_table(testing_table);
  }

534 535 536 537 538 539 540
 private:
  void InitId();

  DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule);
};

class WasmDebugInfo : public FixedArray {
541
 public:
542
  // The 0-th field is used by the Blink Wrapper Tracer.
543
  enum Fields {
544
    kWrapperTracerHeader,
545 546 547 548 549 550
    kInstance,
    kInterpreterHandle,
    kInterpretedFunctions,
    kFieldCount
  };

551
  static Handle<WasmDebugInfo> New(Handle<WasmInstanceObject>);
552

553 554 555 556 557 558 559
  // Setup a WasmDebugInfo with an existing WasmInstance struct.
  // Returns a pointer to the interpreter instantiated inside this
  // WasmDebugInfo.
  // Use for testing only.
  static wasm::WasmInterpreter* SetupForTesting(Handle<WasmInstanceObject>,
                                                wasm::WasmInstance*);

560 561
  static bool IsDebugInfo(Object*);
  static WasmDebugInfo* cast(Object*);
562

563 564 565
  // Set a breakpoint in the given function at the given byte offset within that
  // function. This will redirect all future calls to this function to the
  // interpreter and will always pause at the given offset.
566 567
  static void SetBreakpoint(Handle<WasmDebugInfo>, int func_index, int offset);

568
  // Make a set of functions always execute in the interpreter without setting
569
  // breakpoints.
570 571
  static void RedirectToInterpreter(Handle<WasmDebugInfo>,
                                    Vector<int> func_indexes);
572

573 574
  void PrepareStep(StepAction);

575 576
  // Execute the specified funtion in the interpreter. Read arguments from
  // arg_buffer.
577 578
  // The frame_pointer will be used to identify the new activation of the
  // interpreter for unwinding and frame inspection.
579 580
  // Returns true if exited regularly, false if a trap occured. In the latter
  // case, a pending exception will have been set on the isolate.
581 582
  bool RunInterpreter(Address frame_pointer, int func_index,
                      uint8_t* arg_buffer);
583 584 585 586 587 588 589

  // Get the stack of the wasm interpreter as pairs of <function index, byte
  // offset>. The list is ordered bottom-to-top, i.e. caller before callee.
  std::vector<std::pair<uint32_t, int>> GetInterpretedStack(
      Address frame_pointer);

  std::unique_ptr<wasm::InterpretedFrame> GetInterpretedFrame(
590
      Address frame_pointer, int frame_index);
591

592 593 594 595
  // Unwind the interpreted stack belonging to the passed interpreter entry
  // frame.
  void Unwind(Address frame_pointer);

596 597 598
  // Returns the number of calls / function frames executed in the interpreter.
  uint64_t NumInterpretedCalls();

599
  DECLARE_GETTER(wasm_instance, WasmInstanceObject);
600 601 602 603

  // Update the memory view of the interpreter after executing GrowMemory in
  // compiled code.
  void UpdateMemory(JSArrayBuffer* new_memory);
604 605 606 607 608 609 610 611 612 613 614

  // Get scope details for a specific interpreted frame.
  // This returns a JSArray of length two: One entry for the global scope, one
  // for the local scope. Both elements are JSArrays of size
  // ScopeIterator::kScopeDetailsSize and layout as described in debug-scopes.h.
  // The global scope contains information about globals and the memory.
  // The local scope contains information about parameters, locals, and stack
  // values.
  static Handle<JSArray> GetScopeDetails(Handle<WasmDebugInfo>,
                                         Address frame_pointer,
                                         int frame_index);
615 616
};

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
class WasmInstanceWrapper : public FixedArray {
 public:
  static Handle<WasmInstanceWrapper> New(Isolate* isolate,
                                         Handle<WasmInstanceObject> instance);
  static WasmInstanceWrapper* cast(Object* fixed_array) {
    SLOW_DCHECK(IsWasmInstanceWrapper(fixed_array));
    return reinterpret_cast<WasmInstanceWrapper*>(fixed_array);
  }
  static bool IsWasmInstanceWrapper(Object* obj);
  bool has_instance() { return get(kWrapperInstanceObject)->IsWeakCell(); }
  Handle<WasmInstanceObject> instance_object() {
    Object* obj = get(kWrapperInstanceObject);
    DCHECK(obj->IsWeakCell());
    WeakCell* cell = WeakCell::cast(obj);
    DCHECK(cell->value()->IsJSObject());
    return handle(WasmInstanceObject::cast(cell->value()));
  }
  bool has_next() { return IsWasmInstanceWrapper(get(kNextInstanceWrapper)); }
  bool has_previous() {
    return IsWasmInstanceWrapper(get(kPreviousInstanceWrapper));
  }
  void set_next_wrapper(Object* obj) {
    DCHECK(IsWasmInstanceWrapper(obj));
    set(kNextInstanceWrapper, obj);
  }
  void set_previous_wrapper(Object* obj) {
    DCHECK(IsWasmInstanceWrapper(obj));
    set(kPreviousInstanceWrapper, obj);
  }
  Handle<WasmInstanceWrapper> next_wrapper() {
    Object* obj = get(kNextInstanceWrapper);
    DCHECK(IsWasmInstanceWrapper(obj));
    return handle(WasmInstanceWrapper::cast(obj));
  }
  Handle<WasmInstanceWrapper> previous_wrapper() {
    Object* obj = get(kPreviousInstanceWrapper);
    DCHECK(IsWasmInstanceWrapper(obj));
    return handle(WasmInstanceWrapper::cast(obj));
  }
  void reset_next_wrapper() { set_undefined(kNextInstanceWrapper); }
  void reset_previous_wrapper() { set_undefined(kPreviousInstanceWrapper); }
  void reset() {
    for (int kID = 0; kID < kWrapperPropertyCount; kID++) set_undefined(kID);
  }

 private:
  enum {
    kWrapperInstanceObject,
    kNextInstanceWrapper,
    kPreviousInstanceWrapper,
    kWrapperPropertyCount
  };
};

671 672
#undef DECLARE_CASTS
#undef DECLARE_GETTER
673 674
#undef DECLARE_ACCESSORS
#undef DECLARE_OPTIONAL_ACCESSORS
675
#undef DECLARE_OPTIONAL_GETTER
676 677 678 679 680

}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_OBJECTS_H_