summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/emscripten-helpers.cc
blob: 127a8c458d6113b460de9b5d9b80332289b9c4ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright 2016 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef WABT_EMSCRIPTEN_HELPERS_H_
#define WABT_EMSCRIPTEN_HELPERS_H_

#include <cstddef>

#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>
#include <vector>

#include "wabt/apply-names.h"
#include "wabt/binary-reader-ir.h"
#include "wabt/binary-reader.h"
#include "wabt/binary-writer-spec.h"
#include "wabt/binary-writer.h"
#include "wabt/common.h"
#include "wabt/error-formatter.h"
#include "wabt/feature.h"
#include "wabt/filenames.h"
#include "wabt/generate-names.h"
#include "wabt/ir.h"
#include "wabt/stream.h"
#include "wabt/validator.h"
#include "wabt/wast-lexer.h"
#include "wabt/wast-parser.h"
#include "wabt/wat-writer.h"

using WabtOutputBufferPtr = std::unique_ptr<wabt::OutputBuffer>;
using WabtFilenameOutputBufferPair =
    std::pair<std::string, WabtOutputBufferPtr>;

struct WabtParseWatResult {
  wabt::Result result;
  std::unique_ptr<wabt::Module> module;
};

struct WabtReadBinaryResult {
  wabt::Result result;
  std::unique_ptr<wabt::Module> module;
};

struct WabtWriteModuleResult {
  wabt::Result result;
  WabtOutputBufferPtr buffer;
  WabtOutputBufferPtr log_buffer;
};

struct WabtWriteScriptResult {
  wabt::Result result;
  WabtOutputBufferPtr json_buffer;
  WabtOutputBufferPtr log_buffer;
  std::vector<WabtFilenameOutputBufferPair> module_buffers;
};

struct WabtParseWastResult {
  wabt::Result result;
  std::unique_ptr<wabt::Script> script;
};

extern "C" {

wabt::Features* wabt_new_features(void) {
  return new wabt::Features();
}

void wabt_destroy_features(wabt::Features* f) {
  delete f;
}

#define WABT_FEATURE(variable, flag, default_, help)                   \
  bool wabt_##variable##_enabled(wabt::Features* f) {                  \
    return f->variable##_enabled();                                    \
  }                                                                    \
  void wabt_set_##variable##_enabled(wabt::Features* f, int enabled) { \
    f->set_##variable##_enabled(enabled);                              \
  }
#include "wabt/feature.def"
#undef WABT_FEATURE

wabt::WastLexer* wabt_new_wast_buffer_lexer(const char* filename,
                                            const void* data,
                                            size_t size,
                                            wabt::Errors* errors) {
  std::unique_ptr<wabt::WastLexer> lexer =
      wabt::WastLexer::CreateBufferLexer(filename, data, size, errors);
  return lexer.release();
}

WabtParseWatResult* wabt_parse_wat(wabt::WastLexer* lexer,
                                   wabt::Features* features,
                                   wabt::Errors* errors) {
  wabt::WastParseOptions options(*features);
  WabtParseWatResult* result = new WabtParseWatResult();
  std::unique_ptr<wabt::Module> module;
  result->result = wabt::ParseWatModule(lexer, &module, errors, &options);
  result->module = std::move(module);
  return result;
}

WabtParseWastResult* wabt_parse_wast(wabt::WastLexer* lexer,
                                     wabt::Features* features,
                                     wabt::Errors* errors) {
  wabt::WastParseOptions options(*features);
  WabtParseWastResult* result = new WabtParseWastResult();
  std::unique_ptr<wabt::Script> script;
  result->result = wabt::ParseWastScript(lexer, &script, errors, &options);
  result->script = std::move(script);
  return result;
}

WabtReadBinaryResult* wabt_read_binary(const void* data,
                                       size_t size,
                                       int read_debug_names,
                                       wabt::Features* features,
                                       wabt::Errors* errors) {
  wabt::ReadBinaryOptions options;
  options.features = *features;
  options.read_debug_names = read_debug_names;

  WabtReadBinaryResult* result = new WabtReadBinaryResult();
  wabt::Module* module = new wabt::Module();
  // TODO(binji): Pass through from wabt_read_binary parameter.
  const char* filename = "<binary>";
  result->result =
      wabt::ReadBinaryIr(filename, data, size, options, errors, module);
  result->module.reset(module);
  return result;
}

