Thanks Clemens. This works fine but it blocks the main thread. What would be the best approach to handle this in an Android app? Would writing a custom platform help in this case?
On Wednesday, January 15, 2020 at 5:12:39 PM UTC+2, Clemens Hammacher wrote: > > Hi Darin, > > WebAssembly.instantiate works asynchronously, so you have to keep your > program running for a while to wait for the asynchronous result, and keep > pumping the message loop and running microtasks to actually execute the > compilation tasks and promise resolution tasks that get added from the > background. > > > This is probably not the most elegant solution, but it works: > [...] > script->Run(context).ToLocal(&result); > // Keep running for 10 seconds, pumping the message loop and running > // microtasks. > auto start = std::chrono::system_clock::now(); > do { > v8::platform::PumpMessageLoop(platform.get(), isolate); > isolate->RunMicrotasks(); > } while (std::chrono::duration_cast<std::chrono::seconds>( > std::chrono::system_clock::now() - start) > .count() < 10); > > Hope that helps, > Clemens > > > On Wed, Jan 15, 2020 at 9:15 AM Darin Dimitrov <darin....@gmail.com > <javascript:>> wrote: > >> I have embedded V8 (7.8.279.19) inside my Android application and tried >> executing a script that compiles a WebAssembly module. >> >> Unfortunately the promise returned by the "WebAssembly.compile" method is >> never completed (neither the "then" or the "catch" methods are invoked). >> The same happens with the "WebAssembly.instantiate" method: >> >> std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform >> (); >> v8::V8::InitializePlatform(platform.get()); >> v8::V8::Initialize(); >> >> v8::Isolate::CreateParams create_params; >> create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator:: >> NewDefaultAllocator(); >> v8::Isolate* isolate = v8::Isolate::New(create_params); >> v8::Isolate::Scope isolate_scope(isolate); >> v8::HandleScope scope(isolate); >> auto global_template = v8::ObjectTemplate::New(isolate); >> global_template->Set(v8::String::NewFromUtf8(isolate, "log", v8:: >> NewStringType::kNormal).ToLocalChecked(), v8::FunctionTemplate::New( >> isolate, [](const v8::FunctionCallbackInfo<v8::Value>& info) { >> v8::Isolate* isolate = info.GetIsolate(); >> v8::String::Utf8Value utf(isolate, info[0].As<v8::String>()); >> __android_log_print(ANDROID_LOG_DEBUG, "V8Native", "%s",*utf); >> })); >> >> v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, >> global_template); >> v8::Context::Scope context_scope(context); >> const char* csource = R"( >> let bytes = new Uint8Array([ >> 0,97,115,109,1,0,0,0,1,8,2,96,1,127,0,96,0,0,2,8,1,2,106, >> 115,1,95,0,0,3,2,1,1,8,1,1,10,9,1,7,0,65,185,10,16,0,11 >> ]); >> log('before the call'); >> WebAssembly >> .instantiate(bytes) >> .then(obj => log('success')) >> .catch(err => log('log')); >> )"; >> v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, csource, >> v8::NewStringType::kNormal).ToLocalChecked(); >> v8::Local<v8::Script> script = v8::Script::Compile(context, source). >> ToLocalChecked(); >> v8::Local<v8::Value> result; >> script->Run(context).ToLocal(&result); >> >> >> >> Do you have any idea what I might be missing here? The same javascript code >> works fine in the desktop browser. >> >> -- >> -- >> v8-users mailing list >> v8-u...@googlegroups.com <javascript:> >> http://groups.google.com/group/v8-users >> --- >> You received this message because you are subscribed to the Google Groups >> "v8-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to v8-u...@googlegroups.com <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/v8-users/4c766e82-892c-45f5-984a-aa9dd87f8138%40googlegroups.com >> >> <https://groups.google.com/d/msgid/v8-users/4c766e82-892c-45f5-984a-aa9dd87f8138%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > > Clemens Backes > > Software Engineer > > clem...@google.com <javascript:> > > > Google Germany GmbH > > Erika-Mann-Straße 33 > > 80636 München > > Geschäftsführer: Paul Manicle, Halimah DeLaine Prado > > Registergericht und -nummer: Hamburg, HRB 86891 > > Sitz der Gesellschaft: Hamburg > > Diese E-Mail ist vertraulich. Falls sie diese fälschlicherweise erhalten > haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, > löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, > dass die E-Mail an die falsche Person gesendet wurde. > > > This e-mail is confidential. If you received this communication by > mistake, please don't forward it to anyone else, please erase all copies > and attachments, and please let me know that it has gone to the wrong > person. > > -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/94c52b07-5b54-4e61-9fc2-4f2afecf2096%40googlegroups.com.