]>
git.proxmox.com Git - rustc.git/blob - src/etc/wasm32-shim.js
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // This is a small "shim" program which is used when wasm32 unit tests are run
12 // in this repository. This program is intended to be run in node.js and will
13 // load a wasm module into memory, instantiate it with a set of imports, and
16 // There's a bunch of helper functions defined here in `imports.env`, but note
17 // that most of them aren't actually needed to execute most programs. Many of
18 // these are just intended for completeness or debugging. Hopefully over time
19 // nothing here is needed for completeness.
21 const fs
= require('fs');
22 const process
= require('process');
23 const buffer
= fs
.readFileSync(process
.argv
[2]);
25 Error
.stackTraceLimit
= 20;
27 let m
= new WebAssembly
.Module(buffer
);
31 function copystr(a
, b
) {
32 if (memory
=== null) {
35 let view
= new Uint8Array(memory
.buffer
).slice(a
, a
+ b
);
36 return String
.fromCharCode
.apply(null, view
);
41 // These are generated by LLVM itself for various intrinsic calls. Hopefully
42 // one day this is not necessary and something will automatically do this.
43 fmod: function(x
, y
) { return x
% y
; },
44 exp2: function(x
) { return Math
.pow(2, x
); },
45 exp2f: function(x
) { return Math
.pow(2, x
); },
46 ldexp: function(x
, y
) { return x
* Math
.pow(2, y
); },
47 ldexpf: function(x
, y
) { return x
* Math
.pow(2, y
); },
51 // These are called in src/libstd/sys/wasm/stdio.rs and are used when
52 // debugging is enabled.
53 rust_wasm_write_stdout: function(a
, b
) {
54 let s
= copystr(a
, b
);
56 process
.stdout
.write(s
);
59 rust_wasm_write_stderr: function(a
, b
) {
60 let s
= copystr(a
, b
);
62 process
.stderr
.write(s
);
66 // These are called in src/libstd/sys/wasm/args.rs and are used when
67 // debugging is enabled.
68 rust_wasm_args_count: function() {
71 return process
.argv
.length
- 2;
73 rust_wasm_args_arg_size: function(i
) {
74 return Buffer
.byteLength(process
.argv
[i
+ 2]);
76 rust_wasm_args_arg_fill: function(idx
, ptr
) {
77 let arg
= process
.argv
[idx
+ 2];
78 let view
= new Uint8Array(memory
.buffer
);
79 Buffer
.from(arg
).copy(view
, ptr
);
82 // These are called in src/libstd/sys/wasm/os.rs and are used when
83 // debugging is enabled.
84 rust_wasm_getenv_len: function(a
, b
) {
85 let key
= copystr(a
, b
);
89 if (!(key
in process
.env
)) {
92 return Buffer
.byteLength(process
.env
[key
]);
94 rust_wasm_getenv_data: function(a
, b
, ptr
) {
95 let key
= copystr(a
, b
);
96 let value
= process
.env
[key
];
97 let view
= new Uint8Array(memory
.buffer
);
98 Buffer
.from(value
).copy(view
, ptr
);
102 let module_imports
= WebAssembly
.Module
.imports(m
);
104 for (var i
= 0; i
< module_imports
.length
; i
++) {
105 let imp
= module_imports
[i
];
106 if (imp
.module
!= 'env') {
109 if (imp
.name
== 'memory' && imp
.kind
== 'memory') {
110 memory
= new WebAssembly
.Memory({initial
: 20});
111 imports
.env
.memory
= memory
;
115 let instance
= new WebAssembly
.Instance(m
, imports
);