From 3067079eec5759fbbbf81872b32a9ae4a2dd99fc Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 30 Oct 2015 22:13:30 -0700 Subject: function scopes --- src/wasm-interpreter.cpp | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/wasm-interpreter.cpp') diff --git a/src/wasm-interpreter.cpp b/src/wasm-interpreter.cpp index a055ee17d..84f9b585a 100644 --- a/src/wasm-interpreter.cpp +++ b/src/wasm-interpreter.cpp @@ -18,11 +18,31 @@ public: } } - Literal callFunction(const char *name) { - return callFunction(IString(name)); - } + typedef std::vector LiteralList; Literal callFunction(IString name) { + LiteralList empty; + return callFunction(name, empty); + } + + Literal callFunction(IString name, LiteralList& arguments) { + + class FunctionScope { + public: + std::map locals; + + FunctionScope(Function* function, LiteralList& arguments) { + assert(function->params.size() == arguments.size()); + for (size_t i = 0; i < arguments.size(); i++) { + assert(function->params[i].type == arguments[i].type); + locals[function->params[i].name] = arguments[i]; + } + for (auto& local : function->locals) { + locals[local.name].type = local.type; + } + } + }; + // Stuff that flows around during executing expressions: a literal, or a change in control flow class Flow { public: @@ -43,10 +63,10 @@ public: // Execute a statement class ExpressionRunner : public WasmVisitor { - private: - std::vector arguments; // filled in before a call, cleared by the call - + FunctionScope& scope; public: + ExpressionRunner(FunctionScope& scope) : scope(scope) {} + Flow visitBlock(Block *curr) override { Flow flow; for (auto expression : curr->list) { @@ -96,6 +116,14 @@ public: abort(); } Flow visitCall(Call *curr) override { + LiteralList arguments; + arguments.reserve(curr->operands.size()); + for (auto expression : curr->operands) { + Flow flow = visit(expression); + if (flow.breaking()) return flow; + arguments.push_back(flow.value); + } + return callFunction(curr->target, arguments); } Flow visitCallImport(CallImport *curr) override { } @@ -126,7 +154,9 @@ public: } }; - return ExpressionRunner().visit(functions[name]->body).value; + Function *function = functions[name]; + FunctionScope scope(function, arguments); + return ExpressionRunner(scope).visit(function->body).value; } private: -- cgit v1.2.3