1010#include " src/wast-lexer.h"
1111#include " src/resolve-names.h"
1212#include " src/binary-writer.h"
13- #include " src/error-handler.h"
13+ #include " src/error.h"
14+ #include " src/error-formatter.h"
1415#include " src/ir.h"
1516#include " src/cast.h"
1617#include " src/validator.h"
1920using namespace wabt ;
2021using namespace ChakraWabt ;
2122
22- class MyErrorHandler : public ErrorHandler
23+ void CheckResult (
24+ Result result,
25+ Errors* errors,
26+ WastLexer* lexer,
27+ const char * errorMessage
28+ )
2329{
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))
2831 {
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 )
3233 {
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);
3546 }
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);
4648 }
47-
48- virtual size_t source_line_max_length () const override
49- {
50- return 256 ;
51- }
52-
53- };
49+ }
5450
5551namespace ChakraWabt
5652{
5753 struct Context
5854 {
5955 ChakraContext* chakra;
6056 WastLexer* lexer;
61- MyErrorHandler* errorHandler;
6257 };
6358}
6459
@@ -91,7 +86,7 @@ uint TruncSizeT(size_t value)
9186{
9287 if (value > 0xffffffff )
9388 {
94- throw Error (" Out of Memory" );
89+ throw WabtAPIError (" Out of Memory" );
9590 }
9691 return (uint)value;
9792}
@@ -101,7 +96,7 @@ void set_property(Context* ctx, Js::Var obj, PropertyId id, Js::Var value, const
10196 bool success = ctx->chakra ->spec ->setProperty (obj, id, value, ctx->chakra ->user_data );
10297 if (!success)
10398 {
104- throw Error (messageIfFailed);
99+ throw WabtAPIError (messageIfFailed);
105100 }
106101}
107102
@@ -166,7 +161,7 @@ void write_command_type(Context* ctx, CommandType type, Js::Var cmdObj)
166161 uint i = (uint)type;
167162 if (i > (uint)CommandType::Last)
168163 {
169- throw Error (" invalid command type" );
164+ throw WabtAPIError (" invalid command type" );
170165 }
171166 write_string (ctx, cmdObj, PropertyIds::type, s_command_names[i]);
172167}
@@ -203,7 +198,7 @@ Js::Var create_const_vector(Context* ctx, const ConstVector& consts)
203198 break ;
204199 default :
205200 assert (0 );
206- throw Error (" invalid constant type" );
201+ throw WabtAPIError (" invalid constant type" );
207202 }
208203 write_string (ctx, constDescriptor, PropertyIds::value, buf);
209204 }
@@ -287,19 +282,21 @@ Js::Var create_module(Context* ctx, const Module* module, bool validate = true)
287282{
288283 if (!module )
289284 {
290- throw Error (" No module found" );
285+ throw WabtAPIError (" No module found" );
291286 }
292287 if (validate)
293288 {
294289 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" );
296293 }
297294 MemoryStream stream;
298295 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);
300297 if (!Succeeded (result))
301298 {
302- throw Error (" Error while writing module" );
299+ throw WabtAPIError (" Error while writing module" );
303300 }
304301 const uint8_t * data = stream.output_buffer ().data .data ();
305302 const size_t size = stream.output_buffer ().size ();
@@ -469,25 +466,17 @@ Js::Var write_commands(Context* ctx, Script* script)
469466
470467void Validate (const ChakraContext& ctx, bool isSpec)
471468{
472- if (!ctx.createBuffer ) throw Error (" Missing createBuffer" );
469+ if (!ctx.createBuffer ) throw WabtAPIError (" Missing createBuffer" );
473470 if (isSpec)
474471 {
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" );
491480 }
492481}
493482
@@ -497,32 +486,31 @@ Js::Var ChakraWabt::ConvertWast2Wasm(ChakraContext& chakraCtx, char* buffer, uin
497486
498487 std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer (" " , buffer, (size_t )bufferSize);
499488
500- MyErrorHandler s_error_handler;
501489 WastParseOptions options (GetWabtFeatures (chakraCtx));
502490 Context ctx;
503491 ctx.chakra = &chakraCtx;
504- ctx.errorHandler = &s_error_handler;
505492 ctx.lexer = lexer.get ();
493+ Errors errors;
506494
507495 if (isSpecText)
508496 {
509497 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" );
512500
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" );
515503
516504 return write_commands (&ctx, script.get ());
517505 }
518506 else
519507 {
520508 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" );
523511
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" );
526514
527515 return create_module (&ctx, module .get ());
528516 }
0 commit comments