10
10
#include " src/wast-lexer.h"
11
11
#include " src/resolve-names.h"
12
12
#include " src/binary-writer.h"
13
- #include " src/error-handler.h"
13
+ #include " src/error.h"
14
+ #include " src/error-formatter.h"
14
15
#include " src/ir.h"
15
16
#include " src/cast.h"
16
17
#include " src/validator.h"
19
20
using namespace wabt ;
20
21
using namespace ChakraWabt ;
21
22
22
- class MyErrorHandler : public ErrorHandler
23
+ void CheckResult (
24
+ Result result,
25
+ Errors* errors,
26
+ WastLexer* lexer,
27
+ const char * errorMessage
28
+ )
23
29
{
24
- public:
25
- MyErrorHandler () : ErrorHandler(Location::Type::Text) {}
26
-
27
- virtual bool OnError (ErrorLevel level, const Location& loc, const std::string& error, const std::string& source_line, size_t source_line_column_offset) override
30
+ if (!Succeeded (result))
28
31
{
29
- int colStart = loc.first_column - 1 - (int )source_line_column_offset;
30
- int sourceErrorLength = (loc.last_column - loc.first_column ) - 2 ;
31
- if (sourceErrorLength < 0 )
32
+ if (errors && errors->size () > 0 )
32
33
{
33
- // -2 probably overflowed
34
- sourceErrorLength = 0 ;
34
+ std::string message = FormatErrorsToString (
35
+ *errors,
36
+ Location::Type::Text,
37
+ lexer->MakeLineFinder ().get (),
38
+ wabt::Color (),
39
+ errorMessage,
40
+ PrintHeader::Once,
41
+ 256
42
+ );
43
+ char buf[4096 ];
44
+ wabt_snprintf (buf, 4096 , " Wast Parsing %s" , message.c_str ());
45
+ throw WabtAPIError (buf);
35
46
}
36
- char buf[4096 ];
37
- wabt_snprintf (buf, 4096 , " Wast Parsing %s:%u:%u:\n %s\n %s\n %*s^%*s^" ,
38
- GetErrorLevelName (level),
39
- loc.line ,
40
- loc.first_column ,
41
- error.c_str (),
42
- source_line.c_str (),
43
- colStart, " " ,
44
- sourceErrorLength, " " );
45
- throw Error (buf);
47
+ throw WabtAPIError (errorMessage);
46
48
}
47
-
48
- virtual size_t source_line_max_length () const override
49
- {
50
- return 256 ;
51
- }
52
-
53
- };
49
+ }
54
50
55
51
namespace ChakraWabt
56
52
{
57
53
struct Context
58
54
{
59
55
ChakraContext* chakra;
60
56
WastLexer* lexer;
61
- MyErrorHandler* errorHandler;
62
57
};
63
58
}
64
59
@@ -91,7 +86,7 @@ uint TruncSizeT(size_t value)
91
86
{
92
87
if (value > 0xffffffff )
93
88
{
94
- throw Error (" Out of Memory" );
89
+ throw WabtAPIError (" Out of Memory" );
95
90
}
96
91
return (uint)value;
97
92
}
@@ -101,7 +96,7 @@ void set_property(Context* ctx, Js::Var obj, PropertyId id, Js::Var value, const
101
96
bool success = ctx->chakra ->spec ->setProperty (obj, id, value, ctx->chakra ->user_data );
102
97
if (!success)
103
98
{
104
- throw Error (messageIfFailed);
99
+ throw WabtAPIError (messageIfFailed);
105
100
}
106
101
}
107
102
@@ -166,7 +161,7 @@ void write_command_type(Context* ctx, CommandType type, Js::Var cmdObj)
166
161
uint i = (uint)type;
167
162
if (i > (uint)CommandType::Last)
168
163
{
169
- throw Error (" invalid command type" );
164
+ throw WabtAPIError (" invalid command type" );
170
165
}
171
166
write_string (ctx, cmdObj, PropertyIds::type, s_command_names[i]);
172
167
}
@@ -203,7 +198,7 @@ Js::Var create_const_vector(Context* ctx, const ConstVector& consts)
203
198
break ;
204
199
default :
205
200
assert (0 );
206
- throw Error (" invalid constant type" );
201
+ throw WabtAPIError (" invalid constant type" );
207
202
}
208
203
write_string (ctx, constDescriptor, PropertyIds::value, buf);
209
204
}
@@ -287,19 +282,21 @@ Js::Var create_module(Context* ctx, const Module* module, bool validate = true)
287
282
{
288
283
if (!module )
289
284
{
290
- throw Error (" No module found" );
285
+ throw WabtAPIError (" No module found" );
291
286
}
292
287
if (validate)
293
288
{
294
289
ValidateOptions options (GetWabtFeatures (*ctx->chakra ));
295
- ValidateModule (ctx->lexer , module , ctx->errorHandler , &options);
290
+ Errors errors;
291
+ Result result = ValidateModule (module , &errors, options);
292
+ CheckResult (result, &errors, ctx->lexer , " Failed to validate module" );
296
293
}
297
294
MemoryStream stream;
298
295
WriteBinaryOptions s_write_binary_options;
299
- Result result = WriteBinaryModule (&stream, module , & s_write_binary_options);
296
+ Result result = WriteBinaryModule (&stream, module , s_write_binary_options);
300
297
if (!Succeeded (result))
301
298
{
302
- throw Error (" Error while writing module" );
299
+ throw WabtAPIError (" Error while writing module" );
303
300
}
304
301
const uint8_t * data = stream.output_buffer ().data .data ();
305
302
const size_t size = stream.output_buffer ().size ();
@@ -469,25 +466,17 @@ Js::Var write_commands(Context* ctx, Script* script)
469
466
470
467
void Validate (const ChakraContext& ctx, bool isSpec)
471
468
{
472
- if (!ctx.createBuffer ) throw Error (" Missing createBuffer" );
469
+ if (!ctx.createBuffer ) throw WabtAPIError (" Missing createBuffer" );
473
470
if (isSpec)
474
471
{
475
- if (!ctx.spec ) throw Error (" Missing Spec context" );
476
- if (!ctx.spec ->setProperty ) throw Error (" Missing spec->setProperty" );
477
- if (!ctx.spec ->int32ToVar ) throw Error (" Missing spec->int32ToVar" );
478
- if (!ctx.spec ->int64ToVar ) throw Error (" Missing spec->int64ToVar" );
479
- if (!ctx.spec ->stringToVar ) throw Error (" Missing spec->stringToVar" );
480
- if (!ctx.spec ->createObject ) throw Error (" Missing spec->createObject" );
481
- if (!ctx.spec ->createArray ) throw Error (" Missing spec->createArray" );
482
- if (!ctx.spec ->push ) throw Error (" Missing spec->push" );
483
- }
484
- }
485
-
486
- void CheckResult (Result result, const char * errorMessage)
487
- {
488
- if (!Succeeded (result))
489
- {
490
- throw Error (errorMessage);
472
+ if (!ctx.spec ) throw WabtAPIError (" Missing Spec context" );
473
+ if (!ctx.spec ->setProperty ) throw WabtAPIError (" Missing spec->setProperty" );
474
+ if (!ctx.spec ->int32ToVar ) throw WabtAPIError (" Missing spec->int32ToVar" );
475
+ if (!ctx.spec ->int64ToVar ) throw WabtAPIError (" Missing spec->int64ToVar" );
476
+ if (!ctx.spec ->stringToVar ) throw WabtAPIError (" Missing spec->stringToVar" );
477
+ if (!ctx.spec ->createObject ) throw WabtAPIError (" Missing spec->createObject" );
478
+ if (!ctx.spec ->createArray ) throw WabtAPIError (" Missing spec->createArray" );
479
+ if (!ctx.spec ->push ) throw WabtAPIError (" Missing spec->push" );
491
480
}
492
481
}
493
482
@@ -497,32 +486,31 @@ Js::Var ChakraWabt::ConvertWast2Wasm(ChakraContext& chakraCtx, char* buffer, uin
497
486
498
487
std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer (" " , buffer, (size_t )bufferSize);
499
488
500
- MyErrorHandler s_error_handler;
501
489
WastParseOptions options (GetWabtFeatures (chakraCtx));
502
490
Context ctx;
503
491
ctx.chakra = &chakraCtx;
504
- ctx.errorHandler = &s_error_handler;
505
492
ctx.lexer = lexer.get ();
493
+ Errors errors;
506
494
507
495
if (isSpecText)
508
496
{
509
497
std::unique_ptr<Script> script;
510
- Result result = ParseWastScript (lexer.get (), &script, &s_error_handler , &options);
511
- CheckResult (result, " Invalid wast script" );
498
+ Result result = ParseWastScript (lexer.get (), &script, &errors , &options);
499
+ CheckResult (result, &errors, lexer. get (), " Invalid wast script" );
512
500
513
- result = ResolveNamesScript (lexer. get (), script.get (), &s_error_handler );
514
- CheckResult (result, " Unable to resolve script's names" );
501
+ result = ResolveNamesScript (script.get (), &errors );
502
+ CheckResult (result, &errors, lexer. get (), " Unable to resolve script's names" );
515
503
516
504
return write_commands (&ctx, script.get ());
517
505
}
518
506
else
519
507
{
520
508
std::unique_ptr<Module> module ;
521
- Result result = ParseWatModule (lexer.get (), &module , &s_error_handler , &options);
522
- CheckResult (result, " Invalid wat script" );
509
+ Result result = ParseWatModule (lexer.get (), &module , &errors , &options);
510
+ CheckResult (result, &errors, lexer. get (), " Invalid wat script" );
523
511
524
- result = ResolveNamesModule (lexer. get (), module .get (), &s_error_handler );
525
- CheckResult (result, " Unable to resolve module's names" );
512
+ result = ResolveNamesModule (module .get (), &errors );
513
+ CheckResult (result, &errors, lexer. get (), " Unable to resolve module's names" );
526
514
527
515
return create_module (&ctx, module .get ());
528
516
}
0 commit comments