Skip to content

Commit

Permalink
fix external image flaws in WebAssembly (#3033)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Mar 11, 2021
1 parent 08484b3 commit 89127d8
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 9 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion files/en-us/webassembly/concepts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h3 id="Porting_from_CC">Porting from C/C++</h3>

<p>The Emscripten tool is able to take just about any C/C++ source code and compile it into a .wasm module, plus the necessary JavaScript "glue" code for loading and running the module, and an HTML document to display the results of the code.</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/14647/emscripten-diagram.png" style="display: block; height: 104px; margin: 0px auto; width: 764px;"></p>
<p><img alt="" src="emscripten-diagram.png" style="display: block; margin: 0px auto;"></p>

<p>In a nutshell, the process works as follows:</p>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions files/en-us/webassembly/existing_c_to_wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<p>And you will see the correct version number in the <a href="https://googlechrome.github.io/samples/webassembly/version.html">output</a>:</p>

<p><img alt=" Screenshot of the DevTools console showing the correct versionnumber." src="https://mdn.mozillademos.org/files/15913/version.png" style="border-style: solid; border-width: 1px;"></p>
<p><img alt=" Screenshot of the DevTools console showing the correct versionnumber." src="version.png" style="border-style: solid; border-width: 1px;"></p>

<div class="note">
<p><strong>Note:</strong> libwebp returns the current version a.b.c as a hexadecimal number 0xabc. For example, v0.6.1 is encoded as 0x000601 = 1537.</p>
Expand Down Expand Up @@ -158,7 +158,7 @@ <h3 id="Encode_the_Image">Encode the Image</h3>
<p>Depending on the size of your image, you might run into an error where wasm can't grow the memory enough to accommodate both the input and the output image:</p>

<p><img alt="
Screenshot of the DevTools console showing an error." src="https://mdn.mozillademos.org/files/15922/error.png"></p>
Screenshot of the DevTools console showing an error." src="error.png"></p>

<p>Luckily, the solution to this problem is in the error message. You just need to add <code>-s ALLOW_MEMORY_GROWTH=1</code> to your compilation command.</p>

Expand All @@ -175,4 +175,4 @@ <h3 id="Encode_the_Image">Encode the Image</h3>

<p><a href="https://googlechrome.github.io/samples/webassembly/image.html">Demo</a> | <a href="https://developers.google.com/web/updates/2018/03/emscripting-a-c-library">Original Article</a></p>

<p><img alt="DevTools network panel and the generated image." src="https://mdn.mozillademos.org/files/15914/result.jpg" style="border-style: solid; border-width: 1px;"></p>
<p><img alt="DevTools network panel and the generated image." src="result.jpg" style="border-style: solid; border-width: 1px;"></p>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion files/en-us/webassembly/text_format_to_wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h2 id="Viewing_the_assembly_output">Viewing the assembly output</h2>

<p>This will give you an output in your terminal in the following way:</p>

<p><img alt="several strings of binary with textual descriptions beside them. For example: 0000008: 01 ; section code " src="https://mdn.mozillademos.org/files/14653/assembly-output.png" style="display: block; margin: 0px auto;"></p>
<p><img alt="several strings of binary with textual descriptions beside them. For example: 0000008: 01 ; section code " src="assembly-output.png" style="display: block; margin: 0px auto;"></p>

<h2 id="See_also">See also</h2>

Expand Down
8 changes: 4 additions & 4 deletions files/en-us/webassembly/using_the_javascript_api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3 id="Preparing_the_example">Preparing the example</h3>
<ol>
<li>First we need a wasm module! Grab our <a href="https://github.com/mdn/webassembly-examples/raw/master/js-api-examples/simple.wasm">simple.wasm</a> file and save a copy in a new directory on your local machine.</li>
<li>Next, let’s create a simple HTML file called <code>index.html</code> in the same directory as your wasm file (can use our <a href="https://github.com/mdn/webassembly-examples/blob/master/template/template.html">simple template</a> if you haven’t got one easily available).</li>
<li>Now, to help us understand what is going on here, let’s look at the text representation of our wasm module (which we also meet in <a href="/en-US/docs/WebAssembly/Text_format_to_wasm#A_first_look_at_the_text_format">Converting WebAssembly format to wasm</a>):
<li>Now, to help us understand what is going on here, let’s look at the text representation of our wasm module (which we also meet in <a href="/en-US/docs/WebAssembly/Text_format_to_wasm#a_first_look_at_the_text_format">Converting WebAssembly format to wasm</a>):
<pre class="brush: js">(module
(func $i (import "imports" "imported_func") (param i32))
(func (export "exported_func")
Expand Down Expand Up @@ -83,15 +83,15 @@ <h3 id="Viewing_wasm_in_developer_tools">Viewing wasm in developer tools</h3>

<p>In Firefox 54+, the Developer Tool Debugger Panel has functionality to expose the text representation of any wasm code included in a web page. To view it, you can go to the Debugger Panel and click on the “wasm://” entry.</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/15823/wasm-debug.png" style="display: block; height: 317px; margin: 0px auto; width: 1019px;"></p>
<p><img alt="" src="wasm-debug.png" style="display: block; margin: 0px auto;"></p>

<p>In addition to viewing WebAssembly as text, developers are able to debug (place breakpoints, inspect the callstack, single-step, etc.) WebAssembly using the text format. See <a href="https://www.youtube.com/watch?v=R1WtBkMeGds">WebAssembly debugging with Firefox DevTools</a> for a video preview.</p>

<h2 id="Memory">Memory</h2>

<p>In the low-level memory model of WebAssembly, memory is represented as a contiguous range of untyped bytes called <a href="https://webassembly.org/docs/semantics/#linear-memory">Linear Memory</a> that are read and written by <a href="https://webassembly.org/docs/semantics/#linear-memory-accesses">load and store instructions</a> inside the module.  In this memory model, any load or store can access any byte in the entire linear memory, which is necessary to faithfully represent C/C++ concepts like pointers.</p>

<p>Unlike a native C/C++ program, however, where the available memory range spans the entire process, the memory accessible by a particular WebAssembly Instance is confined to one specific — potentially very small — range contained by a WebAssembly Memory object.  This allows a single web app to use multiple independent libraries — each of which are using WebAssembly internally — to have separate memories that are fully isolated from each other. In addition, newer implementations can also create <a href="/en-US/docs/WebAssembly/Understanding_the_text_format#Shared_memories">shared memories</a>, which can be transferred between Window and Worker contexts using <code><a href="/en-US/docs/Web/API/Window/postMessage">postMessage()</a></code>, and used in multiple places.</p>
<p>Unlike a native C/C++ program, however, where the available memory range spans the entire process, the memory accessible by a particular WebAssembly Instance is confined to one specific — potentially very small — range contained by a WebAssembly Memory object.  This allows a single web app to use multiple independent libraries — each of which are using WebAssembly internally — to have separate memories that are fully isolated from each other. In addition, newer implementations can also create <a href="/en-US/docs/WebAssembly/Understanding_the_text_format#shared_memories">shared memories</a>, which can be transferred between Window and Worker contexts using <code><a href="/en-US/docs/Web/API/Window/postMessage">postMessage()</a></code>, and used in multiple places.</p>

<p>In JavaScript, a Memory instance can be thought of as a resizable <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">ArrayBuffer</a></code> (or <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer">SharedArrayBuffer</a></code>, in the case of shared memories) and, just as with <code>ArrayBuffers</code>, a single web app can create many independent Memory objects.  You can create one using the {{jsxref("WebAssembly.Memory()")}} constructor, which takes as arguments an initial size and (optionally) a maximum size and a <code>shared</code> property that states whether it is a shared memory or not.</p>

Expand Down Expand Up @@ -292,7 +292,7 @@ <h2 id="Multiplicity">Multiplicity</h2>
<li>One memory or table instance can be used by 0–N module instances — these instances all share the same address space, allowing <a href="https://webassembly.org/docs/dynamic-linking">dynamic linking</a>.</li>
</ul>

<p>You can see multiplicity in action in our Understanding text format article — see the <a href="/en-US/docs/WebAssembly/Understanding_the_text_format#Mutating_tables_and_dynamic_linking">Mutating tables and dynamic linking section</a>.</p>
<p>You can see multiplicity in action in our Understanding text format article — see the <a href="/en-US/docs/WebAssembly/Understanding_the_text_format#mutating_tables_and_dynamic_linking">Mutating tables and dynamic linking section</a>.</p>

<h2 id="Summary">Summary</h2>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 89127d8

Please sign in to comment.