Why can't I change the name of the variable in this invocation of Webassembly.Instantiate?

I am adapting a very simple example of Webassembly used in Javascript. I encountered a problem I do not understand.

When I try to change the name of the variable “instance” to “instance1” below, the (firefox) browser’s developer console produces an error message that instance1 is undefined (when I try to refer to instance1 in the next statement in my javascript code.)

This would become a problem when I am working with two different Webassembly functions (and associated supporting data structures) in the same Javascript code, because the name instance is duplicated (I don’t want to use Javascript Immediately-invoked Function Expression in this case, as I want the variables “instance1” and “instance2” to be globally visible in other areas of my Javascript code.

THIS IS FROM THE EXAMPLE I AM WORKING WITH, AND THIS WORKS:

// Make the class into an object, using the log function and memory space previously assigned

const { instance } = await WebAssembly.instantiate(bytes1, {
env: { log1, mem1 }
});

THIS IS MODIFIED TO USE instance1 INSTEAD OF instance AND DOES NOT WORK (instance1 is undefined the first time it is then referenced in the subsequent Javascript statements)

// Make the class into an object, using the log function and memory space previously assigned

const { instance1 } = await WebAssembly.instantiate(bytes1, {
env: { log1, mem1 }
});

Thanks for any advice or request for further clarification of my problem statement!

Dave

Hi @wb0gaz. I’m not really sure what is going on here either. Do you have a copy of your code available somewhere (e.g. on a GitHub repo) for me to grab and tinker with?

Thanks!

Hello chrisdavidmills, thank you for the reply!

As the test is extremely small (and I am not permitted to upload a file attachment containing the source/comments below), I’m taking the liberty of appending it inline (the text below contains the HTML file, the Javascript file, the C source for the WASM file, and the single-line I use to compile the C source into the WASM file.)

If executed as-is, you should see (when executing the web page on browser) a single console log message saying “This should be 202: 202”; if you change both occurrences of the variable ‘instance’ to ‘instance1’ (or something else, I’d presume), you will instead see on the console “TypeError: instance1 is undefined” referring to the invocation at “var x = instance1.exports.add(2, 200);”.

If the use of the word instance in this case is a reserved word, then I’m at a loss to understand!

(FYI I’m a beginner with both javascript and webassembly (but with other programming experience) and am learning by adapting simple examples, specifically focusing on the “SIDE_MODULE” mechanism.)

Thanks for your observations, and i’ll happily reply to any request for clarification about my problem.

Dave

Here is the entire project in-line:

///////////////// begin

///////////////// June 15 2020 posting
///////////////// Why can't I change the name of the variable in this invocation of Webassembly.Instantiate?
///////////////// Brower: Firefox 77.0.1 (64-bit)
///////////////// Server: Apache 2
///////////////// Host: Lubuntu 18.04

///////////////// test.html

(sorry, this isn’t getting posted as intended no matter how I adjust the HTML source code – it’s just a trivial HTML file which includes a specification to load the script file test.js, which is shown below, and is where the problem appears to be).

///////////////// test.js

const mem = new WebAssembly.Memory({ initial: 1 });

const log = (offset, length) => {
const bytes = new Uint8Array(mem.buffer, offset, length);
const string = new TextDecoder(‘utf8’).decode(bytes);
console.log(string);
};

(async function ()

{

const response = await fetch(‘add.wasm’);

const bytes = await response.arrayBuffer();

// if ‘instance’ is changed to ‘instance1’, browser says: “TypeError: instance1 is undefined”

const { instance } = await WebAssembly.instantiate(bytes, {
env: { log, mem }
});

// if ‘instance’ is changed to ‘instance1’, browser says: “TypeError: instance1 is undefined”

var x = instance.exports.add(2, 200);

console.log('This should be 202: ', x);
}

)

();

///////////////// add.c (source for add.wasm, fetched from test.js)

#include <stdio.h>
#include “…/emscripten/emscripten.h”

int main() { return 0; }

int EMSCRIPTEN_KEEPALIVE add(int x, int y) { return x + y; }

///////////////// compile add.c into add.wasm

emcc add.c -s WASM=1 -s SIDE_MODULE=1 -O1 -o add.wasm

///////////////// end