2 * Copyright 2016 WebAssembly Community Group participants
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef WABT_EMSCRIPTEN_HELPERS_H_
18 #define WABT_EMSCRIPTEN_HELPERS_H_
28 #include "wabt/apply-names.h"
29 #include "wabt/binary-reader-ir.h"
30 #include "wabt/binary-reader.h"
31 #include "wabt/binary-writer-spec.h"
32 #include "wabt/binary-writer.h"
33 #include "wabt/common.h"
34 #include "wabt/error-formatter.h"
35 #include "wabt/feature.h"
36 #include "wabt/filenames.h"
37 #include "wabt/generate-names.h"
39 #include "wabt/stream.h"
40 #include "wabt/validator.h"
41 #include "wabt/wast-lexer.h"
42 #include "wabt/wast-parser.h"
43 #include "wabt/wat-writer.h"
45 using WabtOutputBufferPtr
= std::unique_ptr
<wabt::OutputBuffer
>;
46 using WabtFilenameOutputBufferPair
=
47 std::pair
<std::string
, WabtOutputBufferPtr
>;
49 struct WabtParseWatResult
{
51 std::unique_ptr
<wabt::Module
> module
;
54 struct WabtReadBinaryResult
{
56 std::unique_ptr
<wabt::Module
> module
;
59 struct WabtWriteModuleResult
{
61 WabtOutputBufferPtr buffer
;
62 WabtOutputBufferPtr log_buffer
;
65 struct WabtWriteScriptResult
{
67 WabtOutputBufferPtr json_buffer
;
68 WabtOutputBufferPtr log_buffer
;
69 std::vector
<WabtFilenameOutputBufferPair
> module_buffers
;
72 struct WabtParseWastResult
{
74 std::unique_ptr
<wabt::Script
> script
;
79 wabt::Features
* wabt_new_features(void) {
80 return new wabt::Features();
83 void wabt_destroy_features(wabt::Features
* f
) {
87 #define WABT_FEATURE(variable, flag, default_, help) \
88 bool wabt_##variable##_enabled(wabt::Features* f) { \
89 return f->variable##_enabled(); \
91 void wabt_set_##variable##_enabled(wabt::Features* f, int enabled) { \
92 f->set_##variable##_enabled(enabled); \
94 #include "wabt/feature.def"
97 wabt::WastLexer
* wabt_new_wast_buffer_lexer(const char* filename
,
100 wabt::Errors
* errors
) {
101 std::unique_ptr
<wabt::WastLexer
> lexer
=
102 wabt::WastLexer::CreateBufferLexer(filename
, data
, size
, errors
);
103 return lexer
.release();
106 WabtParseWatResult
* wabt_parse_wat(wabt::WastLexer
* lexer
,
107 wabt::Features
* features
,
108 wabt::Errors
* errors
) {
109 wabt::WastParseOptions
options(*features
);
110 WabtParseWatResult
* result
= new WabtParseWatResult();
111 std::unique_ptr
<wabt::Module
> module
;
112 result
->result
= wabt::ParseWatModule(lexer
, &module
, errors
, &options
);
113 result
->module
= std::move(module
);
117 WabtParseWastResult
* wabt_parse_wast(wabt::WastLexer
* lexer
,
118 wabt::Features
* features
,
119 wabt::Errors
* errors
) {
120 wabt::WastParseOptions
options(*features
);
121 WabtParseWastResult
* result
= new WabtParseWastResult();
122 std::unique_ptr
<wabt::Script
> script
;
123 result
->result
= wabt::ParseWastScript(lexer
, &script
, errors
, &options
);
124 result
->script
= std::move(script
);
128 WabtReadBinaryResult
* wabt_read_binary(const void* data
,
130 int read_debug_names
,
131 wabt::Features
* features
,
132 wabt::Errors
* errors
) {
133 wabt::ReadBinaryOptions options
;
134 options
.features
= *features
;
135 options
.read_debug_names
= read_debug_names
;
137 WabtReadBinaryResult
* result
= new WabtReadBinaryResult();
138 wabt::Module
* module
= new wabt::Module();
139 // TODO(binji): Pass through from wabt_read_binary parameter.
140 const char* filename
= "<binary>";
142 wabt::ReadBinaryIr(filename
, data
, size
, options
, errors
, module
);
143 result
->module
.reset(module
);
147 wabt::Result::Enum
wabt_validate_module(wabt::Module
* module
,
148 wabt::Features
* features
,
149 wabt::Errors
* errors
) {
150 wabt::ValidateOptions options
;
151 options
.features
= *features
;
152 return ValidateModule(module
, errors
, options
);
155 wabt::Result::Enum
wabt_validate_script(wabt::Script
* script
,
156 wabt::Features
* features
,
157 wabt::Errors
* errors
) {
158 wabt::ValidateOptions options
;
159 options
.features
= *features
;
160 return ValidateScript(script
, errors
, options
);
163 WabtWriteScriptResult
* wabt_write_binary_spec_script(
164 wabt::Script
* script
,
165 const char* source_filename
,
166 const char* out_filename
,
168 int canonicalize_lebs
,
170 int write_debug_names
) {
171 wabt::MemoryStream log_stream
;
172 wabt::MemoryStream
* log_stream_p
= log
? &log_stream
: nullptr;
174 wabt::WriteBinaryOptions options
;
175 options
.canonicalize_lebs
= canonicalize_lebs
;
176 options
.relocatable
= relocatable
;
177 options
.write_debug_names
= write_debug_names
;
179 std::vector
<wabt::FilenameMemoryStreamPair
> module_streams
;
180 wabt::MemoryStream
json_stream(log_stream_p
);
182 std::string
module_filename_noext(
183 wabt::StripExtension(out_filename
? out_filename
: source_filename
));
185 WabtWriteScriptResult
* result
= new WabtWriteScriptResult();
186 result
->result
= WriteBinarySpecScript(&json_stream
, script
, source_filename
,
187 module_filename_noext
, options
,
188 &module_streams
, log_stream_p
);
190 if (result
->result
== wabt::Result::Ok
) {
191 result
->json_buffer
= json_stream
.ReleaseOutputBuffer();
192 result
->log_buffer
= log
? log_stream
.ReleaseOutputBuffer() : nullptr;
193 std::transform(module_streams
.begin(), module_streams
.end(),
194 std::back_inserter(result
->module_buffers
),
195 [](wabt::FilenameMemoryStreamPair
& pair
) {
196 return WabtFilenameOutputBufferPair(
197 pair
.filename
, pair
.stream
->ReleaseOutputBuffer());
203 wabt::Result::Enum
wabt_apply_names_module(wabt::Module
* module
) {
204 return ApplyNames(module
);
207 wabt::Result::Enum
wabt_generate_names_module(wabt::Module
* module
) {
208 return GenerateNames(module
);
211 WabtWriteModuleResult
* wabt_write_binary_module(wabt::Module
* module
,
213 int canonicalize_lebs
,
215 int write_debug_names
) {
216 wabt::MemoryStream log_stream
;
217 wabt::WriteBinaryOptions options
;
218 options
.canonicalize_lebs
= canonicalize_lebs
;
219 options
.relocatable
= relocatable
;
220 options
.write_debug_names
= write_debug_names
;
222 wabt::MemoryStream
stream(log
? &log_stream
: nullptr);
223 WabtWriteModuleResult
* result
= new WabtWriteModuleResult();
224 result
->result
= WriteBinaryModule(&stream
, module
, options
);
225 if (result
->result
== wabt::Result::Ok
) {
226 result
->buffer
= stream
.ReleaseOutputBuffer();
227 result
->log_buffer
= log
? log_stream
.ReleaseOutputBuffer() : nullptr;
232 WabtWriteModuleResult
* wabt_write_text_module(wabt::Module
* module
,
235 wabt::WriteWatOptions options
;
236 options
.fold_exprs
= fold_exprs
;
237 options
.inline_export
= inline_export
;
239 wabt::MemoryStream stream
;
240 WabtWriteModuleResult
* result
= new WabtWriteModuleResult();
241 result
->result
= WriteWat(&stream
, module
, options
);
242 if (result
->result
== wabt::Result::Ok
) {
243 result
->buffer
= stream
.ReleaseOutputBuffer();
248 void wabt_destroy_module(wabt::Module
* module
) {
252 void wabt_destroy_wast_lexer(wabt::WastLexer
* lexer
) {
257 wabt::Errors
* wabt_new_errors(void) {
258 return new wabt::Errors();
261 wabt::OutputBuffer
* wabt_format_text_errors(wabt::Errors
* errors
,
262 wabt::WastLexer
* lexer
) {
263 auto line_finder
= lexer
->MakeLineFinder();
264 std::string string_result
= FormatErrorsToString(
265 *errors
, wabt::Location::Type::Text
, line_finder
.get());
267 wabt::OutputBuffer
* result
= new wabt::OutputBuffer();
268 std::copy(string_result
.begin(), string_result
.end(),
269 std::back_inserter(result
->data
));
273 wabt::OutputBuffer
* wabt_format_binary_errors(wabt::Errors
* errors
) {
274 std::string string_result
=
275 FormatErrorsToString(*errors
, wabt::Location::Type::Binary
);
277 wabt::OutputBuffer
* result
= new wabt::OutputBuffer();
278 std::copy(string_result
.begin(), string_result
.end(),
279 std::back_inserter(result
->data
));
283 void wabt_destroy_errors(wabt::Errors
* errors
) {
287 // WabtParseWatResult
288 wabt::Result::Enum
wabt_parse_wat_result_get_result(
289 WabtParseWatResult
* result
) {
290 return result
->result
;
293 wabt::Module
* wabt_parse_wat_result_release_module(WabtParseWatResult
* result
) {
294 return result
->module
.release();
297 void wabt_destroy_parse_wat_result(WabtParseWatResult
* result
) {
301 // WabtParseWastResult
302 wabt::Result::Enum
wabt_parse_wast_result_get_result(
303 WabtParseWastResult
* result
) {
304 return result
->result
;
307 wabt::Script
* wabt_parse_wast_result_release_module(
308 WabtParseWastResult
* result
) {
309 return result
->script
.release();
312 void wabt_destroy_parse_wast_result(WabtParseWastResult
* result
) {
316 // WabtReadBinaryResult
317 wabt::Result::Enum
wabt_read_binary_result_get_result(
318 WabtReadBinaryResult
* result
) {
319 return result
->result
;
322 wabt::Module
* wabt_read_binary_result_release_module(
323 WabtReadBinaryResult
* result
) {
324 return result
->module
.release();
327 void wabt_destroy_read_binary_result(WabtReadBinaryResult
* result
) {
331 // WabtWriteModuleResult
332 wabt::Result::Enum
wabt_write_module_result_get_result(
333 WabtWriteModuleResult
* result
) {
334 return result
->result
;
337 wabt::OutputBuffer
* wabt_write_module_result_release_output_buffer(
338 WabtWriteModuleResult
* result
) {
339 return result
->buffer
.release();
342 wabt::OutputBuffer
* wabt_write_module_result_release_log_output_buffer(
343 WabtWriteModuleResult
* result
) {
344 return result
->log_buffer
.release();
347 void wabt_destroy_write_module_result(WabtWriteModuleResult
* result
) {
351 // WabtWriteScriptResult
352 wabt::Result::Enum
wabt_write_script_result_get_result(
353 WabtWriteScriptResult
* result
) {
354 return result
->result
;
357 wabt::OutputBuffer
* wabt_write_script_result_release_json_output_buffer(
358 WabtWriteScriptResult
* result
) {
359 return result
->json_buffer
.release();
362 wabt::OutputBuffer
* wabt_write_script_result_release_log_output_buffer(
363 WabtWriteScriptResult
* result
) {
364 return result
->log_buffer
.release();
367 size_t wabt_write_script_result_get_module_count(
368 WabtWriteScriptResult
* result
) {
369 return result
->module_buffers
.size();
372 const char* wabt_write_script_result_get_module_filename(
373 WabtWriteScriptResult
* result
,
375 return result
->module_buffers
[index
].first
.c_str();
378 wabt::OutputBuffer
* wabt_write_script_result_release_module_output_buffer(
379 WabtWriteScriptResult
* result
,
381 return result
->module_buffers
[index
].second
.release();
384 void wabt_destroy_write_script_result(WabtWriteScriptResult
* result
) {
388 // wabt::OutputBuffer*
389 const void* wabt_output_buffer_get_data(wabt::OutputBuffer
* output_buffer
) {
390 return output_buffer
->data
.data();
393 size_t wabt_output_buffer_get_size(wabt::OutputBuffer
* output_buffer
) {
394 return output_buffer
->data
.size();
397 void wabt_destroy_output_buffer(wabt::OutputBuffer
* output_buffer
) {
398 delete output_buffer
;
403 #endif /* WABT_EMSCRIPTEN_HELPERS_H_ */