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.
21 #include "wasm-binary.h"
22 #include "wasm-linker.h"
26 struct BinaryenRustModule {
27 BufferWithRandomAccess buffer;
28 std::string sourceMapJSON;
31 struct BinaryenRustModuleOptions {
34 uint64_t stackAllocation;
38 bool ignoreUnknownSymbols;
40 std::string startFunction;
41 std::string sourceMapUrl;
43 BinaryenRustModuleOptions() :
50 ignoreUnknownSymbols(false),
58 extern "C" BinaryenRustModuleOptions*
59 BinaryenRustModuleOptionsCreate() {
60 return new BinaryenRustModuleOptions;
64 BinaryenRustModuleOptionsFree(BinaryenRustModuleOptions *options) {
69 BinaryenRustModuleOptionsSetDebugInfo(BinaryenRustModuleOptions *options,
71 options->debugInfo = debugInfo;
75 BinaryenRustModuleOptionsSetStart(BinaryenRustModuleOptions *options,
77 options->startFunction = start;
81 BinaryenRustModuleOptionsSetSourceMapUrl(BinaryenRustModuleOptions *options,
83 options->sourceMapUrl = sourceMapUrl;
87 BinaryenRustModuleOptionsSetStackAllocation(BinaryenRustModuleOptions *options,
89 options->stackAllocation = stack;
93 BinaryenRustModuleOptionsSetImportMemory(BinaryenRustModuleOptions *options,
95 options->importMemory = import;
98 extern "C" BinaryenRustModule*
99 BinaryenRustModuleCreate(const BinaryenRustModuleOptions *options,
100 const char *assembly) {
103 options->stackAllocation,
106 options->importMemory,
107 options->ignoreUnknownSymbols,
108 options->startFunction,
111 S2WasmBuilder mainbuilder(assembly, options->debug);
112 linker.linkObject(mainbuilder);
115 auto ret = make_unique<BinaryenRustModule>();
117 WasmBinaryWriter writer(&linker.getOutput().wasm, ret->buffer, options->debug);
118 writer.setNamesSection(options->debugInfo);
120 std::unique_ptr<std::ostringstream> sourceMapStream = nullptr;
122 sourceMapStream = make_unique<std::ostringstream>();
123 writer.setSourceMap(sourceMapStream.get(), options->sourceMapUrl);
126 // FIXME: support symbol maps?
127 // writer.setSymbolMap(symbolMap);
130 if (sourceMapStream) {
131 ret->sourceMapJSON = sourceMapStream->str();
134 return ret.release();
137 extern "C" const uint8_t*
138 BinaryenRustModulePtr(const BinaryenRustModule *M) {
139 return M->buffer.data();
143 BinaryenRustModuleLen(const BinaryenRustModule *M) {
144 return M->buffer.size();
147 extern "C" const char*
148 BinaryenRustModuleSourceMapPtr(const BinaryenRustModule *M) {
149 return M->sourceMapJSON.data();
153 BinaryenRustModuleSourceMapLen(const BinaryenRustModule *M) {
154 return M->sourceMapJSON.length();
158 BinaryenRustModuleFree(BinaryenRustModule *M) {