Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,8 @@ class ScriptModule {
ScriptModuleType type() const { return type_; }
virtual const Location& location() const = 0;

bool is_definition = false;

protected:
explicit ScriptModule(ScriptModuleType type) : type_(type) {}

Expand Down
1 change: 1 addition & 0 deletions include/wabt/token.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ WABT_TOKEN(Bin, "bin")
WABT_TOKEN(Item, "item")
WABT_TOKEN(Data, "data")
WABT_TOKEN(Declare, "declare")
WABT_TOKEN(Definition, "definition")
WABT_TOKEN(Delegate, "delegate")
WABT_TOKEN(Do, "do")
WABT_TOKEN(Either, "either")
Expand Down
1 change: 1 addition & 0 deletions include/wabt/wast-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class WastParser {
Result Synchronize(SynchronizeFunc);

bool ParseBindVarOpt(std::string* name);
bool ParseDefinitionOpt();
Result ParseVar(Var* out_var);
bool ParseVarOpt(Var* out_var, Var default_var = Var());
Result ParseOffsetExpr(ExprList* out_expr_list);
Expand Down
1 change: 1 addition & 0 deletions src/lexer-keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ code, TokenType::Code
data.drop, TokenType::DataDrop, Opcode::DataDrop
data, TokenType::Data
declare, TokenType::Declare
definition, TokenType::Definition
delegate, TokenType::Delegate
do, TokenType::Do
drop, TokenType::Drop, Opcode::Drop
Expand Down
1,237 changes: 656 additions & 581 deletions src/prebuilt/lexer-keywords.cc

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/test-wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ TEST(WastParser, InvalidBinaryModule) {

EXPECT_EQ(expected_data, binary_mod->data);
}

TEST(WastParser, ModuleDefinition) {
std::string text = "(module definition)";

Errors errors;
auto lexer =
WastLexer::CreateBufferLexer("test", text.c_str(), text.size(), &errors);
Features features;
WastParseOptions options(features);

std::unique_ptr<Script> script;
Result result = ParseWastScript(lexer.get(), &script, &errors, &options);
ASSERT_EQ(result, Result::Ok);
ASSERT_EQ(script->commands.size(), 1U);
Command* cmd = script->commands[0].get();
auto* mod_cmd = cast<ScriptModuleCommand>(cmd);
ASSERT_NE(mod_cmd, nullptr);
ASSERT_TRUE(mod_cmd->script_module->is_definition);
}
33 changes: 29 additions & 4 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,15 @@ bool WastParser::ParseBindVarOpt(std::string* name) {
return true;
}

bool WastParser::ParseDefinitionOpt() {
WABT_TRACE(ParseDefinitionOpt);
if (!PeekMatch(TokenType::Definition)) {
return false;
}
(void)Consume();
return true;
}

Result WastParser::ParseVar(Var* out_var) {
WABT_TRACE(ParseVar);
if (PeekMatch(TokenType::Nat)) {
Expand Down Expand Up @@ -3664,10 +3673,20 @@ Result WastParser::ParseModuleCommand(Script* script, CommandPtr* out_command) {

switch (script_module->type()) {
case ScriptModuleType::Text: {
auto command = std::make_unique<ModuleCommand>();
module = &command->module;
*module = std::move(cast<TextScriptModule>(script_module.get())->module);
*out_command = std::move(command);
if (script_module->is_definition) {
auto command = std::make_unique<ScriptModuleCommand>();
module = &command->module;
*module =
std::move(cast<TextScriptModule>(script_module.get())->module);
command->script_module = std::move(script_module);
*out_command = std::move(command);
} else {
auto command = std::make_unique<ModuleCommand>();
module = &command->module;
*module =
std::move(cast<TextScriptModule>(script_module.get())->module);
*out_command = std::move(command);
}
break;
}

Expand Down Expand Up @@ -3852,6 +3871,9 @@ Result WastParser::ParseScriptModule(
EXPECT(Lpar);
Location loc = GetLocation();
EXPECT(Module);

bool is_definition = ParseDefinitionOpt();

std::string name;
ParseBindVarOpt(&name);

Expand All @@ -3867,6 +3889,7 @@ Result WastParser::ParseScriptModule(
bsm->name = name;
bsm->loc = loc;
bsm->data = std::move(data);
bsm->is_definition = is_definition;
*out_module = std::move(bsm);
break;
}
Expand All @@ -3882,6 +3905,7 @@ Result WastParser::ParseScriptModule(
qsm->name = name;
qsm->loc = loc;
qsm->data = std::move(data);
qsm->is_definition = is_definition;
*out_module = std::move(qsm);
break;
}
Expand All @@ -3890,6 +3914,7 @@ Result WastParser::ParseScriptModule(
auto tsm = std::make_unique<TextScriptModule>();
tsm->module.name = name;
tsm->module.loc = loc;
tsm->is_definition = is_definition;
if (IsModuleField(PeekPair()) || PeekIsCustom()) {
CHECK_RESULT(ParseModuleFieldList(&tsm->module));
} else if (!PeekMatch(TokenType::Rpar)) {
Expand Down
Loading