MCPcopy Create free account
hub / github.com/browserify/rustify / rustify

Function rustify

lib/rustify.js:11–48  ·  view source on GitHub ↗
(filename, code, outdir, done)

Source from the content-addressed store, hash-verified

9module.exports = rustify
10
11function rustify (filename, code, outdir, done) {
12 mkdirp(outdir, function (err) {
13 if (err) return done(err)
14 var base = path.basename(filename).replace(path.extname(filename), '')
15
16 var stdout = ''
17 var stderr = ''
18
19 var rustc = spawn('rustc', [
20 '+nightly',
21 '-O',
22 '--target', 'wasm32-unknown-unknown', filename,
23 '--crate-type=cdylib',
24 '--out-dir', cachedir
25 ])
26
27 rustc.stdout.on('data', (data) => (stdout += data))
28 rustc.stderr.on('data', (data) => (stdout += data))
29 rustc.on('error', (err) => done(err))
30 rustc.on('close', function (code) {
31 if (code !== 0) return done(new Error(`process rustc exited with with code ${code}, stdout: ${stdout}, stderr: ${stderr}`))
32 var src = path.join(cachedir, base + '.wasm')
33 var dest = path.join(cachedir, base + '.min.wasm')
34
35 var wasmGc = spawn('wasm-gc', [ src, dest ])
36 wasmGc.stdout.on('data', (data) => (stdout += data))
37 wasmGc.stderr.on('data', (data) => (stdout += data))
38 wasmGc.on('error', (err) => done(err))
39 wasmGc.on('close', function (code) {
40 if (code !== 0) return done(new Error(`process wasm-gc exited with with code ${code}, stdout: ${stdout}, stderr: ${stderr}`))
41 fs.readFile(dest, function (err, buf) {
42 if (err) return done(err)
43 done(null, buf)
44 })
45 })
46 })
47 })
48}

Callers 1

handleCodeFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected