@@ -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 /**
0 commit comments