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