Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: src/wasm/wasm-objects.cc

Issue 2471883003: [wasm] WebAssembly.Memory object can be referenced by multiple Instance objects. (Closed)
Patch Set: Andreas's review Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/wasm/wasm-objects.h ('k') | test/mjsunit/wasm/import-memory.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 = isolate->factory()->NewJSObject(memory_ctor); 177 Handle<JSObject> memory_obj =
178 isolate->factory()->NewJSObject(memory_ctor, TENURED);
178 memory_obj->SetInternalField(kArrayBuffer, *buffer); 179 memory_obj->SetInternalField(kArrayBuffer, *buffer);
179 memory_obj->SetInternalField(kMaximum, 180 memory_obj->SetInternalField(kMaximum,
180 static_cast<Object*>(Smi::FromInt(maximum))); 181 static_cast<Object*>(Smi::FromInt(maximum)));
181 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym()); 182 Handle<Symbol> memory_sym(isolate->native_context()->wasm_memory_sym());
182 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check(); 183 Object::SetProperty(memory_obj, memory_sym, memory_obj, STRICT).Check();
183 return Handle<WasmMemoryObject>::cast(memory_obj); 184 return Handle<WasmMemoryObject>::cast(memory_obj);
184 } 185 }
185 186
186 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer) 187 DEFINE_ACCESSORS(WasmMemoryObject, buffer, kArrayBuffer, JSArrayBuffer)
188 DEFINE_OPTIONAL_ACCESSORS(WasmMemoryObject, instances_link, kInstancesLink,
189 WasmInstanceWrapper)
187 190
188 uint32_t WasmMemoryObject::current_pages() { 191 uint32_t WasmMemoryObject::current_pages() {
189 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize; 192 return SafeUint32(get_buffer()->byte_length()) / wasm::WasmModule::kPageSize;
190 } 193 }
191 194
192 int32_t WasmMemoryObject::maximum_pages() { 195 int32_t WasmMemoryObject::maximum_pages() {
193 return SafeInt32(GetInternalField(kMaximum)); 196 return SafeInt32(GetInternalField(kMaximum));
194 } 197 }
195 198
196 WasmMemoryObject* WasmMemoryObject::cast(Object* object) { 199 WasmMemoryObject* WasmMemoryObject::cast(Object* object) {
197 DCHECK(object && object->IsJSObject()); 200 DCHECK(object && object->IsJSObject());
198 // TODO(titzer): brand check for WasmMemoryObject. 201 // TODO(titzer): brand check for WasmMemoryObject.
199 return reinterpret_cast<WasmMemoryObject*>(object); 202 return reinterpret_cast<WasmMemoryObject*>(object);
200 } 203 }
201 204
202 void WasmMemoryObject::AddInstance(WasmInstanceObject* instance) { 205 void WasmMemoryObject::AddInstance(Isolate* isolate,
203 // TODO(gdeepti): This should be a weak list of instance objects 206 Handle<WasmInstanceObject> instance) {
204 // for instances that share memory. 207 Handle<WasmInstanceWrapper> instance_wrapper;
205 SetInternalField(kInstance, instance); 208 if (has_instances_link()) {
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);
206 } 225 }
207 226
208 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule, 227 DEFINE_ACCESSORS(WasmInstanceObject, compiled_module, kCompiledModule,
209 WasmCompiledModule) 228 WasmCompiledModule)
210 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, 229 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer,
211 kGlobalsArrayBuffer, JSArrayBuffer) 230 kGlobalsArrayBuffer, JSArrayBuffer)
212 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer, 231 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_buffer, kMemoryArrayBuffer,
213 JSArrayBuffer) 232 JSArrayBuffer)
214 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject, 233 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, kMemoryObject,
215 WasmMemoryObject) 234 WasmMemoryObject)
216 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo, 235 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, kDebugInfo,
217 WasmDebugInfo) 236 WasmDebugInfo)
237 DEFINE_OPTIONAL_ACCESSORS(WasmInstanceObject, instance_wrapper,
238 kWasmMemInstanceWrapper, WasmInstanceWrapper)
218 239
219 WasmModuleObject* WasmInstanceObject::module_object() { 240 WasmModuleObject* WasmInstanceObject::module_object() {
220 return WasmModuleObject::cast(*get_compiled_module()->wasm_module()); 241 return WasmModuleObject::cast(*get_compiled_module()->wasm_module());
221 } 242 }
222 243
223 WasmModule* WasmInstanceObject::module() { 244 WasmModule* WasmInstanceObject::module() {
224 return reinterpret_cast<WasmModuleWrapper*>( 245 return reinterpret_cast<WasmModuleWrapper*>(
225 *get_compiled_module()->module_wrapper()) 246 *get_compiled_module()->module_wrapper())
226 ->get(); 247 ->get();
227 } 248 }
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 if (func_index < 0) return false; 438 if (func_index < 0) return false;
418 439
419 WasmFunction& function = module()->functions[func_index]; 440 WasmFunction& function = module()->functions[func_index];
420 441
421 info->line = func_index; 442 info->line = func_index;
422 info->column = position - function.code_start_offset; 443 info->column = position - function.code_start_offset;
423 info->line_start = function.code_start_offset; 444 info->line_start = function.code_start_offset;
424 info->line_end = function.code_end_offset; 445 info->line_end = function.code_end_offset;
425 return true; 446 return true;
426 } 447 }
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 Handle<FixedArray> array = handle(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 }
OLDNEW
« no previous file with comments | « src/wasm/wasm-objects.h ('k') | test/mjsunit/wasm/import-memory.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698