wabt::Result::Enum wabt_validate_module(wabt::Module* module,
                                        wabt::Features* features,
                                        wabt::Errors* errors) {
  wabt::ValidateOptions options;
  options.features = *features;
  return ValidateModule(module, errors, options);
}

wabt::Result::Enum wabt_validate_script(wabt::Script* script,
                                        wabt::Features* features,
                                        wabt::Errors* errors) {
  wabt::ValidateOptions options;
  options.features = *features;
  return ValidateScript(script, errors, options);
}

WabtWriteScriptResult* wabt_write_binary_spec_script(
    wabt::Script* script,
    const char* source_filename,
    const char* out_filename,
    int log,
    int canonicalize_lebs,
    int relocatable,
    int write_debug_names) {
  wabt::MemoryStream log_stream;
  wabt::MemoryStream* log_stream_p = log ? &log_stream : nullptr;

  wabt::WriteBinaryOptions options;
  options.canonicalize_lebs = canonicalize_lebs;
  options.relocatable = relocatable;
  options.write_debug_names = write_debug_names;

  std::vector<wabt::FilenameMemoryStreamPair> module_streams;
  wabt::MemoryStream json_stream(log_stream_p);

  std::string module_filename_noext(
      wabt::StripExtension(out_filename ? out_filename : source_filename));

  WabtWriteScriptResult* result = new WabtWriteScriptResult();
  result->result = WriteBinarySpecScript(&json_stream, script, source_filename,
                                         module_filename_noext, options,
                                         &module_streams, log_stream_p);

  if (result->result == wabt::Result::Ok) {
    result->json_buffer = json_stream.ReleaseOutputBuffer();
    result->log_buffer = log ? log_stream.ReleaseOutputBuffer() : nullptr;
    std::transform(module_streams.begin(), module_streams.end(),
                   std::back_inserter(result->module_buffers),
                   [](wabt::FilenameMemoryStreamPair& pair) {
                     return WabtFilenameOutputBufferPair(
                         pair.filename, pair.stream->ReleaseOutputBuffer());
                   });
  }
  return result;
}

wabt::Result::Enum wabt_apply_names_module(wabt::Module* module) {
  return ApplyNames(module);
}

wabt::Result::Enum wabt_generate_names_module(wabt::Module* module) {
  return GenerateNames(module);
}

WabtWriteModuleResult* wabt_write_binary_module(wabt::Module* module,
                                                int log,
                                                int canonicalize_lebs,
                                                int relocatable,
                                                int write_debug_names) {
  wabt::MemoryStream log_stream;
  wabt::WriteBinaryOptions options;
  options.canonicalize_lebs = canonicalize_lebs;
  options.relocatable = relocatable;
  options.write_debug_names = write_debug_names;

  wabt::MemoryStream stream(log ? &log_stream : nullptr);
  WabtWriteModuleResult* result = new WabtWriteModuleResult();
  result->result = WriteBinaryModule(&stream, module, options);
  if (result->result == wabt::Result::Ok) {
    result->buffer = stream.ReleaseOutputBuffer();
    result->log_buffer = log ? log_stream.ReleaseOutputBuffer() : nullptr;
  }
  return result;
}

WabtWriteModuleResult* wabt_write_text_module(wabt::Module* module,
                                              int fold_exprs,
                                              int inline_export) {
  wabt::WriteWatOptions options;
  options.fold_exprs = fold_exprs;
  options.inline_export = inline_export;

  wabt::MemoryStream stream;
  WabtWriteModuleResult* result = new WabtWriteModuleResult();
  result->result = WriteWat(&stream, module, options);
  if (result->result == wabt::Result::Ok) {
    result->buffer = stream.ReleaseOutputBuffer();
  }
  return result;
}

void wabt_destroy_module(wabt::Module* module) {
  delete module;
}

void wabt_destroy_wast_lexer(wabt::WastLexer* lexer) {
  delete lexer;
}

// Errors
wabt::Errors* wabt_new_errors(void) {
  return new wabt::Errors();
}

