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 C API inserted on top of the Binaryen C++ API which we use
12 // from Rust. Once we have a real linker for we'll be able to remove all this,
13 // and otherwise this is just all on a "as we need it" basis for now.
20 #include "wasm-binary.h"
21 #include "wasm-linker.h"
25 struct BinaryenRustModule {
26 BufferWithRandomAccess buffer;
29 struct BinaryenRustModuleOptions {
32 uint64_t stackAllocation;
36 bool ignoreUnknownSymbols;
38 std::string startFunction;
40 BinaryenRustModuleOptions() :
47 ignoreUnknownSymbols(false),
54 extern "C" BinaryenRustModuleOptions*
55 BinaryenRustModuleOptionsCreate() {
56 return new BinaryenRustModuleOptions;
60 BinaryenRustModuleOptionsFree(BinaryenRustModuleOptions *options) {
65 BinaryenRustModuleOptionsSetDebugInfo(BinaryenRustModuleOptions *options,
67 options->debugInfo = debugInfo;
71 BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
73 options->startFunction = start;
77 BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
79 options->stackAllocation = stack;
83 BinaryenRustModuleOptionsSetImportMemory(BinaryenRustModuleOptions *options,
85 options->importMemory = import;
88 extern "C" BinaryenRustModule*
89 BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
90 const char *assembly) {
93 options->stackAllocation,
96 options->importMemory,
97 options->ignoreUnknownSymbols,
98 options->startFunction,
101 S2WasmBuilder mainbuilder(assembly, options->debug);
102 linker.linkObject(mainbuilder);
105 auto ret = make_unique<BinaryenRustModule>();
107 WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
108 writer.setNamesSection(options->debugInfo);
109 // FIXME: support source maps?
110 // writer.setSourceMap(sourceMapStream.get(), sourceMapUrl);
112 // FIXME: support symbol maps?
113 // writer.setSymbolMap(symbolMap);
116 return ret.release();
119 extern "C" const uint8_t*
120 BinaryenRustModulePtr(const BinaryenRustModule *M) {
121 return M->buffer.data();
125 BinaryenRustModuleLen(const BinaryenRustModule *M) {
126 return M->buffer.size();
130 BinaryenRustModuleFree(BinaryenRustModule *M) {