Chromium Code Reviews| 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "src/assembler-inl.h" | 7 #include "src/assembler-inl.h" |
| 8 #include "src/base/adapters.h" | 8 #include "src/base/adapters.h" |
| 9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
| 10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
| 11 #include "src/compiler/wasm-compiler.h" | 11 #include "src/compiler/wasm-compiler.h" |
| 12 #include "src/debug/interface-types.h" | 12 #include "src/debug/interface-types.h" |
| 13 #include "src/objects.h" | 13 #include "src/objects.h" |
| 14 #include "src/property-descriptor.h" | 14 #include "src/property-descriptor.h" |
| 15 #include "src/simulator.h" | 15 #include "src/simulator.h" |
| 16 #include "src/snapshot/snapshot.h" | 16 #include "src/snapshot/snapshot.h" |
| 17 #include "src/v8.h" | 17 #include "src/v8.h" |
| 18 | 18 |
| 19 #include "src/asmjs/asm-wasm-builder.h" | |
| 19 #include "src/wasm/function-body-decoder.h" | 20 #include "src/wasm/function-body-decoder.h" |
| 20 #include "src/wasm/module-decoder.h" | 21 #include "src/wasm/module-decoder.h" |
| 21 #include "src/wasm/wasm-js.h" | 22 #include "src/wasm/wasm-js.h" |
| 22 #include "src/wasm/wasm-limits.h" | 23 #include "src/wasm/wasm-limits.h" |
| 23 #include "src/wasm/wasm-module.h" | 24 #include "src/wasm/wasm-module.h" |
| 24 #include "src/wasm/wasm-objects.h" | 25 #include "src/wasm/wasm-objects.h" |
| 25 #include "src/wasm/wasm-result.h" | 26 #include "src/wasm/wasm-result.h" |
| 26 | 27 |
| 27 using namespace v8::internal; | 28 using namespace v8::internal; |
| 28 using namespace v8::internal::wasm; | 29 using namespace v8::internal::wasm; |
| (...skipping 1882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1911 // and globals. | 1912 // and globals. |
| 1912 void ProcessExports(Handle<FixedArray> code_table, | 1913 void ProcessExports(Handle<FixedArray> code_table, |
| 1913 Handle<WasmInstanceObject> instance, | 1914 Handle<WasmInstanceObject> instance, |
| 1914 Handle<WasmCompiledModule> compiled_module) { | 1915 Handle<WasmCompiledModule> compiled_module) { |
| 1915 if (NeedsWrappers()) { | 1916 if (NeedsWrappers()) { |
| 1916 // Fill the table to cache the exported JSFunction wrappers. | 1917 // Fill the table to cache the exported JSFunction wrappers. |
| 1917 js_wrappers_.insert(js_wrappers_.begin(), module_->functions.size(), | 1918 js_wrappers_.insert(js_wrappers_.begin(), module_->functions.size(), |
| 1918 Handle<JSFunction>::null()); | 1919 Handle<JSFunction>::null()); |
| 1919 } | 1920 } |
| 1920 | 1921 |
| 1921 Handle<JSObject> exports_object = instance; | 1922 Handle<JSObject> exports_object; |
| 1922 if (module_->origin == kWasmOrigin) { | 1923 if (module_->origin == kWasmOrigin) { |
| 1923 // Create the "exports" object. | 1924 // Create the "exports" object. |
| 1924 exports_object = isolate_->factory()->NewJSObjectWithNullProto(); | 1925 exports_object = isolate_->factory()->NewJSObjectWithNullProto(); |
| 1925 Handle<String> exports_name = | 1926 } else if (module_->origin == kAsmJsOrigin) { |
| 1926 isolate_->factory()->InternalizeUtf8String("exports"); | 1927 Handle<JSFunction> object_function = Handle<JSFunction>( |
| 1927 JSObject::AddProperty(instance, exports_name, exports_object, NONE); | 1928 isolate_->native_context()->object_function(), isolate_); |
| 1929 exports_object = isolate_->factory()->NewJSObject(object_function); | |
| 1930 } else { | |
| 1931 UNREACHABLE(); | |
| 1928 } | 1932 } |
| 1933 Handle<String> exports_name = | |
| 1934 isolate_->factory()->InternalizeUtf8String("exports"); | |
| 1935 JSObject::AddProperty(instance, exports_name, exports_object, NONE); | |
| 1936 | |
| 1937 Handle<String> foreign_init_name = | |
| 1938 isolate_->factory()->InternalizeUtf8String( | |
| 1939 wasm::AsmWasmBuilder::foreign_init_name); | |
| 1940 Handle<String> single_function_name = | |
| 1941 isolate_->factory()->InternalizeUtf8String( | |
| 1942 wasm::AsmWasmBuilder::single_function_name); | |
| 1929 | 1943 |
| 1930 PropertyDescriptor desc; | 1944 PropertyDescriptor desc; |
| 1931 desc.set_writable(false); | 1945 desc.set_writable(module_->origin == kAsmJsOrigin); |
| 1932 desc.set_enumerable(true); | 1946 desc.set_enumerable(true); |
| 1933 | 1947 |
| 1934 // Count up export indexes. | 1948 // Count up export indexes. |
| 1935 int export_index = 0; | 1949 int export_index = 0; |
| 1936 for (auto exp : module_->export_table) { | 1950 for (auto exp : module_->export_table) { |
| 1937 if (exp.kind == kExternalFunction) { | 1951 if (exp.kind == kExternalFunction) { |
| 1938 ++export_index; | 1952 ++export_index; |
| 1939 } | 1953 } |
| 1940 } | 1954 } |
| 1941 | 1955 |
| 1942 // Store weak references to all exported functions. | 1956 // Store weak references to all exported functions. |
| 1943 Handle<FixedArray> weak_exported_functions; | 1957 Handle<FixedArray> weak_exported_functions; |
| 1944 if (compiled_module->has_weak_exported_functions()) { | 1958 if (compiled_module->has_weak_exported_functions()) { |
| 1945 weak_exported_functions = compiled_module->weak_exported_functions(); | 1959 weak_exported_functions = compiled_module->weak_exported_functions(); |
| 1946 } else { | 1960 } else { |
| 1947 weak_exported_functions = | 1961 weak_exported_functions = |
| 1948 isolate_->factory()->NewFixedArray(export_index); | 1962 isolate_->factory()->NewFixedArray(export_index); |
| 1949 compiled_module->set_weak_exported_functions(weak_exported_functions); | 1963 compiled_module->set_weak_exported_functions(weak_exported_functions); |
| 1950 } | 1964 } |
| 1951 DCHECK_EQ(export_index, weak_exported_functions->length()); | 1965 DCHECK_EQ(export_index, weak_exported_functions->length()); |
| 1952 | 1966 |
| 1953 // Process each export in the export table (go in reverse so asm.js | 1967 // Process each export in the export table (go in reverse so asm.js |
| 1954 // can skip duplicates). | 1968 // can skip duplicates). |
| 1955 for (auto exp : base::Reversed(module_->export_table)) { | 1969 for (auto exp : base::Reversed(module_->export_table)) { |
| 1956 Handle<String> name = | 1970 Handle<String> name = |
| 1957 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( | 1971 WasmCompiledModule::ExtractUtf8StringFromModuleBytes( |
| 1958 isolate_, compiled_module_, exp.name_offset, exp.name_length) | 1972 isolate_, compiled_module_, exp.name_offset, exp.name_length) |
| 1959 .ToHandleChecked(); | 1973 .ToHandleChecked(); |
| 1974 Handle<JSObject> export_to; | |
| 1975 if (module_->origin == kAsmJsOrigin && exp.kind == kExternalFunction && | |
|
titzer
2017/01/31 00:08:13
Can you leave a TODO here? We're adding to string
| |
| 1976 (String::Equals(name, foreign_init_name) || | |
| 1977 String::Equals(name, single_function_name))) { | |
| 1978 export_to = instance; | |
| 1979 } else { | |
| 1980 export_to = exports_object; | |
| 1981 } | |
| 1982 | |
| 1960 switch (exp.kind) { | 1983 switch (exp.kind) { |
| 1961 case kExternalFunction: { | 1984 case kExternalFunction: { |
| 1962 // Wrap and export the code as a JSFunction. | 1985 // Wrap and export the code as a JSFunction. |
| 1963 WasmFunction& function = module_->functions[exp.index]; | 1986 WasmFunction& function = module_->functions[exp.index]; |
| 1964 int func_index = | 1987 int func_index = |
| 1965 static_cast<int>(module_->functions.size() + --export_index); | 1988 static_cast<int>(module_->functions.size() + --export_index); |
| 1966 Handle<JSFunction> js_function = js_wrappers_[exp.index]; | 1989 Handle<JSFunction> js_function = js_wrappers_[exp.index]; |
| 1967 if (js_function.is_null()) { | 1990 if (js_function.is_null()) { |
| 1968 // Wrap the exported code as a JSFunction. | 1991 // Wrap the exported code as a JSFunction. |
| 1969 Handle<Code> export_code = | 1992 Handle<Code> export_code = |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2046 desc.set_value(isolate_->factory()->NewNumber(num)); | 2069 desc.set_value(isolate_->factory()->NewNumber(num)); |
| 2047 break; | 2070 break; |
| 2048 } | 2071 } |
| 2049 default: | 2072 default: |
| 2050 UNREACHABLE(); | 2073 UNREACHABLE(); |
| 2051 break; | 2074 break; |
| 2052 } | 2075 } |
| 2053 | 2076 |
| 2054 // Skip duplicates for asm.js. | 2077 // Skip duplicates for asm.js. |
| 2055 if (module_->origin == kAsmJsOrigin) { | 2078 if (module_->origin == kAsmJsOrigin) { |
| 2056 v8::Maybe<bool> status = | 2079 v8::Maybe<bool> status = JSReceiver::HasOwnProperty(export_to, name); |
| 2057 JSReceiver::HasOwnProperty(exports_object, name); | |
| 2058 if (status.FromMaybe(false)) { | 2080 if (status.FromMaybe(false)) { |
| 2059 continue; | 2081 continue; |
| 2060 } | 2082 } |
| 2061 } | 2083 } |
| 2062 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( | 2084 v8::Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 2063 isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); | 2085 isolate_, export_to, name, &desc, Object::THROW_ON_ERROR); |
| 2064 if (!status.IsJust()) { | 2086 if (!status.IsJust()) { |
| 2065 thrower_->LinkError("export of %.*s failed.", name->length(), | 2087 thrower_->LinkError("export of %.*s failed.", name->length(), |
| 2066 name->ToCString().get()); | 2088 name->ToCString().get()); |
| 2067 return; | 2089 return; |
| 2068 } | 2090 } |
| 2069 } | 2091 } |
| 2070 | 2092 |
| 2071 if (module_->origin == kWasmOrigin) { | 2093 if (module_->origin == kWasmOrigin) { |
| 2072 v8::Maybe<bool> success = JSReceiver::SetIntegrityLevel( | 2094 v8::Maybe<bool> success = JSReceiver::SetIntegrityLevel( |
| 2073 exports_object, FROZEN, Object::DONT_THROW); | 2095 exports_object, FROZEN, Object::DONT_THROW); |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2793 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections); | 2815 Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections); |
| 2794 JSArray::SetContent(array_object, storage); | 2816 JSArray::SetContent(array_object, storage); |
| 2795 array_object->set_length(Smi::FromInt(num_custom_sections)); | 2817 array_object->set_length(Smi::FromInt(num_custom_sections)); |
| 2796 | 2818 |
| 2797 for (int i = 0; i < num_custom_sections; i++) { | 2819 for (int i = 0; i < num_custom_sections; i++) { |
| 2798 storage->set(i, *matching_sections[i]); | 2820 storage->set(i, *matching_sections[i]); |
| 2799 } | 2821 } |
| 2800 | 2822 |
| 2801 return array_object; | 2823 return array_object; |
| 2802 } | 2824 } |
| OLD | NEW |