OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef V8_WASM_OBJECTS_H_ |
| 6 #define V8_WASM_OBJECTS_H_ |
| 7 |
| 8 #include "src/objects-inl.h" |
| 9 #include "src/wasm/managed.h" |
| 10 |
| 11 #define DECLARE_CASTS(name) \ |
| 12 static bool Is##name(Object* object); \ |
| 13 static name* cast(Object* object) |
| 14 |
| 15 namespace v8 { |
| 16 namespace internal { |
| 17 namespace wasm { |
| 18 struct WasmModule; |
| 19 } |
| 20 |
| 21 class WasmCompiledModule; |
| 22 class WasmDebugInfo; |
| 23 class WasmInstanceObject; |
| 24 |
| 25 // Representation of a WebAssembly.Module JavaScript-level object. |
| 26 class WasmModuleObject : public JSObject { |
| 27 public: |
| 28 // TODO(titzer): add the brand as an internal field instead of a property. |
| 29 enum Fields { kCompiledModule, kFieldCount }; |
| 30 |
| 31 DECLARE_CASTS(WasmModuleObject); |
| 32 |
| 33 WasmCompiledModule* compiled_module(); |
| 34 wasm::WasmModule* module(); |
| 35 int num_functions(); |
| 36 bool is_asm_js(); |
| 37 int GetAsmWasmSourcePosition(int func_index, int byte_offset); |
| 38 WasmDebugInfo* debug_info(); |
| 39 void set_debug_info(WasmDebugInfo* debug_info); |
| 40 MaybeHandle<String> GetFunctionName(Isolate* isolate, int func_index); |
| 41 |
| 42 static Handle<WasmModuleObject> New( |
| 43 Isolate* isolate, Handle<WasmCompiledModule> compiled_module); |
| 44 }; |
| 45 |
| 46 // Representation of a WebAssembly.Table JavaScript-level object. |
| 47 class WasmTableObject : public JSObject { |
| 48 public: |
| 49 // TODO(titzer): add the brand as an internal field instead of a property. |
| 50 enum Fields { kArray, kMaximum, kDispatchTables, kFieldCount }; |
| 51 |
| 52 DECLARE_CASTS(WasmTableObject); |
| 53 |
| 54 uint32_t current_length(); |
| 55 uint32_t maximum_length(); |
| 56 |
| 57 static Handle<WasmTableObject> New(Isolate* isolate, uint32_t initial, |
| 58 bool has_maximum, uint32_t maximum, |
| 59 Handle<FixedArray>* js_functions); |
| 60 static bool Grow(Handle<WasmTableObject> table, uint32_t count); |
| 61 static Handle<FixedArray> AddDispatchTable( |
| 62 Isolate* isolate, Handle<WasmTableObject> table, |
| 63 Handle<WasmInstanceObject> instance, int table_index, |
| 64 Handle<FixedArray> dispatch_table); |
| 65 }; |
| 66 |
| 67 // Representation of a WebAssembly.Memory JavaScript-level object. |
| 68 class WasmMemoryObject : public JSObject { |
| 69 public: |
| 70 // TODO(titzer): add the brand as an internal field instead of a property. |
| 71 enum Fields : uint8_t { kArrayBuffer, kMaximum, kInstance, kFieldCount }; |
| 72 |
| 73 DECLARE_CASTS(WasmMemoryObject); |
| 74 |
| 75 void AddInstance(WasmInstanceObject* object); |
| 76 JSArrayBuffer* get_buffer(); |
| 77 void set_buffer(JSArrayBuffer* buffer); |
| 78 uint32_t current_pages(); |
| 79 int32_t maximum_pages(); // returns < 0 if there is no maximum |
| 80 |
| 81 static Handle<WasmMemoryObject> New(Isolate* isolate, |
| 82 Handle<JSArrayBuffer> buffer, |
| 83 int maximum); |
| 84 |
| 85 static bool Grow(Handle<WasmMemoryObject> memory, uint32_t count); |
| 86 }; |
| 87 |
| 88 // Representation of a WebAssembly.Instance JavaScript-level object. |
| 89 class WasmInstanceObject : public JSObject { |
| 90 public: |
| 91 // TODO(titzer): add the brand as an internal field instead of a property. |
| 92 enum Fields { |
| 93 kCompiledModule, |
| 94 kMemoryObject, |
| 95 kMemoryArrayBuffer, |
| 96 kGlobalsArrayBuffer, |
| 97 kDebugInfo, |
| 98 kFieldCount |
| 99 }; |
| 100 |
| 101 DECLARE_CASTS(WasmInstanceObject); |
| 102 |
| 103 WasmCompiledModule* compiled_module(); |
| 104 WasmModuleObject* module_object(); |
| 105 wasm::WasmModule* module(); |
| 106 uint64_t mem_size(); |
| 107 Script* script(); |
| 108 |
| 109 static Handle<WasmInstanceObject> New(Isolate* isolate); |
| 110 }; |
| 111 |
| 112 // Representation of an exported WASM function. |
| 113 class WasmExportedFunction : public JSFunction { |
| 114 public: |
| 115 enum Fields { kInstance, kIndex, kFieldCount }; |
| 116 |
| 117 DECLARE_CASTS(WasmExportedFunction); |
| 118 |
| 119 WasmInstanceObject* instance(); |
| 120 int function_index(); |
| 121 |
| 122 static Handle<WasmExportedFunction> New(Isolate* isolate, |
| 123 Handle<WasmInstanceObject> instance, |
| 124 Handle<String> name, |
| 125 Handle<Code> export_wrapper, |
| 126 int arity, int func_index); |
| 127 }; |
| 128 |
| 129 class WasmCompiledModule : public FixedArray { |
| 130 public: |
| 131 enum Fields { kFieldCount }; |
| 132 |
| 133 static WasmCompiledModule* cast(Object* fixed_array) { |
| 134 SLOW_DCHECK(IsWasmCompiledModule(fixed_array)); |
| 135 return reinterpret_cast<WasmCompiledModule*>(fixed_array); |
| 136 } |
| 137 |
| 138 #define WCM_OBJECT_OR_WEAK(TYPE, NAME, ID) \ |
| 139 Handle<TYPE> NAME() const { return handle(ptr_to_##NAME()); } \ |
| 140 \ |
| 141 MaybeHandle<TYPE> maybe_##NAME() const { \ |
| 142 if (has_##NAME()) return NAME(); \ |
| 143 return MaybeHandle<TYPE>(); \ |
| 144 } \ |
| 145 \ |
| 146 TYPE* ptr_to_##NAME() const { \ |
| 147 Object* obj = get(ID); \ |
| 148 if (!obj->Is##TYPE()) return nullptr; \ |
| 149 return TYPE::cast(obj); \ |
| 150 } \ |
| 151 \ |
| 152 void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); } \ |
| 153 \ |
| 154 void set_ptr_to_##NAME(TYPE* value) { set(ID, value); } \ |
| 155 \ |
| 156 bool has_##NAME() const { return get(ID)->Is##TYPE(); } \ |
| 157 \ |
| 158 void reset_##NAME() { set_undefined(ID); } |
| 159 |
| 160 #define WCM_OBJECT(TYPE, NAME) WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME) |
| 161 |
| 162 #define WCM_SMALL_NUMBER(TYPE, NAME) \ |
| 163 TYPE NAME() const { \ |
| 164 return static_cast<TYPE>(Smi::cast(get(kID_##NAME))->value()); \ |
| 165 } \ |
| 166 void set_##NAME(TYPE value) { set(kID_##NAME, Smi::FromInt(value)); } |
| 167 |
| 168 #define WCM_WEAK_LINK(TYPE, NAME) \ |
| 169 WCM_OBJECT_OR_WEAK(WeakCell, weak_##NAME, kID_##NAME); \ |
| 170 \ |
| 171 Handle<TYPE> NAME() const { \ |
| 172 return handle(TYPE::cast(weak_##NAME()->value())); \ |
| 173 } |
| 174 |
| 175 #define CORE_WCM_PROPERTY_TABLE(MACRO) \ |
| 176 MACRO(OBJECT, FixedArray, code_table) \ |
| 177 MACRO(OBJECT, Foreign, module_wrapper) \ |
| 178 MACRO(OBJECT, SeqOneByteString, module_bytes) \ |
| 179 MACRO(OBJECT, Script, asm_js_script) \ |
| 180 MACRO(OBJECT, FixedArray, function_tables) \ |
| 181 MACRO(OBJECT, FixedArray, empty_function_tables) \ |
| 182 MACRO(OBJECT, ByteArray, asm_js_offset_tables) \ |
| 183 MACRO(OBJECT, JSArrayBuffer, memory) \ |
| 184 MACRO(SMALL_NUMBER, uint32_t, min_mem_pages) \ |
| 185 MACRO(SMALL_NUMBER, uint32_t, max_mem_pages) \ |
| 186 MACRO(WEAK_LINK, WasmCompiledModule, next_instance) \ |
| 187 MACRO(WEAK_LINK, WasmCompiledModule, prev_instance) \ |
| 188 MACRO(WEAK_LINK, JSObject, owning_instance) \ |
| 189 MACRO(WEAK_LINK, JSObject, wasm_module) |
| 190 |
| 191 #if DEBUG |
| 192 #define DEBUG_ONLY_TABLE(MACRO) MACRO(SMALL_NUMBER, uint32_t, instance_id) |
| 193 #else |
| 194 #define DEBUG_ONLY_TABLE(IGNORE) |
| 195 uint32_t instance_id() const { return -1; } |
| 196 #endif |
| 197 |
| 198 #define WCM_PROPERTY_TABLE(MACRO) \ |
| 199 CORE_WCM_PROPERTY_TABLE(MACRO) \ |
| 200 DEBUG_ONLY_TABLE(MACRO) |
| 201 |
| 202 private: |
| 203 enum PropertyIndices { |
| 204 #define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME, |
| 205 WCM_PROPERTY_TABLE(INDICES) Count |
| 206 #undef INDICES |
| 207 }; |
| 208 |
| 209 public: |
| 210 static Handle<WasmCompiledModule> New( |
| 211 Isolate* isolate, Handle<Managed<wasm::WasmModule>> module_wrapper); |
| 212 |
| 213 static Handle<WasmCompiledModule> Clone(Isolate* isolate, |
| 214 Handle<WasmCompiledModule> module) { |
| 215 Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast( |
| 216 isolate->factory()->CopyFixedArray(module)); |
| 217 ret->InitId(); |
| 218 ret->reset_weak_owning_instance(); |
| 219 ret->reset_weak_next_instance(); |
| 220 ret->reset_weak_prev_instance(); |
| 221 return ret; |
| 222 } |
| 223 |
| 224 uint32_t mem_size() const; |
| 225 uint32_t default_mem_size() const; |
| 226 |
| 227 wasm::WasmModule* module() const; |
| 228 |
| 229 #define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME) |
| 230 WCM_PROPERTY_TABLE(DECLARATION) |
| 231 #undef DECLARATION |
| 232 |
| 233 static bool IsWasmCompiledModule(Object* obj); |
| 234 |
| 235 void PrintInstancesChain(); |
| 236 |
| 237 static void RecreateModuleWrapper(Isolate* isolate, |
| 238 Handle<FixedArray> compiled_module); |
| 239 |
| 240 private: |
| 241 void InitId(); |
| 242 |
| 243 DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); |
| 244 }; |
| 245 |
| 246 class WasmDebugInfo : public FixedArray { |
| 247 public: |
| 248 enum class Fields { kFieldCount }; |
| 249 |
| 250 static Handle<WasmDebugInfo> New(Handle<JSObject> wasm); |
| 251 |
| 252 static bool IsDebugInfo(Object* object); |
| 253 static WasmDebugInfo* cast(Object* object); |
| 254 |
| 255 JSObject* wasm_instance(); |
| 256 |
| 257 bool SetBreakPoint(int byte_offset); |
| 258 |
| 259 // Get the Script for the specified function. |
| 260 static Script* GetFunctionScript(Handle<WasmDebugInfo> debug_info, |
| 261 int func_index); |
| 262 |
| 263 // Disassemble the specified function from this module. |
| 264 static Handle<String> DisassembleFunction(Handle<WasmDebugInfo> debug_info, |
| 265 int func_index); |
| 266 |
| 267 // Get the offset table for the specified function, mapping from byte offsets |
| 268 // to position in the disassembly. |
| 269 // Returns an array with three entries per instruction: byte offset, line and |
| 270 // column. |
| 271 static Handle<FixedArray> GetFunctionOffsetTable( |
| 272 Handle<WasmDebugInfo> debug_info, int func_index); |
| 273 |
| 274 // Get the asm.js source position from a byte offset. |
| 275 // Must only be called if the associated wasm object was created from asm.js. |
| 276 static int GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info, |
| 277 int func_index, int byte_offset); |
| 278 }; |
| 279 |
| 280 } // namespace internal |
| 281 } // namespace v8 |
| 282 |
| 283 #endif // V8_WASM_OBJECTS_H_ |
OLD | NEW |