Skip to content

Commit

Permalink
Merge pull request #15 from thomscoder/soleModule
Browse files Browse the repository at this point in the history
feat: support empty module
  • Loading branch information
thomscoder authored Nov 22, 2022
2 parents 54c4962 + 9ab1ba1 commit 2b41122
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 6 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func Compile(ast []types.AstNode) Module {
// MAGIC and VERSION don't change until a newer version of WebAssembly gets released
module = append(module, defaults.MAGIC...)
module = append(module, defaults.VERSION...)

// if the module is empty return
if node.Expression.Value == nil {
return module
}

case texts.FuncStatement:
// num of types (i32, f32, i64, f64) inside the function
functionType = append(functionType, sectionData{0x01}...)
Expand Down
19 changes: 15 additions & 4 deletions compiler/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ func Parser(tokens []types.Token) []types.AstNode {

currentToken := iterator.next(tokens)

// If the module is empty return the Ast with only the module
if currentToken.done {
nodes = append(nodes, types.AstNode{
Type: texts.ModuleStatement,
Expression: types.ExpressionNode{},
})
return nodes
}

Expand Down Expand Up @@ -88,8 +93,9 @@ func parseStatement(currentToken *iteratorEmulatorStruct, eatToken func(val stri
case "module":
eatToken("module")
return types.AstNode{
Type: texts.ModuleStatement,
Expression: types.ExpressionNode{},
Type: texts.ModuleStatement,
// Check if the module is empty by inspecting the next node
Expression: parseExpression(currentToken, eatToken, index),
}

case "func":
Expand Down Expand Up @@ -222,7 +228,12 @@ func parseExpression(currentToken *iteratorEmulatorStruct, eatToken func(val str

return log

}
// Make node aware of which node is coming after
default:
log := types.ExpressionNode{
Value: currentToken.token.Value,
}

return types.ExpressionNode{}
return log
}
}
17 changes: 14 additions & 3 deletions example/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,25 @@ const runLunaAddition = async () => {

try {
const wasmer = await WebAssembly.instantiate(wasm);
const fn = Object.keys(wasmer.instance.exports)[0]
moduleContainer.innerHTML = `<p>Compiled successfully!\nExported function <span class="exported">${fn}</span></p>\n`
const fn = Object.keys(wasmer.instance.exports)[0];

// if module is empty, then make input section disappear
if (typeof wasmer.instance.exports[fn] == "undefined" && wasm.length == 8) {
moduleContainer.innerHTML = `<p>Module is <span class="exported">empty</span></p>\n`
input1.setAttribute("disabled", "true")
input2.setAttribute("disabled", "true")
btn.setAttribute('disabled', "true")
} else {
moduleContainer.innerHTML = `<p>Compiled successfully!\nExported function <span class="exported">${fn}</span></p>\n`
input1.removeAttribute("disabled")
input2.removeAttribute("disabled")
btn.removeAttribute('disabled')
}

for (const hex of wasm) {
moduleContainer.innerHTML += `<p class="hex-dump">${hex}</p>`
}

btn.removeAttribute('disabled')

btn.addEventListener('click', () => {
const n1 = Number(input1.value);
Expand Down
Binary file modified example/main.wasm
Binary file not shown.

1 comment on commit 2b41122

@vercel
Copy link

@vercel vercel bot commented on 2b41122 Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

luna – ./

luna-demo.vercel.app
luna-git-master-thomscoder.vercel.app
luna-thomscoder.vercel.app

Please sign in to comment.