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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 DCHECK(object && object->IsJSObject()); | 167 DCHECK(object && object->IsJSObject()); |
168 // TODO(titzer): brand check for WasmTableObject. | 168 // TODO(titzer): brand check for WasmTableObject. |
169 return reinterpret_cast<WasmTableObject*>(object); | 169 return reinterpret_cast<WasmTableObject*>(object); |
170 } | 170 } |
171 | 171 |
172 Handle<WasmMemoryObject> WasmMemoryObject::New(Isolate* isolate, | 172 Handle<WasmMemoryObject> WasmMemoryObject::New(Isolate* isolate, |
173 Handle<JSArrayBuffer> buffer, | 173 Handle<JSArrayBuffer> buffer, |
174 int maximum) { | 174 int maximum) { |
175 Handle<JSFunction> memory_ctor( | 175 Handle<JSFunction> memory_ctor( |
176 isolate->native_context()->wasm_memory_constructor()); | 176 isolate->native_context()->wasm_memory_constructor()); |
177 Handle<JSObject> memory_obj = | 177 Handle<JSObject> memory_obj = isolate->factory()->NewJSObject(memory_ctor); |
178 isolate->factory()->NewJSObject(memory_ctor, TENURED); | |
179 memory_obj->SetInternalField(kArrayBuffer, *buffer); | 178 memory_obj->SetInternalField(kArrayBuffer, *buffer); |
180 memory_obj->SetInternalField(kMaximum, | 179 memory_obj->SetInternalField(kMaximum, |
181 static_cast<Object*>(Smi::FromInt(maximum))); | 180 static_cast<Object*>(Smi::FromInt(maximum))); |
182 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym()); | 181 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym()); |
183 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check(); | 182 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check(); |
184 return Handle<WasmMemoryObject>::cast(memory_obj); | 183 return Handle<WasmMemoryObject>::cast(memory_obj); |
185 } | 184 } |
186 | 185 |
187 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer) | 186 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer) |
188 DEFINE_OPTIONAL_ACCESSORS(WasmMemoryObject, instances_link, kInstancesLink, | |
189 WasmInstanceWrapper) | |
190 | 187 |
191 uint32_t WasmMemoryObject::current_pages() { | 188 uint32_t WasmMemoryObject::current_pages() { |
192 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize; | 189 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize; |
193 } | 190 } |
194 | 191 |
195 int32_t WasmMemoryObject::maximum_pages() { | 192 int32_t WasmMemoryObject::maximum_pages() { |
196 return SafeInt32(GetInternalField(kMaximum)); | 193 return SafeInt32(GetInternalField(kMaximum)); |
197 } | 194 } |
198 | 195 |
199 WasmMemoryObject* WasmMemoryObject::cast(Object* object) { | 196 WasmMemoryObject* WasmMemoryObject::cast(Object* object) { |
200 DCHECK(object && object->IsJSObject()); | 197 DCHECK(object && object->IsJSObject()); |
201 // TODO(titzer): brand check for WasmMemoryObject. | 198 // TODO(titzer): brand check for WasmMemoryObject. |
202 return reinterpret_cast<WasmMemoryObject*>(object); | 199 return reinterpret_cast<WasmMemoryObject*>(object); |
203 } | 200 } |
204 | 201 |
205 void WasmMemoryObject::AddInstance(Isolate* isolate, | 202 void WasmMemoryObject::AddInstance(WasmInstanceObject* instance) { |
206 Handle<WasmInstanceObject> instance) { | 203 // TODO(gdeepti): This should be a weak list of instance objects |
207 Handle<WasmInstanceWrapper> instance_wrapper; | 204 // for instances that share memory. |
208 if (has_instances_link()) { | 205 SetInternalField(kInstance, instance); |
209 Handle<WasmInstanceWrapper> current_wrapper(get_instances_link()); | |
210 DCHECK(WasmInstanceWrapper::IsWasmInstanceWrapper(*current_wrapper)); | |
211 DCHECK(!current_wrapper->has_previous()); | |
212 instance_wrapper = WasmInstanceWrapper::New(isolate, instance); | |
213 instance_wrapper->set_next_wrapper(*current_wrapper); | |
214 current_wrapper->set_previous_wrapper(*instance_wrapper); | |
215 } else { | |
216 instance_wrapper = WasmInstanceWrapper::New(isolate, instance); | |
217 } | |
218 set_instances_link(*instance_wrapper); | |
219 instance->set_instance_wrapper(*instance_wrapper); | |
220 } | |
221 | |
222 void WasmMemoryObject::ResetInstancesLink(Isolate* isolate) { | |
223 Handle<Object> undefined = isolate->factory()->undefined_value(); | |
224 SetInternalField(kInstancesLink, *undefined); | |
225 } | 206 } |
226 | 207 |
227 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule, | 208 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule, |
228 WasmCompiledModule) | 209 WasmCompiledModule) |
229 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, | 210 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, |
230 kGlobalsArrayBuffer, JSArrayBuffer) | 211 kGlobalsArrayBuffer, JSArrayBuffer) |
231 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer, | 212 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer, |
232 JSArrayBuffer) | 213 JSArrayBuffer) |
233 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject, | 214 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject, |
234 WasmMemoryObject) | 215 WasmMemoryObject) |
235 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo, | 216 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo, |
236 WasmDebugInfo) | 217 WasmDebugInfo) |
237 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, instance_wrapper, | |
238 kWasmMemInstanceWrapper, WasmInstanceWrapper) | |
239 | 218 |
240 WasmModuleObject* WasmInstanceObject::module_object() { | 219 WasmModuleObject* WasmInstanceObject::module_object() { |
241 return WasmModuleObject::cast(*get_compiled_module()->wasm_module()); | 220 return WasmModuleObject::cast(*get_compiled_module()->wasm_module()); |
242 } | 221 } |
243 | 222 |
244 WasmModule* WasmInstanceObject::module() { | 223 WasmModule* WasmInstanceObject::module() { |
245 return reinterpret_cast<WasmModuleWrapper*>( | 224 return reinterpret_cast<WasmModuleWrapper*>( |
246 *get_compiled_module()->module_wrapper()) | 225 *get_compiled_module()->module_wrapper()) |
247 ->get(); | 226 ->get(); |
248 } | 227 } |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 if (func_index < 0) return false; | 417 if (func_index < 0) return false; |
439 | 418 |
440 WasmFunction& function = module()->functions[func_index]; | 419 WasmFunction& function = module()->functions[func_index]; |
441 | 420 |
442 info->line = func_index; | 421 info->line = func_index; |
443 info->column = position - function.code_start_offset; | 422 info->column = position - function.code_start_offset; |
444 info->line_start = function.code_start_offset; | 423 info->line_start = function.code_start_offset; |
445 info->line_end = function.code_end_offset; | 424 info->line_end = function.code_end_offset; |
446 return true; | 425 return true; |
447 } | 426 } |
448 | |
449 Handle<WasmInstanceWrapper> WasmInstanceWrapper::New( | |
450 Isolate* isolate, Handle<WasmInstanceObject> instance) { | |
451 Handle<FixedArray> array = | |
452 isolate->factory()->NewFixedArray(kWrapperPropertyCount, TENURED); | |
453 Handle<WasmInstanceWrapper> instance_wrapper( | |
454 reinterpret_cast<WasmInstanceWrapper*>(*array), isolate); | |
455 instance_wrapper->set_instance_object(instance, isolate); | |
456 return instance_wrapper; | |
457 } | |
458 | |
459 bool WasmInstanceWrapper::IsWasmInstanceWrapper(Object* obj) { | |
460 if (!obj->IsFixedArray()) return false; | |
461 FixedArray* array = FixedArray::cast(obj); | |
462 if (array->length() != kWrapperPropertyCount) return false; | |
463 if (!array->get(kWrapperInstanceObject)->IsWeakCell()) return false; | |
464 Isolate* isolate = array->GetIsolate(); | |
465 if (!array->get(kNextInstanceWrapper)->IsUndefined(isolate) && | |
466 !array->get(kNextInstanceWrapper)->IsFixedArray()) | |
467 return false; | |
468 if (!array->get(kPreviousInstanceWrapper)->IsUndefined(isolate) && | |
469 !array->get(kPreviousInstanceWrapper)->IsFixedArray()) | |
470 return false; | |
471 return true; | |
472 } | |
473 | |
474 void WasmInstanceWrapper::set_instance_object(Handle<JSObject> instance, | |
475 Isolate* isolate) { | |
476 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(instance); | |
477 set(kWrapperInstanceObject, *cell); | |
478 } | |
OLD | NEW |