| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_WASM_MODULE_H_ | 5 #ifndef V8_WASM_MODULE_H_ |
| 6 #define V8_WASM_MODULE_H_ | 6 #define V8_WASM_MODULE_H_ |
| 7 | 7 |
| 8 #include "src/wasm/wasm-opcodes.h" | 8 #include "src/wasm/wasm-opcodes.h" |
| 9 #include "src/wasm/wasm-result.h" | 9 #include "src/wasm/wasm-result.h" |
| 10 | 10 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 const byte* module_start; // starting address for the module bytes. | 162 const byte* module_start; // starting address for the module bytes. |
| 163 const byte* module_end; // end address for the module bytes. | 163 const byte* module_end; // end address for the module bytes. |
| 164 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 164 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
| 165 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. | 165 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. |
| 166 bool mem_export; // true if the memory is exported. | 166 bool mem_export; // true if the memory is exported. |
| 167 bool mem_external; // true if the memory is external. | 167 bool mem_external; // true if the memory is external. |
| 168 int start_function_index; // start function, if any. | 168 int start_function_index; // start function, if any. |
| 169 ModuleOrigin origin; // origin of the module | 169 ModuleOrigin origin; // origin of the module |
| 170 | 170 |
| 171 std::vector<WasmGlobal> globals; // globals in this module. | 171 std::vector<WasmGlobal> globals; // globals in this module. |
| 172 uint32_t globals_size; // size of globals table. |
| 172 std::vector<FunctionSig*> signatures; // signatures in this module. | 173 std::vector<FunctionSig*> signatures; // signatures in this module. |
| 173 std::vector<WasmFunction> functions; // functions in this module. | 174 std::vector<WasmFunction> functions; // functions in this module. |
| 174 std::vector<WasmDataSegment> data_segments; // data segments in this module. | 175 std::vector<WasmDataSegment> data_segments; // data segments in this module. |
| 175 std::vector<uint16_t> function_table; // function table. | 176 std::vector<uint16_t> function_table; // function table. |
| 176 std::vector<WasmImport> import_table; // import table. | 177 std::vector<WasmImport> import_table; // import table. |
| 177 std::vector<WasmExport> export_table; // export table. | 178 std::vector<WasmExport> export_table; // export table. |
| 178 | 179 |
| 179 WasmModule(); | 180 WasmModule(); |
| 180 | 181 |
| 181 // Get a string stored in the module bytes representing a name. | 182 // Get a string stored in the module bytes representing a name. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 195 // Get a string stored in the module bytes representing a name. | 196 // Get a string stored in the module bytes representing a name. |
| 196 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { | 197 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const { |
| 197 if (offset == 0 && length == 0) return {NULL, 0}; // no name. | 198 if (offset == 0 && length == 0) return {NULL, 0}; // no name. |
| 198 CHECK(BoundsCheck(offset, offset + length)); | 199 CHECK(BoundsCheck(offset, offset + length)); |
| 199 DCHECK_GE(static_cast<int>(length), 0); | 200 DCHECK_GE(static_cast<int>(length), 0); |
| 200 return {reinterpret_cast<const char*>(module_start + offset), | 201 return {reinterpret_cast<const char*>(module_start + offset), |
| 201 static_cast<int>(length)}; | 202 static_cast<int>(length)}; |
| 202 } | 203 } |
| 203 | 204 |
| 204 // Get a string stored in the module bytes representing a function name. | 205 // Get a string stored in the module bytes representing a function name. |
| 205 WasmName GetNameOrNull(WasmFunction* function) const { | 206 WasmName GetNameOrNull(const WasmFunction* function) const { |
| 206 return GetNameOrNull(function->name_offset, function->name_length); | 207 return GetNameOrNull(function->name_offset, function->name_length); |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Checks the given offset range is contained within the module bytes. | 210 // Checks the given offset range is contained within the module bytes. |
| 210 bool BoundsCheck(uint32_t start, uint32_t end) const { | 211 bool BoundsCheck(uint32_t start, uint32_t end) const { |
| 211 size_t size = module_end - module_start; | 212 size_t size = module_end - module_start; |
| 212 return start <= size && end <= size; | 213 return start <= size && end <= size; |
| 213 } | 214 } |
| 214 | 215 |
| 215 // Creates a new instantiation of the module in the given isolate. | 216 // Creates a new instantiation of the module in the given isolate. |
| 216 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi, | 217 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi, |
| 217 Handle<JSArrayBuffer> memory); | 218 Handle<JSArrayBuffer> memory) const; |
| 218 }; | 219 }; |
| 219 | 220 |
| 220 // An instantiated WASM module, including memory, function table, etc. | 221 // An instantiated WASM module, including memory, function table, etc. |
| 221 struct WasmModuleInstance { | 222 struct WasmModuleInstance { |
| 222 WasmModule* module; // static representation of the module. | 223 const WasmModule* module; // static representation of the module. |
| 223 // -- Heap allocated -------------------------------------------------------- | 224 // -- Heap allocated -------------------------------------------------------- |
| 224 Handle<JSObject> js_object; // JavaScript module object. | 225 Handle<JSObject> js_object; // JavaScript module object. |
| 225 Handle<Context> context; // JavaScript native context. | 226 Handle<Context> context; // JavaScript native context. |
| 226 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. | 227 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
| 227 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. | 228 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
| 228 Handle<FixedArray> function_table; // indirect function table. | 229 Handle<FixedArray> function_table; // indirect function table. |
| 229 std::vector<Handle<Code>> function_code; // code objects for each function. | 230 std::vector<Handle<Code>> function_code; // code objects for each function. |
| 230 std::vector<Handle<Code>> import_code; // code objects for each import. | 231 std::vector<Handle<Code>> import_code; // code objects for each import. |
| 231 // -- raw memory ------------------------------------------------------------ | 232 // -- raw memory ------------------------------------------------------------ |
| 232 byte* mem_start; // start of linear memory. | 233 byte* mem_start; // start of linear memory. |
| 233 size_t mem_size; // size of the linear memory. | 234 size_t mem_size; // size of the linear memory. |
| 234 // -- raw globals ----------------------------------------------------------- | 235 // -- raw globals ----------------------------------------------------------- |
| 235 byte* globals_start; // start of the globals area. | 236 byte* globals_start; // start of the globals area. |
| 236 size_t globals_size; // size of the globals area. | |
| 237 | 237 |
| 238 explicit WasmModuleInstance(WasmModule* m) | 238 explicit WasmModuleInstance(const WasmModule* m) |
| 239 : module(m), | 239 : module(m), mem_start(nullptr), mem_size(0), globals_start(nullptr) {} |
| 240 mem_start(nullptr), | |
| 241 mem_size(0), | |
| 242 globals_start(nullptr), | |
| 243 globals_size(0) {} | |
| 244 }; | 240 }; |
| 245 | 241 |
| 246 // forward declaration. | 242 // forward declaration. |
| 247 class WasmLinker; | 243 class WasmLinker; |
| 248 | 244 |
| 249 // Interface provided to the decoder/graph builder which contains only | 245 // Interface provided to the decoder/graph builder which contains only |
| 250 // minimal information about the globals, functions, and function tables. | 246 // minimal information about the globals, functions, and function tables. |
| 251 struct ModuleEnv { | 247 struct ModuleEnv { |
| 252 WasmModule* module; | 248 const WasmModule* module; |
| 253 WasmModuleInstance* instance; | 249 WasmModuleInstance* instance; |
| 254 WasmLinker* linker; | 250 WasmLinker* linker; |
| 255 ModuleOrigin origin; | 251 ModuleOrigin origin; |
| 256 | 252 |
| 257 bool IsValidGlobal(uint32_t index) { | 253 bool IsValidGlobal(uint32_t index) { |
| 258 return module && index < module->globals.size(); | 254 return module && index < module->globals.size(); |
| 259 } | 255 } |
| 260 bool IsValidFunction(uint32_t index) { | 256 bool IsValidFunction(uint32_t index) { |
| 261 return module && index < module->functions.size(); | 257 return module && index < module->functions.size(); |
| 262 } | 258 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 const WasmFunction* function_; | 300 const WasmFunction* function_; |
| 305 const WasmModule* module_; | 301 const WasmModule* module_; |
| 306 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) | 302 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) |
| 307 : function_(function), module_(menv ? menv->module : nullptr) {} | 303 : function_(function), module_(menv ? menv->module : nullptr) {} |
| 308 }; | 304 }; |
| 309 | 305 |
| 310 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 306 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 311 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 307 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
| 312 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 308 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
| 313 | 309 |
| 314 typedef Result<WasmModule*> ModuleResult; | 310 typedef Result<const WasmModule*> ModuleResult; |
| 315 typedef Result<WasmFunction*> FunctionResult; | 311 typedef Result<WasmFunction*> FunctionResult; |
| 316 | 312 |
| 317 // For testing. Decode, verify, and run the last exported function in the | 313 // For testing. Decode, verify, and run the last exported function in the |
| 318 // given encoded module. | 314 // given encoded module. |
| 319 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, | 315 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 320 const byte* module_end, bool asm_js = false); | 316 const byte* module_end, bool asm_js = false); |
| 321 | 317 |
| 322 // For testing. Decode, verify, and run the last exported function in the | 318 // For testing. Decode, verify, and run the last exported function in the |
| 323 // given decoded module. | 319 // given decoded module. |
| 324 int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); | 320 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module); |
| 325 | 321 |
| 326 // Extract a function name from the given wasm object. | 322 // Extract a function name from the given wasm object. |
| 327 // Returns a null handle if the function is unnamed or the name is not a valid | 323 // Returns a null handle if the function is unnamed or the name is not a valid |
| 328 // UTF-8 string. | 324 // UTF-8 string. |
| 329 MaybeHandle<String> GetWasmFunctionName(Handle<JSObject> wasm, | 325 MaybeHandle<String> GetWasmFunctionName(Handle<JSObject> wasm, |
| 330 uint32_t func_index); | 326 uint32_t func_index); |
| 331 | 327 |
| 332 } // namespace wasm | 328 } // namespace wasm |
| 333 } // namespace internal | 329 } // namespace internal |
| 334 } // namespace v8 | 330 } // namespace v8 |
| 335 | 331 |
| 336 #endif // V8_WASM_MODULE_H_ | 332 #endif // V8_WASM_MODULE_H_ |
| OLD | NEW |