| 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 "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-typer.h" | 8 #include "src/asmjs/asm-typer.h" |
| 8 #include "src/asmjs/asm-wasm-builder.h" | 9 #include "src/asmjs/asm-wasm-builder.h" |
| 9 #include "src/assert-scope.h" | 10 #include "src/assert-scope.h" |
| 10 #include "src/ast/ast.h" | 11 #include "src/ast/ast.h" |
| 11 #include "src/ast/scopes.h" | 12 #include "src/ast/scopes.h" |
| 12 #include "src/execution.h" | 13 #include "src/execution.h" |
| 13 #include "src/factory.h" | 14 #include "src/factory.h" |
| 14 #include "src/handles.h" | 15 #include "src/handles.h" |
| 15 #include "src/isolate.h" | 16 #include "src/isolate.h" |
| 16 #include "src/objects.h" | 17 #include "src/objects.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 buffer.start, buffer.end); | 118 buffer.start, buffer.end); |
| 118 } | 119 } |
| 119 | 120 |
| 120 if (result.failed()) { | 121 if (result.failed()) { |
| 121 thrower.Failed("", result); | 122 thrower.Failed("", result); |
| 122 } | 123 } |
| 123 | 124 |
| 124 if (result.val) delete result.val; | 125 if (result.val) delete result.val; |
| 125 } | 126 } |
| 126 | 127 |
| 127 v8::internal::wasm::ZoneBuffer* TranslateAsmModule( | 128 i::MaybeHandle<i::FixedArray> TranslateAsmModule(i::ParseInfo* info, |
| 128 i::ParseInfo* info, ErrorThrower* thrower, | 129 ErrorThrower* thrower) { |
| 129 i::Handle<i::FixedArray>* foreign_args) { | |
| 130 info->set_global(); | 130 info->set_global(); |
| 131 info->set_lazy(false); | 131 info->set_lazy(false); |
| 132 info->set_allow_lazy_parsing(false); | 132 info->set_allow_lazy_parsing(false); |
| 133 info->set_toplevel(true); | 133 info->set_toplevel(true); |
| 134 | 134 |
| 135 if (!i::Compiler::ParseAndAnalyze(info)) { | 135 if (!i::Compiler::ParseAndAnalyze(info)) { |
| 136 return nullptr; | 136 return i::MaybeHandle<i::FixedArray>(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 if (info->scope()->declarations()->length() == 0) { | 139 if (info->scope()->declarations()->length() == 0) { |
| 140 thrower->Error("Asm.js validation failed: no declarations in scope"); | 140 thrower->Error("Asm.js validation failed: no declarations in scope"); |
| 141 return nullptr; | 141 return i::MaybeHandle<i::FixedArray>(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (!info->scope()->declarations()->at(0)->IsFunctionDeclaration()) { | 144 if (!info->scope()->declarations()->at(0)->IsFunctionDeclaration()) { |
| 145 thrower->Error("Asm.js validation failed: non-function declaration"); | 145 thrower->Error("Asm.js validation failed: non-function declaration"); |
| 146 return nullptr; | 146 return i::MaybeHandle<i::FixedArray>(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 info->set_literal( | 149 info->set_literal( |
| 150 info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); | 150 info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun()); |
| 151 | 151 |
| 152 v8::internal::wasm::AsmTyper typer(info->isolate(), info->zone(), | 152 return i::AsmJs::ConvertAsmToWasm(info); |
| 153 *(info->script()), info->literal()); | |
| 154 if (!typer.Validate()) { | |
| 155 thrower->Error("Asm.js validation failed: %s", typer.error_message()); | |
| 156 return nullptr; | |
| 157 } | |
| 158 | |
| 159 v8::internal::wasm::AsmWasmBuilder builder(info->isolate(), info->zone(), | |
| 160 info->literal(), &typer); | |
| 161 | |
| 162 return builder.Run(foreign_args); | |
| 163 } | 153 } |
| 164 | 154 |
| 165 i::MaybeHandle<i::JSObject> InstantiateModuleCommon( | 155 i::MaybeHandle<i::JSObject> InstantiateModule( |
| 166 const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start, | 156 const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start, |
| 167 const byte* end, ErrorThrower* thrower, | 157 const byte* end, ErrorThrower* thrower, |
| 168 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { | 158 internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) { |
| 169 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 159 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
| 170 | 160 |
| 171 // Decode but avoid a redundant pass over function bodies for verification. | 161 // Decode but avoid a redundant pass over function bodies for verification. |
| 172 // Verification will happen during compilation. | 162 // Verification will happen during compilation. |
| 173 i::Zone zone(isolate->allocator()); | 163 i::Zone zone(isolate->allocator()); |
| 174 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( | 164 internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( |
| 175 isolate, &zone, start, end, false, origin); | 165 isolate, &zone, start, end, false, origin); |
| 176 | 166 |
| 177 i::MaybeHandle<i::JSObject> object; | 167 i::MaybeHandle<i::JSObject> object; |
| 178 if (result.failed() && origin == internal::wasm::kAsmJsOrigin) { | 168 if (result.failed()) { |
| 179 thrower->Error("Asm.js converted module failed to decode"); | |
| 180 } else if (result.failed()) { | |
| 181 thrower->Failed("", result); | 169 thrower->Failed("", result); |
| 182 } else { | 170 } else { |
| 183 // Success. Instantiate the module and return the object. | 171 // Success. Instantiate the module and return the object. |
| 184 i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null(); | 172 i::Handle<i::JSObject> ffi = i::Handle<i::JSObject>::null(); |
| 185 if (args.Length() > 1 && args[1]->IsObject()) { | 173 if (args.Length() > 1 && args[1]->IsObject()) { |
| 186 Local<Object> obj = Local<Object>::Cast(args[1]); | 174 Local<Object> obj = Local<Object>::Cast(args[1]); |
| 187 ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); | 175 ffi = i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj)); |
| 188 } | 176 } |
| 189 | 177 |
| 190 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); | 178 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); |
| 191 if (args.Length() > 2 && args[2]->IsArrayBuffer()) { | 179 if (args.Length() > 2 && args[2]->IsArrayBuffer()) { |
| 192 Local<Object> obj = Local<Object>::Cast(args[2]); | 180 Local<Object> obj = Local<Object>::Cast(args[2]); |
| 193 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); | 181 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); |
| 194 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); | 182 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); |
| 195 } | 183 } |
| 196 | 184 |
| 197 i::MaybeHandle<i::FixedArray> compiled_module = | 185 i::MaybeHandle<i::FixedArray> compiled_module = |
| (...skipping 21 matching lines...) Expand all Loading... |
| 219 thrower.Error("Asm module text should be a string"); | 207 thrower.Error("Asm module text should be a string"); |
| 220 return; | 208 return; |
| 221 } | 209 } |
| 222 | 210 |
| 223 i::Factory* factory = isolate->factory(); | 211 i::Factory* factory = isolate->factory(); |
| 224 i::Zone zone(isolate->allocator()); | 212 i::Zone zone(isolate->allocator()); |
| 225 Local<String> source = Local<String>::Cast(args[0]); | 213 Local<String> source = Local<String>::Cast(args[0]); |
| 226 i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source)); | 214 i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source)); |
| 227 i::ParseInfo info(&zone, script); | 215 i::ParseInfo info(&zone, script); |
| 228 | 216 |
| 229 i::Handle<i::Object> foreign; | 217 auto wasm_data = TranslateAsmModule(&info, &thrower); |
| 230 if (args.Length() > 1 && args[1]->IsObject()) { | 218 if (wasm_data.is_null()) { |
| 231 Local<Object> local_foreign = Local<Object>::Cast(args[1]); | 219 thrower.Error("asm.js failed to validate"); |
| 232 foreign = v8::Utils::OpenHandle(*local_foreign); | |
| 233 } | |
| 234 | |
| 235 i::Handle<i::FixedArray> foreign_args; | |
| 236 auto module = TranslateAsmModule(&info, &thrower, &foreign_args); | |
| 237 if (module == nullptr) { | |
| 238 return; | 220 return; |
| 239 } | 221 } |
| 240 | 222 |
| 241 i::MaybeHandle<i::Object> maybe_module_object = | 223 i::Handle<i::JSReceiver> stdlib; |
| 242 InstantiateModuleCommon(args, module->begin(), module->end(), &thrower, | 224 if (args.Length() > 1 && args[1]->IsObject()) { |
| 243 internal::wasm::kAsmJsOrigin); | 225 Local<Object> obj = Local<Object>::Cast(args[1]); |
| 226 i::Handle<i::Object> hobj = |
| 227 i::Handle<i::Object>::cast(v8::Utils::OpenHandle(*obj)); |
| 228 if (hobj->IsJSReceiver()) { |
| 229 stdlib = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); |
| 230 } |
| 231 } |
| 232 |
| 233 i::Handle<i::JSReceiver> foreign; |
| 234 if (args.Length() > 2 && args[2]->IsObject()) { |
| 235 Local<Object> obj = Local<Object>::Cast(args[2]); |
| 236 i::Handle<i::Object> hobj = |
| 237 i::Handle<i::Object>::cast(v8::Utils::OpenHandle(*obj)); |
| 238 if (hobj->IsJSReceiver()) { |
| 239 foreign = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj)); |
| 240 } |
| 241 } |
| 242 |
| 243 i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); |
| 244 if (args.Length() > 3 && args[3]->IsArrayBuffer()) { |
| 245 Local<Object> obj = Local<Object>::Cast(args[3]); |
| 246 i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); |
| 247 memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); |
| 248 } |
| 249 |
| 250 if (!i::AsmJs::IsStdlibValid(isolate, wasm_data.ToHandleChecked(), stdlib)) { |
| 251 thrower.Error("Asm module uses missing stdlib function"); |
| 252 return; |
| 253 } |
| 254 |
| 255 i::MaybeHandle<i::Object> maybe_module_object = i::AsmJs::InstantiateAsmWasm( |
| 256 isolate, wasm_data.ToHandleChecked(), memory, foreign); |
| 244 if (maybe_module_object.is_null()) { | 257 if (maybe_module_object.is_null()) { |
| 245 return; | 258 return; |
| 246 } | 259 } |
| 247 | 260 |
| 248 i::Handle<i::Name> name = | 261 args.GetReturnValue().Set( |
| 249 factory->NewStringFromStaticChars("__foreign_init__"); | 262 v8::Utils::ToLocal(maybe_module_object.ToHandleChecked())); |
| 250 | |
| 251 i::Handle<i::Object> module_object = maybe_module_object.ToHandleChecked(); | |
| 252 i::MaybeHandle<i::Object> maybe_init = | |
| 253 i::Object::GetProperty(module_object, name); | |
| 254 DCHECK(!maybe_init.is_null()); | |
| 255 | |
| 256 i::Handle<i::Object> init = maybe_init.ToHandleChecked(); | |
| 257 i::Handle<i::Object> undefined = isolate->factory()->undefined_value(); | |
| 258 i::Handle<i::Object>* foreign_args_array = | |
| 259 new i::Handle<i::Object>[foreign_args->length()]; | |
| 260 for (int j = 0; j < foreign_args->length(); j++) { | |
| 261 if (!foreign.is_null()) { | |
| 262 i::MaybeHandle<i::Name> name = i::Object::ToName( | |
| 263 isolate, i::Handle<i::Object>(foreign_args->get(j), isolate)); | |
| 264 if (!name.is_null()) { | |
| 265 i::MaybeHandle<i::Object> val = | |
| 266 i::Object::GetProperty(foreign, name.ToHandleChecked()); | |
| 267 if (!val.is_null()) { | |
| 268 foreign_args_array[j] = val.ToHandleChecked(); | |
| 269 continue; | |
| 270 } | |
| 271 } | |
| 272 } | |
| 273 foreign_args_array[j] = undefined; | |
| 274 } | |
| 275 i::MaybeHandle<i::Object> retval = i::Execution::Call( | |
| 276 isolate, init, undefined, foreign_args->length(), foreign_args_array); | |
| 277 delete[] foreign_args_array; | |
| 278 | |
| 279 if (retval.is_null()) { | |
| 280 thrower.Error( | |
| 281 "WASM.instantiateModuleFromAsm(): foreign init function failed"); | |
| 282 } | |
| 283 } | 263 } |
| 284 | 264 |
| 285 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { | 265 void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 286 HandleScope scope(args.GetIsolate()); | 266 HandleScope scope(args.GetIsolate()); |
| 287 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); | 267 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate()); |
| 288 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); | 268 ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); |
| 289 | 269 |
| 290 if (args.Length() < 1) { | 270 if (args.Length() < 1) { |
| 291 thrower.Error("Argument 0 must be a buffer source"); | 271 thrower.Error("Argument 0 must be a buffer source"); |
| 292 return; | 272 return; |
| 293 } | 273 } |
| 294 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); | 274 RawBuffer buffer = GetRawBufferSource(args[0], &thrower); |
| 295 if (buffer.start == nullptr) return; | 275 if (buffer.start == nullptr) return; |
| 296 | 276 |
| 297 InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower); | 277 InstantiateModule(args, buffer.start, buffer.end, &thrower); |
| 298 } | 278 } |
| 299 | 279 |
| 300 static i::MaybeHandle<i::JSObject> CreateModuleObject( | 280 static i::MaybeHandle<i::JSObject> CreateModuleObject( |
| 301 v8::Isolate* isolate, const v8::Local<v8::Value> source, | 281 v8::Isolate* isolate, const v8::Local<v8::Value> source, |
| 302 ErrorThrower* thrower) { | 282 ErrorThrower* thrower) { |
| 303 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 283 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 304 i::MaybeHandle<i::JSObject> nothing; | 284 i::MaybeHandle<i::JSObject> nothing; |
| 305 | 285 |
| 306 RawBuffer buffer = GetRawBufferSource(source, thrower); | 286 RawBuffer buffer = GetRawBufferSource(source, thrower); |
| 307 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); | 287 if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>(); |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 int unused_property_fields = in_object_properties - pre_allocated; | 530 int unused_property_fields = in_object_properties - pre_allocated; |
| 551 Handle<Map> map = Map::CopyInitialMap( | 531 Handle<Map> map = Map::CopyInitialMap( |
| 552 prev_map, instance_size, in_object_properties, unused_property_fields); | 532 prev_map, instance_size, in_object_properties, unused_property_fields); |
| 553 | 533 |
| 554 context->set_wasm_function_map(*map); | 534 context->set_wasm_function_map(*map); |
| 555 } | 535 } |
| 556 } | 536 } |
| 557 | 537 |
| 558 } // namespace internal | 538 } // namespace internal |
| 559 } // namespace v8 | 539 } // namespace v8 |
| OLD | NEW |