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

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

Issue 2964943002: [wasm] Introduce instance types for WebAssembly.* objects. (Closed)
Patch Set: Address rossberg comments. Created 3 years, 5 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 | « src/wasm/wasm-interpreter.h ('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/assert-scope.h" 7 #include "src/assert-scope.h"
8 #include "src/ast/ast.h" 8 #include "src/ast/ast.h"
9 #include "src/execution.h" 9 #include "src/execution.h"
10 #include "src/factory.h" 10 #include "src/factory.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 Reset(); 62 Reset();
63 } else if (isolate()->has_pending_exception()) { 63 } else if (isolate()->has_pending_exception()) {
64 Reset(); 64 Reset();
65 isolate()->OptionalRescheduleException(false); 65 isolate()->OptionalRescheduleException(false);
66 } else if (error()) { 66 } else if (error()) {
67 isolate()->ScheduleThrow(*Reify()); 67 isolate()->ScheduleThrow(*Reify());
68 } 68 }
69 } 69 }
70 }; 70 };
71 71
72 // TODO(wasm): move brand check to the respective types, and don't throw 72 // TODO(titzer): move brand check to the respective types, and don't throw
73 // in it, rather, use a provided ErrorThrower, or let caller handle it. 73 // in it, rather, use a provided ErrorThrower, or let caller handle it.
74 static bool HasBrand(i::Handle<i::Object> value, i::Handle<i::Symbol> sym) { 74 static bool HasBrand(i::Handle<i::Object> value, i::Handle<i::Symbol> sym) {
75 if (!value->IsJSObject()) return false; 75 if (!value->IsJSObject()) return false;
76 i::Handle<i::JSObject> object = i::Handle<i::JSObject>::cast(value); 76 i::Handle<i::JSObject> object = i::Handle<i::JSObject>::cast(value);
77 Maybe<bool> has_brand = i::JSObject::HasOwnProperty(object, sym); 77 Maybe<bool> has_brand = i::JSObject::HasOwnProperty(object, sym);
78 return has_brand.FromMaybe(false); 78 return has_brand.FromMaybe(false);
79 } 79 }
80 80
81 static bool BrandCheck(i::Handle<i::Object> value, i::Handle<i::Symbol> sym, 81 static bool BrandCheck(i::Handle<i::Object> value, i::Handle<i::Symbol> sym,
82 ErrorThrower* thrower, const char* msg) { 82 ErrorThrower* thrower, const char* msg) {
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 thrower.TypeError("Argument 0 required, must be numeric value of pages"); 773 thrower.TypeError("Argument 0 required, must be numeric value of pages");
774 return; 774 return;
775 } 775 }
776 i::Handle<i::WasmMemoryObject> receiver = 776 i::Handle<i::WasmMemoryObject> receiver =
777 i::Handle<i::WasmMemoryObject>::cast(Utils::OpenHandle(*args.This())); 777 i::Handle<i::WasmMemoryObject>::cast(Utils::OpenHandle(*args.This()));
778 int64_t max_size64 = receiver->maximum_pages(); 778 int64_t max_size64 = receiver->maximum_pages();
779 if (max_size64 < 0 || 779 if (max_size64 < 0 ||
780 max_size64 > static_cast<int64_t>(i::FLAG_wasm_max_mem_pages)) { 780 max_size64 > static_cast<int64_t>(i::FLAG_wasm_max_mem_pages)) {
781 max_size64 = i::FLAG_wasm_max_mem_pages; 781 max_size64 = i::FLAG_wasm_max_mem_pages;
782 } 782 }
783 i::Handle<i::JSArrayBuffer> old_buffer(receiver->buffer()); 783 i::Handle<i::JSArrayBuffer> old_buffer(receiver->array_buffer());
784 uint32_t old_size = 784 uint32_t old_size =
785 old_buffer->byte_length()->Number() / i::wasm::kSpecMaxWasmMemoryPages; 785 old_buffer->byte_length()->Number() / i::wasm::kSpecMaxWasmMemoryPages;
786 int64_t new_size64 = old_size + delta_size; 786 int64_t new_size64 = old_size + delta_size;
787 if (delta_size < 0 || max_size64 < new_size64 || new_size64 < old_size) { 787 if (delta_size < 0 || max_size64 < new_size64 || new_size64 < old_size) {
788 thrower.RangeError(new_size64 < old_size ? "trying to shrink memory" 788 thrower.RangeError(new_size64 < old_size ? "trying to shrink memory"
789 : "maximum memory size exceeded"); 789 : "maximum memory size exceeded");
790 return; 790 return;
791 } 791 }
792 int32_t ret = i::WasmMemoryObject::Grow(i_isolate, receiver, 792 int32_t ret = i::WasmMemoryObject::Grow(i_isolate, receiver,
793 static_cast<uint32_t>(delta_size)); 793 static_cast<uint32_t>(delta_size));
(...skipping 16 matching lines...) Expand all
810 ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Memory.buffer"); 810 ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Memory.buffer");
811 Local<Context> context = isolate->GetCurrentContext(); 811 Local<Context> context = isolate->GetCurrentContext();
812 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 812 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
813 if (!BrandCheck(Utils::OpenHandle(*args.This()), 813 if (!BrandCheck(Utils::OpenHandle(*args.This()),
814 i::Handle<i::Symbol>(i_context->wasm_memory_sym()), &thrower, 814 i::Handle<i::Symbol>(i_context->wasm_memory_sym()), &thrower,
815 "Receiver is not a WebAssembly.Memory")) { 815 "Receiver is not a WebAssembly.Memory")) {
816 return; 816 return;
817 } 817 }
818 i::Handle<i::WasmMemoryObject> receiver = 818 i::Handle<i::WasmMemoryObject> receiver =
819 i::Handle<i::WasmMemoryObject>::cast(Utils::OpenHandle(*args.This())); 819 i::Handle<i::WasmMemoryObject>::cast(Utils::OpenHandle(*args.This()));
820 i::Handle<i::Object> buffer(receiver->buffer(), i_isolate); 820 i::Handle<i::Object> buffer(receiver->array_buffer(), i_isolate);
821 DCHECK(buffer->IsJSArrayBuffer()); 821 DCHECK(buffer->IsJSArrayBuffer());
822 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 822 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
823 return_value.Set(Utils::ToLocal(buffer)); 823 return_value.Set(Utils::ToLocal(buffer));
824 } 824 }
825 } // namespace 825 } // namespace
826 826
827 // TODO(titzer): we use the API to create the function template because the 827 // TODO(titzer): we use the API to create the function template because the
828 // internal guts are too ugly to replicate here. 828 // internal guts are too ugly to replicate here.
829 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate, 829 static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate,
830 FunctionCallback func) { 830 FunctionCallback func) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 872
873 // Install Maps. 873 // Install Maps.
874 Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate); 874 Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate);
875 875
876 InstanceType instance_type = prev_map->instance_type(); 876 InstanceType instance_type = prev_map->instance_type();
877 int embedder_fields = JSObject::GetEmbedderFieldCount(*prev_map); 877 int embedder_fields = JSObject::GetEmbedderFieldCount(*prev_map);
878 CHECK_EQ(0, embedder_fields); 878 CHECK_EQ(0, embedder_fields);
879 int pre_allocated = 879 int pre_allocated =
880 prev_map->GetInObjectProperties() - prev_map->unused_property_fields(); 880 prev_map->GetInObjectProperties() - prev_map->unused_property_fields();
881 int instance_size = 0; 881 int instance_size = 0;
882 int in_object_properties = 0; 882 int in_object_properties = WasmExportedFunction::kFieldCount;
883 int wasm_embedder_fields = embedder_fields + 1 // module instance object 883 JSFunction::CalculateInstanceSizeHelper(instance_type, embedder_fields,
884 + 1 // function arity 884 in_object_properties, &instance_size,
885 + 1; // function signature
886 JSFunction::CalculateInstanceSizeHelper(instance_type, wasm_embedder_fields,
887 0, &instance_size,
888 &in_object_properties); 885 &in_object_properties);
889 886
890 int unused_property_fields = in_object_properties - pre_allocated; 887 int unused_property_fields = in_object_properties - pre_allocated;
891 Handle<Map> map = Map::CopyInitialMap( 888 Handle<Map> map = Map::CopyInitialMap(
892 prev_map, instance_size, in_object_properties, unused_property_fields); 889 prev_map, instance_size, in_object_properties, unused_property_fields);
893 890
894 context->set_wasm_function_map(*map); 891 context->set_wasm_function_map(*map);
895 892
896 // Install symbols. 893 // Install symbols.
897 894
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 WebAssemblyInstantiateStreaming, 1); 932 WebAssemblyInstantiateStreaming, 1);
936 } 933 }
937 934
938 // Setup Module 935 // Setup Module
939 Handle<JSFunction> module_constructor = 936 Handle<JSFunction> module_constructor =
940 InstallFunc(isolate, webassembly, "Module", WebAssemblyModule, 1); 937 InstallFunc(isolate, webassembly, "Module", WebAssemblyModule, 1);
941 context->set_wasm_module_constructor(*module_constructor); 938 context->set_wasm_module_constructor(*module_constructor);
942 Handle<JSObject> module_proto = 939 Handle<JSObject> module_proto =
943 factory->NewJSObject(module_constructor, TENURED); 940 factory->NewJSObject(module_constructor, TENURED);
944 i::Handle<i::Map> module_map = isolate->factory()->NewMap( 941 i::Handle<i::Map> module_map = isolate->factory()->NewMap(
945 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + 942 i::WASM_MODULE_TYPE, i::JSObject::kHeaderSize +
946 WasmModuleObject::kFieldCount * i::kPointerSize); 943 WasmModuleObject::kFieldCount * i::kPointerSize);
947 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); 944 JSFunction::SetInitialMap(module_constructor, module_map, module_proto);
948 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, 945 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports,
949 1); 946 1);
950 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, 947 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports,
951 1); 948 1);
952 InstallFunc(isolate, module_constructor, "customSections", 949 InstallFunc(isolate, module_constructor, "customSections",
953 WebAssemblyModuleCustomSections, 2); 950 WebAssemblyModuleCustomSections, 2);
954 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), 951 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(),
955 module_constructor, DONT_ENUM); 952 module_constructor, DONT_ENUM);
956 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(), 953 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(),
957 v8_str(isolate, "WebAssembly.Module"), ro_attributes); 954 v8_str(isolate, "WebAssembly.Module"), ro_attributes);
958 955
959 // Setup Instance 956 // Setup Instance
960 Handle<JSFunction> instance_constructor = 957 Handle<JSFunction> instance_constructor =
961 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); 958 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1);
962 context->set_wasm_instance_constructor(*instance_constructor); 959 context->set_wasm_instance_constructor(*instance_constructor);
963 Handle<JSObject> instance_proto = 960 Handle<JSObject> instance_proto =
964 factory->NewJSObject(instance_constructor, TENURED); 961 factory->NewJSObject(instance_constructor, TENURED);
965 i::Handle<i::Map> instance_map = isolate->factory()->NewMap( 962 i::Handle<i::Map> instance_map = isolate->factory()->NewMap(
966 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + 963 i::WASM_INSTANCE_TYPE, WasmInstanceObject::kSize);
967 WasmInstanceObject::kFieldCount * i::kPointerSize);
968 JSFunction::SetInitialMap(instance_constructor, instance_map, instance_proto); 964 JSFunction::SetInitialMap(instance_constructor, instance_map, instance_proto);
969 JSObject::AddProperty(instance_proto, 965 JSObject::AddProperty(instance_proto,
970 isolate->factory()->constructor_string(), 966 isolate->factory()->constructor_string(),
971 instance_constructor, DONT_ENUM); 967 instance_constructor, DONT_ENUM);
972 JSObject::AddProperty(instance_proto, factory->to_string_tag_symbol(), 968 JSObject::AddProperty(instance_proto, factory->to_string_tag_symbol(),
973 v8_str(isolate, "WebAssembly.Instance"), ro_attributes); 969 v8_str(isolate, "WebAssembly.Instance"), ro_attributes);
974 970
975 // Setup Table 971 // Setup Table
976 Handle<JSFunction> table_constructor = 972 Handle<JSFunction> table_constructor =
977 InstallFunc(isolate, webassembly, "Table", WebAssemblyTable, 1); 973 InstallFunc(isolate, webassembly, "Table", WebAssemblyTable, 1);
978 context->set_wasm_table_constructor(*table_constructor); 974 context->set_wasm_table_constructor(*table_constructor);
979 Handle<JSObject> table_proto = 975 Handle<JSObject> table_proto =
980 factory->NewJSObject(table_constructor, TENURED); 976 factory->NewJSObject(table_constructor, TENURED);
981 i::Handle<i::Map> table_map = isolate->factory()->NewMap( 977 i::Handle<i::Map> table_map =
982 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + 978 isolate->factory()->NewMap(i::WASM_TABLE_TYPE, WasmTableObject::kSize);
983 WasmTableObject::kFieldCount * i::kPointerSize);
984 JSFunction::SetInitialMap(table_constructor, table_map, table_proto); 979 JSFunction::SetInitialMap(table_constructor, table_map, table_proto);
985 JSObject::AddProperty(table_proto, isolate->factory()->constructor_string(), 980 JSObject::AddProperty(table_proto, isolate->factory()->constructor_string(),
986 table_constructor, DONT_ENUM); 981 table_constructor, DONT_ENUM);
987 InstallGetter(isolate, table_proto, "length", WebAssemblyTableGetLength); 982 InstallGetter(isolate, table_proto, "length", WebAssemblyTableGetLength);
988 InstallFunc(isolate, table_proto, "grow", WebAssemblyTableGrow, 1); 983 InstallFunc(isolate, table_proto, "grow", WebAssemblyTableGrow, 1);
989 InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet, 1); 984 InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet, 1);
990 InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet, 2); 985 InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet, 2);
991 JSObject::AddProperty(table_proto, factory->to_string_tag_symbol(), 986 JSObject::AddProperty(table_proto, factory->to_string_tag_symbol(),
992 v8_str(isolate, "WebAssembly.Table"), ro_attributes); 987 v8_str(isolate, "WebAssembly.Table"), ro_attributes);
993 988
994 // Setup Memory 989 // Setup Memory
995 Handle<JSFunction> memory_constructor = 990 Handle<JSFunction> memory_constructor =
996 InstallFunc(isolate, webassembly, "Memory", WebAssemblyMemory, 1); 991 InstallFunc(isolate, webassembly, "Memory", WebAssemblyMemory, 1);
997 context->set_wasm_memory_constructor(*memory_constructor); 992 context->set_wasm_memory_constructor(*memory_constructor);
998 Handle<JSObject> memory_proto = 993 Handle<JSObject> memory_proto =
999 factory->NewJSObject(memory_constructor, TENURED); 994 factory->NewJSObject(memory_constructor, TENURED);
1000 i::Handle<i::Map> memory_map = isolate->factory()->NewMap( 995 i::Handle<i::Map> memory_map =
1001 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + 996 isolate->factory()->NewMap(i::WASM_MEMORY_TYPE, WasmMemoryObject::kSize);
1002 WasmMemoryObject::kFieldCount * i::kPointerSize);
1003 JSFunction::SetInitialMap(memory_constructor, memory_map, memory_proto); 997 JSFunction::SetInitialMap(memory_constructor, memory_map, memory_proto);
1004 JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(), 998 JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(),
1005 memory_constructor, DONT_ENUM); 999 memory_constructor, DONT_ENUM);
1006 InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow, 1); 1000 InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow, 1);
1007 InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer); 1001 InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer);
1008 JSObject::AddProperty(memory_proto, factory->to_string_tag_symbol(), 1002 JSObject::AddProperty(memory_proto, factory->to_string_tag_symbol(),
1009 v8_str(isolate, "WebAssembly.Memory"), ro_attributes); 1003 v8_str(isolate, "WebAssembly.Memory"), ro_attributes);
1010 1004
1011 // Setup errors 1005 // Setup errors
1012 attributes = static_cast<PropertyAttributes>(DONT_ENUM); 1006 attributes = static_cast<PropertyAttributes>(DONT_ENUM);
(...skipping 15 matching lines...) Expand all
1028 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); 1022 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate);
1029 return HasBrand(value, symbol); 1023 return HasBrand(value, symbol);
1030 } 1024 }
1031 1025
1032 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { 1026 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) {
1033 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); 1027 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate);
1034 return HasBrand(value, symbol); 1028 return HasBrand(value, symbol);
1035 } 1029 }
1036 } // namespace internal 1030 } // namespace internal
1037 } // namespace v8 1031 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-interpreter.h ('k') | src/wasm/wasm-module.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698