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

Unified Diff: src/wasm/wasm-js.cc

Issue 2121593002: [wasm] Compile and Instantiation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: test Created 4 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 side-by-side diff with in-line comments
Download patch
Index: src/wasm/wasm-js.cc
diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc
index d85e3da412f9d559de8e277cb6864d5da1b3771f..1401e00f68be1a84f8f990ece3bef8d6d3260c87 100644
--- a/src/wasm/wasm-js.cc
+++ b/src/wasm/wasm-js.cc
@@ -299,22 +299,37 @@ void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
}
-
static i::MaybeHandle<i::JSObject> CreateModuleObject(
v8::Isolate* isolate, const v8::Local<v8::Value> source,
ErrorThrower* thrower) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
+ i::MaybeHandle<i::JSObject> nothing;
RawBuffer buffer = GetRawBufferSource(source, thrower);
if (buffer.start == nullptr) return i::MaybeHandle<i::JSObject>();
- // TODO(rossberg): Once we can, do compilation here.
DCHECK(source->IsArrayBuffer() || source->IsTypedArray());
+ i::Zone zone(i_isolate->allocator());
+ i::wasm::ModuleResult result = i::wasm::DecodeWasmModule(
+ i_isolate, &zone, buffer.start, buffer.end, false, i::wasm::kWasmOrigin);
+ std::unique_ptr<const i::wasm::WasmModule> decoded_module(result.val);
+ if (result.failed()) {
+ thrower->Failed("", result);
+ return nothing;
+ }
+ i::MaybeHandle<i::FixedArray> compiled_module =
+ decoded_module->CompileFunctions(i_isolate);
+ if (compiled_module.is_null()) return nothing;
rossberg 2016/07/04 10:04:00 It seems like CompileFunctions eagerly raises exce
Mircea Trofin 2016/07/14 16:02:58 Done, + test
Local<Context> context = isolate->GetCurrentContext();
i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
i::Handle<i::JSFunction> module_cons(i_context->wasm_module_constructor());
+ i::Handle<i::Map> map = i_isolate->factory()->NewMap(
rossberg 2016/07/04 10:04:00 It isn't desirable to create a new map creation fo
Mircea Trofin 2016/07/14 16:02:58 Done.
+ i::JS_OBJECT_TYPE, i::JSObject::kHeaderSize + i::kPointerSize);
+ module_cons->set_prototype_or_initial_map(*map);
+ map->SetConstructor(*module_cons);
i::Handle<i::JSObject> module_obj =
i_isolate->factory()->NewJSObject(module_cons);
+ module_obj->SetInternalField(0, *compiled_module.ToHandleChecked());
i::Handle<i::Object> module_ref = Utils::OpenHandle(*source);
i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
i::Object::SetProperty(module_obj, module_sym, module_ref, i::STRICT).Check();
@@ -366,25 +381,47 @@ void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope scope(args.GetIsolate());
v8::Isolate* isolate = args.GetIsolate();
- ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
- "WebAssembly.Instance()");
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
- if (args.Length() < 1) {
+ ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
+
+ if (args.Length() < 1 || !args[0]->IsObject()) {
rossberg 2016/07/04 10:04:00 This check is not sufficient (nor necessary). You
Mircea Trofin 2016/07/05 06:02:42 Done.
rossberg 2016/07/14 13:30:28 Hm, I still don't see the code that addresses this
Mircea Trofin 2016/07/14 16:02:58 Done (hopefully this time I won't delete my own pa
thrower.Error("Argument 0 must be a WebAssembly.Module");
return;
}
- Local<Context> context = isolate->GetCurrentContext();
- i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
- i::Handle<i::Symbol> module_sym(i_context->wasm_module_sym());
- i::MaybeHandle<i::Object> source =
- i::Object::GetProperty(Utils::OpenHandle(*args[0]), module_sym);
- if (source.is_null()) return;
+ Local<Object> obj = Local<Object>::Cast(args[0]);
- RawBuffer buffer =
- GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower);
- if (buffer.start == nullptr) return;
+ i::Handle<i::JSObject> module_obj =
+ i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj));
+ if (module_obj->GetInternalFieldCount() < 1 ||
+ !module_obj->GetInternalField(0)->IsFixedArray()) {
+ thrower.Error("Argument 0 is an invalid WebAssembly.Module");
+ return;
+ }
- InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
+ i::Handle<i::FixedArray> compiled_code = i::Handle<i::FixedArray>(
+ i::FixedArray::cast(module_obj->GetInternalField(0)));
+
+ i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null();
+ if (args.Length() > 1 && args[1]->IsObject()) {
+ Local<Object> obj = Local<Object>::Cast(args[1]);
+ ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj));
+ }
+
+ i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
+ if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
+ Local<Object> obj = Local<Object>::Cast(args[2]);
+ i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
+ memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
+ }
+ i::MaybeHandle<i::JSObject> instance =
+ i::wasm::WasmModule::Instantiate(i_isolate, compiled_code, ffi, memory);
+ if (instance.is_null()) {
+ thrower.Error("Could not instantiate module");
+ return;
+ }
+ v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
+ return_value.Set(Utils::ToLocal(instance.ToHandleChecked()));
}
} // namespace
« no previous file with comments | « no previous file | test/mjsunit/wasm/instantiate-module-basic.js » ('j') | test/mjsunit/wasm/instantiate-module-basic.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698