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 #include "src/wasm/wasm-objects.h" | 5 #include "src/wasm/wasm-objects.h" |
6 #include "src/wasm/wasm-module.h" | 6 #include "src/wasm/wasm-module.h" |
7 | 7 |
8 #define TRACE(...) \ | 8 #define TRACE(...) \ |
9 do { \ | 9 do { \ |
10 if (FLAG_trace_wasm_instances) PrintF(__VA_ARGS__); \ | 10 if (FLAG_trace_wasm_instances) PrintF(__VA_ARGS__); \ |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 Handle<JSObject> memory_obj = isolate->factory()->NewJSObject(memory_ctor); | 177 Handle<JSObject> memory_obj = isolate->factory()->NewJSObject(memory_ctor); |
178 memory_obj->SetInternalField(kArrayBuffer, *buffer); | 178 memory_obj->SetInternalField(kArrayBuffer, *buffer); |
179 memory_obj->SetInternalField(kMaximum, | 179 memory_obj->SetInternalField(kMaximum, |
180 static_cast<Object*>(Smi::FromInt(maximum))); | 180 static_cast<Object*>(Smi::FromInt(maximum))); |
181 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym()); | 181 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym()); |
182 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check(); | 182 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check(); |
183 return Handle<WasmMemoryObject>::cast(memory_obj); | 183 return Handle<WasmMemoryObject>::cast(memory_obj); |
184 } | 184 } |
185 | 185 |
186 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer) | 186 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer) |
187 DEFINE_OPTIONAL_ACCESSORS(WasmMemoryObject, instances_link, kInstancesLink, | |
188 WasmInstanceWrapper) | |
189 | 187 |
190 uint32_t WasmMemoryObject::current_pages() { | 188 uint32_t WasmMemoryObject::current_pages() { |
191 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize; | 189 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize; |
192 } | 190 } |
193 | 191 |
194 int32_t WasmMemoryObject::maximum_pages() { | 192 int32_t WasmMemoryObject::maximum_pages() { |
195 return SafeInt32(GetInternalField(kMaximum)); | 193 return SafeInt32(GetInternalField(kMaximum)); |
196 } | 194 } |
197 | 195 |
198 WasmMemoryObject* WasmMemoryObject::cast(Object* object) { | 196 WasmMemoryObject* WasmMemoryObject::cast(Object* object) { |
199 DCHECK(object && object->IsJSObject()); | 197 DCHECK(object && object->IsJSObject()); |
200 // TODO(titzer): brand check for WasmMemoryObject. | 198 // TODO(titzer): brand check for WasmMemoryObject. |
201 return reinterpret_cast<WasmMemoryObject*>(object); | 199 return reinterpret_cast<WasmMemoryObject*>(object); |
202 } | 200 } |
203 | 201 |
204 void WasmMemoryObject::AddInstance(Isolate* isolate, | 202 void WasmMemoryObject::AddInstance(WasmInstanceObject* instance) { |
205 Handle<WasmInstanceObject> instance) { | 203 // TODO(gdeepti): This should be a weak list of instance objects |
206 Handle<WasmInstanceWrapper> instance_wrapper; | 204 // for instances that share memory. |
207 if (has_instances_link()) { | 205 SetInternalField(kInstance, instance); |
208 Handle<WasmInstanceWrapper> current_wrapper(get_instances_link()); | |
209 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*current_wrapper)); | |
210 DCHECK(!current_wrapper->has_previous()); | |
211 instance_wrapper = WasmInstanceWrapper::New(isolate, instance); | |
212 instance_wrapper->set_next_wrapper(*current_wrapper); | |
213 current_wrapper->set_previous_wrapper(*instance_wrapper); | |
214 } else { | |
215 instance_wrapper = WasmInstanceWrapper::New(isolate, instance); | |
216 } | |
217 set_instances_link(*instance_wrapper); | |
218 instance->set_instance_wrapper(*instance_wrapper); | |
219 } | |
220 | |
221 void WasmMemoryObject::ResetInstancesLink(Isolate* isolate) { | |
222 Handle<Object> undefined = isolate->factory()->undefined_value(); | |
223 SetInternalField(kInstancesLink, *undefined); | |
224 } | 206 } |
225 | 207 |
226 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule, | 208 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule, |
227 WasmCompiledModule) | 209 WasmCompiledModule) |
228 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, | 210 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, |
229 kGlobalsArrayBuffer, JSArrayBuffer) | 211 kGlobalsArrayBuffer, JSArrayBuffer) |
230 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer, | 212 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer, |
231 JSArrayBuffer) | 213 JSArrayBuffer) |
232 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject, | 214 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject, |
233 WasmMemoryObject) | 215 WasmMemoryObject) |
234 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo, | 216 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo, |
235 WasmDebugInfo) | 217 WasmDebugInfo) |
236 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, instance_wrapper, | |
237 kWasmMemInstanceWrapper, WasmInstanceWrapper) | |
238 | 218 |
239 WasmModuleObject* WasmInstanceObject::module_object() { | 219 WasmModuleObject* WasmInstanceObject::module_object() { |
240 return WasmModuleObject::cast(*get_compiled_module()->wasm_module()); | 220 return WasmModuleObject::cast(*get_compiled_module()->wasm_module()); |
241 } | 221 } |
242 | 222 |
243 WasmModule* WasmInstanceObject::module() { | 223 WasmModule* WasmInstanceObject::module() { |
244 return reinterpret_cast<WasmModuleWrapper*>( | 224 return reinterpret_cast<WasmModuleWrapper*>( |
245 *get_compiled_module()->module_wrapper()) | 225 *get_compiled_module()->module_wrapper()) |
246 ->get(); | 226 ->get(); |
247 } | 227 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 function.name_length); | 378 function.name_length); |
399 } | 379 } |
400 | 380 |
401 int WasmCompiledModule::GetFunctionOffset(uint32_t func_index) const { | 381 int WasmCompiledModule::GetFunctionOffset(uint32_t func_index) const { |
402 std::vector<WasmFunction>& functions = module()->functions; | 382 std::vector<WasmFunction>& functions = module()->functions; |
403 if (static_cast<uint32_t>(func_index) >= functions.size()) return -1; | 383 if (static_cast<uint32_t>(func_index) >= functions.size()) return -1; |
404 DCHECK_GE(static_cast<uint32_t>(kMaxInt), | 384 DCHECK_GE(static_cast<uint32_t>(kMaxInt), |
405 functions[func_index].code_start_offset); | 385 functions[func_index].code_start_offset); |
406 return static_cast<int>(functions[func_index].code_start_offset); | 386 return static_cast<int>(functions[func_index].code_start_offset); |
407 } | 387 } |
408 | |
409 Handle<WasmInstanceWrapper> WasmInstanceWrapper::New( | |
410 Isolate* isolate, Handle<WasmInstanceObject> instance) { | |
411 Handle<FixedArray> array = | |
412 isolate->factory()->NewFixedArray(kWrapperPropertyCount, TENURED); | |
413 Handle<WasmInstanceWrapper> instance_wrapper( | |
414 reinterpret_cast<WasmInstanceWrapper*>(*array), isolate); | |
415 instance_wrapper->set_instance_object(instance, isolate); | |
416 return instance_wrapper; | |
417 } | |
418 | |
419 bool WasmInstanceWrapper::IsWasmInstanceWrapper(Object* obj) { | |
420 if (!obj->IsFixedArray()) return false; | |
421 FixedArray* array = FixedArray::cast(obj); | |
422 if (array->length() != kWrapperPropertyCount) return false; | |
423 if (!array->get(kWrapperInstanceObject)->IsWeakCell()) return false; | |
424 Isolate* isolate = array->GetIsolate(); | |
425 if (!array->get(kNextInstanceWrapper)->IsUndefined(isolate) && | |
426 !array->get(kNextInstanceWrapper)->IsFixedArray()) | |
427 return false; | |
428 if (!array->get(kPreviousInstanceWrapper)->IsUndefined(isolate) && | |
429 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) | |
430 return false; | |
431 return true; | |
432 } | |
433 | |
434 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, | |
435 Isolate* isolate) { | |
436 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); | |
437 set(kWrapperInstanceObject, *cell); | |
438 } | |
OLD | NEW |