wabt::OutputBuffer* wabt_format_text_errors(wabt::Errors* errors,
                                            wabt::WastLexer* lexer) {
  auto line_finder = lexer->MakeLineFinder();
  std::string string_result = FormatErrorsToString(
      *errors, wabt::Location::Type::Text, line_finder.get());

  wabt::OutputBuffer* result = new wabt::OutputBuffer();
  std::copy(string_result.begin(), string_result.end(),
            std::back_inserter(result->data));
  return result;
}

wabt::OutputBuffer* wabt_format_binary_errors(wabt::Errors* errors) {
  std::string string_result =
      FormatErrorsToString(*errors, wabt::Location::Type::Binary);

  wabt::OutputBuffer* result = new wabt::OutputBuffer();
  std::copy(string_result.begin(), string_result.end(),
            std::back_inserter(result->data));
  return result;
}

void wabt_destroy_errors(wabt::Errors* errors) {
  delete errors;
}

// WabtParseWatResult
wabt::Result::Enum wabt_parse_wat_result_get_result(
    WabtParseWatResult* result) {
  return result->result;
}

wabt::Module* wabt_parse_wat_result_release_module(WabtParseWatResult* result) {
  return result->module.release();
}

void wabt_destroy_parse_wat_result(WabtParseWatResult* result) {
  delete result;
}

// WabtParseWastResult
wabt::Result::Enum wabt_parse_wast_result_get_result(
    WabtParseWastResult* result) {
  return result->result;
}

wabt::Script* wabt_parse_wast_result_release_module(
    WabtParseWastResult* result) {
  return result->script.release();
}

void wabt_destroy_parse_wast_result(WabtParseWastResult* result) {
  delete result;
}

// WabtReadBinaryResult
wabt::Result::Enum wabt_read_binary_result_get_result(
    WabtReadBinaryResult* result) {
  return result->result;
}

wabt::Module* wabt_read_binary_result_release_module(
    WabtReadBinaryResult* result) {
  return result->module.release();
}

void wabt_destroy_read_binary_result(WabtReadBinaryResult* result) {
  delete result;
}

// WabtWriteModuleResult
wabt::Result::Enum wabt_write_module_result_get_result(
    WabtWriteModuleResult* result) {
  return result->result;
}

wabt::OutputBuffer* wabt_write_module_result_release_output_buffer(
    WabtWriteModuleResult* result) {
  return result->buffer.release();
}

wabt::OutputBuffer* wabt_write_module_result_release_log_output_buffer(
    WabtWriteModuleResult* result) {
  return result->log_buffer.release();
}

void wabt_destroy_write_module_result(WabtWriteModuleResult* result) {
  delete result;
}

// WabtWriteScriptResult
wabt::Result::Enum wabt_write_script_result_get_result(
    WabtWriteScriptResult* result) {
  return result->result;
}

wabt::OutputBuffer* wabt_write_script_result_release_json_output_buffer(
    WabtWriteScriptResult* result) {
  return result->json_buffer.release();
}

wabt::OutputBuffer* wabt_write_script_result_release_log_output_buffer(
    WabtWriteScriptResult* result) {
  return result->log_buffer.release();
}

size_t wabt_write_script_result_get_module_count(
    WabtWriteScriptResult* result) {
  return result->module_buffers.size();
}

const char* wabt_write_script_result_get_module_filename(
    WabtWriteScriptResult* result,
    size_t index) {
  return result->module_buffers[index].first.c_str();
}

wabt::OutputBuffer* wabt_write_script_result_release_module_output_buffer(
    WabtWriteScriptResult* result,
    size_t index) {
  return result->module_buffers[index].second.release();
}

void wabt_destroy_write_script_result(WabtWriteScriptResult* result) {
  delete result;
}

// wabt::OutputBuffer*
const void* wabt_output_buffer_get_data(wabt::OutputBuffer* output_buffer) {
  return output_buffer->data.data();
}

size_t wabt_output_buffer_get_size(wabt::OutputBuffer* output_buffer) {
  return output_buffer->data.size();
}

void wabt_destroy_output_buffer(wabt::OutputBuffer* output_buffer) {
  delete output_buffer;
}

}  // extern "C"

#endif /* WABT_EMSCRIPTEN_HELPERS_H_ */