Skip to content

Commit 1e4b1c7

Browse files
committed
create correct init for float types
1 parent 1e4d2b7 commit 1e4b1c7

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

lib/wasm/WebAssemblyGenerator.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ function getNextFuncIndex(ast, countImportedFunc) {
166166
return t.indexLiteral(vectorOfSize + countImportedFunc);
167167
}
168168

169+
/**
170+
* Create a init instruction for a global
171+
* @param {t.GlobalType} globalType the global type
172+
* @returns {t.Instruction} init expression
173+
*/
174+
const createDefaultInitForGlobal = globalType => {
175+
if (globalType.valtype[0] === "i") {
176+
// create NumberLiteral global initializer
177+
return t.objectInstruction("const", globalType.valtype, [
178+
t.numberLiteralFromRaw(66)
179+
]);
180+
} else if (globalType.valtype[0] === "f") {
181+
// create FloatLiteral global initializer
182+
return t.objectInstruction("const", globalType.valtype, [
183+
t.floatLiteral(66, false, false, "66")
184+
]);
185+
} else {
186+
throw new Error("unknown type: " + globalType.valtype);
187+
}
188+
};
189+
169190
/**
170191
* Rewrite the import globals:
171192
* - removes the ModuleImport instruction
@@ -190,21 +211,7 @@ const rewriteImportedGlobals = state => bin => {
190211

191212
globalType.mutability = "var";
192213

193-
let init;
194-
195-
if (globalType.valtype[0] === "i") {
196-
// create NumberLiteral global initializer
197-
init = t.objectInstruction("const", globalType.valtype, [
198-
t.numberLiteralFromRaw(0)
199-
]);
200-
} else if (globalType.valtype[0] === "f") {
201-
// create FloatLiteral global initializer
202-
init = t.objectInstruction("const", globalType.valtype, [
203-
t.floatLiteral(0, false, false, "0")
204-
]);
205-
} else {
206-
throw new Error("unknown type: " + globalType.valtype);
207-
}
214+
const init = createDefaultInitForGlobal(globalType);
208215

209216
newGlobals.push(t.global(globalType, [init]));
210217

@@ -223,12 +230,7 @@ const rewriteImportedGlobals = state => bin => {
223230

224231
const initialGlobalidx = init.args[0];
225232

226-
const valtype = node.globalType.valtype;
227-
228-
node.init = [
229-
// Poisong globals, they are meant to be rewritten
230-
t.objectInstruction("const", valtype, [t.numberLiteralFromRaw(0)])
231-
];
233+
node.init = [createDefaultInitForGlobal(node.globalType)];
232234

233235
additionalInitCode.push(
234236
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const n = 1;
2+
export const m = 1.25
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
it("should export imported global", function() {
2-
return import("./module").then(function({ v, w }) {
2+
return import("./module").then(function({ v, w, x, test }) {
33
expect(v).toBe(1);
44
expect(w).toBe(1);
5+
expect(x).toBe(1.25);
6+
expect(test()).toBe(2);
57
});
68
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
(module
22
(import "./env.js" "n" (global i32))
3+
(import "./env.js" "m" (global $g2 f64))
34
(export "v" (global 0))
45
(global $g i32 (get_global 0))
56
(export "w" (global $g))
7+
(export "x" (global $g2))
8+
(func (export "test") (result i32)
9+
get_global $g2
10+
get_global $g2
11+
f64.add
12+
drop
13+
get_global 0
14+
get_global $g
15+
i32.add
16+
)
617
)

0 commit comments

Comments
 (0)