English 中文(简体)
瓦西姆法典如何在浏览器中操作,它如何与贾瓦文互动
原标题:How does Wasm code run in browsers and how does it interact with JavaScript

我很想知道网上大会(Wasm)的代码在浏览器中是如何运作的。 我的理解是,浏览器中的 Java成文法由 Java本发动机执行,但我对谁执行Wasm binary不知。 是否也是 Java本发动机?

Additionally, I d like to understand how asynchronous JavaScript functions interact with Wasm. When we load a Wasm binary through JavaScript and call an exported Wasm function, it s added to the JavaScript call stack. But what happens if the Wasm function calls an asynchronous JavaScript function inside its body? Is the callback of the asynchronous function added to the JS queue stack? Do JS and Wasm share the same call stack and event queue, or do they each have their own? If they each have their own stacks and queues, how do they coordinate execution?

让我们在瓦萨姆模块中说,我们把联合材料作为同义功能进口,把它称作我们的一项 was子功能。

(import "env" "async_js"
(func $async_js (param i32) (result i32))) 
    
(func $fnc (result i32)   
  (call $async_js (i32.const 1))
  ;;  other code here  
)

现在,当装上 was子模块时,我们便称起 f功能。

wasm.instance.exports.fnc()

So, the JS async function is called, the callback of the JS async function is added to the js queue stack and the rest of the wasm fnc function is executed, right? but how does the browser know where to continue or how to switch between wasm and js?

问题回答

网上大会目前不支持等待承诺,因此你提供的守则不会像你预期的那样发挥作用。

Async Javascript functions always return a Promise object. Your imported "async" function declared as (func $async_js (param i32) (result i32)) has an i32 return type, which means that it is not async.

Instead of WebAssembly awaiting the function call, what actually happens is the returned Javascript Promise object gets converted to an integer (which happens to be 0) when passing it to WebAssembly, and your WebAssembly function returns immediately. To be clear, the called async function still executes, and its callback is added to the queue stack as normal, but it would not "continue" into the wasm code as it was never "awaited" there. The code snippet below shows this behaviour:

void async function(){
  "use strict";
  // `binary` is the compiled version of the following WAT code:
  // (module
  //   (import "env" "asyncJs" (func $async_js (param i32) (result i32)))
  //   (func $fnc (export "fnc") (result i32)   
  //     i32.const 1
  //     call $async_js
  //   )
  // )
  const binary = new Uint32Array([1836278016,1,1610746369,2130804481,2130772064,50401026,125202021,1853453153,7555683,16909056,17237761,1668179459,134873344,1090520577,184553473,1845763584,23424353,134218256,1853453153,1936351075,1852179201,33882723,65536]).buffer;
  const module = new WebAssembly.Module(binary);
  const instance = new WebAssembly.Instance(module, {
    "env": {
      "asyncJs": async function(){
        // Wait for a second
        await new Promise(resolve => window.setTimeout(resolve, 1000));
        document.body.append(document.createTextNode(`Awaited timeout.`));
        return 1;
      }
    }
  });
  const start = new Date;
  const result = await instance.exports.fnc(); // The await here does nothing because WebAssembly functions are never async!
  const duration = new Date - start;
  document.body.append(document.createTextNode(`Result: ${result}, which took ${duration}ms.`));
}();

你们应当看到,网上大会如何立即运作,作为大会的任务决定(在此情况下是第二次会长)之后,它的作用就产生了信息。 as功能(1)的回报价值在任何地方都没有使用。

Javascript Promise Integration 网上大会的提议旨在增加功能,使进口的“合成功能”可在网上大会停止使用,直至所谓的功能解决(或拒绝)许诺。 然而,这一提案没有被任何浏览器或网络大会操作时间完全执行,并且没有在任何浏览器中进行卸载。

从现在起,从网上大会成功获取承诺价值的唯一途径是完全放弃目前的执行阶段,并在承诺下决心后从贾瓦特收到呼吁。

为了回答你的其他问题,必须指出的是,所有网络大会的职能都可以像任何其他 Java印职能一样对待:可以称之为,可以称为其他 Java(或网络大会)职能,而且它们可能具有价值。 无法确定某项职能是否执行 Java文或网上大会代码。

实际上,这意味着,当大会网站的职能被召唤时,它就象正常的呼声中增加,而且没有理由有单独的主角,因为所有网络大会的代码都同步运作。 因此,你可以认为 Java稿发动机既执行 Java稿,又执行网上大会的代码,尽管执行办法可能选择以不同方式汇编和执行网上大会的代码。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签