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

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

Issue 2410763002: [wasm] GrowMemory should use maximum size declared in WebAssembly.Memory (Closed)
Patch Set: Formatting Created 4 years, 2 months 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 | « no previous file | src/wasm/wasm-module.cc » ('j') | src/wasm/wasm-module.cc » ('J')
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"
11 #include "src/ast/ast.h" 11 #include "src/ast/ast.h"
12 #include "src/execution.h" 12 #include "src/execution.h"
13 #include "src/factory.h" 13 #include "src/factory.h"
14 #include "src/handles.h" 14 #include "src/handles.h"
15 #include "src/isolate.h" 15 #include "src/isolate.h"
16 #include "src/objects.h" 16 #include "src/objects.h"
17 #include "src/parsing/parse-info.h" 17 #include "src/parsing/parse-info.h"
18 18
19 #include "src/wasm/module-decoder.h" 19 #include "src/wasm/module-decoder.h"
20 #include "src/wasm/wasm-js.h" 20 #include "src/wasm/wasm-js.h"
21 #include "src/wasm/wasm-module.h" 21 #include "src/wasm/wasm-module.h"
22 #include "src/wasm/wasm-result.h" 22 #include "src/wasm/wasm-result.h"
23 23
24 typedef uint8_t byte; 24 typedef uint8_t byte;
25 25
26 using v8::internal::wasm::ErrorThrower; 26 using v8::internal::wasm::ErrorThrower;
27 27
28 namespace v8 { 28 namespace v8 {
29 29
30 static const int kWasmMemoryBufferFieldIndex = 0;
31 static const int kWasmMemoryMaximumFieldIndex = 1;
32 static const int kWasmTableArrayFieldIndex = 0; 30 static const int kWasmTableArrayFieldIndex = 0;
33 static const int kWasmTableMaximumFieldIndex = 1; 31 static const int kWasmTableMaximumFieldIndex = 1;
34 32
33 enum WasmMemoryObjectData {
34 kWasmMemoryBuffer,
35 kWasmMemoryMaximum,
36 kWasmMemoryInstanceObject
37 };
38
35 namespace { 39 namespace {
36 i::Handle<i::String> v8_str(i::Isolate* isolate, const char* str) { 40 i::Handle<i::String> v8_str(i::Isolate* isolate, const char* str) {
37 return isolate->factory()->NewStringFromAsciiChecked(str); 41 return isolate->factory()->NewStringFromAsciiChecked(str);
38 } 42 }
39 Local<String> v8_str(Isolate* isolate, const char* str) { 43 Local<String> v8_str(Isolate* isolate, const char* str) {
40 return Utils::ToLocal(v8_str(reinterpret_cast<i::Isolate*>(isolate), str)); 44 return Utils::ToLocal(v8_str(reinterpret_cast<i::Isolate*>(isolate), str));
41 } 45 }
42 46
43 struct RawBuffer { 47 struct RawBuffer {
44 const byte* start; 48 const byte* start;
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 572
569 void WebAssemblyMemoryGrow(const v8::FunctionCallbackInfo<v8::Value>& args) { 573 void WebAssemblyMemoryGrow(const v8::FunctionCallbackInfo<v8::Value>& args) {
570 v8::Isolate* isolate = args.GetIsolate(); 574 v8::Isolate* isolate = args.GetIsolate();
571 Local<Context> context = isolate->GetCurrentContext(); 575 Local<Context> context = isolate->GetCurrentContext();
572 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 576 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
573 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()), 577 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()),
574 i::Handle<i::Symbol>(i_context->wasm_memory_sym()), 578 i::Handle<i::Symbol>(i_context->wasm_memory_sym()),
575 "Receiver is not a WebAssembly.Memory")) { 579 "Receiver is not a WebAssembly.Memory")) {
576 return; 580 return;
577 } 581 }
582 if (args.Length() < 1 || !args[0]->IsUint32()) {
583 v8::Local<v8::Value> e = v8::Exception::TypeError(
584 v8_str(isolate, "Argument 0 must be numeric value of pages"));
585 isolate->ThrowException(e);
586 return;
587 }
578 588
579 // TODO(rossberg): grow memory. 589 uint32_t delta = args[0]->Uint32Value(context).FromJust();
bradnelson 2016/10/17 22:23:26 Still not groking from before. If you use ToUint32
gdeepti 2016/10/18 02:34:17 ToUint32(&delta) method can be used only on intern
bradnelson 2016/10/18 04:29:38 Ah ok.
580 v8::Local<v8::Value> e = 590 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
581 v8::Exception::TypeError(v8_str(isolate, "Memory#grow unimplemented")); 591 i::Handle<i::JSObject> receiver =
582 isolate->ThrowException(e); 592 i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This()));
593 i::Handle<i::Object> instance_object(receiver->GetInternalField(2),
bradnelson 2016/10/17 22:13:24 where's this 2 come from?
gdeepti 2016/10/18 02:34:17 From the internal field of the memory object, repl
594 i_isolate);
595 i::Handle<i::JSObject> instance(
596 i::Handle<i::JSObject>::cast(instance_object));
597
598 // TODO(gdeepti) Implement growing memory when shared by different
599 // instances.
600 uint32_t ret = internal::wasm::GrowInstanceMemory(i_isolate, instance, delta);
601 if (ret == -1) {
bradnelson 2016/10/17 22:13:25 unsigned, compared to -1 ?
gdeepti 2016/10/18 02:34:17 Oops, fixed. For the tests I currently have, the c
602 v8::Local<v8::Value> e = v8::Exception::Error(
603 v8_str(isolate, "Unable to grow instance memory."));
604 isolate->ThrowException(e);
605 return;
606 }
607 i::MaybeHandle<i::JSArrayBuffer> buffer =
608 internal::wasm::GetInstanceMemory(i_isolate, instance);
609 if (buffer.is_null()) {
610 v8::Local<v8::Value> e = v8::Exception::Error(
611 v8_str(isolate, "WebAssembly.Memory buffer object not set."));
612 isolate->ThrowException(e);
613 return;
614 }
615 receiver->SetInternalField(kWasmMemoryBuffer, *buffer.ToHandleChecked());
616 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
617 return_value.Set(ret);
583 } 618 }
584 619
585 void WebAssemblyMemoryGetBuffer( 620 void WebAssemblyMemoryGetBuffer(
586 const v8::FunctionCallbackInfo<v8::Value>& args) { 621 const v8::FunctionCallbackInfo<v8::Value>& args) {
587 v8::Isolate* isolate = args.GetIsolate(); 622 v8::Isolate* isolate = args.GetIsolate();
588 Local<Context> context = isolate->GetCurrentContext(); 623 Local<Context> context = isolate->GetCurrentContext();
589 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 624 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
590 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()), 625 if (!BrandCheck(isolate, Utils::OpenHandle(*args.This()),
591 i::Handle<i::Symbol>(i_context->wasm_memory_sym()), 626 i::Handle<i::Symbol>(i_context->wasm_memory_sym()),
592 "Receiver is not a WebAssembly.Memory")) { 627 "Receiver is not a WebAssembly.Memory")) {
593 return; 628 return;
594 } 629 }
595 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 630 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
596 i::Handle<i::JSObject> receiver = 631 i::Handle<i::JSObject> receiver =
597 i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This())); 632 i::Handle<i::JSObject>::cast(Utils::OpenHandle(*args.This()));
598 i::Handle<i::Object> buffer( 633 i::Handle<i::Object> buffer(receiver->GetInternalField(kWasmMemoryBuffer),
599 receiver->GetInternalField(kWasmMemoryBufferFieldIndex), i_isolate); 634 i_isolate);
600 DCHECK(buffer->IsJSArrayBuffer()); 635 DCHECK(buffer->IsJSArrayBuffer());
601 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 636 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
602 return_value.Set(Utils::ToLocal(buffer)); 637 return_value.Set(Utils::ToLocal(buffer));
603 } 638 }
604 } // namespace 639 } // namespace
605 640
606 i::Handle<i::JSObject> i::WasmJs::CreateWasmMemoryObject( 641 i::Handle<i::JSObject> i::WasmJs::CreateWasmMemoryObject(
607 i::Isolate* i_isolate, i::Handle<i::JSArrayBuffer> buffer, bool has_maximum, 642 i::Isolate* i_isolate, i::Handle<i::JSArrayBuffer> buffer, bool has_maximum,
608 int maximum) { 643 int maximum) {
609 i::Handle<i::JSFunction> memory_ctor( 644 i::Handle<i::JSFunction> memory_ctor(
610 i_isolate->native_context()->wasm_memory_constructor()); 645 i_isolate->native_context()->wasm_memory_constructor());
611 i::Handle<i::JSObject> memory_obj = 646 i::Handle<i::JSObject> memory_obj =
612 i_isolate->factory()->NewJSObject(memory_ctor); 647 i_isolate->factory()->NewJSObject(memory_ctor);
613 memory_obj->SetInternalField(kWasmMemoryBufferFieldIndex, *buffer); 648 memory_obj->SetInternalField(kWasmMemoryBuffer, *buffer);
614 memory_obj->SetInternalField( 649 memory_obj->SetInternalField(
615 kWasmMemoryMaximumFieldIndex, 650 kWasmMemoryMaximum,
616 has_maximum 651 has_maximum
617 ? static_cast<i::Object*>(i::Smi::FromInt(maximum)) 652 ? static_cast<i::Object*>(i::Smi::FromInt(maximum))
618 : static_cast<i::Object*>(i_isolate->heap()->undefined_value())); 653 : static_cast<i::Object*>(i_isolate->heap()->undefined_value()));
619 i::Handle<i::Symbol> memory_sym( 654 i::Handle<i::Symbol> memory_sym(
620 i_isolate->native_context()->wasm_memory_sym()); 655 i_isolate->native_context()->wasm_memory_sym());
621 i::Object::SetProperty(memory_obj, memory_sym, memory_obj, i::STRICT).Check(); 656 i::Object::SetProperty(memory_obj, memory_sym, memory_obj, i::STRICT).Check();
622 return memory_obj; 657 return memory_obj;
623 } 658 }
624 659
625 // TODO(titzer): we use the API to create the function template because the 660 // TODO(titzer): we use the API to create the function template because the
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet); 771 InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet);
737 InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet); 772 InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet);
738 773
739 // Setup Memory 774 // Setup Memory
740 Handle<JSFunction> memory_constructor = 775 Handle<JSFunction> memory_constructor =
741 InstallFunc(isolate, wasm_object, "Memory", WebAssemblyMemory); 776 InstallFunc(isolate, wasm_object, "Memory", WebAssemblyMemory);
742 context->set_wasm_memory_constructor(*memory_constructor); 777 context->set_wasm_memory_constructor(*memory_constructor);
743 Handle<JSObject> memory_proto = 778 Handle<JSObject> memory_proto =
744 factory->NewJSObject(memory_constructor, TENURED); 779 factory->NewJSObject(memory_constructor, TENURED);
745 map = isolate->factory()->NewMap( 780 map = isolate->factory()->NewMap(
746 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + 2 * i::kPointerSize); 781 i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + 3 * i::kPointerSize);
bradnelson 2016/10/17 22:13:24 Add a size enum item above so this can have a name
gdeepti 2016/10/18 02:34:17 Done.
747 JSFunction::SetInitialMap(memory_constructor, map, memory_proto); 782 JSFunction::SetInitialMap(memory_constructor, map, memory_proto);
748 JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(), 783 JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(),
749 memory_constructor, DONT_ENUM); 784 memory_constructor, DONT_ENUM);
750 InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow); 785 InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow);
751 InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer); 786 InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer);
752 787
753 // Setup errors 788 // Setup errors
754 attributes = static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); 789 attributes = static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY);
755 Handle<JSFunction> compile_error( 790 Handle<JSFunction> compile_error(
756 isolate->native_context()->wasm_compile_error_function()); 791 isolate->native_context()->wasm_compile_error_function());
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 if (has_brand.IsNothing()) return false; 879 if (has_brand.IsNothing()) return false;
845 if (has_brand.ToChecked()) return true; 880 if (has_brand.ToChecked()) return true;
846 } 881 }
847 return false; 882 return false;
848 } 883 }
849 884
850 Handle<JSArrayBuffer> WasmJs::GetWasmMemoryArrayBuffer(Isolate* isolate, 885 Handle<JSArrayBuffer> WasmJs::GetWasmMemoryArrayBuffer(Isolate* isolate,
851 Handle<Object> value) { 886 Handle<Object> value) {
852 DCHECK(IsWasmMemoryObject(isolate, value)); 887 DCHECK(IsWasmMemoryObject(isolate, value));
853 Handle<Object> buf( 888 Handle<Object> buf(
854 JSObject::cast(*value)->GetInternalField(kWasmMemoryBufferFieldIndex), 889 JSObject::cast(*value)->GetInternalField(kWasmMemoryBuffer), isolate);
855 isolate);
856 return Handle<JSArrayBuffer>::cast(buf); 890 return Handle<JSArrayBuffer>::cast(buf);
857 } 891 }
858 } // namespace internal 892 } // namespace internal
859 } // namespace v8 893 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/wasm/wasm-module.cc » ('j') | src/wasm/wasm-module.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698