CS/COE 1520
pitt.edu/~ach54/cs1520
CS/COE 1520 pitt.edu/~ach54/cs1520 WebAssembly WebAssembly - - PowerPoint PPT Presentation
CS/COE 1520 pitt.edu/~ach54/cs1520 WebAssembly WebAssembly WebAssembly is a low-level language that runs within the web browser with near-native performance for those tasks that demand that level of performance Eg. Augmented/Virtual
pitt.edu/~ach54/cs1520
web browser with near-native performance for those tasks that demand that level of performance
○
WebAssembly Community Group
2
3
WebAssembly file
JavaScript
synchronous
WebGL, etc)
4
compiled by the browser into executable machine code.
array of bytes read and written by WebAssembly’s low-level memory access instructions.
including a Memory, Table, and set of imported values.
5
○ As a compilation target. Several languages are currently supported ■ C/C++ ■ Rust ■ AssemblyScript ○ Writing WebAssembly directly
6
using clang to compile c) into WebAssembly with a JavaScript “glue” file
module as well as placing in any code needed to convert C libraries into Web APIs
○
7
○ emcc -o hello.js hello.c
HTML (hello.html)
○ emcc -o hello.html hello.c ○ The hello.html file will show a console output as well as a canvas for any WebGL content ○ You can view this content with emrun hello.html
8
○ To do that, we need to make two changes to our source file ■ Add #include <emscripten/emscripten.h> to the top of
■ Add EMSCRIPTEN_KEEPALIVE to any function we want to export to JavaScript. Place it between the function’s return type and name
EXTRA_EXPORTED_RUNTIME_METHODS=['ccall']
9
source file
○ The name of the function ○ The data type of the return value ○ An array of data types representing the C functions arguments ○ An array of values to send ot the C function
10
src="hello.js"></script> to your html file before any code that will call a C function
11
file is loaded and lets you load handwritten WebAssembly which won’t come with a “glue” file like emscripten creates.
let importObjects = { imports: { log: arg => console.log(arg) } } fetch('add.wasm') .then(response => response.arrayBuffer()) .then(buffer => WebAssembly.instantiate(buffer, importObjects)) .then(({module, instance}) => instance.exports.add(x, y));
12
we can edit with a text editor.
s-expression being a node of that tree
○ An s-expression is represented as a pair of parentheses surrounding a space separated list ○ The first item in the list is a label for that node ○ Subsequent items are either function arguments or child nodes
(i32.add (i32.const 3) (i32.const 3))
13
14