// 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_ #include "src/debug/debug.h" #include "src/debug/interface-types.h" #include "src/objects.h" #include "src/trap-handler/trap-handler.h" #include "src/wasm/wasm-limits.h" namespace v8 { namespace internal { namespace wasm { class InterpretedFrame; struct WasmModule; } class WasmCompiledModule; class WasmDebugInfo; class WasmInstanceObject; class WasmInstanceWrapper; #define DECLARE_CASTS(name) \ static bool Is##name(Object* object); \ static name* cast(Object* object) #define DECLARE_GETTER(name, type) type* name() #define DECLARE_ACCESSORS(name, type) \ void set_##name(type* value); \ DECLARE_GETTER(name, type) #define DECLARE_OPTIONAL_ACCESSORS(name, type) \ bool has_##name(); \ DECLARE_ACCESSORS(name, type) #define DECLARE_OPTIONAL_GETTER(name, type) \ bool has_##name(); \ DECLARE_GETTER(name, type) // Representation of a WebAssembly.Module JavaScript-level object. class WasmModuleObject : public JSObject { public: // TODO(titzer): add the brand as an internal field instead of a property. enum Fields { kCompiledModule, kFieldCount }; DECLARE_CASTS(WasmModuleObject); WasmCompiledModule* compiled_module(); static Handle New( Isolate* isolate, Handle compiled_module); }; // Representation of a WebAssembly.Table JavaScript-level object. class WasmTableObject : public JSObject { public: // TODO(titzer): add the brand as an internal field instead of a property. enum Fields { kFunctions, kMaximum, kDispatchTables, kFieldCount }; DECLARE_CASTS(WasmTableObject); DECLARE_ACCESSORS(functions, FixedArray); FixedArray* dispatch_tables(); uint32_t current_length(); bool has_maximum_length(); int64_t maximum_length(); // Returns < 0 if no maximum. static Handle New(Isolate* isolate, uint32_t initial, int64_t maximum, Handle* js_functions); static void Grow(Isolate* isolate, Handle table, uint32_t count); static Handle AddDispatchTable( Isolate* isolate, Handle table, Handle instance, int table_index, Handle function_table, Handle signature_table); }; // Representation of a WebAssembly.Memory JavaScript-level object. class WasmMemoryObject : public JSObject { public: // TODO(titzer): add the brand as an internal field instead of a property. enum Fields : uint8_t { kArrayBuffer, kMaximum, kInstancesLink, kFieldCount }; DECLARE_CASTS(WasmMemoryObject); DECLARE_ACCESSORS(buffer, JSArrayBuffer); DECLARE_OPTIONAL_ACCESSORS(instances_link, WasmInstanceWrapper); void AddInstance(Isolate* isolate, Handle object); void ResetInstancesLink(Isolate* isolate); uint32_t current_pages(); bool has_maximum_pages(); int32_t maximum_pages(); // Returns < 0 if there is no maximum. static Handle New(Isolate* isolate, Handle buffer, int32_t maximum); static bool Grow(Isolate* isolate, Handle memory, uint32_t count); }; // Representation of a WebAssembly.Instance JavaScript-level object. class WasmInstanceObject : public JSObject { public: // TODO(titzer): add the brand as an internal field instead of a property. enum Fields { kCompiledModule, kMemoryObject, kMemoryArrayBuffer, kGlobalsArrayBuffer, kDebugInfo, kWasmMemInstanceWrapper, 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); DECLARE_OPTIONAL_ACCESSORS(instance_wrapper, WasmInstanceWrapper); WasmModuleObject* module_object(); wasm::WasmModule* module(); // Get the debug info associated with the given wasm object. // If no debug info exists yet, it is created automatically. static Handle GetOrCreateDebugInfo( Handle instance); static Handle New( Isolate* isolate, Handle compiled_module); }; // Representation of an exported WASM function. class WasmExportedFunction : public JSFunction { public: enum Fields { kInstance, kIndex, kFieldCount }; DECLARE_CASTS(WasmExportedFunction); WasmInstanceObject* instance(); int function_index(); static Handle New(Isolate* isolate, Handle instance, MaybeHandle maybe_name, int func_index, int arity, Handle export_wrapper); }; // Information shared by all WasmCompiledModule objects for the same module. class WasmSharedModuleData : public FixedArray { enum Fields { kModuleWrapper, kModuleBytes, kScript, kAsmJsOffsetTable, kBreakPointInfos, 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); DECLARE_OPTIONAL_GETTER(breakpoint_infos, FixedArray); static Handle New( Isolate* isolate, Handle module_wrapper, Handle module_bytes, Handle