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

Side by Side Diff: src/wasm/wasm-js.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/runtime/runtime-wasm.cc ('k') | src/wasm/wasm-module.h » ('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/api-natives.h" 5 #include "src/api-natives.h"
6 #include "src/api.h" 6 #include "src/api.h"
7 #include "src/asmjs/asm-js.h" 7 #include "src/asmjs/asm-js.h"
8 #include "src/asmjs/asm-typer.h" 8 #include "src/asmjs/asm-typer.h"
9 #include "src/asmjs/asm-wasm-builder.h" 9 #include "src/asmjs/asm-wasm-builder.h"
10 #include "src/assert-scope.h" 10 #include "src/assert-scope.h"
(...skipping 10 matching lines...) Expand all
21 #include "src/wasm/wasm-module.h" 21 #include "src/wasm/wasm-module.h"
22 #include "src/wasm/wasm-objects.h" 22 #include "src/wasm/wasm-objects.h"
23 #include "src/wasm/wasm-result.h" 23 #include "src/wasm/wasm-result.h"
24 24
25 typedef uint8_t byte; 25 typedef uint8_t byte;
26 26
27 using v8::internal::wasm::ErrorThrower; 27 using v8::internal::wasm::ErrorThrower;
28 28
29 namespace v8 { 29 namespace v8 {
30 30
31 enum WasmMemoryObjectData {
32 kWasmMemoryBuffer,
33 kWasmMemoryMaximum,
34 kWasmMemoryInstanceObject
35 };
36
37 namespace { 31 namespace {
38 i::Handle<i::String> v8_str(i::Isolate* isolate, const char* str) { 32 i::Handle<i::String> v8_str(i::Isolate* isolate, const char* str) {
39 return isolate->factory()->NewStringFromAsciiChecked(str); 33 return isolate->factory()->NewStringFromAsciiChecked(str);
40 } 34 }
41 Local<String> v8_str(Isolate* isolate, const char* str) { 35 Local<String> v8_str(Isolate* isolate, const char* str) {
42 return Utils::ToLocal(v8_str(reinterpret_cast<i::Isolate*>(isolate), str)); 36 return Utils::ToLocal(v8_str(reinterpret_cast<i::Isolate*>(isolate), str));
43 } 37 }
44 38
45 struct RawBuffer { 39 struct RawBuffer {
46 const byte* start; 40 const byte* start;
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); 217 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
224 if (args.Length() > 2 && args[2]->IsObject()) { 218 if (args.Length() > 2 && args[2]->IsObject()) {
225 Local<Object> obj = Local<Object>::Cast(args[2]); 219 Local<Object> obj = Local<Object>::Cast(args[2]);
226 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); 220 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
227 if (i::WasmJs::IsWasmMemoryObject(i_isolate, mem_obj)) { 221 if (i::WasmJs::IsWasmMemoryObject(i_isolate, mem_obj)) {
228 memory = i::Handle<i::JSArrayBuffer>( 222 memory = i::Handle<i::JSArrayBuffer>(
229 i::Handle<i::WasmMemoryObject>::cast(mem_obj)->get_buffer(), 223 i::Handle<i::WasmMemoryObject>::cast(mem_obj)->get_buffer(),
230 i_isolate); 224 i_isolate);
231 } else { 225 } else {
232 thrower.TypeError("Argument 2 must be a WebAssembly.Memory"); 226 thrower.TypeError("Argument 2 must be a WebAssembly.Memory");
227 return;
233 } 228 }
234 } 229 }
235 i::MaybeHandle<i::JSObject> instance = 230 i::MaybeHandle<i::JSObject> instance =
236 i::wasm::WasmModule::Instantiate(i_isolate, &thrower, i_obj, ffi, memory); 231 i::wasm::WasmModule::Instantiate(i_isolate, &thrower, i_obj, ffi, memory);
237 if (instance.is_null()) { 232 if (instance.is_null()) {
238 if (!thrower.error()) thrower.RuntimeError("Could not instantiate module"); 233 if (!thrower.error()) thrower.RuntimeError("Could not instantiate module");
239 return; 234 return;
240 } 235 }
241 DCHECK(!i_isolate->has_pending_exception()); 236 DCHECK(!i_isolate->has_pending_exception());
242 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 237 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 } 521 }
527 if (args.Length() < 1) { 522 if (args.Length() < 1) {
528 v8::Local<v8::Value> e = v8::Exception::TypeError( 523 v8::Local<v8::Value> e = v8::Exception::TypeError(
529 v8_str(isolate, "Argument 0 required, must be numeric value of pages")); 524 v8_str(isolate, "Argument 0 required, must be numeric value of pages"));
530 isolate->ThrowException(e); 525 isolate->ThrowException(e);
531 return; 526 return;
532 } 527 }
533 528
534 uint32_t delta = args[0]->Uint32Value(context).FromJust(); 529 uint32_t delta = args[0]->Uint32Value(context).FromJust();
535 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 530 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
536 i::Handle<i::JSObject> receiver = 531 i::Handle<i::Object> receiver =
537 i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This())); 532 i::Handle<i::Object>::cast(Utils::OpenHandle(*args.This()));
538 i::Handle<i::Object> instance_object( 533 int32_t ret = i::wasm::GrowWebAssemblyMemory(i_isolate, receiver, delta);
539 receiver->GetInternalField(kWasmMemoryInstanceObject), i_isolate);
540 i::Handle<i::WasmInstanceObject> instance(
541 i::Handle<i::WasmInstanceObject>::cast(instance_object));
542
543 // TODO(gdeepti) Implement growing memory when shared by different
544 // instances.
545 int32_t ret = internal::wasm::GrowInstanceMemory(i_isolate, instance, delta);
546 if (ret == -1) { 534 if (ret == -1) {
547 v8::Local<v8::Value> e = v8::Exception::Error( 535 v8::Local<v8::Value> e = v8::Exception::Error(
548 v8_str(isolate, "Unable to grow instance memory.")); 536 v8_str(isolate, "Unable to grow instance memory."));
549 isolate->ThrowException(e); 537 isolate->ThrowException(e);
550 return; 538 return;
551 } 539 }
552 i::MaybeHandle<i::JSArrayBuffer> buffer =
553 internal::wasm::GetInstanceMemory(i_isolate, instance);
554 if (buffer.is_null()) {
555 v8::Local<v8::Value> e = v8::Exception::Error(
556 v8_str(isolate, "WebAssembly.Memory buffer object not set."));
557 isolate->ThrowException(e);
558 return;
559 }
560 receiver->SetInternalField(kWasmMemoryBuffer, *buffer.ToHandleChecked());
561 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 540 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
562 return_value.Set(ret); 541 return_value.Set(ret);
563 } 542 }
564 543
565 void WebAssemblyMemoryGetBuffer( 544 void WebAssemblyMemoryGetBuffer(
566 const v8::FunctionCallbackInfo<v8::Value>& args) { 545 const v8::FunctionCallbackInfo<v8::Value>& args) {
567 v8::Isolate* isolate = args.GetIsolate(); 546 v8::Isolate* isolate = args.GetIsolate();
568 Local<Context> context = isolate->GetCurrentContext(); 547 Local<Context> context = isolate->GetCurrentContext();
569 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 548 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
570 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()), 549 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()),
571 i::Handle<i::Symbol>(i_context->wasm_memory_sym()), 550 i::Handle<i::Symbol>(i_context->wasm_memory_sym()),
572 "Receiver is not a WebAssembly.Memory")) { 551 "Receiver is not a WebAssembly.Memory")) {
573 return; 552 return;
574 } 553 }
575 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 554 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
576 i::Handle<i::JSObject> receiver = 555 i::Handle<i::WasmMemoryObject> receiver =
577 i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This())); 556 i::Handle<i::WasmMemoryObject>::cast(Utils::OpenHandle(*args.This()));
578 i::Handle<i::Object> buffer(receiver->GetInternalField(kWasmMemoryBuffer), 557 i::Handle<i::Object> buffer(receiver->get_buffer(), i_isolate);
579 i_isolate);
580 DCHECK(buffer->IsJSArrayBuffer()); 558 DCHECK(buffer->IsJSArrayBuffer());
581 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 559 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
582 return_value.Set(Utils::ToLocal(buffer)); 560 return_value.Set(Utils::ToLocal(buffer));
583 } 561 }
584 } // namespace 562 } // namespace
585 563
586 // TODO(titzer): we use the API to create the function template because the 564 // TODO(titzer): we use the API to create the function template because the
587 // internal guts are too ugly to replicate here. 565 // internal guts are too ugly to replicate here.
588 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, 566 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate,
589 FunctionCallback func) { 567 FunctionCallback func) {
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); 761 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate);
784 return HasBrand(value, symbol); 762 return HasBrand(value, symbol);
785 } 763 }
786 764
787 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { 765 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) {
788 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); 766 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate);
789 return HasBrand(value, symbol); 767 return HasBrand(value, symbol);
790 } 768 }
791 } // namespace internal 769 } // namespace internal
792 } // namespace v8 770 } // namespace v8
OLDNEW
« no previous file with comments | « src/runtime/runtime-wasm.cc ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698