Skip to content

Commit 2b41122

Browse files
authored
Merge pull request #15 from thomscoder/soleModule
feat: support empty module
2 parents 54c4962 + 9ab1ba1 commit 2b41122

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

compiler/compiler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ func Compile(ast []types.AstNode) Module {
169169
// MAGIC and VERSION don't change until a newer version of WebAssembly gets released
170170
module = append(module, defaults.MAGIC...)
171171
module = append(module, defaults.VERSION...)
172+
173+
// if the module is empty return
174+
if node.Expression.Value == nil {
175+
return module
176+
}
177+
172178
case texts.FuncStatement:
173179
// num of types (i32, f32, i64, f64) inside the function
174180
functionType = append(functionType, sectionData{0x01}...)

compiler/parser.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ func Parser(tokens []types.Token) []types.AstNode {
5454

5555
currentToken := iterator.next(tokens)
5656

57+
// If the module is empty return the Ast with only the module
5758
if currentToken.done {
59+
nodes = append(nodes, types.AstNode{
60+
Type: texts.ModuleStatement,
61+
Expression: types.ExpressionNode{},
62+
})
5863
return nodes
5964
}
6065

@@ -88,8 +93,9 @@ func parseStatement(currentToken *iteratorEmulatorStruct, eatToken func(val stri
8893
case "module":
8994
eatToken("module")
9095
return types.AstNode{
91-
Type: texts.ModuleStatement,
92-
Expression: types.ExpressionNode{},
96+
Type: texts.ModuleStatement,
97+
// Check if the module is empty by inspecting the next node
98+
Expression: parseExpression(currentToken, eatToken, index),
9399
}
94100

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

223229
return log
224230

225-
}
231+
// Make node aware of which node is coming after
232+
default:
233+
log := types.ExpressionNode{
234+
Value: currentToken.token.Value,
235+
}
226236

227-
return types.ExpressionNode{}
237+
return log
238+
}
228239
}

example/js/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,25 @@ const runLunaAddition = async () => {
8585

8686
try {
8787
const wasmer = await WebAssembly.instantiate(wasm);
88-
const fn = Object.keys(wasmer.instance.exports)[0]
89-
moduleContainer.innerHTML = `<p>Compiled successfully!\nExported function <span class="exported">${fn}</span></p>\n`
88+
const fn = Object.keys(wasmer.instance.exports)[0];
89+
90+
// if module is empty, then make input section disappear
91+
if (typeof wasmer.instance.exports[fn] == "undefined" && wasm.length == 8) {
92+
moduleContainer.innerHTML = `<p>Module is <span class="exported">empty</span></p>\n`
93+
input1.setAttribute("disabled", "true")
94+
input2.setAttribute("disabled", "true")
95+
btn.setAttribute('disabled', "true")
96+
} else {
97+
moduleContainer.innerHTML = `<p>Compiled successfully!\nExported function <span class="exported">${fn}</span></p>\n`
98+
input1.removeAttribute("disabled")
99+
input2.removeAttribute("disabled")
100+
btn.removeAttribute('disabled')
101+
}
90102

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

95-
btn.removeAttribute('disabled')
96107

97108
btn.addEventListener('click', () => {
98109
const n1 = Number(input1.value);

example/main.wasm

717 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)