You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/webassembly/c_to_wasm/index.md
+59-60Lines changed: 59 additions & 60 deletions
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@ tags:
9
9
- WebAssembly
10
10
- wasm
11
11
---
12
+
12
13
{{WebAssemblySidebar}}
13
14
14
15
When you've written a new code module in a language like C/C++, you can compile it into WebAssembly using a tool like [Emscripten](https://emscripten.org/). Let's look at how it works.
@@ -36,20 +37,20 @@ This is the simplest case we'll look at, whereby you get emscripten to generate
36
37
37
38
1. First we need an example to compile. Take a copy of the following simple C example, and save it in a file called `hello.c` in a new directory on your local drive:
38
39
39
-
```cpp
40
-
#include<stdio.h>
40
+
```cpp
41
+
#include<stdio.h>
41
42
42
-
intmain() {
43
-
printf("Hello World\n");
44
-
return 0;
45
-
}
46
-
```
43
+
intmain() {
44
+
printf("Hello World\n");
45
+
return 0;
46
+
}
47
+
```
47
48
48
49
2. Now, using the terminal window you used to enter the Emscripten compiler environment, navigate to the same directory as your `hello.c` file, and run the following command:
49
50
50
-
```bash
51
-
emcc hello.c -o hello.html
52
-
```
51
+
```bash
52
+
emcc hello.c -o hello.html
53
+
```
53
54
54
55
The options we've passed in with the command are as follows:
55
56
@@ -76,26 +77,26 @@ Sometimes you will want to use a custom HTML template. Let's look at how we can
76
77
77
78
1. First of all, save the following C code in a file called `hello2.c`, in a new directory:
78
79
79
-
```cpp
80
-
#include <stdio.h>
80
+
```cpp
81
+
#include<stdio.h>
81
82
82
-
int main() {
83
-
printf("Hello World\n");
84
-
return 0;
85
-
}
86
-
```
83
+
intmain() {
84
+
printf("Hello World\n");
85
+
return 0;
86
+
}
87
+
```
87
88
88
89
2. Search for the file `shell_minimal.html` in your emsdk repo. Copy it into a subdirectory called `html_template` inside your previous new directory.
89
90
3. Now navigate into your new directory (again, in your Emscripten compiler environment terminal window), and run the following command:
The options we've passed are slightly different this time:
96
+
The options we've passed are slightly different this time:
96
97
97
-
- We've specified `-o hello2.html`, meaning that the compiler will still output the JavaScript glue code and `.html`.
98
-
- We've also specified `--shell-file html_template/shell_minimal.html` — this provides the path to the HTML template you want to use to create the HTML you will run your example through.
98
+
- We've specified `-o hello2.html`, meaning that the compiler will still output the JavaScript glue code and `.html`.
99
+
- We've also specified `--shell-file html_template/shell_minimal.html` — this provides the path to the HTML template you want to use to create the HTML you will run your example through.
99
100
100
101
4. Now let's run this example. The above command will have generated `hello2.html`, which will have much the same content as the template with some glue code added into load the generated wasm, run it, etc. Open it in your browser and you'll see much the same output as the last example.
101
102
@@ -109,60 +110,58 @@ If you have a function defined in your C code that you want to call as needed fr
109
110
110
111
1. To start with, save the following code as `hello3.c` in a new directory:
By default, Emscripten-generated code always just calls the `main()` function, and other functions are eliminated as dead code. Putting `EMSCRIPTEN_KEEPALIVE` before a functionname stops this from happening. You also need to import the `emscripten.h` library to use `EMSCRIPTEN_KEEPALIVE`.
133
+
By default, Emscripten-generated code always just calls the `main()` function, and other functions are eliminated as dead code. Putting `EMSCRIPTEN_KEEPALIVE` before a function name stops this from happening. You also need to import the `emscripten.h` library to use `EMSCRIPTEN_KEEPALIVE`.
133
134
134
-
>**Note:** We are including the `#ifdef` blocks so that if you are trying to include this in C++ code, the example will still work. Due to C versus C++ name mangling rules, this would otherwise break, but here we are setting it so that it treats it as an external C function if you are using C++.
135
+
> **Note:** We are including the `#ifdef` blocks so that if you are trying to include this in C++ code, the example will still work. Due to C versus C++ name mangling rules, this would otherwise break, but here we are setting it so that it treats it as an external C function if you are using C++.
135
136
136
137
2. Now add `html_template/shell_minimal.html` with `\{\{{ SCRIPT }}}` as content into this new directory too, just for convenience (you'd obviously put this in a central place in your real dev environment).
137
138
3. Now let's run the compilation step again. From inside your latest directory (and while inside your Emscripten compiler environment terminal window), compile your C code with the following command. (Note that we need to compile with `NO_EXIT_RUNTIME`, which is necessary as otherwise when `main()` exits the runtime would be shut down — necessary for proper C emulation, e.g., atexits are called — and it wouldn't be valid to call compiled code.)
Copy file name to clipboardExpand all lines: files/en-us/webassembly/caching_modules/index.md
+47-40Lines changed: 47 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ tags:
10
10
- compile
11
11
- wasm
12
12
---
13
+
13
14
{{WebAssemblySidebar}}
14
15
15
16
> **Warning:** Experimental {{jsxref("WebAssembly.Module")}} IndexedDB serialization support is being removed from browsers; see {{bug("1469395")}} and [this spec issue](https://github.com/WebAssembly/spec/issues/821).
@@ -43,56 +44,62 @@ function instantiateCachedURL(dbVersion, url, importObject) {
43
44
The first helper function contained inside `instantiateCachedURL()` — `openDatabase()` — creates an object store for storing wasm modules, and also handles clearing out the database if the `dbVersion` is updated; it returns a promise resolving to the new database.
console.log(`Clearing out version ${event.oldVersion} wasm cache`);
58
+
db.deleteObjectStore(storeName);
59
+
}
60
+
console.log(`Creating version ${event.newVersion} wasm cache`);
61
+
db.createObjectStore(storeName);
62
+
};
63
+
});
64
+
}
62
65
```
63
66
64
67
### Looking up modules in the database
65
68
66
69
Our next function — `lookupInDatabase()` — provides a simple promise-based operation for looking up the given `url` in the object store we created above. It resolves with the stored compiled module, or rejects with an error.
Copy file name to clipboardExpand all lines: files/en-us/webassembly/concepts/index.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ tags:
13
13
- text format
14
14
- web platform
15
15
---
16
+
16
17
{{WebAssemblySidebar}}
17
18
18
19
This article explains the concepts behind how WebAssembly works including its goals, the problems it solves, and how it runs inside the web browser's rendering engine.
Copy file name to clipboardExpand all lines: files/en-us/webassembly/existing_c_to_wasm/index.md
+18-13Lines changed: 18 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ tags:
8
8
- WebAssembly
9
9
- wasm
10
10
---
11
+
11
12
{{WebAssemblySidebar}}
12
13
13
14
A core use-case for WebAssembly is to take the existing ecosystem of C libraries and allow developers to use them on the web.
@@ -52,7 +53,7 @@ Now you only need some HTML and JavaScript to load your new module:
52
53
<script>
53
54
Module.onRuntimeInitialized=async () => {
54
55
constapi= {
55
-
version:Module.cwrap('version', 'number', []),
56
+
version:Module.cwrap("version", "number", []),
56
57
};
57
58
console.log(api.version());
58
59
};
@@ -72,16 +73,16 @@ Getting the encoder's version number is great, but encoding an actual image woul
72
73
The first question you need to answer is: how do I get the image into wasm? Looking at the [encoding API of libwebp](https://developers.google.com/speed/webp/docs/api#simple_encoding_api), you'll find that it expects an array of bytes in RGB, RGBA, BGR or BGRA. Luckily, the Canvas API has {{domxref("CanvasRenderingContext2D.getImageData")}} — that gives you an {{jsxref("Uint8ClampedArray")}} containing the image data in RGBA:
const p = api.create_buffer(image.width, image.height);
121
122
Module.HEAP8.set(image.data, p);
122
123
// ... call encoder ...
@@ -164,7 +165,11 @@ Now with all of that in place, you can call the encoding function, grab the poin
164
165
api.encode(p, image.width, image.height, 100);
165
166
const resultPointer = api.get_result_pointer();
166
167
const resultSize = api.get_result_size();
167
-
const resultView = new Uint8Array(Module.HEAP8.buffer, resultPointer, resultSize);
168
+
const resultView = new Uint8Array(
169
+
Module.HEAP8.buffer,
170
+
resultPointer,
171
+
resultSize
172
+
);
168
173
const result = new Uint8Array(resultView);
169
174
api.free_result(resultPointer);
170
175
```
@@ -180,11 +185,11 @@ Luckily, the solution to this problem is in the error message. You just need to
180
185
And there you have it. You have compiled a WebP encoder and transcoded a JPEG image to WebP. To prove that it worked, turn your result buffer into a blob and use it on an `<img>` element:
0 commit comments