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 //! Rustc bindings to the binaryen project.
13 //! This crate is a small shim around the binaryen project which provides us the
14 //! ability to take LLVM's output and generate a wasm module. Specifically this
15 //! only supports one operation, creating a module from LLVM's assembly format
16 //! and then serializing that module to a wasm module.
21 use std
::ffi
::{CString, CStr}
;
23 /// In-memory representation of a serialized wasm module.
25 ptr
: *mut BinaryenRustModule
,
29 /// Creates a new wasm module from the LLVM-assembly provided (in a C string
32 /// The actual module creation can be tweaked through the various options in
33 /// `ModuleOptions` as well. Any errors are just returned as a bland string.
34 pub fn new(assembly
: &CStr
, opts
: &ModuleOptions
) -> Result
<Module
, String
> {
36 let ptr
= BinaryenRustModuleCreate(opts
.ptr
, assembly
.as_ptr());
38 Err(format
!("failed to create binaryen module"))
45 /// Returns the data of the serialized wasm module. This is a `foo.wasm`
47 pub fn data(&self) -> &[u8] {
49 let ptr
= BinaryenRustModulePtr(self.ptr
);
50 let len
= BinaryenRustModuleLen(self.ptr
);
51 slice
::from_raw_parts(ptr
, len
)
56 impl Drop
for Module
{
59 BinaryenRustModuleFree(self.ptr
);
64 pub struct ModuleOptions
{
65 ptr
: *mut BinaryenRustModuleOptions
,
69 pub fn new() -> ModuleOptions
{
71 let ptr
= BinaryenRustModuleOptionsCreate();
76 /// Turns on or off debug info.
78 /// From what I can tell this just creates a "names" section of the wasm
79 /// module which contains a table of the original function names.
80 pub fn debuginfo(&mut self, debug
: bool
) -> &mut Self {
82 BinaryenRustModuleOptionsSetDebugInfo(self.ptr
, debug
);
87 /// Configures a `start` function for the module, to be executed when it's
89 pub fn start(&mut self, func
: &str) -> &mut Self {
90 let func
= CString
::new(func
).unwrap();
92 BinaryenRustModuleOptionsSetStart(self.ptr
, func
.as_ptr());
97 /// Configures how much stack is initially allocated for the module. 1MB is
98 /// probably good enough for now.
99 pub fn stack(&mut self, amt
: u64) -> &mut Self {
101 BinaryenRustModuleOptionsSetStackAllocation(self.ptr
, amt
);
106 /// Flags whether the initial memory should be imported or exported. So far
107 /// we export it by default.
108 pub fn import_memory(&mut self, import
: bool
) -> &mut Self {
110 BinaryenRustModuleOptionsSetImportMemory(self.ptr
, import
);
116 impl Drop
for ModuleOptions
{
119 BinaryenRustModuleOptionsFree(self.ptr
);
124 enum BinaryenRustModule {}
125 enum BinaryenRustModuleOptions {}
128 fn BinaryenRustModuleCreate(opts
: *const BinaryenRustModuleOptions
,
129 assembly
: *const libc
::c_char
)
130 -> *mut BinaryenRustModule
;
131 fn BinaryenRustModulePtr(module
: *const BinaryenRustModule
) -> *const u8;
132 fn BinaryenRustModuleLen(module
: *const BinaryenRustModule
) -> usize;
133 fn BinaryenRustModuleFree(module
: *mut BinaryenRustModule
);
135 fn BinaryenRustModuleOptionsCreate()
136 -> *mut BinaryenRustModuleOptions
;
137 fn BinaryenRustModuleOptionsSetDebugInfo(module
: *mut BinaryenRustModuleOptions
,
139 fn BinaryenRustModuleOptionsSetStart(module
: *mut BinaryenRustModuleOptions
,
140 start
: *const libc
::c_char
);
141 fn BinaryenRustModuleOptionsSetStackAllocation(
142 module
: *mut BinaryenRustModuleOptions
,
145 fn BinaryenRustModuleOptionsSetImportMemory(
146 module
: *mut BinaryenRustModuleOptions
,
149 fn BinaryenRustModuleOptionsFree(module
: *mut BinaryenRustModuleOptions
);